Compare commits

..

2 Commits

Author SHA1 Message Date
2427e58cf6
add request timeouts 2024-08-07 23:23:06 +02:00
7b47ca1c58
solve small linter nitpicks 2024-08-07 22:56:19 +02:00
3 changed files with 24 additions and 5 deletions

View File

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

View File

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

21
main.go
View File

@ -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)
@ -84,7 +95,7 @@ func runner(remoteip string, command string, args ...string) string {
return string(cmd)
}
// validatehosts checks the given host+port combinations for validity and returns valid hosts + valid ports seperately.
// validatehosts checks the given host+port combinations for validity and returns valid hosts + valid ports separately.
func validatehosts(hosts []string) ([]string, []string) {
var validHosts []string
var validPorts []string
@ -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()
}