From b6ba3486d60c2d65ffc9940c387130a0ef480296 Mon Sep 17 00:00:00 2001 From: hoteas Date: Fri, 2 Sep 2022 00:04:23 +0800 Subject: [PATCH] =?UTF-8?q?IP=E8=8E=B7=E5=8F=96=E7=9C=9F=E5=AE=9E=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application.go | 18 +++++++++++------- context.go | 17 ++++++++++------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/application.go b/application.go index b4c962e..4628731 100644 --- a/application.go +++ b/application.go @@ -357,14 +357,18 @@ func (that *Application) handler(w http.ResponseWriter, req *http.Request) { defer func() { //是否展示日志 if that.WebConnectLog != nil { - ipStr := Substr(context.Req.RemoteAddr, 0, strings.Index(context.Req.RemoteAddr, ":")) + //负载均衡优化 - if ipStr == "127.0.0.1" { - if req.Header.Get("X-Forwarded-For") != "" { - ipStr = req.Header.Get("X-Forwarded-For") - } else if req.Header.Get("X-Real-IP") != "" { - ipStr = req.Header.Get("X-Real-IP") - } + ipStr := "" + if req.Header.Get("X-Forwarded-For") != "" { + ipStr = req.Header.Get("X-Forwarded-For") + } else if req.Header.Get("X-Real-IP") != "" { + ipStr = req.Header.Get("X-Real-IP") + } + //负载均衡优化 + if ipStr == "" { + //RemoteAddr := that.Req.RemoteAddr + ipStr = Substr(context.Req.RemoteAddr, 0, strings.Index(context.Req.RemoteAddr, ":")) } that.WebConnectLog.Infoln(ipStr, context.Req.Method, diff --git a/context.go b/context.go index 9d23f22..df14cd7 100644 --- a/context.go +++ b/context.go @@ -75,14 +75,17 @@ func (that *Context) View() { if that.Session("user_id").Data != nil { that.Log["user_id"] = that.Session("user_id").ToCeilInt() } - ipStr := Substr(that.Req.RemoteAddr, 0, strings.Index(that.Req.RemoteAddr, ":")) //负载均衡优化 - if ipStr == "127.0.0.1" { - if that.Req.Header.Get("X-Forwarded-For") != "" { - ipStr = that.Req.Header.Get("X-Forwarded-For") - } else if that.Req.Header.Get("X-Real-IP") != "" { - ipStr = that.Req.Header.Get("X-Real-IP") - } + ipStr := "" + if that.Req.Header.Get("X-Forwarded-For") != "" { + ipStr = that.Req.Header.Get("X-Forwarded-For") + } else if that.Req.Header.Get("X-Real-IP") != "" { + ipStr = that.Req.Header.Get("X-Real-IP") + } + //负载均衡优化 + if ipStr == "" { + //RemoteAddr := that.Req.RemoteAddr + ipStr = Substr(that.Req.RemoteAddr, 0, strings.Index(that.Req.RemoteAddr, ":")) } that.Log["ip"] = ipStr that.Db.Insert("logs", that.Log)