refactor🎨: 更新客户端ip获取方法

This commit is contained in:
zhangwenjian committed 2021-06-14 20:08:30 +08:00
1 parent 880a3700d1
commit f0e8f18c6d
2 files changed
+17 -5

No files matched your search

+1 -1
View File
@@ -2,7 +2,7 @@ package global
const (
// Version go-admin version info
Version = "2.0.0-beta.3"
Version = "2.0.0-beta.4"
)
var (
+16 -4
View File
@@ -1,17 +1,29 @@
package common
import (
"fmt"
"github.com/gin-gonic/gin"
"strings"
)
func GetClientIP(ctx *gin.Context) string {
ip := ctx.Request.Header.Get("X-Forwarded-For")
func GetClientIP(c *gin.Context) string {
ClientIP := c.ClientIP()
fmt.Println("ClientIP:", ClientIP)
RemoteIP, _ := c.RemoteIP()
fmt.Println("RemoteIP:", RemoteIP)
ip := c.Request.Header.Get("X-Forwarded-For")
if strings.Contains(ip, "127.0.0.1") || ip == "" {
ip = ctx.Request.Header.Get("X-real-ip")
ip = c.Request.Header.Get("X-real-ip")
}
if ip == "" {
return "127.0.0.1"
ip = "127.0.0.1"
}
if RemoteIP.String() != "127.0.0.1" {
ip = RemoteIP.String()
}
if ClientIP != "127.0.0.1" {
ip = ClientIP
}
fmt.Println("ip:", ip)
return ip
}