Node.js区分http请求;具有相同公共IP的多个设备 [英] Nodejs distinguishing http requests; multiple devices with same public IP

查看:160
本文介绍了Node.js区分http请求;具有相同公共IP的多个设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Нello!我正在尝试表示通过带有节点的http进行的客户端连接.现在我有类似的东西:

Нello! I'm trying to represent client connections over http with node. Right now I have something like:

let names = [ 'john', 'margaret', 'thompson', /* ... tons more ... */ ];
let nextNameInd = 0;   

let clientsIndexedByIp = {};
let createNewClient = ip => {
  return {
    ip,
    name: names[nextNameInd++],
    numRequests: 0
  };
};

require('http').createServer((req, res) => {

  let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;

  // If this is a connection we've never seen before, create a client for it
  if (!clientsIndexedByIp.hasOwnProperty(ip)) {
    clientsIndexedByIp[ip] = createNewClient(ip);
  }

  let client = clientsIndexedByIp[ip];
  client.numRequests++;

  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify(client));

}).listen(80, '<my public ip>', 511);

我在某些远程服务器上运行此代码,它可以正常工作;我可以查询该服务器并获得预期的响应.但是我有一个问题:我的笔记本电脑和智能手机都连接到同一个wifi.如果我同时从笔记本电脑和智能手机查询该服务器,则该服务器会认为这两个设备具有相同的IP地址,并且只会为两个设备创建一个客户端"对象.

I run this code on some remote server and it works fine; I can query that server and get the expected response. But I have an issue: my laptop and my smartphone are both connected to the same wifi; if I query that server from both my laptop and smartphone the server thinks that both devices have the same IP address and it only creates one "client" object for the both of them.

例如每个响应的名称"参数都相同.

E.g. the "name" parameter of the response is the same for each.

在我的笔记本电脑和智能手机上检查whatsmyip.org会显示相同的IP地址-这使我感到惊讶,因为我对IP的理解被证明是错误的.在此之前,我认为所有设备都具有唯一的IP.

Checking whatsmyip.org on both my laptop and smartphone shows me the same IP address - this surprised me, as my understanding of IPs turned out to be wrong. Until this point I thought all devices had a unique IP.

即使两个设备位于同一wifi网络上,我也希望不同的设备与不同的客户端关联.我认为我用来消除设备歧义的数据(仅请求IP(req.headers['x-forwarded-for'] || req.connection.remoteAddress))是不够的.

I want different devices to become associated with different clients, even when two devices are on the same wifi network. I assume that the data I'm using to disambiguate devices, their request IP alone (req.headers['x-forwarded-for'] || req.connection.remoteAddress), is insufficient.

如何区分连接到同一路由器的多个设备? req对象中是否有一些额外的数据可以做到这一点?

How can I differentiate between multiple devices connected to the same router? Is there some extra bit of data in the req object which allows for this?

或者我的笔记本电脑和智能手机都具有相同的IP地址只是一种网络配置错误的情况?

Or is it just a case of network misconfiguration that both my laptop and smartphone have the same IP address?

谢谢!

推荐答案

如果您使用express-fingerprint模块,则适用于大多数用例,例如:

If you use the express-fingerprint module, this will work for most use cases, for example:

const express = require('express');
const app = express();
const port = 3000;
var Fingerprint = require('express-fingerprint')

app.use(Fingerprint( { parameters:[
    Fingerprint.useragent,
    Fingerprint.geoip ]
}));

app.get('/test', function(req, res){
    console.log("Client fingerprint hash: ", req.fingerprint.hash);
    res.send("Your client Id: " + req.fingerprint.hash);
});

app.listen(port);

每个客户端都有一个唯一的哈希,可以用来识别它们.值得理解的是,这种方法会有局限性,并且在某些用例中将cookie分配给客户端会更好.

Each client will have a unique hash you can use to identify them. It's worth understanding that this approach will have limitations and assigning a cookie to the client would work better for some use cases.

这篇关于Node.js区分http请求;具有相同公共IP的多个设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆