mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-22 16:47:13 +00:00
28 lines
464 B
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
|
|
} |