fix: 用 TrustedPlatform 替代 SetTrustedProxies 获取真实 IP
SetTrustedProxies 在 Unix socket 下无效:socket 连接的 RemoteAddr 是 UnixAddr(如'@'),无法匹配任何 CIDR,Gin 跳过 header 解析。 TrustedPlatform 直接从指定 header 取值,不做来源校验,才是 Unix socket 反代的正确方案。
This commit is contained in:
@@ -83,10 +83,10 @@ func main() {
|
||||
|
||||
r := gin.Default()
|
||||
|
||||
// 信任反向代理,从 X-Real-IP / X-Forwarded-For 获取真实客户端 IP
|
||||
// Unix socket 下无法按来源 IP 判断信任,全信任(安全边界由 Caddy 把控)
|
||||
_ = r.SetTrustedProxies([]string{"0.0.0.0/0", "::/0"})
|
||||
r.RemoteIPHeaders = []string{"X-Real-IP", "X-Forwarded-For"}
|
||||
// 信任反向代理传来的真实 IP
|
||||
// TrustedPlatform 直接从指定 header 取 IP,不做来源校验
|
||||
// 这是 Unix socket 反代的正确做法(socket 连接没有来源 IP,无法做 CIDR 匹配)
|
||||
r.TrustedPlatform = "X-Real-IP"
|
||||
|
||||
// 加载 HTML 模板
|
||||
r.SetHTMLTemplate(loadTemplates())
|
||||
|
||||
Reference in New Issue
Block a user