mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-07 04:29:41 +08:00
Compare commits
5 Commits
d7ab468dde
...
55382278e7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
55382278e7 | ||
|
|
16857146c8 | ||
|
|
334dbdb8ac | ||
|
|
9ef3ade402 | ||
|
|
6f54838d7f |
@ -1022,6 +1022,9 @@ func (c *Context) ClientIP() string {
|
||||
}
|
||||
}
|
||||
}
|
||||
if !remoteIP.IsValid() {
|
||||
return ""
|
||||
}
|
||||
return remoteIP.String()
|
||||
}
|
||||
|
||||
|
||||
@ -1984,6 +1984,12 @@ func TestContextClientIP(t *testing.T) {
|
||||
c.Request.RemoteAddr = addr.String()
|
||||
assert.Equal(t, "20.20.20.20", c.ClientIP())
|
||||
|
||||
// unix address with no valid forwarded header: remoteIP stays zero, must return ""
|
||||
c.Request.Header.Del("X-Forwarded-For")
|
||||
c.Request.Header.Del("X-Real-IP")
|
||||
assert.Empty(t, c.ClientIP())
|
||||
resetContextForClientIPTests(c)
|
||||
|
||||
// reset
|
||||
c.Request = c.Request.WithContext(context.Background())
|
||||
resetContextForClientIPTests(c)
|
||||
|
||||
10
gin.go
10
gin.go
@ -418,15 +418,17 @@ func (engine *Engine) prepareTrustedCIDRs() ([]netip.Prefix, error) {
|
||||
if err != nil {
|
||||
return cidrs, &net.ParseError{Type: "IP address", Text: trustedProxy}
|
||||
}
|
||||
addr = addr.Unmap()
|
||||
bits := 128
|
||||
if addr.Is4() {
|
||||
trustedProxy += "/32"
|
||||
} else {
|
||||
trustedProxy += "/128"
|
||||
bits = 32
|
||||
}
|
||||
cidrs = append(cidrs, netip.PrefixFrom(addr, bits))
|
||||
continue
|
||||
}
|
||||
prefix, err := netip.ParsePrefix(trustedProxy)
|
||||
if err != nil {
|
||||
return cidrs, err
|
||||
return cidrs, &net.ParseError{Type: "CIDR address", Text: trustedProxy}
|
||||
}
|
||||
cidrs = append(cidrs, prefix.Masked())
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user