package main import ( "encoding/json" "net/http" "os" ) func GetIP(r *http.Request) string { forwarded := r.Header.Get("X-FORWARDED-FOR") if forwarded != "" { return forwarded } return r.RemoteAddr } func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(GetIP(r))) }) http.HandleFunc("/json", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") resp, _ := json.Marshal(map[string]string{ "ip": GetIP(r), }) w.Write(resp) }) listen := ":8000" os.Stdout.Write([]byte("Listening on " + listen)) http.ListenAndServe(listen, nil) }