Compare commits

..

No commits in common. "2427e58cf65540c0541ec6649bd1d5b7d8b52c95" and "e4d0d9bf61a54920af394466707d6a4091574425" have entirely different histories.

3 changed files with 5 additions and 24 deletions

View File

@ -58,9 +58,8 @@ The app currently has 4 runtime flags:
- `-o / --logfilepath` -- sets the log output file - `-o / --logfilepath` -- sets the log output file
- `-x / --disable-x-forwarded-for` -- disables checking for the X-Forwarded-For header - `-x / --disable-x-forwarded-for` -- disables checking for the X-Forwarded-For header
- `-l / --allow-private` -- allows lookups of private IP ranges - `-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`, `PROBEHOST_DISABLE_X_FORWARDED_FOR` and `PROBEHOST_REQUEST_TTL` 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` and `PROBEHOST_DISABLE_X_FORWARDED_FOR` 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. The app will log every request including the IP that's querying and show failed requests on stdout.

View File

@ -10,7 +10,6 @@ services:
- PROBEHOST_ALLOW_PRIVATE=false - PROBEHOST_ALLOW_PRIVATE=false
- PROBEHOST_DISABLE_X_FORWARDED_FOR=false - PROBEHOST_DISABLE_X_FORWARDED_FOR=false
- PROBEHOST_LISTEN_PORT=8000 - PROBEHOST_LISTEN_PORT=8000
- PROBEHOST_REQUEST_TTL=180
ports: ports:
- 1234:8000 - 1234:8000
volumes: volumes:

25
main.go
View File

@ -8,7 +8,6 @@ import (
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
"time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag" flag "github.com/spf13/pflag"
@ -17,10 +16,9 @@ import (
var logStdout = log.New() var logStdout = log.New()
var logFile = log.New() var logFile = log.New()
var listenPort = 8080 // port to listen on var listenPort = 8080 // port to listen on
var disableXForwardedFor bool // whether to disable parsing the X-Forwarded-For header or not 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 allowPrivate bool // whether to allow private IP ranges or not
var requestTTL = 180 // maximum request time to live in seconds
func init() { func init() {
logStdout.SetFormatter(&log.TextFormatter{ logStdout.SetFormatter(&log.TextFormatter{
@ -44,19 +42,10 @@ 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.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.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(&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.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() flag.Parse()
logpath, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0660) logpath, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0660)
@ -95,7 +84,7 @@ func runner(remoteip string, command string, args ...string) string {
return string(cmd) return string(cmd)
} }
// validatehosts checks the given host+port combinations for validity and returns valid hosts + valid ports separately. // validatehosts checks the given host+port combinations for validity and returns valid hosts + valid ports seperately.
func validatehosts(hosts []string) ([]string, []string) { func validatehosts(hosts []string) ([]string, []string) {
var validHosts []string var validHosts []string
var validPorts []string var validPorts []string
@ -241,13 +230,7 @@ func main() {
http.HandleFunc("/tracert/", traceroute) http.HandleFunc("/tracert/", traceroute)
http.HandleFunc("/traceroute/", traceroute) http.HandleFunc("/traceroute/", traceroute)
http.HandleFunc("/nping/", nping) http.HandleFunc("/nping/", nping)
server := &http.Server{
Addr: fmt.Sprint(":", listenPort),
ReadHeaderTimeout: time.Duration(requestTTL) * time.Second,
}
logStdout.Info("Serving on :", listenPort) logStdout.Info("Serving on :", listenPort)
logFile.Info("Serving on :", listenPort) logFile.Info("Serving on :", listenPort)
_ = server.ListenAndServe() _ = http.ListenAndServe(fmt.Sprint(":", listenPort), nil)
} }