mirror of
https://github.com/skidoodle/ipinfo.git
synced 2025-02-15 08:29:17 +01:00
chore: Refactor IP address retrieval logic in main.go
This commit is contained in:
parent
7e607bf733
commit
f4b529eb7f
1 changed files with 9 additions and 8 deletions
17
main.go
17
main.go
|
@ -187,14 +187,15 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
|||
IPAddress = requestedThings[1]
|
||||
}
|
||||
|
||||
if IPAddress == "" || IPAddress == "self" {
|
||||
if realIP, ok := r.Header["X-Forwarded-For"]; ok && len(realIP) > 0 {
|
||||
IPAddress = realIP[0]
|
||||
} else {
|
||||
IPAddress = extractIP(r.RemoteAddr)
|
||||
}
|
||||
}
|
||||
|
||||
if IPAddress == "" || IPAddress == "self" {
|
||||
if realIP := r.Header.Get("CF-Connecting-IP"); realIP != "" {
|
||||
IPAddress = realIP
|
||||
} else if realIP := r.Header.Get("X-Forwarded-For"); realIP != "" {
|
||||
IPAddress = strings.Split(realIP, ",")[0]
|
||||
} else {
|
||||
IPAddress = extractIP(r.RemoteAddr)
|
||||
}
|
||||
}
|
||||
ip := net.ParseIP(IPAddress)
|
||||
if ip == nil {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue