diff --git a/README.md b/README.md index bb2dd28..25fc81f 100644 --- a/README.md +++ b/README.md @@ -58,8 +58,9 @@ The app currently has 4 runtime flags: - `-o / --logfilepath` -- sets the log output file - `-x / --disable-x-forwarded-for` -- disables checking for the X-Forwarded-For header - `-l / --allow-private` -- allows lookups of private IP ranges +- `--request-ttl` -- sets the maximum request time to live in seconds -All the Flags also have an accompanying environment value: `PROBEHOST_LOGPATH`, `PROBEHOST_ALLOW_PRIVATE`, `PROBEHOST_LISTEN_PORT` and `PROBEHOST_DISABLE_X_FORWARDED_FOR` but the options given via commandline have priority. +All the Flags also have an accompanying environment value: `PROBEHOST_LOGPATH`, `PROBEHOST_ALLOW_PRIVATE`, `PROBEHOST_LISTEN_PORT`, `PROBEHOST_DISABLE_X_FORWARDED_FOR` and `PROBEHOST_REQUEST_TTL` but the options given via commandline have priority. The app will log every request including the IP that's querying and show failed requests on stdout. diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index ba8b9f0..42b3452 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -10,6 +10,7 @@ services: - PROBEHOST_ALLOW_PRIVATE=false - PROBEHOST_DISABLE_X_FORWARDED_FOR=false - PROBEHOST_LISTEN_PORT=8000 + - PROBEHOST_REQUEST_TTL=180 ports: - 1234:8000 volumes: diff --git a/main.go b/main.go index 8e4dd2b..6a18795 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "os/exec" "strconv" "strings" + "time" log "github.com/sirupsen/logrus" flag "github.com/spf13/pflag" @@ -19,6 +20,7 @@ var logFile = log.New() var listenPort = 8080 // port to listen on var disableXForwardedFor bool // whether to disable parsing the X-Forwarded-For header or not var allowPrivate bool // whether to allow private IP ranges or not +var requestTTL = 180 // maximum request time to live in seconds func init() { logStdout.SetFormatter(&log.TextFormatter{ @@ -42,10 +44,19 @@ func init() { } } + if val, exists := os.LookupEnv("PROBEHOST_REQUEST_TTL"); exists { + var err error + requestTTL, err = strconv.Atoi(val) + if err != nil { + logStdout.Fatal("Failed to read PROBEHOST_REQUEST_TTL: ", err.Error()) + } + } + flag.StringVarP(&logFilePath, "logFilePath", "o", logFilePath, "sets the output file for the log") flag.IntVarP(&listenPort, "port", "p", listenPort, "sets the port to listen on") flag.BoolVarP(&disableXForwardedFor, "disable-x-forwarded-for", "x", disableXForwardedFor, "whether to show x-forwarded-for or the requesting IP") flag.BoolVarP(&allowPrivate, "allow-private", "l", allowPrivate, "whether to show lookups of private IP ranges") + flag.IntVar(&requestTTL, "request-ttl", requestTTL, "sets the maximum request time to live in seconds") flag.Parse() logpath, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0660) @@ -230,7 +241,13 @@ func main() { http.HandleFunc("/tracert/", traceroute) http.HandleFunc("/traceroute/", traceroute) http.HandleFunc("/nping/", nping) + + server := &http.Server{ + Addr: fmt.Sprint(":", listenPort), + ReadHeaderTimeout: time.Duration(requestTTL) * time.Second, + } + logStdout.Info("Serving on :", listenPort) logFile.Info("Serving on :", listenPort) - _ = http.ListenAndServe(fmt.Sprint(":", listenPort), nil) + _ = server.ListenAndServe() }