惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
Jina AI
Jina AI
Stack Overflow Blog
Stack Overflow Blog
量子位
博客园_首页
Vercel News
Vercel News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Forbes - Security
Forbes - Security
IT之家
IT之家
N
News and Events Feed by Topic
S
Security Affairs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Webroot Blog
Webroot Blog
Recorded Future
Recorded Future
L
LangChain Blog
Y
Y Combinator Blog
AI
AI
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Know Your Adversary
Know Your Adversary
AWS News Blog
AWS News Blog
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
L
Lohrmann on Cybersecurity
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google Online Security Blog
Google Online Security Blog
V2EX - 技术
V2EX - 技术
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
PCI Perspectives
PCI Perspectives
I
Intezer
T
Tenable Blog
G
Google Developers Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Troy Hunt's Blog
L
LINUX DO - 最新话题
云风的 BLOG
云风的 BLOG
C
CXSECURITY Database RSS Feed - CXSecurity.com
有赞技术团队
有赞技术团队
O
OpenAI News
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
C
Check Point Blog
Last Week in AI
Last Week in AI
S
Schneier on Security
Simon Willison's Weblog
Simon Willison's Weblog
Blog — PlanetScale
Blog — PlanetScale

博客园 - 喜欢Ⅰ

先避免毁灭性错误,再谈聪明决策 通往天堂的三个阶梯 人类随机数趣闻 - 即使人们会觉得它更随机,但实际上它更不随机 C 里面如何使用链表 list 项目代码套路 墨菲定律 - 人类一种误判心理 消息队列, 一种取舍的选择 Redis Stream CORS 跨域请求一种后端适配解决方案 自我的智慧 市场教父 André Kostolany Exception Handling Considered Harmful MySQL CREATE TABLE Template 模板设计简单交流 字符串转整型 atomic 原子自增工程案例 对炒股看法 吃饱年代 我是个怎样的人 格林童话之祖父和孙子 Linux 守护进程 智慧 ~ 引子 ~ 三则故事 交易人生 大道至简
HTTP 尝试获取 Client IP
喜欢Ⅰ · 2021-11-29 · via 博客园 - 喜欢Ⅰ

HTTP 中获取 Client IP 相关策略需求, 在当下网络环境中多数只能提供建议作用. 更多的是

通过其它唯一标识来挖掘更多潜在价值. 

本文主要就一个内容, 如何最大可能尝试在 HTTP 请求中获取 Client IP.

首先我们要大致了解些先验知识.

1. HTTP header key 不区分大小写

https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2

2. X-Forwarded-For 是 HTTP 代理事实标准

https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/X-Forwarded-For

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

3. 大部分网关使用的是 nginx

http {
    # 下面三行为重点, 添加后就可以获取到客户端真实 IP
    set_real_ip_from 0.0.0.0/0;
    real_ip_header X-Forwarded-For;
    real_ip_recursive on;

    # 下面三行为常见反向代理传递真实客户端 IP 的配置
    # 配置在 http{} 中, 则全局应用在下面的所有 server 中
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

https://amos-x.com/index.php/amos/archives/nginx-realip/

https://cloud.tencent.com/developer/article/1521273

基于上面信息, 我们在项目中开始构建脚手架方法

import (
    "fmt"
    "net"
    "net/http"
    "strings"
)

// X-Forwarded-For (XFF) 在客户端访问服务器的过程中如果需要经过 HTTP 代理或者负载均衡服务器,
// 可以被用来获取最初发起请求的客户端的 IP 地址, 这个消息首部成为事实上的标准.
var xForwardedForKeys = [...]string{"X-Forwarded-For", "x-forwarded-for", "X-FORWARDED-FOR"}

// XRealIP nginx 反向代理服务 IP
var xRealIPKeys = [...]string{"X-Real-IP", "X-Real-Ip", "x-real-ip", "X-REAL-IP"}

// GetClientIP 获取客户端 ip
func GetClientIP(r *http.Request) (ip string) {
    // X-Forwarded-For: <client>, <proxy1>, <proxy2>
    for _, xForwardedForKey := range xForwardedForKeys {
        xForwardedFor := strings.TrimSpace(r.Header.Get(xForwardedForKey))
        if len(xForwardedFor) > 0 {
            xForwardedFors := strings.Split(xForwardedFor, ",")
            ip = strings.TrimSpace(xForwardedFors[0])
            if len(ip) > 0 {
                return
            }
            break
        }
    }

    for _, xRealIPKey := range xRealIPKeys {
        ip = strings.TrimSpace(r.Header.Get(xRealIPKey))
        if len(ip) > 0 {
            return
        }
    }

    // 兜底直接使用 client 请求的 ip 地址
    ip, _, _ = net.SplitHostPort(r.RemoteAddr)
    return
}