简体   繁体   English

Node.js:获取客户端的 IP

[英]Node.js: Get client's IP

req.connection.remoteAddress, req.headers['x-forwarded-for'], req.ip, req.ips, what does it all mean? req.connection.remoteAddress、req.headers['x-forwarded-for']、req.ip、req.ips,这都是什么意思?

Is there a straight forward way to simply get the IP address of the client/user-agent making the request to my site in Node.js/Express?是否有一种直接的方法可以简单地获取向 Node.js/Express 中的站点发出请求的客户端/用户代理的 IP 地址? I'm not understanding all the proxy stuff or what all the differences between all the properties of the req object.我不了解所有代理内容或 req 对象的所有属性之间的所有差异。 Also, I don't understand what the 'trust proxy' option is for Express.另外,我不明白 Express 的“信任代理”选项是什么。

Could someone give me a straight forward explanation to what the difference is between all of these properties, and answer how I can just simply get the client's IP?有人可以直接向我解释所有这些属性之间的区别,并回答我如何简单地获取客户端的 IP 吗?

req.ip is the straightforward way to get the client's IP address in Express. req.ip是在 Express 中获取客户端 IP 地址的直接方法。 You can see the logic it uses (which involves grabbing the first item from the array of proxy addresses req.ips , where that array is constructed from the x-forwarded-for headers) here .你可以看到它使用逻辑(其涉及从代理地址的阵列抓取的第一项req.ips ,其中该阵列被从构造x-forwarded-for头) 这里

// Get client IP address from request object ----------------------
getClientAddress = function (req) {
        return (req.headers['x-forwarded-for'] || '').split(',')[0] 
        || req.connection.remoteAddress;
};

As other's have noted, due to the use potential use of proxies, you really should use req.ip and NOT use the X-Forwarded-For header like so many people are recommending.正如其他人所指出的,由于代理的潜在用途,您真的应该使用req.ip而不是像很多人推荐的那样使用 X-Forwarded-For 标头。 As long as you properly configure a proxy as a trusted proxy, req.ip will always return the end-user's IP address.只要您将代理正确配置为受信任的代理, req.ip将始终返回最终用户的 IP 地址。

eg If you had a proxy that was connecting from 8.8.8.8, you'd do:例如,如果你有一个从 8.8.8.8 连接的代理,你会这样做:

var express = require('express');
var app = express();
app.set('trust proxy', '8.8.8.8');

Since you trust the proxy, this would now make it so what is passed in the X-Forwarded-For header will be stored in req.ip , but ONLY if it originates from one of the trusted proxies.由于您信任代理,因此现在可以将 X-Forwarded-For 标头中req.ip存储在req.ip ,但req.ip是它来自受信任的代理之一。

More on trust proxy can be found here .可以在此处找到有关信任代理的更多信息

Now, as others have noted in the comments;现在,正如其他人在评论中指出的那样; especially when developing locally you may get the ip in the format of "::ffff:127.0.0.1".特别是在本地开发时,您可能会以“::ffff:127.0.0.1”格式获取ip

To always get the IPv4 Address I have:要始终获得 IPv4 地址,我有:

getClientAddress = function (req) {
    return req.ip.split(":").pop();
};

very simple很简单

function getClientIP(req){
    return req.headers['x-forwarded-for'] || req.connection.remoteAddress;
}

Getting the client IP is pretty straightforward:获取客户端 IP 非常简单:

 var ip = req.headers['x-forwarded-for'] || 
     req.connection.remoteAddress || 
     req.socket.remoteAddress ||
     req.connection.socket.remoteAddress;
     console.log(ip);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM