之前也是简单记载了一下获取http中的ip地址 这是之前的文章 https://blog.csdn.net/shunuanwei/article/details/121746761?spm=1001.2014.3001.5501 这次,我突然想到这个地方 ,我在仔细研究一下,或者说,在调整一下代码 嗯~~
import "net/http"
func RemoteIp(req *http.Request) string {
var remoteAddr string
remoteAddr = req.RemoteAddr
if remoteAddr != "" {
return remoteAddr
}
remoteAddr = req.Header.Get("ipv4")
if remoteAddr != "" {
return remoteAddr
}
remoteAddr = req.Header.Get("XForwardedFor")
if remoteAddr != "" {
return remoteAddr
}
remoteAddr = req.Header.Get("X-Forwarded-For")
if remoteAddr != "" {
return remoteAddr
}
remoteAddr = req.Header.Get("X-Real-Ip")
if remoteAddr != "" {
return remoteAddr
} else {
remoteAddr = "127.0.0.1"
}
return remoteAddr
}
|