mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 17:05:18 +00:00
34 lines
764 B
Go
34 lines
764 B
Go
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 {
|
|
ip := r.RemoteAddr
|
|
if i := strings.LastIndexByte(ip, ':'); i != -1 {
|
|
ip = ip[:i]
|
|
}
|
|
|
|
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
|
|
}
|