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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 喜欢Ⅰ

人类随机数趣闻 - 即使人们会觉得它更随机,但实际上它更不随机 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
}