@@ -77,6 +77,7 @@ Gin is a web framework written in Go (Golang). It features a martini-like API wi
77
77
- [ http2 server push] ( #http2-server-push )
78
78
- [ Define format for the log of routes] ( #define-format-for-the-log-of-routes )
79
79
- [ Set and get a cookie] ( #set-and-get-a-cookie )
80
+ - [ Don't trust all proxies] ( #don't-trust-all-proxies )
80
81
- [ Testing] ( #testing )
81
82
- [ Users] ( #users )
82
83
@@ -2164,6 +2165,34 @@ func main() {
2164
2165
}
2165
2166
```
2166
2167
2168
+ ** Notice:** If you are using a CDN service, you can set the ` Engine.TrustedPlatform `
2169
+ to skip TrustedProxies check, it has a higher priority than TrustedProxies.
2170
+ Look at the example below:
2171
+ ``` go
2172
+ import (
2173
+ " fmt"
2174
+
2175
+ " github.com/gin-gonic/gin"
2176
+ )
2177
+
2178
+ func main () {
2179
+
2180
+ router := gin.Default ()
2181
+ // Use predefined header gin.PlatformXXX
2182
+ router.TrustedPlatform = gin.PlatformGoogleAppEngine
2183
+ // Or set your own trusted request header for another trusted proxy service
2184
+ // Don't set it to any suspect request header, it's unsafe
2185
+ router.TrustedPlatform = " X-CDN-IP"
2186
+
2187
+ router.GET (" /" , func (c *gin.Context ) {
2188
+ // If you set TrustedPlatform, ClientIP() will resolve the
2189
+ // corresponding header and return IP directly
2190
+ fmt.Printf (" ClientIP: %s \n " , c.ClientIP ())
2191
+ })
2192
+ router.Run ()
2193
+ }
2194
+ ```
2195
+
2167
2196
## Testing
2168
2197
2169
2198
The ` net/http/httptest ` package is preferable way for HTTP testing.
0 commit comments