diff --git a/app/hdl/hdl_demo.go b/app/hdl/hdl_demo.go index fe1000b..ba6503f 100644 --- a/app/hdl/hdl_demo.go +++ b/app/hdl/hdl_demo.go @@ -26,7 +26,7 @@ import ( // @Failure 400 {object} md.Response "具体错误" // @Router /api/v1/test [GET] func Demo(c *gin.Context) { - fmt.Println("headers::::", c.Request.Header) + fmt.Println("headers::::", utils.GetIP(c.Request)) utils.FilePutContents("headers", utils.SerializeStr(c.Request.Header)) var args interface{} if c.Request.Method == "GET" { diff --git a/app/utils/ip.go b/app/utils/ip.go index 6ed8286..4e3a71d 100644 --- a/app/utils/ip.go +++ b/app/utils/ip.go @@ -72,6 +72,10 @@ func ClientIP(r *http.Request) string { // 解析 X-Real-IP 和 X-Forwarded-For 以便于反向代理(nginx 或 haproxy)可以正常工作。 func ClientPublicIP(r *http.Request) string { var ip string + if ip = strings.TrimSpace(r.Header.Get("Ali-Cdn-Real-Ip")); ip != "" { + return ip + } + for _, ip = range strings.Split(r.Header.Get("X-Forwarded-For"), ",") { if ip = strings.TrimSpace(ip); ip != "" && !HasLocalIPAddr(ip) { return ip