Skip to content

Commit bb3519d

Browse files
authored
chore(IP): add TrustedPlatform constant for Fly.io. (#3839)
Also add some more detail to the docs for how to use TrustedPlatform. https://fly.io/docs/reference/runtime-environment/#fly-client-ip
1 parent 82bcd6d commit bb3519d

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

context_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,6 +1569,12 @@ func TestContextClientIP(t *testing.T) {
15691569
c.Request.Header.Del("CF-Connecting-IP")
15701570
assert.Equal(t, "40.40.40.40", c.ClientIP())
15711571

1572+
c.engine.TrustedPlatform = PlatformFlyIO
1573+
assert.Equal(t, "70.70.70.70", c.ClientIP())
1574+
1575+
c.Request.Header.Del("Fly-Client-IP")
1576+
assert.Equal(t, "40.40.40.40", c.ClientIP())
1577+
15721578
c.engine.TrustedPlatform = ""
15731579

15741580
// no port
@@ -1581,6 +1587,7 @@ func resetContextForClientIPTests(c *Context) {
15811587
c.Request.Header.Set("X-Forwarded-For", " 20.20.20.20, 30.30.30.30")
15821588
c.Request.Header.Set("X-Appengine-Remote-Addr", "50.50.50.50")
15831589
c.Request.Header.Set("CF-Connecting-IP", "60.60.60.60")
1590+
c.Request.Header.Set("Fly-Client-IP", "70.70.70.70")
15841591
c.Request.RemoteAddr = " 40.40.40.40:42123 "
15851592
c.engine.TrustedPlatform = ""
15861593
c.engine.trustedCIDRs = defaultTrustedCIDRs

docs/doc.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,10 +2214,16 @@ import (
22142214
func main() {
22152215
router := gin.Default()
22162216
// Use predefined header gin.PlatformXXX
2217+
// Google App Engine
22172218
router.TrustedPlatform = gin.PlatformGoogleAppEngine
2218-
// Or set your own trusted request header for another trusted proxy service
2219-
// Don't set it to any suspect request header, it's unsafe
2220-
router.TrustedPlatform = "X-CDN-IP"
2219+
// Cloudflare
2220+
router.TrustedPlatform = gin.PlatformCloudflare
2221+
// Fly.io
2222+
router.TrustedPlatform = gin.PlatformFlyIO
2223+
// Or, you can set your own trusted request header. But be sure your CDN
2224+
// prevents users from passing this header! For example, if your CDN puts
2225+
// the client IP in X-CDN-Client-IP:
2226+
router.TrustedPlatform = "X-CDN-Client-IP"
22212227

22222228
router.GET("/", func(c *gin.Context) {
22232229
// If you set TrustedPlatform, ClientIP() will resolve the

gin.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ const (
7777
// PlatformCloudflare when using Cloudflare's CDN. Trust CF-Connecting-IP for determining
7878
// the client's IP
7979
PlatformCloudflare = "CF-Connecting-IP"
80+
// PlatformFlyIO when running on Fly.io. Trust Fly-Client-IP for determining the client's IP
81+
PlatformFlyIO = "Fly-Client-IP"
8082
)
8183

8284
// Engine is the framework's instance, it contains the muxer, middleware and configuration settings.

0 commit comments

Comments
 (0)