Skip to content

Instantly share code, notes, and snippets.

@billglover
Created October 31, 2018 23:33
Show Gist options
  • Select an option

  • Save billglover/993b500632787aa6088777db481c0d53 to your computer and use it in GitHub Desktop.

Select an option

Save billglover/993b500632787aa6088777db481c0d53 to your computer and use it in GitHub Desktop.
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