Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is remoteAddress hidden?

I'm using Node.js with https://github.com/websockets/ws.

I'm trying to store the users remoteAddress in a temporary socket variable. I can access this variable by: socket.upgradeReq.connection.remoteAddress

The problem is, where in the world did remoteAddress come from? The variable 'remoteAddress' is not even found in any of the WS node_plugin files in the lib directory. (I used Notepad++ to search through every file.)

The only reason I came about finding access to this variable is from this topic: How to get client IP address using websocket (einaros / ws) lib in node.js?

Heck, even when displaying console.log(self.upgradeReq.connection); in my console I still cannot find it!

Images of the object console list:

Enter image description here

Enter image description here

Where in the world? Am I missing something?

like image 216
NiCk Newman Avatar asked Oct 19 '22 16:10

NiCk Newman


1 Answers

It comes Node.js's built-in net module. Here's how to trace it:

The ws documentation refer to upgradeReq as "The HTTP request that initiated the upgrade". Glancing through the ws code makes it clear that it uses Node.js's built-in http library for handling HTTP requests.

The Node.js http request documentation says that http.ClientRequest.connection is a reference to a given request's underlying TCP socket.

Node.js's TCP code is in the net library. The net docs document socket.remoteAddress.

like image 65
joews Avatar answered Oct 27 '22 10:10

joews