DEV Community

Cover image for Tunneling socket could not be established: causes and fixes
proxymint
proxymint

Posted on Originally published at proxymint.com

Tunneling socket could not be established: causes and fixes

Originally published on proxymint.

The Node error means a proxy refused or never answered a CONNECT request. Read the statusCode or cause in the message, then apply the one fix that matches it.

TL;DR

  • The error comes from a Node proxy tunnel that failed. The tunnel-agent or tunnel package sent an HTTP CONNECT to a proxy for an HTTPS site and got no 200 back, so no tunnel opened and the request never left the client.
  • The text after the comma is the cause. statusCode= followed by a number means the proxy answered with a refusal; cause= followed by a socket error means the CONNECT never got an answer.
  • The error code is always ECONNRESET. Both libraries hard-code it for every tunnel failure, so a wrong password and a dead socket look the same to code that only checks err.code.
  • An https:// proxy URL is the most common single mistake. It makes the client speak TLS to a plain HTTP proxy and fail with EPROTO; the proxy URL should start with http:// even for HTTPS sites.

Key takeaways

  • Match on the message, not on err.code. Branch on statusCode=407, statusCode=502 or the cause text to decide between fixing configuration and retrying.
  • Test the same proxy URL with curl -v. The line after the CONNECT request shows the proxy's status, which separates a proxy problem from a Node setting problem.
  • Print every proxy setting before changing one. env | grep -i _proxy and npm config get https-proxy reveal stale values in variables and .npmrc files.
  • Trust the company CA instead of turning off strict-ssl. NODE_EXTRA_CA_CERTS and npm cafile fix certificate errors from TLS-inspecting proxies without disabling checks.
  • Add an outer timeout around request calls. A stalled CONNECT can wait past the library's own timeout, because that timer starts only after the tunnel opens.

Read the full guide on proxymint, with the commands, tables and sources.

Top comments (0)