2022-03-21 16:29:52 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// copied from https://github.com/go-chi/chi/blob/c97bc988430d623a14f50b7019fb40529036a35a/middleware/realip.go#L42
|
|
|
|
|
|
|
|
var trueClientIP = http.CanonicalHeaderKey("True-Client-IP")
|
|
|
|
var xForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For")
|
|
|
|
var xRealIP = http.CanonicalHeaderKey("X-Real-IP")
|
|
|
|
|
|
|
|
func extractIP(r *http.Request) string {
|
2022-12-21 17:29:51 +00:00
|
|
|
ip := r.RemoteAddr
|
|
|
|
if i := strings.LastIndexByte(ip, ':'); i != -1 {
|
|
|
|
ip = ip[:i]
|
|
|
|
}
|
2022-03-21 16:29:52 +00:00
|
|
|
|
|
|
|
if tcip := r.Header.Get(trueClientIP); tcip != "" {
|
|
|
|
ip = tcip
|
|
|
|
} else if xrip := r.Header.Get(xRealIP); xrip != "" {
|
|
|
|
ip = xrip
|
|
|
|
} else if xff := r.Header.Get(xForwardedFor); xff != "" {
|
|
|
|
i := strings.Index(xff, ",")
|
|
|
|
if i == -1 {
|
|
|
|
i = len(xff)
|
|
|
|
}
|
|
|
|
ip = xff[:i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return ip
|
|
|
|
}
|