fix(go): handle ipv6 addresses correctly
This commit is contained in:
parent
265c998df0
commit
8ad7222bfb
1 changed files with 10 additions and 2 deletions
12
main.go
12
main.go
|
@ -80,10 +80,18 @@ func getIPCountry(ip string) string {
|
||||||
|
|
||||||
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
sourceIP := func() string {
|
sourceIP := func() string {
|
||||||
if ip := r.Header.Get("X-Forwarded-For"); ip != "" {
|
if ip := r.Header.Get("X-Forwarded-For"); ip != "" {
|
||||||
return strings.Split(ip, ",")[0]
|
return strings.Split(ip, ",")[0]
|
||||||
}
|
}
|
||||||
return strings.Split(r.RemoteAddr, ":")[0]
|
|
||||||
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||||
|
if err != nil {
|
||||||
|
errorText := fmt.Sprintf("Unable to parse remote address: %v", err)
|
||||||
|
log.Printf(errorText)
|
||||||
|
return errorText
|
||||||
|
}
|
||||||
|
|
||||||
|
return host
|
||||||
}()
|
}()
|
||||||
|
|
||||||
isHeadless := strings.HasPrefix(r.Header.Get("User-Agent"), "curl/")
|
isHeadless := strings.HasPrefix(r.Header.Get("User-Agent"), "curl/")
|
||||||
|
|
Loading…
Reference in a new issue