mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 21:32:11 +08:00
add remote proto mechanism
This commit is contained in:
parent
e868fd1d3d
commit
0a659fdad0
24
context.go
24
context.go
@ -814,6 +814,30 @@ func (c *Context) RemoteIP() string {
|
|||||||
return ip
|
return ip
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Context) ClientProto() string {
|
||||||
|
var isTls = c.Request.TLS != nil
|
||||||
|
remoteIP := net.ParseIP(c.RemoteIP())
|
||||||
|
if remoteIP == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
trusted := c.engine.isTrustedProxy(remoteIP)
|
||||||
|
|
||||||
|
if trusted && c.engine.ForwardedByClientProto && c.engine.RemoteProtoHeaders != nil {
|
||||||
|
for _, headerName := range c.engine.RemoteProtoHeaders {
|
||||||
|
proto, valid := c.engine.validateHeader(c.requestHeader(headerName))
|
||||||
|
if valid {
|
||||||
|
return proto
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if isTls {
|
||||||
|
return "https"
|
||||||
|
} else {
|
||||||
|
return "http"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ContentType returns the Content-Type header of the request.
|
// ContentType returns the Content-Type header of the request.
|
||||||
func (c *Context) ContentType() string {
|
func (c *Context) ContentType() string {
|
||||||
return filterFlags(c.requestHeader("Content-Type"))
|
return filterFlags(c.requestHeader("Content-Type"))
|
||||||
|
6
gin.go
6
gin.go
@ -112,6 +112,8 @@ type Engine struct {
|
|||||||
// `(*gin.Context).Request.RemoteAddr`.
|
// `(*gin.Context).Request.RemoteAddr`.
|
||||||
ForwardedByClientIP bool
|
ForwardedByClientIP bool
|
||||||
|
|
||||||
|
ForwardedByClientProto bool
|
||||||
|
|
||||||
// AppEngine was deprecated.
|
// AppEngine was deprecated.
|
||||||
// Deprecated: USE `TrustedPlatform` WITH VALUE `gin.PlatformGoogleAppEngine` INSTEAD
|
// Deprecated: USE `TrustedPlatform` WITH VALUE `gin.PlatformGoogleAppEngine` INSTEAD
|
||||||
// #726 #755 If enabled, it will trust some headers starting with
|
// #726 #755 If enabled, it will trust some headers starting with
|
||||||
@ -136,6 +138,8 @@ type Engine struct {
|
|||||||
// network origins of list defined by `(*gin.Engine).SetTrustedProxies()`.
|
// network origins of list defined by `(*gin.Engine).SetTrustedProxies()`.
|
||||||
RemoteIPHeaders []string
|
RemoteIPHeaders []string
|
||||||
|
|
||||||
|
RemoteProtoHeaders []string
|
||||||
|
|
||||||
// TrustedPlatform if set to a constant of value gin.Platform*, trusts the headers set by
|
// TrustedPlatform if set to a constant of value gin.Platform*, trusts the headers set by
|
||||||
// that platform, for example to determine the client IP
|
// that platform, for example to determine the client IP
|
||||||
TrustedPlatform string
|
TrustedPlatform string
|
||||||
@ -189,7 +193,9 @@ func New() *Engine {
|
|||||||
RedirectFixedPath: false,
|
RedirectFixedPath: false,
|
||||||
HandleMethodNotAllowed: false,
|
HandleMethodNotAllowed: false,
|
||||||
ForwardedByClientIP: true,
|
ForwardedByClientIP: true,
|
||||||
|
ForwardedByClientProto: true,
|
||||||
RemoteIPHeaders: []string{"X-Forwarded-For", "X-Real-IP"},
|
RemoteIPHeaders: []string{"X-Forwarded-For", "X-Real-IP"},
|
||||||
|
RemoteProtoHeaders: []string{"X-Forwarded-Proto"},
|
||||||
TrustedPlatform: defaultPlatform,
|
TrustedPlatform: defaultPlatform,
|
||||||
UseRawPath: false,
|
UseRawPath: false,
|
||||||
RemoveExtraSlash: false,
|
RemoveExtraSlash: false,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user