From aa7d67e387baf6ed5e5f87b842f02bdd20cdf599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Fri, 25 Aug 2023 15:30:59 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BD=91=E7=AB=99=E8=AE=BE=E7=BD=AE=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=98=AF=E5=90=A6=E6=94=AF=E6=8C=81${serverAddr}?= =?UTF-8?q?=E9=80=89=E9=A1=B9/=E5=A2=9E=E5=BC=BA${serverAddr}=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/http_request.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/internal/nodes/http_request.go b/internal/nodes/http_request.go index da5c1fb..4ceb41d 100644 --- a/internal/nodes/http_request.go +++ b/internal/nodes/http_request.go @@ -855,13 +855,19 @@ func (this *HTTPRequest) Format(source string) string { case "serverName": return this.ServerName case "serverAddr": - var requestConn = this.RawReq.Context().Value(HTTPConnContextKey) - if requestConn != nil { - conn, ok := requestConn.(net.Conn) - if ok { - host, _, _ := net.SplitHostPort(conn.LocalAddr().String()) - if len(host) > 0 { - return host + var nodeConfig = this.nodeConfig + if nodeConfig != nil && nodeConfig.GlobalServerConfig != nil && nodeConfig.GlobalServerConfig.HTTPAll.EnableServerAddrVariable { + if len(this.requestRemoteAddrs()) > 1 { + return "" // hidden for security + } + var requestConn = this.RawReq.Context().Value(HTTPConnContextKey) + if requestConn != nil { + conn, ok := requestConn.(net.Conn) + if ok { + host, _, _ := net.SplitHostPort(conn.LocalAddr().String()) + if len(host) > 0 { + return host + } } } } @@ -1234,7 +1240,7 @@ func (this *HTTPRequest) requestRemoteAddrs() (result []string) { var forwardedFor = this.RawReq.Header.Get("X-Forwarded-For") if len(forwardedFor) > 0 { commaIndex := strings.Index(forwardedFor, ",") - if commaIndex > 0 { + if commaIndex > 0 && !lists.ContainsString(result, forwardedFor[:commaIndex]) { result = append(result, forwardedFor[:commaIndex]) } } @@ -1242,7 +1248,7 @@ func (this *HTTPRequest) requestRemoteAddrs() (result []string) { // Real-IP { realIP, ok := this.RawReq.Header["X-Real-IP"] - if ok && len(realIP) > 0 { + if ok && len(realIP) > 0 && !lists.ContainsString(result, realIP[0]) { result = append(result, realIP[0]) } } @@ -1250,7 +1256,7 @@ func (this *HTTPRequest) requestRemoteAddrs() (result []string) { // Real-Ip { realIP, ok := this.RawReq.Header["X-Real-Ip"] - if ok && len(realIP) > 0 { + if ok && len(realIP) > 0 && !lists.ContainsString(result, realIP[0]) { result = append(result, realIP[0]) } } @@ -1260,7 +1266,9 @@ func (this *HTTPRequest) requestRemoteAddrs() (result []string) { var remoteAddr = this.RawReq.RemoteAddr host, _, err := net.SplitHostPort(remoteAddr) if err == nil { - result = append(result, host) + if !lists.ContainsString(result, host) { + result = append(result, host) + } } else { result = append(result, remoteAddr) }