Get client IP when using Cloudflare (#2723)

Co-authored-by: thinkerou <thinkerou@gmail.com>
This commit is contained in:
Alessandro (Ale) Segala
2021-05-27 19:03:59 -07:00
committed by GitHub
parent 0cbb30aa94
commit 6703dea51c
3 changed files with 18 additions and 1 deletions
+6 -1
View File
@@ -731,10 +731,15 @@ func (c *Context) ShouldBindBodyWith(obj interface{}, bb binding.BindingBody) (e
// If the headers are nots syntactically valid OR the remote IP does not correspong to a trusted proxy,
// the remote IP (coming form Request.RemoteAddr) is returned.
func (c *Context) ClientIP() string {
if c.engine.AppEngine {
switch {
case c.engine.AppEngine:
if addr := c.requestHeader("X-Appengine-Remote-Addr"); addr != "" {
return addr
}
case c.engine.CloudflareProxy:
if addr := c.requestHeader("CF-Connecting-IP"); addr != "" {
return addr
}
}
remoteIP, trusted := c.RemoteIP()