如何在Node.js中检索客户端和服务器的IP地址和端口号

编程入门 行业动态 更新时间:2024-10-27 14:28:54
本文介绍了如何在Node.js中检索客户端和服务器的IP地址和端口号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图进行大量搜索,以找到一种了解客户端和服务器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:

  • 客户端ip :req.ip可以识别.
  • 客户端端口:我进行了很多搜索,但是找不到任何找到此客户端临时端口的方法.检查req和res对象后,我发现有时res.connection._peername包含客户端IP地址和端口号.但这不是查找端口号的可靠方法,因为在某些请求中缺少此属性.那么,了解端口的正确方法是什么?
  • 服务器ip :在这里,我有兴趣了解服务器的外部或公共ip.我做了一些搜索,发现了这个链接使用某些外部api检索外部ip.这是找到外部IP的唯一可能方法吗?还是有其他可能的方法?
  • 服务器端口:可以通过listener.address().port [source] .但是可以从req或res对象中得知它吗?其实我想从中间件中知道只有req,res和next(基本上是app.use(function(req,res,next){...})))的端口号.
  • Client ip : can be known by req.ip.
  • Client port : I searched a lot but I couldn't find any way to find this client ephemeral port. After inspecting the req and res objects, I found that sometimes res.connection._peername contains the client ip address and port number. But this is not the reliable way to find the port number because in some request this property is missing. So, what is the correct way to know about the port ?
  • Server ip : Here I am interested in knowing the external or public ip for the server. I searched a bit for the same and I found this link which uses some external api to retrieve the external ip. Is it the only possible way to find the external IP ? Or are there any other way possible ?
  • Server port : It can be known with listener.address().port [source] . But can it be known from req or res objects ? Actually I want to know the port number from within the middleware where I have req,res and nextonly( basically app.use(function(req,res,next){...}))).
  • 推荐答案

    如果与客户端直接连接(没有代理),则可以使用这些属性获得这四个值:

    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.localPort

    net.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地址和端口号

    本文发布于:2023-08-07 03:31:53,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1316721.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:客户端   端口号   地址   服务器   如何在

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!