grain/server/utils/getClientInfo.go

28 lines
464 B
Go

package utils
import (
"net/http"
"strings"
)
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
}