Created
October 31, 2018 23:33
-
-
Save billglover/993b500632787aa6088777db481c0d53 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "log" | |
| "net" | |
| "net/http" | |
| "os" | |
| ) | |
| func main() { | |
| port := os.Getenv("PORT") | |
| if port == "" { | |
| log.Fatal("environment variable PORT is required") | |
| } | |
| http.HandleFunc("/ip", handler) | |
| log.Fatal(http.ListenAndServe(":"+port, nil)) | |
| } | |
| func handler(rw http.ResponseWriter, req *http.Request) { | |
| xff := req.Header.Get("X-Forwarded-For") | |
| ip := net.ParseIP(xff) | |
| if ip == nil { | |
| rw.WriteHeader(http.StatusBadRequest) | |
| return | |
| } | |
| fmt.Fprintln(rw, ip) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment