From 8faf66401d5fa6ebd665b2c8f43770c8308b11fe Mon Sep 17 00:00:00 2001 From: Nils Date: Fri, 24 Dec 2021 14:41:14 +0100 Subject: [PATCH] add validate function and multiple input --- main.go | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index b0bd8d2..0588a23 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "os/exec" "strings" "net/http" + "net" log "github.com/sirupsen/logrus" ) @@ -59,25 +60,45 @@ func showhelp(w http.ResponseWriter, req *http.Request) { fmt.Fprintln(w, "placeholder") } +func validatehosts(hosts []string) []string{ + var valid []string + for _, host := range hosts { + if net.ParseIP(host) != nil { + valid = append(valid, host) + } else if _, err := net.LookupIP(host); err == nil { + valid = append(valid, host) + } + } + return valid +} + func ping(w http.ResponseWriter, req *http.Request) { geturl := strings.Split(req.URL.String(), "/") - target := geturl[2] - res := runner(req.RemoteAddr, "ping", "-c10", target) + targets := strings.Split(geturl[2], ",") + hosts := validatehosts(targets) + var res string + for _, host := range hosts { + res = fmt.Sprint(res, runner(req.RemoteAddr, "ping", "-c10", host), "\n") + } if res == "" { fmt.Fprintln(w, http.StatusInternalServerError) } else { - fmt.Fprint(w, res) + fmt.Fprint(w, strings.TrimSpace(res), "\n") } } func mtr(w http.ResponseWriter, req *http.Request) { geturl := strings.Split(req.URL.String(), "/") - target := geturl[2] - res := runner(req.RemoteAddr, "mtr", "-c10", "-w", target) + targets := strings.Split(geturl[2], ",") + hosts := validatehosts(targets) + var res string + for _, host := range hosts { + res = fmt.Sprint(res, runner(req.RemoteAddr, "mtr", "-c10", "-w", host), "\n") + } if res == "" { fmt.Fprintln(w, http.StatusInternalServerError) } else { - fmt.Fprint(w, res) + fmt.Fprint(w, strings.TrimSpace(res), "\n") } }