mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-17 05:42:09 +08:00
Treat RemoteAddr "@" as 127.0.0.1
This commit is contained in:
parent
a06d546f5c
commit
95f5ef6ff5
@ -785,10 +785,14 @@ func (e *Engine) isTrustedProxy(ip net.IP) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RemoteIP parses the IP from Request.RemoteAddr, normalizes and returns the IP (without the port).
|
// RemoteIP parses the IP from Request.RemoteAddr, normalizes and returns the IP (without the port).
|
||||||
|
// If Request.RemoteAddr is @ which may be proxy pass by a unix listener, treat it as 127.0.0.1.
|
||||||
// It also checks if the remoteIP is a trusted proxy or not.
|
// It also checks if the remoteIP is a trusted proxy or not.
|
||||||
// In order to perform this validation, it will see if the IP is contained within at least one of the CIDR blocks
|
// In order to perform this validation, it will see if the IP is contained within at least one of the CIDR blocks
|
||||||
// defined by Engine.SetTrustedProxies()
|
// defined by Engine.SetTrustedProxies()
|
||||||
func (c *Context) RemoteIP() (net.IP, bool) {
|
func (c *Context) RemoteIP() (net.IP, bool) {
|
||||||
|
if c.Request.RemoteAddr == "@" {
|
||||||
|
c.Request.RemoteAddr = "127.0.0.1:"
|
||||||
|
}
|
||||||
ip, _, err := net.SplitHostPort(strings.TrimSpace(c.Request.RemoteAddr))
|
ip, _, err := net.SplitHostPort(strings.TrimSpace(c.Request.RemoteAddr))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false
|
return nil, false
|
||||||
|
@ -1401,6 +1401,12 @@ func TestContextClientIP(t *testing.T) {
|
|||||||
c.Request.RemoteAddr = "50.50.50.50"
|
c.Request.RemoteAddr = "50.50.50.50"
|
||||||
assert.Empty(t, c.ClientIP())
|
assert.Empty(t, c.ClientIP())
|
||||||
|
|
||||||
|
// @
|
||||||
|
c.Request.RemoteAddr = "@"
|
||||||
|
assert.Equal(t, "127.0.0.1", c.ClientIP())
|
||||||
|
c.Request.Header.Set("X-Forwarded-For", "30.30.30.30 ")
|
||||||
|
assert.Equal(t, "30.30.30.30", c.ClientIP())
|
||||||
|
|
||||||
// Tests exercising the TrustedProxies functionality
|
// Tests exercising the TrustedProxies functionality
|
||||||
resetContextForClientIPTests(c)
|
resetContextForClientIPTests(c)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user