grain/server/utils/getClientInfo.go

28 lines
464 B
Go
Raw Normal View History

package utils
import (
"net/http"
"strings"
)
2024-08-09 13:03:14 +00:00
type ClientInfo struct {
IP string
UserAgent string
Origin string
}
func GetClientIP(r *http.Request) string {
xff := r.Header.Get("X-Forwarded-For")
if xff != "" {
ips := strings.Split(xff, ",")
if len(ips) > 0 {
return strings.TrimSpace(ips[0])
}
}
remoteAddr := r.RemoteAddr
if idx := strings.LastIndex(remoteAddr, ":"); idx != -1 {
return remoteAddr[:idx]
}
return remoteAddr
}