问,如何获取客户端ip ?

ys89576041 · · 1102 次点击
polaris
社区,需要你我一同完善!
```go const ( XForwardedFor = "X-Forwarded-For" XRealIP = "X-Real-IP" ) // RemoteIp 返回远程客户端的 IP,如 192.168.1.1 func RemoteIp(req *http.Request) string { remoteAddr := req.RemoteAddr if ip := req.Header.Get(XRealIP); ip != "" { remoteAddr = ip } else if ip = req.Header.Get(XForwardedFor); ip != "" { remoteAddr = ip } else { remoteAddr, _, _ = net.SplitHostPort(remoteAddr) } if remoteAddr == "::1" { remoteAddr = "127.0.0.1" } return remoteAddr } ``` 供参考
#1
更多评论
<a href="/user/polaris" title="@polaris">@polaris</a> 非常感谢!
#2