richcms是一个使用golang+mysql开发的通用cms系统,今天分享richcms系统中获取IP地址的方法。richcms的在线文档地址:https://www.richcms.net/docs
func getIP(c *gow.Context) (ip string) {
ip = c.GetHeader("X-Original-Forwarded-For")
if ip == "" {
ip = c.GetHeader("Remote-Host")
}
if ip == "" {
ip = c.GetHeader("X-Real-IP")
}
if ip == "" {
ip = c.GetHeader("X-Forwarded-For")
}
if ip == "" {
addr := c.Request.RemoteAddr
ip, _, _ = net.SplitHostPort(addr)
}
if ip == "" {
ip = "10.10.10.2"
}
ips := strings.Split(ip, ",")
if len(ips) > 0 {
ip = ips[0]
}
ip = strings.TrimSpace(ip)
netIP := net.ParseIP(ip)
if netIP != nil {
return ip
}
return ip
}