add clientIP for handler recorder object
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetClientIP(req *http.Request) net.IP {
|
||||
if req == nil {
|
||||
return nil
|
||||
}
|
||||
// cloudflare CDN
|
||||
sip := req.Header.Get("CF-Connecting-IP")
|
||||
if sip == "" {
|
||||
ss := strings.Split(req.Header.Get("X-Forwarded-For"), ",")
|
||||
if len(ss) > 0 && ss[0] != "" {
|
||||
sip = ss[0]
|
||||
}
|
||||
}
|
||||
if sip == "" {
|
||||
sip = req.Header.Get("X-Real-Ip")
|
||||
}
|
||||
|
||||
return net.ParseIP(sip)
|
||||
}
|
||||
Reference in New Issue
Block a user