MCPcopy Create free account
hub / github.com/cronitorio/cronitor-cli / getClientIP

Function getClientIP

cmd/dash.go:3245–3274  ·  view source on GitHub ↗

getClientIP extracts the client IP from the request, handling proxy headers

(r *http.Request)

Source from the content-addressed store, hash-verified

3243
3244// getClientIP extracts the client IP from the request, handling proxy headers
3245func getClientIP(r *http.Request) string {
3246 // Check X-Forwarded-For header first (for reverse proxies)
3247 xff := r.Header.Get("X-Forwarded-For")
3248 if xff != "" {
3249 // Take the first IP in the chain
3250 ips := strings.Split(xff, ",")
3251 if len(ips) > 0 {
3252 ip := strings.TrimSpace(ips[0])
3253 if net.ParseIP(ip) != nil {
3254 return ip
3255 }
3256 }
3257 }
3258
3259 // Check X-Real-IP header
3260 xri := r.Header.Get("X-Real-IP")
3261 if xri != "" {
3262 ip := strings.TrimSpace(xri)
3263 if net.ParseIP(ip) != nil {
3264 return ip
3265 }
3266 }
3267
3268 // Fall back to RemoteAddr
3269 host, _, err := net.SplitHostPort(r.RemoteAddr)
3270 if err != nil {
3271 return r.RemoteAddr
3272 }
3273 return host
3274}
3275
3276// Global rate limiter instance
3277var authRateLimiter = NewRateLimiter()

Callers 3

createIPFilterMiddlewareFunction · 0.85
dash.goFile · 0.85
handleSettingsFunction · 0.85

Calls 1

GetMethod · 0.80

Tested by

no test coverage detected