Created
November 29, 2016 23:03
-
-
Save codekoala/2f6a1f65a721818d34894d3e71460644 to your computer and use it in GitHub Desktop.
Very basic icanhazip.com-like service
This file contains 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 ( | |
"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