From 0bd0f90d4a4803d5322ffc03b37c392d3e4887ac Mon Sep 17 00:00:00 2001 From: Lucas Schumacher Date: Sun, 6 Oct 2024 20:35:04 -0400 Subject: [PATCH] Add proxy support --- whatsMyIpServer.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/whatsMyIpServer.go b/whatsMyIpServer.go index d8b7e43..7daba43 100644 --- a/whatsMyIpServer.go +++ b/whatsMyIpServer.go @@ -3,10 +3,24 @@ package main import ( "fmt" "net/http" + "os" ) +var PROXY_HEADER string = os.Getenv("PROXY_HEADER") + func respondWithIp(writer http.ResponseWriter, request *http.Request) { - fmt.Fprintf(writer, "%s\n", request.RemoteAddr) + 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() {