我试图进行大量搜索,以找到一种了解客户端和服务器ip地址和端口号的方法.到目前为止,我发现:
I tried to search a lot to find a way to know the client and server ip address and port number. So far I found:
推荐答案
如果与客户端直接连接(没有代理),则可以使用这些属性获得这四个值:
You can get those four values with these properties if you have a direct connection with the client (no proxies in the way):
req.connection.remoteAddress req.connection.remotePort req.connection.localAddress req.connection.localPortnet.js中的相关node.js源代码指针: github/nodejs/node/blob/863952ebadd4909a66bb9da7868bf75bbbe1462d/lib/net.js#L608
Relevant node.js source code pointer in net.js: github/nodejs/node/blob/863952ebadd4909a66bb9da7868bf75bbbe1462d/lib/net.js#L608
注意:当您将它们作为属性值访问时,它们在技术上被实现为吸气剂.您无法设置这些属性.
Note: While you access these as property values, they are technically implemented as getters. You cannot set these properties.
如果实际服务器前面有服务器基础结构(例如负载平衡器,代理等),则上述本地值可能会返回本地服务器,而不是客户端最初连接的实际公共IP/端口.检索原始公共IP/端口的唯一方法是,如果您的基础结构将原始文件设置为HTTP标头(某些代理服务器设置了IP,不知道是否有代理服务器设置了端口),因为它们不存在于当前的原始TCP中来自代理的连接.
If there is server infrastructure such as load balancers, proxies, etc... in front of your actual server, the above local values may return the local server, not what the actual public IP/port that the client originally connected to. The only way to retrieve the original public IP/port would be if your infrastructure sets the originals as a HTTP headers (which some proxies set IP, don't know if any proxies set port) since they are not present in the current raw TCP connection from the proxy.
对于涉及代理的情况,您可能还需要查看一些HTTP标头:
For cases where a proxy is involved, you may also want to look at some HTTP headers:
X-Forwarded-For - The IP address of the client before it went through the proxy X-Forwarded-Port - The port of the client before it went through the proxy如果X-Forwarded标头经过了多个代理,例如:
The X-Forwarded headers may also be a comma separated list if it has gone through multiple proxies such as:
X-Forwarded-For: OriginatingClientIPAddress, proxy1-IPAddress, proxy2-IPAddress从 RFC 7239 2014开始,还有一个标准标头:
There is also a standard header as of RFC 7239 2014:
Forwarded: for=192.0.2.60; proto=http; by=203.0.113.43更多推荐
如何在Node.js中检索客户端和服务器的IP地址和端口号
发布评论