Skip to content

Instantly share code, notes, and snippets.

@codekoala
Created November 29, 2016 23:03
Show Gist options
  • Save codekoala/2f6a1f65a721818d34894d3e71460644 to your computer and use it in GitHub Desktop.
Save codekoala/2f6a1f65a721818d34894d3e71460644 to your computer and use it in GitHub Desktop.
Very basic icanhazip.com-like service
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
"strings"
)
var addr = flag.String("l", ":8080", "listen on port")
func main() {
flag.Parse()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ra := r.RemoteAddr
if ip := r.Header.Get("X-Forwarded-For"); ip != "" {
ra = ip
} else if ip := r.Header.Get("X-Real-IP"); ip != "" {
ra = ip
} else {
ra, _, _ = net.SplitHostPort(ra)
}
ra = strings.Split(ra, ",")[0]
fmt.Fprintf(w, ra)
})
log.Printf("Listening for requests on %s", *addr)
log.Fatal(http.ListenAndServe(*addr, nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment