improve function GetClientIp for ghttp.Request
@@ -152,27 +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 {
- readlIps := r.Header.Get("X-Forwarded-For")
- if readlIps != "" && len(readlIps) != 0 && !strings.EqualFold("unknown", readlIps) {
- ipArray := strings.Split(readlIps, ",")
+ 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 == "" || len(readlIps) == 0 || strings.EqualFold("unknown", readlIps) {
+ if r.clientIp == "" || strings.EqualFold("unknown", realIps) {
r.clientIp = r.Header.Get("Proxy-Client-IP")
}
- if r.clientIp == "" || len(readlIps) == 0 || strings.EqualFold("unknown", readlIps) {
+ if r.clientIp == "" || strings.EqualFold("unknown", realIps) {
r.clientIp = r.Header.Get("WL-Proxy-Client-IP")
}
- if r.clientIp == "" || len(readlIps) == 0 || strings.EqualFold("unknown", readlIps) {
+ if r.clientIp == "" || strings.EqualFold("unknown", realIps) {
r.clientIp = r.Header.Get("HTTP_CLIENT_IP")
}
- if r.clientIp == "" || len(readlIps) == 0 || strings.EqualFold("unknown", readlIps) {
+ if r.clientIp == "" || strings.EqualFold("unknown", realIps) {
r.clientIp = r.Header.Get("HTTP_X_FORWARDED_FOR")
}
- if r.clientIp == "" || len(readlIps) == 0 || strings.EqualFold("unknown", readlIps) {
+ if r.clientIp == "" || strings.EqualFold("unknown", realIps) {
r.clientIp = r.Header.Get("X-Real-IP")
}
- if r.clientIp == "" || len(readlIps) == 0 || strings.EqualFold("unknown", readlIps) {
+ if r.clientIp == "" || strings.EqualFold("unknown", realIps) {
array, _ := gregex.MatchString(`(.+):(\d+)`, r.RemoteAddr)
if len(array) > 1 {
r.clientIp = array[1]
