问,如何获取客户端ip ?

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