mirror of
https://github.com/btouchard/ackify.git
synced 2026-02-13 17:29:14 -06:00
- Now can activate OIDC and/or MagicLink for user authentication. - Add page to choose authentication method (if only OIDC is enabled, auto redirecting to login screen)
34 lines
859 B
Go
34 lines
859 B
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
package shared
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// GetClientIP extracts the real client IP address from the request
|
|
// It checks X-Forwarded-For, X-Real-IP, and falls back to RemoteAddr
|
|
func GetClientIP(r *http.Request) string {
|
|
// Check X-Forwarded-For header (proxy/load balancer)
|
|
if forwardedFor := r.Header.Get("X-Forwarded-For"); forwardedFor != "" {
|
|
// X-Forwarded-For can contain multiple IPs, take the first one
|
|
ips := strings.Split(forwardedFor, ",")
|
|
if len(ips) > 0 {
|
|
return strings.TrimSpace(ips[0])
|
|
}
|
|
}
|
|
|
|
// Check X-Real-IP header
|
|
if realIP := r.Header.Get("X-Real-IP"); realIP != "" {
|
|
return strings.TrimSpace(realIP)
|
|
}
|
|
|
|
// Fall back to RemoteAddr
|
|
ip := r.RemoteAddr
|
|
// Remove port if present
|
|
if idx := strings.LastIndex(ip, ":"); idx != -1 {
|
|
ip = ip[:idx]
|
|
}
|
|
return ip
|
|
}
|