real remote addr

This commit is contained in:
2025-10-26 13:14:31 +03:00
parent 842444b70b
commit cf3ec88bb1

21
main.go
View File

@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"io" "io"
"log" "log"
"net"
"net/http" "net/http"
"os" "os"
"strings" "strings"
@@ -27,12 +28,26 @@ type ConfigTelegraam struct {
} }
func GetRemoteAddr(r *http.Request) string { func GetRemoteAddr(r *http.Request) string {
realIP := r.Header.Get("X-Real-IP") // Сначала смотрим X-Forwarded-For
if realIP != "" { if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
// X-Forwarded-For может содержать несколько IP через запятую
ips := strings.Split(xff, ",")
if len(ips) > 0 {
return strings.TrimSpace(ips[0])
}
}
// Потом X-Real-IP
if realIP := r.Header.Get("X-Real-IP"); realIP != "" {
return realIP return realIP
} else { }
// И в конце RemoteAddr
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return r.RemoteAddr return r.RemoteAddr
} }
return host
} }
func BuildMessage(r *http.Request) string { func BuildMessage(r *http.Request) string {