From f0e8f18c6dc52cdc16aa5f6dbee9a3250190ea3a Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Mon, 14 Jun 2021 20:08:30 +0800 Subject: [PATCH] =?UTF-8?q?refactor=F0=9F=8E=A8:=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AFip=E8=8E=B7=E5=8F=96=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/global/adm.go | 2 +- common/ip.go | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/common/global/adm.go b/common/global/adm.go index 6081753b..cde84ec1 100644 --- a/common/global/adm.go +++ b/common/global/adm.go @@ -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 ( diff --git a/common/ip.go b/common/ip.go index bc8b97f6..7822f259 100644 --- a/common/ip.go +++ b/common/ip.go @@ -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 }