ip.go

HTTP server that replies with the client's IP address.

raw

package main

import (
	"fmt"
	"log"
	"net"
	"net/http"
)

const xff = true

func index(w http.ResponseWriter, r *http.Request) {
	var ip string
	if r.URL.Path != "/" {
		return
	}
	if xff {
		ip = r.Header.Get("X-Forwarded-For")
	} else {
		ip, _, _ = net.SplitHostPort(r.RemoteAddr)
	}
	fmt.Fprintln(w, ip)
}

func main() {
	http.HandleFunc("/", index)
	log.Fatal(http.ListenAndServe(":7004", nil))
}