refactor for switch

This commit is contained in:
Notealot 2021-10-21 10:17:38 +08:00
parent 4307d8386f
commit 40b983805d
2 changed files with 3 additions and 11 deletions

View File

@ -743,16 +743,8 @@ func (c *Context) ClientIP() string {
switch c.engine.TrustedPlatform {
case "":
// TrustedPlatform is empty, do nothing
case PlatformGoogleAppEngine:
if addr := c.requestHeader("X-Appengine-Remote-Addr"); addr != "" {
return addr
}
case PlatformCloudflare:
if addr := c.requestHeader("CF-Connecting-IP"); addr != "" {
return addr
}
default:
// Developers can define their own header of Trusted Platform
// Developers can define their own header of Trusted Platform or use predefined constants
if addr := c.requestHeader(c.engine.TrustedPlatform); addr != "" {
return addr
}

4
gin.go
View File

@ -59,10 +59,10 @@ type RoutesInfo []RouteInfo
const (
// When running on Google App Engine. Trust X-Appengine-Remote-Addr
// for determining the client's IP
PlatformGoogleAppEngine = "google-app-engine"
PlatformGoogleAppEngine = "X-Appengine-Remote-Addr"
// When using Cloudflare's CDN. Trust CF-Connecting-IP for determining
// the client's IP
PlatformCloudflare = "cloudflare"
PlatformCloudflare = "CF-Connecting-IP"
)
// Engine is the framework's instance, it contains the muxer, middleware and configuration settings.