From 89e122cd312924cf24809164b74061379515c058 Mon Sep 17 00:00:00 2001 From: Kui_Ye <938424168@qq.com> Date: Thu, 11 Jun 2020 09:49:16 +0800 Subject: [PATCH] marge getip func --- net/ghttp/ghttp_request.go | 54 +++++++++++++++----------------------- 1 file changed, 21 insertions(+), 33 deletions(-) diff --git a/net/ghttp/ghttp_request.go b/net/ghttp/ghttp_request.go index 421ad4c90..3cc0e6d9c 100644 --- a/net/ghttp/ghttp_request.go +++ b/net/ghttp/ghttp_request.go @@ -13,7 +13,6 @@ import ( "github.com/gogf/gf/os/gres" "github.com/gogf/gf/os/gsession" "github.com/gogf/gf/os/gview" - "github.com/gogf/gf/text/gstr" "github.com/gogf/gf/util/guid" "net/http" "strings" @@ -51,7 +50,6 @@ type Request struct { exit bool // A bool marking whether current request is exited. parsedHost string // The parsed host name for current host used by GetHost function. clientIp string // The parsed client ip for current host used by GetClientIp function. - clientRealIp string // The parsed client real ip for current host used by GetClientIp function. bodyContent []byte // Request body content. isFileRequest bool // A bool marking whether current request is file serving. viewObject *gview.View // Custom template view engine object for this response. @@ -154,7 +152,27 @@ func (r *Request) IsAjaxRequest() bool { // GetClientIp returns the client ip of this request without port. func (r *Request) GetClientIp() string { if len(r.clientIp) == 0 { - if r.clientIp = r.Header.Get("X-Real-IP"); r.clientIp == "" { + readlIps := r.Header.Get("X-Forwarded-For") + if readlIps != "" && len(readlIps) != 0 && !strings.EqualFold("unknown", readlIps) { + ipArray := strings.Split(readlIps, ",") + r.clientIp = ipArray[0] + } + if r.clientIp == "" || len(readlIps) == 0 || strings.EqualFold("unknown", readlIps) { + r.clientIp = r.Header.Get("Proxy-Client-IP") + } + if r.clientIp == "" || len(readlIps) == 0 || strings.EqualFold("unknown", readlIps) { + r.clientIp = r.Header.Get("WL-Proxy-Client-IP") + } + if r.clientIp == "" || len(readlIps) == 0 || strings.EqualFold("unknown", readlIps) { + r.clientIp = r.Header.Get("HTTP_CLIENT_IP") + } + if r.clientIp == "" || len(readlIps) == 0 || strings.EqualFold("unknown", readlIps) { + r.clientIp = r.Header.Get("HTTP_X_FORWARDED_FOR") + } + if r.clientIp == "" || len(readlIps) == 0 || strings.EqualFold("unknown", readlIps) { + r.clientIp = r.Header.Get("X-Real-IP") + } + if r.clientIp == "" || len(readlIps) == 0 || strings.EqualFold("unknown", readlIps) { array, _ := gregex.MatchString(`(.+):(\d+)`, r.RemoteAddr) if len(array) > 1 { r.clientIp = array[1] @@ -166,36 +184,6 @@ func (r *Request) GetClientIp() string { return r.clientIp } -func (r *Request) GetClientRealIp() string { - if len(r.clientRealIp) == 0 { - readlIps := r.Header.Get("X-Forwarded-For") - if readlIps != "" && len(readlIps) != 0 && !strings.EqualFold("unknown", readlIps) { - ipArray := gstr.Split(readlIps, ",") - // g.Log().Info("ipArray",ipArray) - r.clientRealIp = ipArray[0] - } - if r.clientRealIp == "" || len(readlIps) == 0 || strings.EqualFold("unknown", readlIps) { - r.clientRealIp = r.Header.Get("Proxy-Client-IP") - } - if r.clientRealIp == "" || len(readlIps) == 0 || strings.EqualFold("unknown", readlIps) { - r.clientRealIp = r.Header.Get("WL-Proxy-Client-IP") - } - if r.clientRealIp == "" || len(readlIps) == 0 || strings.EqualFold("unknown", readlIps) { - r.clientRealIp = r.Header.Get("HTTP_CLIENT_IP") - } - if r.clientRealIp == "" || len(readlIps) == 0 || strings.EqualFold("unknown", readlIps) { - r.clientRealIp = r.Header.Get("HTTP_X_FORWARDED_FOR") - } - if r.clientRealIp == "" || len(readlIps) == 0 || strings.EqualFold("unknown", readlIps) { - r.clientRealIp = r.Header.Get("X-Real-IP") - } - if r.clientRealIp == "" || len(readlIps) == 0 || strings.EqualFold("unknown", readlIps) { - r.clientRealIp = r.RemoteAddr - } - } - return r.clientRealIp -} - // GetUrl returns current URL of this request. func (r *Request) GetUrl() string { scheme := "http"