Node.js 如何在节点中确定用户的IP地址

Node.js 如何在节点中确定用户的IP地址,node.js,ip,Node.js,Ip,如何从控制器中确定给定请求的IP地址?例如,在express中: app.post('/get/ip/address', function (req, res) { // need access to IP address here }) 在您的请求对象中有一个名为connection的属性,它是一个net.Socket对象。net.Socket对象具有属性remoteAddress,因此您应该能够通过此调用获取IP: request.connection.remoteAddress

如何从控制器中确定给定请求的IP地址?例如,在express中:

app.post('/get/ip/address', function (req, res) {
    // need access to IP address here
})
在您的请求对象中有一个名为connection的属性,它是一个net.Socket对象。net.Socket对象具有属性remoteAddress,因此您应该能够通过此调用获取IP:

request.connection.remoteAddress
有关和的详细信息,请参阅文档

编辑

正如@juand在评论中指出的,如果服务器位于代理之后,那么获取远程IP的正确方法是request.headers['x-forwarded-for']

请注意,有时您可以在req.headers['x-forwarded-for']中获得多个IP地址。此外,x-forwarded-for标头并不总是被设置,这可能会引发错误

该字段的一般格式为:

x-forwarded-for:客户端、代理1、代理2、代理3

其中,该值是一个以逗号+空格分隔的IP地址列表,最左边是原始客户端,每个通过请求的后续代理添加从中接收请求的IP地址。在本例中,请求通过proxy1、proxy2和proxy3传递。proxy3显示为请求的远程地址

这是由建议的解决方案,在未设置x-forwarded-for的情况下,在下面的注释中建议了一个修复程序:

var ip = (req.headers['x-forwarded-for'] || '').split(',').pop().trim() || 
         req.socket.remoteAddress
使用现代JS的建议:

仅当设置为时才处理x-forwarded-for,如果设置为,则取第一个地址 使用的其他参数 const parseIp=req=> 请求标头['x-forwarded-for']?.split','.shift ||请求套接字?远程地址 console.logparseIpreq // => 127.0.0.1 您可以保持干燥,只需使用支持IPv4和IPv6的服务器即可

安装:

在app.js或中间件中:


它将尽最大努力获取用户的IP地址,或返回127.0.0.1以指示无法确定用户的IP地址。查看该文件以获取高级选项。

如果您使用的是express 3.x或更高版本,则可以使用信任代理设置,它将遍历x-forwarded-for标头中的地址链,并将链中未配置为信任代理的最新ip放入req对象的ip属性中。

您可以使用,检索用户的ip地址。它处理许多不同的边缘情况,其中一些在其他答案中提到

披露:我创建了这个模块

安装:

在您的应用程序中:

希望这对request.headers['x-forwarded-for']| | request.connection.remoteAddress有所帮助

如果存在x-forwarded-for标头,则使用该标头,否则使用.remoteAddress属性


x-forwarded-for标头添加到通过负载平衡器或为HTTP或HTTPS设置的其他类型代理的请求中。在使用代理协议在TCP级别进行平衡时,也可以将此标头添加到请求中。这是因为request.connection.remoteAddress属性将包含负载平衡器的私有IP地址,而不是客户端的公共IP地址。通过使用OR语句,按照上面的顺序检查是否存在x-forwarded-for标头,如果存在则使用它,否则使用request.connection.remoteAddress

也有同样的问题……我对javascript也是新手,但我用req.connection.remoteAddress解决了这个问题;这给了我第个IP地址,但采用ipv6格式::ffff.192.168.0.101,然后是.slice以删除前7位数字

var ip = req.connection.remoteAddress;

if (ip.length < 15) 
{   
   ip = ip;
}
else
{
   var nyIP = ip.slice(7);
   ip = nyIP;
}

以下功能包含了所有案例,将有助于

var ip;
if (req.headers['x-forwarded-for']) {
    ip = req.headers['x-forwarded-for'].split(",")[0];
} else if (req.connection && req.connection.remoteAddress) {
    ip = req.connection.remoteAddress;
} else {
    ip = req.ip;
}console.log("client IP is *********************" + ip);
函数getCallerIPrequest{ var ip=请求.头文件['x-forwarded-for']|| request.connection.remoteAddress|| request.socket.remoteAddress|| request.connection.socket.remoteAddress; ip=ip.split','[0]; ip=ip.split':'.slice-1;//如果ip以以下格式返回:ffff:146.xxx.xxx.xxx 返回ip; }如果使用express。。。
我在查这个,然后我想等等,我在用快递。Duh.

如果你有多个IP,这对我很有用:

var ipaddress=req.headers['x-forwarded-for']|| 请求连接远程地址| | 请求套接字远程地址||
请求连接.套接字.远程地址.拆分,[0] 在nodejs中简单获取远程ip:

var ip = req.header('x-forwarded-for') || req.connection.remoteAddress;

有两种方法可以获取ip地址:

设ip=req.ip

设ip=req.connection.remoteAddress

但上述方法存在一个问题

如果您在Nginx或任何代理之后运行应用程序,则每个IP地址都将是127.0.0.1

因此,获取用户ip地址的最佳解决方案是:-

let ip = req.header('x-forwarded-for') || req.connection.remoteAddress;

我意识到这已经被彻底解决了,但这里有一个我编写的现代ES6版本,它遵循了airbnb base eslint标准

const getIpAddressFromRequest = (request) => {
  let ipAddr = request.connection.remoteAddress;

  if (request.headers && request.headers['x-forwarded-for']) {
    [ipAddr] = request.headers['x-forwarded-for'].split(',');
  }

  return ipAddr;
};
X-Forwarded-For标头可能包含以逗号分隔的代理IP列表。顺序是client,proxy1,proxy2,…,proxyN。在现实世界中,人们实现的代理可能会在这个标题中提供他们想要的任何内容。如果您在负载平衡器或其他东西后面,您至少可以信任第一个
列表中的IP至少是一些请求通过的任何代理。

在节点10.14中,在nginx后面,您可以通过nginx头请求来检索IP,如下所示:

proxy_set_header X-Real-IP $remote_addr;
然后在app.js中:

app.set('trust proxy', true);
之后,无论您希望它出现在何处:

var userIp = req.header('X-Real-IP') || req.connection.remoteAddress;
警告: 不要盲目地将此用于重要的速率限制:

let ip = request.headers['x-forwarded-for'].split(',')[0];
很容易欺骗:

curl --header "X-Forwarded-For: 1.2.3.4" "https://example.com"
在这种情况下,用户的实际IP地址为:

let ip = request.headers['x-forwarded-for'].split(',')[1];

我很惊讶没有其他答案提到这一点。

如果您正在使用Graphql瑜伽,您可以使用以下功能:

const getRequestIpAddress=request=>{ const requestIpAddress=request.request.headers['X-Forwarded-For']| | request.request.connection.remoteAddress if!requestIpAddress返回null const ipv4=new RegExp?:?:25[0-5]| 2[0-4][0-9]|[01]?[0-9][0-9]?\.{3}:25[0-5]| 2[0-4][0-9]|[01]?[0-9][0-9]? 常量[ipAddress]=requestIpAddress.matchipv4 返回IP地址
} 我正在使用nginx后面的express和

req.headers.origin
为我做了这个把戏

我已经试过了,但是都没用

console.log(clientIp);
console.log(req.ip);

console.log(req.headers['x-forwarded-for']);
console.log(req.connection.remoteAddress);
console.log(req.socket.remoteAddress);
console.log(req.connection.socket.remoteAddress.split(",")[0]);
在me Nginx的代理后运行Express应用程序时,必须将应用程序变量trust proxy设置为true。Express提供了一些其他信任代理值,您可以在其文档中查看这些值,但下面的步骤对我有用

app.set'trust proxy',在您的Express应用程序中为true。 app.set'trust proxy',true

在Nginx中为$remote\u addr添加代理\u set\u头X-Forwarded 服务器块的配置。 现在,您可以从 请求头'x-forwarded-for'或请求连接远程地址;ipfilter的完整代码 req.connection已被弃用。使用req.connection.remoteAddress获取客户端IP可能仍然有效,但不鼓励这样做

幸运的是,他从那以后一直在那里node@0.5.10是一个完美的替代品:

远程IP地址的字符串表示形式。例如,“74.125.127.100”或“2001:4860:a005::68”。如果套接字被破坏(例如,如果客户端断开连接),则值可能未定义


usefunctionreq,res,next{var ipInfo=getIPreq;console.logipInfo;/{clientIp:'127.0.0.1',clientproutable:false}next;}

如果您使用的是express.js



这给了我一个不同于tismyip.com给我的IP地址。为什么会这样?我在一个实例上安装了API服务。当我尝试从我的计算机访问它时,我得到的IP地址是10.2.XXX.YYY,而我的真实世界的IP地址是67.250.AAA.BBBit的request.headers['X-Forwarded-For']注意,net.Stream现在是net.Socket,文档保存在这里:对于任何感兴趣的人,对于Heroku,它是:request.headers['X-Forwarded-For']但是如何防止这些头的欺骗呢?这通常可以很好地工作,但是由于某种原因,我最近遇到了一个错误,无法读取undefined的属性“remoteAddress”,因为显然所有内容都是null/未定义的,包括req.connection.socket。我不确定为什么/什么条件会导致这种情况,但最好检查req.connection.socket是否存在,以避免发生这种情况时服务器崩溃。最后一行req.connection.socket.remoteAddress抛出错误。小心。返回的ip地址是::1。为什么?很惊讶没人提到这个。如果x-forwarded-for返回多个IP,则需要修剪数组结果。否则,您将得到一个带有前导空格的无效ip地址。像请求头一样执行['x-forwarded-for']| |.split'、'.pop.trim;或者使用逗号和空格进行拆分,或者返回127.0.0.1以表示无法确定用户的IP地址127.0.0.1和unknown之间存在很大差异…它返回了一些奇怪的信息:ffff:从Heroku测试时不是我的IP地址@edmar miyake的回答对我来说是正确的。我想知道如果你在“x-forwarded-for”案例中使用right2left查找,IP会是什么。var ip_info=get_ipreq,right_most_proxy=True,在某些设置中,客户端ip可能是最右边的ip。该方法为我返回clientIp:“::1”。它似乎不起作用。@JamEngulfer-ipware只有在ip地址通过request.headers[]正确传递给应用程序时才起作用。示例:AWS LBS在“x-forwarded-for”中发送Ip地址,而定制NginX使用其他变量。ipware尽最大努力找出IP地址,但前提是IP已在标头中传递。这不是一个好方法,因为ipv6不仅仅是7位+IPv4,而且可能完全不同。@Radek如果验证地址的开头,它符合specsee ctrl-f search for IPv4 mapped ip=ip.Length我个人从npm请求ip包装getClientIp以创建函数getClientIp4req{var ip=typeof req=='string'?req:getClientIpreq;return ip.length如果您使用的是Express,您可以使用req.ip source-试试这个:对于从本地主机工作的人-就像我一样,下面所有答案的结果都是所有答案
ks可能会来::1。这让我困惑了一段时间。后来发现::1是真实的IP地址,是本地主机的IPV6表示法。希望这对我有所帮助。在所有答案中,我认为这是最好的。这是一个全面的、防御性的代码,使用look-before-you-leaps.request.connection不推荐使用,请使用socket代替。如果您是对的,如果您希望ip作为字符串,则可以将最后一行替换为:ip=ip.split':'。不鼓励使用slice-1[0]代码只回答问题。你能解释一下这个答案是如何比旧的、解释得更好、投票率更高的答案更好吗?检查包请求ip的源代码,检查x-forwarded-for和各种其他标题,以查找流行的负载平衡器,如AWS ELB、Cloudflare、Akamai、nginx、,Rackspace LB和Riverbed的Stingrayit为我返回null。同样的事情是使用request.headers['x-forwarded-for']从数组中弹出来处理这个问题,这比从索引1获取元素更一般,索引1可以通过curl-header x-forwarded-for:1.2.3.4, 5.6.7.8 https://example.com.Will 如果服务器在代理后运行,则返回127.0.0.1的变体。使用或。@DanDascalescu如果设置app.set'trust proxy',true,req.ip将返回真实的ip地址,即使在proxy后面。谢谢你!这就是最终促使我启动并运行的原因——对nginx.conf的修改,奇怪的是,没有人提到过。request.connection被弃用了,请改用socket。
var userIp = req.header('X-Real-IP') || req.connection.remoteAddress;
let ip = request.headers['x-forwarded-for'].split(',')[0];
curl --header "X-Forwarded-For: 1.2.3.4" "https://example.com"
let ip = request.headers['x-forwarded-for'].split(',')[1];
console.log(clientIp);
console.log(req.ip);

console.log(req.headers['x-forwarded-for']);
console.log(req.connection.remoteAddress);
console.log(req.socket.remoteAddress);
console.log(req.connection.socket.remoteAddress.split(",")[0]);
  location /  {
                proxy_pass    http://localhost:3001;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $remote_addr;  # this line
                proxy_cache_bypass $http_upgrade; 
        }
module.exports =  function(req, res, next) {
    let enable = true; // true/false
    let blacklist = ['x.x.x.x'];
    let whitelist = ['x.x.x.x'];
    let clientIp = req.header('x-forwarded-for') || req.connection.remoteAddress;
    if (!clientIp) {
        return res.json('Error');
    }
    if (enable
        && paths.some((path) => (path === req.originalUrl))) {

        let blacklist = blacklist || [];
        if (blacklist.some((ip) => clientIp.match(ip) !== null)) {
            return res.json({ status: 401, error: 'Your IP is black-listed !'});
        }
        let whitelist = whitelist || [];
        if (whitelist.length === 0 || whitelist.some((ip) => clientIp.match(ip) !== null)) {
            next();
            return;
        } else {
            return res.json({ status: 401, error: 'Your IP is not listed !'});
        }
    }
    next();
};
    const express = require('express')
    const app = express()
    const port = 3000

    app.get('/', (req, res) => {
    var ip = req.ip
    console.log(ip);
    res.send('Hello World!')
    })

   // Run as nodejs ip.js
    app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
    })
app.post('/get/ip/address', function (req, res) {
      res.send(req.ip);
})