Skip to content

Conversation

cheapsteak
Copy link

Was running into

events.js:163
      throw er; // Unhandled 'error' event
      ^

Error: socket hang up
    at createHangUpError (_http_client.js:302:15)
    at Socket.socketOnEnd (_http_client.js:394:23)
    at emitNone (events.js:91:20)
    at Socket.emit (events.js:188:7)
    at endReadableNT (_stream_readable.js:975:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)

Found the issue via this SO thread:

whenever you emit an 'error' event and no one listens to it, it will throw.

In this case the culprit seems to be the http.request that's missing a listener for the error

Should fix #60

@roninhack
Copy link

Not enough for windows and mac. Error handling not catching in windows.
EADDRINUSE , ECONNRESET and "Unknown Error" can not handling in windows.

@mfgea
Copy link

mfgea commented Aug 31, 2018

I tracked this error down. Seems to be happenning when creating the tlsSpoofing server and it's corresponding request; none of those are handling errors:

In lib/proxy.js (line ~230):

      this._tlsSpoofingServer = https.createServer({
        key,
        cert,
        SNICallback,
      }, (fromClient, toClient) => {
        let shp = 'https://' + fromClient.headers.host
          , fullUrl = shp + fromClient.url
          , addr = this._server.address()
        let toServer = http.request({
          host: 'localhost',
          port: addr.port,
          method: fromClient.method,
          path: fullUrl,
          headers: fromClient.headers,
        }, fromServer => {
          toClient.writeHead(fromServer.statusCode, fromServer.headers)
          fromServer.pipe(toClient)
        })
        fromClient.pipe(toServer)
      })

Adding error listeners to the toServer request and the this._tlsSpoofingServer seems to solve the issue of the app crashing:

      this._tlsSpoofingServer = https.createServer({
        key,
        cert,
        SNICallback,
      }, (fromClient, toClient) => {
        let shp = 'https://' + fromClient.headers.host
          , fullUrl = shp + fromClient.url
          , addr = this._server.address()
        let toServer = http.request({
          host: 'localhost',
          port: addr.port,
          method: fromClient.method,
          path: fullUrl,
          headers: fromClient.headers,
        }, fromServer => {
          toClient.writeHead(fromServer.statusCode, fromServer.headers)
          fromServer.pipe(toClient)
        })
        toServer.on('error', (err) => {
            this.emit('error', err);
        })
        fromClient.pipe(toServer)
      })
      _this._tlsSpoofingServer.on('error', (err) => {
          this.emit('error', err);
      })

I'll send a PR later today

@greim
Copy link
Owner

greim commented Sep 19, 2018

Thanks for this. A similar fix just landed in #109

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

socket error not handled
4 participants