Skip to content
Myr

chore(IP): add TrustedPlatform constant for Fly.io. (#3839)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Also add some more detail to the docs for how to use TrustedPlatform. https://fly.io/docs/reference/runtime-environment/#fly-client-ip

authored by Andy Brody · committed by GitHub · parent 82bcd6d · Browse files · 3 files+18 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • https: //fly.io/docs/reference/runtime-environment/#fly-client-ip

Diff stats

3 files+18 added−3 removed

  • other:+18 added−3 removed

3 files+18 added−3 removed

modified:docs/doc.md+9 added-3 removed
2212 unmodified lines
22132214func main() {2215 router := gin.Default()2216 // Use predefined header gin.PlatformXXX2217 // Google App Engine2218 router.TrustedPlatform = gin.PlatformGoogleAppEngine2218 // Or set your own trusted request header for another trusted proxy service2219 // Don't set it to any suspect request header, it's unsafe2220 router.TrustedPlatform = "X-CDN-IP"2219 // Cloudflare2220 router.TrustedPlatform = gin.PlatformCloudflare2221 // Fly.io2222 router.TrustedPlatform = gin.PlatformFlyIO2223 // Or, you can set your own trusted request header. But be sure your CDN2224 // prevents users from passing this header! For example, if your CDN puts2225 // the client IP in X-CDN-Client-IP:2226 router.TrustedPlatform = "X-CDN-Client-IP"22272228 router.GET("/", func(c *gin.Context) {2229 // If you set TrustedPlatform, ClientIP() will resolve the2230 // corresponding header and return IP directly
57 unmodified lines
modified:context_test.go+7 added
1567 unmodified lines
15681569 c.Request.Header.Del("CF-Connecting-IP")1570 assert.Equal(t, "40.40.40.40", c.ClientIP())15711572 c.engine.TrustedPlatform = PlatformFlyIO1573 assert.Equal(t, "70.70.70.70", c.ClientIP())15741575 c.Request.Header.Del("Fly-Client-IP")1576 assert.Equal(t, "40.40.40.40", c.ClientIP())15771578 c.engine.TrustedPlatform = ""15791580 // no port1581 c.Request.RemoteAddr = "50.50.50.50"
4 unmodified lines
1586 c.Request.Header.Set("X-Real-IP", " 10.10.10.10 ")1587 c.Request.Header.Set("X-Forwarded-For", " 20.20.20.20, 30.30.30.30")1588 c.Request.Header.Set("X-Appengine-Remote-Addr", "50.50.50.50")1589 c.Request.Header.Set("CF-Connecting-IP", "60.60.60.60")1590 c.Request.Header.Set("Fly-Client-IP", "70.70.70.70")1591 c.Request.RemoteAddr = " 40.40.40.40:42123 "1592 c.engine.TrustedPlatform = ""1593 c.engine.trustedCIDRs = defaultTrustedCIDRs1594 c.engine.AppEngine = false
860 unmodified lines
modified:gin.go+2 added
75 unmodified lines
76 PlatformGoogleAppEngine = "X-Appengine-Remote-Addr"77 // PlatformCloudflare when using Cloudflare's CDN. Trust CF-Connecting-IP for determining78 // the client's IP79 PlatformCloudflare = "CF-Connecting-IP"80 // PlatformFlyIO when running on Fly.io. Trust Fly-Client-IP for determining the client's IP81 PlatformFlyIO = "Fly-Client-IP"82)8384// Engine is the framework's instance, it contains the muxer, middleware and configuration settings.85// Create an instance of Engine, by using New() or Default()
635 unmodified lines