mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-27 01:50:30 +08:00
服务支持自定义访客IP地址获取方式/对X-Real-IP等Header值进行有效性验证
This commit is contained in:
@@ -322,6 +322,11 @@ func (this *HTTPRequest) configureWeb(web *serverconfigs.HTTPWebConfig, isTop bo
|
|||||||
this.web.Root = web.Root
|
this.web.Root = web.Root
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// remote addr
|
||||||
|
if web.RemoteAddr != nil && (web.RemoteAddr.IsPrior || isTop) && web.RemoteAddr.IsOn {
|
||||||
|
this.web.RemoteAddr = web.RemoteAddr
|
||||||
|
}
|
||||||
|
|
||||||
// charset
|
// charset
|
||||||
if web.Charset != nil && (web.Charset.IsPrior || isTop) {
|
if web.Charset != nil && (web.Charset.IsPrior || isTop) {
|
||||||
this.web.Charset = web.Charset
|
this.web.Charset = web.Charset
|
||||||
@@ -505,7 +510,9 @@ func (this *HTTPRequest) Format(source string) string {
|
|||||||
case "edgeVersion":
|
case "edgeVersion":
|
||||||
return teaconst.Version
|
return teaconst.Version
|
||||||
case "remoteAddr":
|
case "remoteAddr":
|
||||||
return this.requestRemoteAddr()
|
return this.requestRemoteAddr(true)
|
||||||
|
case "remoteAddrValue":
|
||||||
|
return this.requestRemoteAddr(false)
|
||||||
case "rawRemoteAddr":
|
case "rawRemoteAddr":
|
||||||
addr := this.RawReq.RemoteAddr
|
addr := this.RawReq.RemoteAddr
|
||||||
host, _, err := net.SplitHostPort(addr)
|
host, _, err := net.SplitHostPort(addr)
|
||||||
@@ -761,22 +768,36 @@ func (this *HTTPRequest) addVarMapping(varMapping map[string]string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 获取请求的客户端地址
|
// 获取请求的客户端地址
|
||||||
func (this *HTTPRequest) requestRemoteAddr() string {
|
func (this *HTTPRequest) requestRemoteAddr(supportVar bool) string {
|
||||||
|
if supportVar &&
|
||||||
|
this.web.RemoteAddr != nil &&
|
||||||
|
this.web.RemoteAddr.IsOn &&
|
||||||
|
!this.web.RemoteAddr.IsEmpty() {
|
||||||
|
var remoteAddr = this.Format(this.web.RemoteAddr.Value)
|
||||||
|
if net.ParseIP(remoteAddr) != nil {
|
||||||
|
return remoteAddr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// X-Forwarded-For
|
// X-Forwarded-For
|
||||||
forwardedFor := this.RawReq.Header.Get("X-Forwarded-For")
|
forwardedFor := this.RawReq.Header.Get("X-Forwarded-For")
|
||||||
if len(forwardedFor) > 0 {
|
if len(forwardedFor) > 0 {
|
||||||
commaIndex := strings.Index(forwardedFor, ",")
|
commaIndex := strings.Index(forwardedFor, ",")
|
||||||
if commaIndex > 0 {
|
if commaIndex > 0 {
|
||||||
return forwardedFor[:commaIndex]
|
forwardedFor = forwardedFor[:commaIndex]
|
||||||
|
}
|
||||||
|
if net.ParseIP(forwardedFor) != nil {
|
||||||
|
return forwardedFor
|
||||||
}
|
}
|
||||||
return forwardedFor
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Real-IP
|
// Real-IP
|
||||||
{
|
{
|
||||||
realIP, ok := this.RawReq.Header["X-Real-IP"]
|
realIP, ok := this.RawReq.Header["X-Real-IP"]
|
||||||
if ok && len(realIP) > 0 {
|
if ok && len(realIP) > 0 {
|
||||||
return realIP[0]
|
if net.ParseIP(realIP[0]) != nil {
|
||||||
|
return realIP[0]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -784,7 +805,9 @@ func (this *HTTPRequest) requestRemoteAddr() string {
|
|||||||
{
|
{
|
||||||
realIP, ok := this.RawReq.Header["X-Real-Ip"]
|
realIP, ok := this.RawReq.Header["X-Real-Ip"]
|
||||||
if ok && len(realIP) > 0 {
|
if ok && len(realIP) > 0 {
|
||||||
return realIP[0]
|
if net.ParseIP(realIP[0]) != nil {
|
||||||
|
return realIP[0]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ func (this *HTTPRequest) doFastcgi() (shouldStop bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !env.Has("REMOTE_ADDR") {
|
if !env.Has("REMOTE_ADDR") {
|
||||||
env["REMOTE_ADDR"] = this.requestRemoteAddr()
|
env["REMOTE_ADDR"] = this.requestRemoteAddr(true)
|
||||||
}
|
}
|
||||||
if !env.Has("QUERY_STRING") {
|
if !env.Has("QUERY_STRING") {
|
||||||
u, err := url.ParseRequestURI(this.uri)
|
u, err := url.ParseRequestURI(this.uri)
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ func (this *HTTPRequest) log() {
|
|||||||
RequestId: strconv.FormatInt(this.requestFromTime.UnixNano(), 10) + strconv.FormatInt(atomic.AddInt64(&requestId, 1), 10) + sharedNodeConfig.PaddedId(),
|
RequestId: strconv.FormatInt(this.requestFromTime.UnixNano(), 10) + strconv.FormatInt(atomic.AddInt64(&requestId, 1), 10) + sharedNodeConfig.PaddedId(),
|
||||||
NodeId: sharedNodeConfig.Id,
|
NodeId: sharedNodeConfig.Id,
|
||||||
ServerId: this.Server.Id,
|
ServerId: this.Server.Id,
|
||||||
RemoteAddr: this.requestRemoteAddr(),
|
RemoteAddr: this.requestRemoteAddr(true),
|
||||||
RawRemoteAddr: addr,
|
RawRemoteAddr: addr,
|
||||||
RemotePort: int32(this.requestRemotePort()),
|
RemotePort: int32(this.requestRemotePort()),
|
||||||
RemoteUser: this.requestRemoteUser(),
|
RemoteUser: this.requestRemoteUser(),
|
||||||
|
|||||||
@@ -9,6 +9,6 @@ func (this *HTTPRequest) doStat() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 内置的统计
|
// 内置的统计
|
||||||
stats.SharedHTTPRequestStatManager.AddRemoteAddr(this.Server.Id, this.requestRemoteAddr())
|
stats.SharedHTTPRequestStatManager.AddRemoteAddr(this.Server.Id, this.requestRemoteAddr(true))
|
||||||
stats.SharedHTTPRequestStatManager.AddUserAgent(this.Server.Id, this.requestHeader("User-Agent"))
|
stats.SharedHTTPRequestStatManager.AddUserAgent(this.Server.Id, this.requestHeader("User-Agent"))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -275,7 +275,7 @@ func (this *HTTPRequest) WAFRaw() *http.Request {
|
|||||||
|
|
||||||
// WAFRemoteIP 客户端IP
|
// WAFRemoteIP 客户端IP
|
||||||
func (this *HTTPRequest) WAFRemoteIP() string {
|
func (this *HTTPRequest) WAFRemoteIP() string {
|
||||||
return this.requestRemoteAddr()
|
return this.requestRemoteAddr(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WAFGetCacheBody 获取缓存中的Body
|
// WAFGetCacheBody 获取缓存中的Body
|
||||||
|
|||||||
Reference in New Issue
Block a user