mirror of https://github.com/chubin/wttr.in
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
536 B
26 lines
536 B
2 years ago
|
package util
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
"net"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
// ReadUserIP returns IP address of the client from http.Request,
|
||
|
// taking into account the HTTP headers.
|
||
|
func ReadUserIP(r *http.Request) string {
|
||
|
IPAddress := r.Header.Get("X-Real-Ip")
|
||
|
if IPAddress == "" {
|
||
|
IPAddress = r.Header.Get("X-Forwarded-For")
|
||
|
}
|
||
|
if IPAddress == "" {
|
||
|
IPAddress = r.RemoteAddr
|
||
|
var err error
|
||
|
IPAddress, _, err = net.SplitHostPort(IPAddress)
|
||
|
if err != nil {
|
||
|
log.Printf("ERROR: userip: %q is not IP:port\n", IPAddress)
|
||
|
}
|
||
|
}
|
||
|
return IPAddress
|
||
|
}
|