From ccb2a4130596359cfb2f9f49cf7b6091b434d61f Mon Sep 17 00:00:00 2001 From: Nils Date: Fri, 31 Dec 2021 01:27:16 +0100 Subject: [PATCH] adapt to possible reverse proxy --- main.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index bb89918..8b9e8d9 100644 --- a/main.go +++ b/main.go @@ -15,11 +15,13 @@ var logstdout = log.New() var logfile = log.New() var listenport int +var usexforwardedfor bool func init() { var logfilepath string flag.StringVar(&logfilepath, "logfilepath", "probehost2.log", "sets the output file for the log") flag.IntVar(&listenport, "port", 8000, "sets the port to listen on") + flag.BoolVar(&usexforwardedfor, "use-x-forwarded-for", true, "specifies whether to show x-forwarded-for or the requesting IP") flag.Parse() logstdout.SetFormatter(&log.TextFormatter{ @@ -94,10 +96,16 @@ func prerunner(req *http.Request, cmd string, cmdopts map[string]string, default } var res string var args []string + var remoteaddr string + if req.Header.Get("X-Forwarded-For") != "" && usexforwardedfor != false { + remoteaddr = req.Header.Get("X-Forwarded-For") + } else { + remoteaddr = req.RemoteAddr + } for _, host := range hosts { args = append(args, opts...) args = append(args, host) - res = fmt.Sprint(res, runner(req.RemoteAddr, cmd, args...), "\n") + res = fmt.Sprint(res, runner(remoteaddr, cmd, args...), "\n") } return res }