代码实例:
package main
import (
"net"
"net/http"
"strings"
)
func GetIP(w http.ResponseWriter, r *http.Request) {
// 尝试从 X-Forwarded-For 中获取
xForwardedFor := r.Header.Get(`X-Forwarded-For`)
ip := strings.TrimSpace(strings.Split(xForwardedFor, `,`)[0])
if ip == `` {
// 尝试从 X-Real-Ip 中获取
ip = strings.TrimSpace(r.Header.Get(`X-Real-Ip`))
if ip == `` {
// 直接从 Remote Addr 中获取
_ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr))
if err != nil {
panic(err)
} else {
ip = _ip
}
}
}
// 从控制台输出获取到的IP地址
println(ip)
}
func main() {
http.HandleFunc(`/`, GetIP)
err := http.ListenAndServe(`:80`, nil)
if err != nil {
panic(err)
}
}
网友评论