This commit is contained in:
John Guo
2022-02-22 21:32:48 +08:00
parent f0cd3c084e
commit 20a50cb198

View File

@ -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
}