From e0cf7af7a99f8c3689c471591507f2b7b1c372c3 Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Sun, 13 Jun 2021 21:20:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor=F0=9F=8E=A8:=20=E8=B0=83=E6=95=B4?= =?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=E4=BB=A5=E5=8F=8A=E6=97=A5=E5=BF=97=E4=B8=AD=E5=AF=B9?= =?UTF-8?q?=E5=BA=94=E4=BD=8D=E7=BD=AE=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/ip.go | 17 +++++++++++++++++ common/middleware/customerror.go | 3 ++- common/middleware/handler/auth.go | 7 +++++-- common/middleware/logger.go | 11 ++++------- 4 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 common/ip.go diff --git a/common/ip.go b/common/ip.go new file mode 100644 index 00000000..bc8b97f6 --- /dev/null +++ b/common/ip.go @@ -0,0 +1,17 @@ +package common + +import ( + "github.com/gin-gonic/gin" + "strings" +) + +func GetClientIP(ctx *gin.Context) string { + ip := ctx.Request.Header.Get("X-Forwarded-For") + if strings.Contains(ip, "127.0.0.1") || ip == "" { + ip = ctx.Request.Header.Get("X-real-ip") + } + if ip == "" { + return "127.0.0.1" + } + return ip +} diff --git a/common/middleware/customerror.go b/common/middleware/customerror.go index 4c56baee..a16dccbb 100644 --- a/common/middleware/customerror.go +++ b/common/middleware/customerror.go @@ -2,6 +2,7 @@ package middleware import ( "fmt" + "go-admin/common" "net/http" "strconv" "strings" @@ -33,7 +34,7 @@ func CustomError(c *gin.Context) { c.Request.URL, statusCode, c.Request.RequestURI, - c.ClientIP(), + common.GetClientIP(c), p[2], ) c.JSON(http.StatusOK, gin.H{ diff --git a/common/middleware/handler/auth.go b/common/middleware/handler/auth.go index a685c71f..fcb79c7c 100644 --- a/common/middleware/handler/auth.go +++ b/common/middleware/handler/auth.go @@ -1,7 +1,9 @@ package handler import ( + "fmt" "go-admin/app/admin/models" + "go-admin/common" "net/http" "github.com/gin-gonic/gin" @@ -115,8 +117,9 @@ func LoginLogToDB(c *gin.Context, status string, msg string, username string) { l := make(map[string]interface{}) ua := user_agent.New(c.Request.UserAgent()) - l["ipaddr"] = c.ClientIP() - l["loginLocation"] = pkg.GetLocation(c.ClientIP(),gaConfig.ExtConfig.AMap.Key) + l["ipaddr"] = common.GetClientIP(c) + fmt.Println("gaConfig.ExtConfig.AMap.Key", gaConfig.ExtConfig.AMap.Key) + l["loginLocation"] = pkg.GetLocation(common.GetClientIP(c),gaConfig.ExtConfig.AMap.Key) l["loginTime"] = pkg.GetCurrentTime() l["status"] = status l["remark"] = c.Request.UserAgent() diff --git a/common/middleware/logger.go b/common/middleware/logger.go index bbf97680..627afd4c 100644 --- a/common/middleware/logger.go +++ b/common/middleware/logger.go @@ -5,6 +5,7 @@ import ( "bytes" "encoding/json" "fmt" + "go-admin/common" gaConfig "go-admin/config" "io" "io/ioutil" @@ -80,12 +81,7 @@ func LoggerToFile() gin.HandlerFunc { // 状态码 statusCode := c.Writer.Status() // 请求IP - clientIP := c.ClientIP() - - - fmt.Println("Headers:", c.Request.Header.Get("X-Real-IP")) - - fmt.Println("clientIP:", clientIP) + clientIP := common.GetClientIP(c) // 执行时间 latencyTime := endTime.Sub(startTime) // 日志格式 @@ -111,6 +107,7 @@ func SetDBOperLog(c *gin.Context, clientIP string, statusCode int, reqUri string l["_fullPath"] = c.FullPath() l["operUrl"] = reqUri l["operIp"] = clientIP + fmt.Println("gaConfig.ExtConfig.AMap.Key", gaConfig.ExtConfig.AMap.Key) l["operLocation"] = pkg.GetLocation(clientIP, gaConfig.ExtConfig.AMap.Key) l["operName"] = user.GetUserName(c) l["requestMethod"] = c.Request.Method @@ -135,4 +132,4 @@ func SetDBOperLog(c *gin.Context, clientIP string, statusCode int, reqUri string log.Errorf("Append message error, %s", err.Error()) } } -} \ No newline at end of file +}