node.js 在 socket.io 中获取客户端的 IP 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6458083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:18:58  来源:igfitidea点击:

Get the client's IP address in socket.io

node.jsip-addresssocket.io

提问by Toji

When using socket.IO in a Node.js server, is there an easy way to get the IP address of an incoming connection? I know you can get it from a standard HTTP connection, but socket.io is a bit of a different beast.

在 Node.js 服务器中使用 socket.IO 时,是否有一种简单的方法来获取传入连接的 IP 地址?我知道您可以从标准 HTTP 连接获取它,但 socket.io 有点不同。

回答by Toji

Okay, as of 0.7.7 this is available, but not in the manner that lubar describes. I ended up needing to parse through some commit logs on git hub to figure this one out, but the following code does actually work for me now:

好的,从 0.7.7 开始,这是可用的,但不是以 lubar 描述的方式。我最终需要解析 git hub 上的一些提交日志来解决这个问题,但下面的代码现在确实对我有用:

var io = require('socket.io').listen(server);

io.sockets.on('connection', function (socket) {
  var address = socket.handshake.address;
  console.log('New connection from ' + address.address + ':' + address.port);
});

回答by flatroze

for 1.0.4:

对于 1.0.4:

io.sockets.on('connection', function (socket) {
  var socketId = socket.id;
  var clientIp = socket.request.connection.remoteAddress;

  console.log(clientIp);
});

回答by zuim

If you use an other server as reverse proxy all of the mentioned fields will contain localhost. The easiest workaround is to add headers for ip/port in the proxy server.

如果您使用其他服务器作为反向代理,则所有提到的字段都将包含 localhost。最简单的解决方法是在代理服务器中为 ip/port 添加标头。

Example for nginx: add this after your proxy_pass:

nginx 示例:在您的后面添加proxy_pass

proxy_set_header  X-Real-IP $remote_addr;
proxy_set_header  X-Real-Port $remote_port;

This will make the headers available in the socket.io node server:

这将使头文件在 socket.io 节点服务器中可用:

var ip = socket.handshake.headers["x-real-ip"];
var port = socket.handshake.headers["x-real-port"];

Note that the header is internally converted to lower case.

请注意,标头在内部转换为小写。

If you are connecting the node server directly to the client,

如果您将节点服务器直接连接到客户端,

var ip = socket.conn.remoteAddress;

works with socket.io version 1.4.6 for me.

适用于我的 socket.io 版本 1.4.6。

回答by SlyBeaver

For latest socket.io version use

对于最新的 socket.io 版本使用

socket.request.connection.remoteAddress

For example:

例如:

var socket = io.listen(server);
socket.on('connection', function (client) {
  var client_ip_address = socket.request.connection.remoteAddress;
}

beware that the code below returns the Server's IP, not the Client's IP

请注意,下面的代码返回的是服务器的 IP,而不是客户端的 IP

var address = socket.handshake.address;
console.log('New connection from ' + address.address + ':' + address.port);

回答by Balthazar

Using the latest 1.0.6version of Socket.IOand have my app deployed on Heroku, I get the client IPand portusing the headersinto the socket handshake:

使用最新1.0.6版本Socket.IO并在 上部署我的应用程序Heroku,我获取客户端IPport使用headerssocket handshake

var socketio = require('socket.io').listen(server);

socketio.on('connection', function(socket) {

  var sHeaders = socket.handshake.headers;
  console.info('[%s:%s] CONNECT', sHeaders['x-forwarded-for'], sHeaders['x-forwarded-port']);

}

回答by nha

Since socket.io 1.1.0, I use :

从 socket.io 1.1.0 开始,我使用:

io.on('connection', function (socket) {
  console.log('connection :', socket.request.connection._peername);
  // connection : { address: '192.168.1.86', family: 'IPv4', port: 52837 }
}

Edit: Note that this is not part of the official API, and therefore not guaranteed to work in future releases of socket.io.

编辑:请注意,这不是官方 API 的一部分,因此不能保证在 socket.io 的未来版本中工作。

Also see this relevant link : engine.io issue

另请参阅此相关链接: engine.io issue

回答by lubar

Version 0.7.7 of Socket.IO now claims to return the client's IP address. I've had success with:

Socket.IO 的 0.7.7 版本现在声称返回客户端的 IP 地址。我在以下方面取得了成功:

var socket = io.listen(server);
socket.on('connection', function (client) {
  var ip_address = client.connection.remoteAddress;
}

回答by Robert Larsen

This seems to work:

这似乎有效:

var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
  var endpoint = socket.manager.handshaken[socket.id].address;
  console.log('Client connected from: ' + endpoint.address + ":" + endpoint.port);
});

回答by Ufb007

I have found that within the socket.handshake.headers there is a forwarded foraddress which doesn't appear on my local machine. And I was able to get the remote address using:

我发现在 socket.handshake.headers 中有一个转发地址,它没有出现在我的本地机器上。我能够使用以下方法获取远程地址:

socket.handshake.headers['x-forwarded-for']

This is in the server side and not client side.

这是在服务器端而不是客户端。

回答by geniant

Very easy. First put

好简单。先放

io.sockets.on('connection', function (socket) {

console.log(socket);

You will see all fields of socket. then use CTRL+F and search the word address. Finally, when you find the field remoteAddress use dots to filter data. in my case it is

您将看到套接字的所有字段。然后使用 CTRL+F 并搜索单词地址。最后,当您找到字段 remoteAddress 时,使用点来过滤数据。就我而言,它是

console.log(socket.conn.remoteAddress);