From ac8aac1f605982acfe4c5b4cfe5f22e7260bf552 Mon Sep 17 00:00:00 2001 From: Nils Date: Fri, 24 Dec 2021 02:10:42 +0100 Subject: [PATCH] add mtr func --- main.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 8f7d086..b0bd8d2 100644 --- a/main.go +++ b/main.go @@ -28,6 +28,10 @@ func init() { } func runner(remoteip string, command string, args... string) string{ + logfile.WithFields(log.Fields{ + "remote_ip": remoteip, + "command": fmt.Sprint(command, args), + }).Info("request initiated:") cmd, err := exec.Command(command, args...).Output() if err != nil { if ! strings.Contains(err.Error(), "1") { // dont exit if error code is 1 @@ -35,7 +39,7 @@ func runner(remoteip string, command string, args... string) string{ "remote_ip": remoteip, "command": fmt.Sprint(command, args), "error": err.Error(), - }).Warn("the following request failed:") + }).Warn("request failed:") logfile.WithFields(log.Fields{ "remote_ip": remoteip, "command": fmt.Sprint(command, args), @@ -58,16 +62,28 @@ func showhelp(w http.ResponseWriter, req *http.Request) { func ping(w http.ResponseWriter, req *http.Request) { geturl := strings.Split(req.URL.String(), "/") target := geturl[2] - pingres := runner(req.RemoteAddr, "ping", "-c5", target) - if pingres == "" { + res := runner(req.RemoteAddr, "ping", "-c10", target) + if res == "" { fmt.Fprintln(w, http.StatusInternalServerError) } else { - fmt.Fprintln(w, pingres) + fmt.Fprint(w, res) + } +} + +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) + if res == "" { + fmt.Fprintln(w, http.StatusInternalServerError) + } else { + fmt.Fprint(w, res) } } func main() { http.HandleFunc("/ping/", ping) + http.HandleFunc("/mtr/", mtr) http.HandleFunc("/", showhelp) fmt.Println("Serving on :8000") http.ListenAndServe(":8000", nil)