marge getip func

This commit is contained in:
Kui_Ye
2020-06-11 09:49:16 +08:00
parent 3138ad10ec
commit 89e122cd31

View File

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