How to find out the remote address in node.js if it is an HTTPS request?

Hi. in node.js, if this is an http request, I can get remoteAddress in req.connection.remoteAddress,

so how to get it if the request is https? I find that there is req.socket.remoteAddress, but I'm not sure. Please advise. thanks.

+4
source share
4 answers

Something seems to be weird / broken. As for node 0.4.7, it looks like http has remoteAddress available at:

  • req.connection.remoteAddress
  • req.socket.remoteAddress

on https, both of them are undefined, but

  • req.connection.socket.remoteAddress

really works. It is not available on http, though, so you need to check carefully. I can not imagine that this behavior was intentional.

+17
source

Since the direct pointer "express js ip" directly points to this, it is somehow relevant.

Express 3.0.0 alpha now offers a new way to obtain IP addresses for client requests. Just use req.ip If you are doing proxy-jigger-pokery, you may be interested in app.set("trust proxy", true); and req.ips .

I recommend that you read the entire discussion in the Google Group Express .

+8
source
 var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress; 

Please note that sometimes you can get more than one IP address in req.headers ['x-forwarded-for'], especially when working with mobile phones that access your server (Wi-Fi and carrier data).

+3
source

Also req.headers['x-forwarded-for'] easy to manipulate, so you need a properly configured proxy server.

It is better to check req.connection.remoteAddress before a list of known proxies before going with req.headers['x-forwarded-for'] .

0
source

All Articles