resolve conversation

This commit is contained in:
Notealot 2021-11-29 14:03:13 +08:00
parent 48d71d8b8d
commit 839cc536f8

38
gin.go
View File

@ -422,11 +422,12 @@ func (engine *Engine) parseTrustedProxies() error {
// isTrustedProxy will check whether the IP address is included in the trusted list according to Engine.trustedCIDRs // isTrustedProxy will check whether the IP address is included in the trusted list according to Engine.trustedCIDRs
func (engine *Engine) isTrustedProxy(ip net.IP) bool { func (engine *Engine) isTrustedProxy(ip net.IP) bool {
if engine.trustedCIDRs != nil { if engine.trustedCIDRs == nil {
for _, cidr := range engine.trustedCIDRs { return false
if cidr.Contains(ip) { }
return true for _, cidr := range engine.trustedCIDRs {
} if cidr.Contains(ip) {
return true
} }
} }
return false return false
@ -434,20 +435,21 @@ func (engine *Engine) isTrustedProxy(ip net.IP) bool {
// validateHeader will parse X-Forwarded-For header and return the trusted client IP address // validateHeader will parse X-Forwarded-For header and return the trusted client IP address
func (engine *Engine) validateHeader(header string) (clientIP string, valid bool) { func (engine *Engine) validateHeader(header string) (clientIP string, valid bool) {
if header != "" { if header == "" {
items := strings.Split(header, ",") return "", false
for i := len(items) - 1; i >= 0; i-- { }
ipStr := strings.TrimSpace(items[i]) items := strings.Split(header, ",")
ip := net.ParseIP(ipStr) for i := len(items) - 1; i >= 0; i-- {
if ip == nil { ipStr := strings.TrimSpace(items[i])
break ip := net.ParseIP(ipStr)
} if ip == nil {
break
}
// X-Forwarded-For is appended by proxy // X-Forwarded-For is appended by proxy
// Check IPs in reverse order and stop when find untrusted proxy // Check IPs in reverse order and stop when find untrusted proxy
if (i == 0) || (!engine.isTrustedProxy(ip)) { if (i == 0) || (!engine.isTrustedProxy(ip)) {
return ipStr, true return ipStr, true
}
} }
} }
return "", false return "", false