adapt to possible reverse proxy

This commit is contained in:
Nils 2021-12-31 01:27:16 +01:00
parent ba622db043
commit ccb2a41305
Signed by: byreqz
GPG Key ID: 396A62D7D436749E
1 changed files with 9 additions and 1 deletions

10
main.go
View File

@ -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
}