Adds ForwardedByClientIP option
This commit is contained in:
parent
042046e1f8
commit
58b5e15870
24
context.go
24
context.go
@ -251,17 +251,19 @@ func (c *Context) BindWith(obj interface{}, b binding.Binding) error {
|
|||||||
// Best effort algoritm to return the real client IP, it parses
|
// Best effort algoritm to return the real client IP, it parses
|
||||||
// X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy.
|
// X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy.
|
||||||
func (c *Context) ClientIP() string {
|
func (c *Context) ClientIP() string {
|
||||||
clientIP := strings.TrimSpace(c.requestHeader("X-Real-Ip"))
|
if c.engine.ForwardedByClientIP {
|
||||||
if len(clientIP) > 0 {
|
clientIP := strings.TrimSpace(c.requestHeader("X-Real-Ip"))
|
||||||
return clientIP
|
if len(clientIP) > 0 {
|
||||||
}
|
return clientIP
|
||||||
clientIP = c.requestHeader("X-Forwarded-For")
|
}
|
||||||
if index := strings.IndexByte(clientIP, ','); index >= 0 {
|
clientIP = c.requestHeader("X-Forwarded-For")
|
||||||
clientIP = clientIP[0:index]
|
if index := strings.IndexByte(clientIP, ','); index >= 0 {
|
||||||
}
|
clientIP = clientIP[0:index]
|
||||||
clientIP = strings.TrimSpace(clientIP)
|
}
|
||||||
if len(clientIP) > 0 {
|
clientIP = strings.TrimSpace(clientIP)
|
||||||
return clientIP
|
if len(clientIP) > 0 {
|
||||||
|
return clientIP
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return strings.TrimSpace(c.Request.RemoteAddr)
|
return strings.TrimSpace(c.Request.RemoteAddr)
|
||||||
}
|
}
|
||||||
|
8
gin.go
8
gin.go
@ -14,7 +14,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin/render"
|
"github.com/gin-gonic/gin/render"
|
||||||
)
|
)
|
||||||
|
|
||||||
const Version = "v1.0rc1"
|
const Version = "v1.0rc2"
|
||||||
|
|
||||||
var default404Body = []byte("404 page not found")
|
var default404Body = []byte("404 page not found")
|
||||||
var default405Body = []byte("405 method not allowed")
|
var default405Body = []byte("405 method not allowed")
|
||||||
@ -59,6 +59,7 @@ type (
|
|||||||
// If no other Method is allowed, the request is delegated to the NotFound
|
// If no other Method is allowed, the request is delegated to the NotFound
|
||||||
// handler.
|
// handler.
|
||||||
HandleMethodNotAllowed bool
|
HandleMethodNotAllowed bool
|
||||||
|
ForwardedByClientIP bool
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -74,7 +75,8 @@ func New() *Engine {
|
|||||||
RedirectTrailingSlash: true,
|
RedirectTrailingSlash: true,
|
||||||
RedirectFixedPath: false,
|
RedirectFixedPath: false,
|
||||||
HandleMethodNotAllowed: false,
|
HandleMethodNotAllowed: false,
|
||||||
trees: make(methodTrees, 0, 5),
|
ForwardedByClientIP: true,
|
||||||
|
trees: make(methodTrees, 0, 9),
|
||||||
}
|
}
|
||||||
engine.RouterGroup.engine = engine
|
engine.RouterGroup.engine = engine
|
||||||
engine.pool.New = func() interface{} {
|
engine.pool.New = func() interface{} {
|
||||||
@ -90,7 +92,7 @@ func Default() *Engine {
|
|||||||
return engine
|
return engine
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) allocateContext() (context *Context) {
|
func (engine *Engine) allocateContext() *Context {
|
||||||
return &Context{engine: engine}
|
return &Context{engine: engine}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user