htmgo/htmgo-site/internal/urlhelper/ip.go
2024-11-12 18:15:59 -06:00

27 lines
545 B
Go

package urlhelper
import (
"net/http"
"strings"
)
func GetClientIp(r *http.Request) string {
// Try to get the real client IP from the 'CF-Connecting-IP' header
if ip := r.Header.Get("CF-Connecting-IP"); ip != "" {
return ip
}
// If not available, fall back to 'X-Forwarded-For'
if ip := r.Header.Get("X-Forwarded-For"); ip != "" {
return ip
}
// Otherwise, use the default remote address (this will be Cloudflare's IP)
remote := r.RemoteAddr
if strings.HasPrefix(remote, "[::1]") {
return "localhost"
}
return remote
}