Get remoteAddress with https.Server # clientError in Node.js

In Node.js and http.Serverand https.Serveremit an event clientError, but with different arguments:
http.Server#clientError(exception, socket)
https.Server#clientError(exception, securePair)

If securePairis an instance tls.SecurePair, securePair.cleartexta tls.CleartextStreamand securePair.encrypteda tls.CryptoStream.

The question arises: how to get the address and port of the client that called clientError? Theoretically, this should work:
socket = securePair.cleartext.socket;
console.log(socket.remoteAddress + ':' + socket.remotePort);

In fact, when I try to connect to the HTTPS server using HTTP (pretending to be an HTTP server) and cancel after a few seconds, I get the clientErrortype ECONNRESET, but socket.remoteAddressthey socket.remotePortare undefined(although it securePair.cleartext.socketreally is net.Socket, not undefined).

+4
source share
1 answer

Unfortunately, generosity didn't seem to help much: - (I had to investigate this myself.

Apparently, a fix was made (but node.js has not yet been released) to be able to get these values โ€‹โ€‹even after the socket has been closed: https://github.com/joyent/node/commit/8c38b07252d86f4eef91344f8df4f0c3e38bca35

The workaround is to save the address during the connection:

srv.on('connect', function (socket) {
  socket.stored_remoteAddress = socket.remoteAddress;
});
srv.on('clientError', function (err, socket) {
  console.log(socket.cleartext.socket.stored_remoteAddress);
}

It works, but not really.


Apparently, even this does not work completely: - (

Node.js 0.11 does not emit a clientError event for SSL errors (for example), so there is no way to catch the situation there.

Node.js 0.12 clientError, TLS, 'cleartext'. "connect" "clientError" , ( "clientError" ).

, , - node.js(0.12.2, , ) , .

+2

All Articles