fix: login session IP parsing and username lookup

This commit is contained in:
orohi
2026-06-17 05:39:57 +03:00
parent b7753505b8
commit f94143acf0
3 changed files with 45 additions and 5 deletions
+30
View File
@@ -0,0 +1,30 @@
package httputil
import (
"net"
"net/http"
"strings"
)
func ClientIP(r *http.Request) string {
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
if ip := strings.TrimSpace(strings.Split(xff, ",")[0]); ip != "" {
return ip
}
}
if xri := strings.TrimSpace(r.Header.Get("X-Real-IP")); xri != "" {
return xri
}
if host, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
return host
}
return r.RemoteAddr
}
func ValidIP(ip string) (string, bool) {
parsed := net.ParseIP(strings.TrimSpace(ip))
if parsed == nil {
return "", false
}
return parsed.String(), true
}