|
|
@@ -0,0 +1,28 @@
|
|
|
+package net
|
|
|
+
|
|
|
+import (
|
|
|
+ "net"
|
|
|
+ "net/http"
|
|
|
+ "strings"
|
|
|
+)
|
|
|
+
|
|
|
+func ClientIP(request *http.Request) string {
|
|
|
+ clientIP := request.Header.Get("X-Forward-For")
|
|
|
+ clientIP = strings.TrimSpace(strings.Split(clientIP, ",")[0])
|
|
|
+ if clientIP == "" {
|
|
|
+ clientIP = strings.TrimSpace(request.Header.Get("X-Real-Ip"))
|
|
|
+ }
|
|
|
+ if clientIP != "" {
|
|
|
+ return clientIP
|
|
|
+ }
|
|
|
+
|
|
|
+ if addr := request.Header.Get("X-Appengine-Remote-Addr"); addr != "" {
|
|
|
+ return addr
|
|
|
+ }
|
|
|
+
|
|
|
+ if ip, _, err := net.SplitHostPort(strings.TrimSpace(request.RemoteAddr)); err == nil {
|
|
|
+ return ip
|
|
|
+ }
|
|
|
+
|
|
|
+ return ""
|
|
|
+}
|