From 6505594fd06008618908080c5920d1f165ad15b6 Mon Sep 17 00:00:00 2001 From: hoteas <等> Date: Sat, 20 Aug 2022 22:38:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AE=B0=E5=BD=95IP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- context.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/context.go b/context.go index a134dca..9d23f22 100644 --- a/context.go +++ b/context.go @@ -5,6 +5,7 @@ import ( . "code.hoteas.com/golang/hotime/db" "encoding/json" "net/http" + "strings" "time" ) @@ -74,6 +75,16 @@ 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") + } + } + that.Log["ip"] = ipStr that.Db.Insert("logs", that.Log) }