diff --git a/net/ghttp/ghttp_request.go b/net/ghttp/ghttp_request.go index da33d83d7..cbdf12fef 100644 --- a/net/ghttp/ghttp_request.go +++ b/net/ghttp/ghttp_request.go @@ -184,30 +184,31 @@ func (r *Request) IsAjaxRequest() bool { // GetClientIp returns the client ip of this request without port. // Note that this ip address might be modified by client header. func (r *Request) GetClientIp() string { - if len(r.clientIp) == 0 { - realIps := r.Header.Get("X-Forwarded-For") - if realIps != "" && len(realIps) != 0 && !strings.EqualFold("unknown", realIps) { - ipArray := strings.Split(realIps, ",") - r.clientIp = ipArray[0] - } - if r.clientIp == "" || strings.EqualFold("unknown", realIps) { - r.clientIp = r.Header.Get("Proxy-Client-IP") - } - if r.clientIp == "" || strings.EqualFold("unknown", realIps) { - r.clientIp = r.Header.Get("WL-Proxy-Client-IP") - } - if r.clientIp == "" || strings.EqualFold("unknown", realIps) { - r.clientIp = r.Header.Get("HTTP_CLIENT_IP") - } - if r.clientIp == "" || strings.EqualFold("unknown", realIps) { - r.clientIp = r.Header.Get("HTTP_X_FORWARDED_FOR") - } - if r.clientIp == "" || strings.EqualFold("unknown", realIps) { - r.clientIp = r.Header.Get("X-Real-IP") - } - if r.clientIp == "" || strings.EqualFold("unknown", realIps) { - r.clientIp = r.GetRemoteIp() - } + if r.clientIp == "" { + return r.clientIp + } + realIps := r.Header.Get("X-Forwarded-For") + if realIps != "" && len(realIps) != 0 && !strings.EqualFold("unknown", realIps) { + ipArray := strings.Split(realIps, ",") + r.clientIp = ipArray[0] + } + if r.clientIp == "" { + r.clientIp = r.Header.Get("Proxy-Client-IP") + } + if r.clientIp == "" { + r.clientIp = r.Header.Get("WL-Proxy-Client-IP") + } + if r.clientIp == "" { + r.clientIp = r.Header.Get("HTTP_CLIENT_IP") + } + if r.clientIp == "" { + r.clientIp = r.Header.Get("HTTP_X_FORWARDED_FOR") + } + if r.clientIp == "" { + r.clientIp = r.Header.Get("X-Real-IP") + } + if r.clientIp == "" { + r.clientIp = r.GetRemoteIp() } return r.clientIp }