Skip to content

Commit

Permalink
Setting trusted platform using an enum-like (#2739)
Browse files Browse the repository at this point in the history
  • Loading branch information
ItalyPaleAle authored and thinkerou committed Nov 21, 2021
1 parent bf218f9 commit 11bb26b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion context_appengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
package gin

func init() {
defaultAppEngine = true
defaultPlatform = PlatformGoogleAppEngine
}
9 changes: 8 additions & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1406,7 +1406,7 @@ func TestContextClientIP(t *testing.T) {

c.Request.Header.Del("X-Forwarded-For")
c.Request.Header.Del("X-Real-IP")
c.engine.AppEngine = true
c.engine.TrustedPlatform = PlatformGoogleAppEngine
assert.Equal(t, "50.50.50.50", c.ClientIP())

c.Request.Header.Del("X-Appengine-Remote-Addr")
Expand Down Expand Up @@ -1470,8 +1470,15 @@ func TestContextClientIP(t *testing.T) {
assert.Equal(t, "10.10.10.10", c.ClientIP())

c.engine.RemoteIPHeaders = []string{}
c.engine.TrustedPlatform = PlatformGoogleAppEngine
assert.Equal(t, "50.50.50.50", c.ClientIP())

// Test the legacy flag
c.engine.TrustedPlatform = ""
c.engine.AppEngine = true
assert.Equal(t, "50.50.50.50", c.ClientIP())
c.engine.AppEngine = false
c.engine.TrustedPlatform = PlatformGoogleAppEngine

c.Request.Header.Del("X-Appengine-Remote-Addr")
assert.Equal(t, "40.40.40.40", c.ClientIP())
Expand Down
10 changes: 10 additions & 0 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ type RouteInfo struct {
// RoutesInfo defines a RouteInfo array.
type RoutesInfo []RouteInfo

// Trusted platforms
const (
// When running on Google App Engine. Trust X-Appengine-Remote-Addr
// for determining the client's IP
PlatformGoogleAppEngine = "google-app-engine"
// When using Cloudflare's CDN. Trust CF-Connecting-IP for determining
// the client's IP
PlatformCloudflare = "cloudflare"
)

// Engine is the framework's instance, it contains the muxer, middleware and configuration settings.
// Create an instance of Engine, by using New() or Default()
type Engine struct {
Expand Down

0 comments on commit 11bb26b

Please sign in to comment.