Working simple IP logger

This commit is contained in:
Calamitas 2022-07-12 23:45:09 -05:00
parent e90e769fa5
commit 6c0ef42e6a

View File

@ -7,17 +7,22 @@ import (
) )
func handler(w http.ResponseWriter, r *http.Request) { func handler(w http.ResponseWriter, r *http.Request) {
ip := getIP(w, r) ip := GetRealIP(r)
fmt.Fprintf(w, ip) fmt.Fprintf(w, ip)
} }
func getIP(w http.ResponseWriter, r *http.Request) string { func GetRealIP(r *http.Request) string {
IP := r.Header.Get("X-REAL-IP") IP := r.Header.Get("X-Real-IP")
if IP != "" { if IP == "" {
return IP IP = r.Header.Get("X-Forwarder-For")
} else {
return "No IPV4"
} }
if IP == "" {
IP = r.RemoteAddr
}
if IP == "" {
IP = "No IPV4 address"
}
return IP
} }
func main() { func main() {
http.HandleFunc("/", handler) http.HandleFunc("/", handler)