WhatsMyIpServer/whatsMyIpServer.go

30 lines
686 B
Go

package main
import (
"fmt"
"net/http"
"os"
)
var PROXY_HEADER string = os.Getenv("PROXY_HEADER")
func respondWithIp(writer http.ResponseWriter, request *http.Request) {
remote := request.RemoteAddr
if PROXY_HEADER != "" {
ipHeader := request.Header[PROXY_HEADER]
if len(ipHeader) != 1 {
fmt.Println("Error: Reading proxy header", PROXY_HEADER, "in request from", request.RemoteAddr, "got", ipHeader)
writer.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(writer, "Internal Server Error")
return
}
remote = ipHeader[0]
}
fmt.Fprintf(writer, "%s\n", remote)
}
func main() {
http.HandleFunc("/", respondWithIp)
http.ListenAndServe(":8080", nil)
}