Skip to main content

如何获取客户端的 IP 地址

¥How to get the IP address of the client

直接连接

¥Direct connection

客户端的 IP 地址可以在 handshake 对象中找到:

¥The IP address of the client can be found in the handshake object:

io.on("connection", (socket) => {
const ipAddress = socket.handshake.address;

console.log(ipAddress); // prints something like "203.0.113.195" (IPv4) or "2001:db8:85a3:8d3:1319:8a2e:370:7348" (IPv6)
});

在代理后面

¥Behind a proxy

如果你使用像 nginx 这样的代理,则 address 属性将是代理的 IP 地址。

¥If you are behind a proxy like nginx, the address attribute will be the IP address of the proxy.

在这种情况下,客户端的 IP 地址将在请求标头中找到。

¥In that case, the IP address of the client will be found in the request headers.

X-Forwarded-For 标头

¥X-Forwarded-For header

X-Forwarded-For 请求标头是事实上的标准标头,用于识别通过代理服务器连接到 Web 服务器的客户端的原始 IP 地址。

¥The X-Forwarded-For request header was a de-facto standard header for identifying the originating IP address of a client connecting to a web server through a proxy server.

参考:https://web.nodejs.cn/en-US/docs/Web/HTTP/Headers/X-Forwarded-For

¥Reference: https://web.nodejs.cn/en-US/docs/Web/HTTP/Headers/X-Forwarded-For

格式:

¥Format:

X-Forwarded-For: <client>, <proxy1>, <proxy2>

以下是检索客户端 IP 地址的方法:

¥Here's how you can retrieve the IP address of the client:

io.on("connection", (socket) => {
const ipAddress = socket.handshake.headers["x-forwarded-for"].split(",")[0];

console.log(ipAddress);
});
note

X-Forwarded-For 标头现已弃用(尽管仍广泛使用),取而代之的是标准 Forwarded 标头。

¥The X-Forwarded-For header is now deprecated (although still widely used) in favor of the standard Forwarded header.

Forwarded 标头

¥Forwarded header

Forwarded 请求标头是用于识别通过代理服务器连接到 Web 服务器的客户端的原始 IP 地址的标准标头。

¥The Forwarded request header is the standard header for identifying the originating IP address of a client connecting to a web server through a proxy server.

参考:https://web.nodejs.cn/en-US/docs/Web/HTTP/Headers/Forwarded

¥Reference: https://web.nodejs.cn/en-US/docs/Web/HTTP/Headers/Forwarded

格式:

¥Format:

Forwarded: by=<identifier>;for=<identifier>;host=<host>;proto=http|https

以下是检索客户端 IP 地址的方法:

¥Here's how you can retrieve the IP address of the client:

function parseHeader(header) {
for (const directive of header.split(",")[0].split(";")) {
if (directive.startsWith("for=")) {
return directive.substring(4);
}
}
}

io.on("connection", (socket) => {
const ipAddress = parseHeader(socket.handshake.headers["forwarded"] || "");

console.log(ipAddress);
});
note

parseHeader() 方法并未涵盖规范允许的每种边缘情况。如果你需要更强大的方法,请检查 forwarded-parse 包。

¥This parseHeader() method does not cover every edge case allowed by the specification. If you need a more robust method, please check the forwarded-parse package.

CloudFlare

CloudFlare 使用特定标头:cf-connecting-ip

¥CloudFlare uses a specific header: cf-connecting-ip

参考:https://developers.cloudflare.com/fundamentals/reference/http-request-headers/

¥Reference: https://developers.cloudflare.com/fundamentals/reference/http-request-headers/

以下是检索客户端 IP 地址的方法:

¥Here's how you can retrieve the IP address of the client:

io.on("connection", (socket) => {
const ipAddress = socket.handshake.headers["cf-connecting-ip"];

console.log(ipAddress);
});

快速

¥Fastly

快速使用特定标头:fastly-client-ip

¥Fastly uses a specific header: fastly-client-ip

参考:https://developer.fastly.com/reference/http/http-headers/Fastly-Client-IP/

¥Reference: https://developer.fastly.com/reference/http/http-headers/Fastly-Client-IP/

以下是检索客户端 IP 地址的方法:

¥Here's how you can retrieve the IP address of the client:

io.on("connection", (socket) => {
const ipAddress = socket.handshake.headers["fastly-client-ip"];

console.log(ipAddress);
});