2021-12-19 01:16:27 +00:00
|
|
|
package main
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-12-23 00:35:47 +00:00
|
|
|
"os"
|
2021-12-19 01:16:27 +00:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"net/http"
|
2021-12-23 00:35:47 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-12-19 01:16:27 +00:00
|
|
|
)
|
|
|
|
|
2021-12-23 00:35:47 +00:00
|
|
|
var logstdout = log.New()
|
|
|
|
var logfile = log.New()
|
|
|
|
|
|
|
|
func init() {
|
2021-12-24 00:51:57 +00:00
|
|
|
logstdout.SetFormatter(&log.TextFormatter{
|
|
|
|
FullTimestamp: true})
|
2021-12-23 00:35:47 +00:00
|
|
|
logstdout.SetOutput(os.Stdout)
|
|
|
|
logstdout.SetLevel(log.WarnLevel)
|
|
|
|
|
|
|
|
logpath, err := os.OpenFile("probehost2.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0660)
|
|
|
|
if err != nil {
|
|
|
|
logstdout.Fatal("Failed to initialize the logfile: ", err.Error())
|
|
|
|
}
|
|
|
|
logfile.SetLevel(log.InfoLevel)
|
|
|
|
logfile.SetOutput(logpath)
|
|
|
|
logfile.Info("probehost2 initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
func runner(remoteip string, command string, args... string) string{
|
2021-12-24 01:10:42 +00:00
|
|
|
logfile.WithFields(log.Fields{
|
|
|
|
"remote_ip": remoteip,
|
|
|
|
"command": fmt.Sprint(command, args),
|
|
|
|
}).Info("request initiated:")
|
2021-12-19 01:16:27 +00:00
|
|
|
cmd, err := exec.Command(command, args...).Output()
|
|
|
|
if err != nil {
|
|
|
|
if ! strings.Contains(err.Error(), "1") { // dont exit if error code is 1
|
2021-12-23 00:35:47 +00:00
|
|
|
logstdout.WithFields(log.Fields{
|
|
|
|
"remote_ip": remoteip,
|
|
|
|
"command": fmt.Sprint(command, args),
|
|
|
|
"error": err.Error(),
|
2021-12-24 01:10:42 +00:00
|
|
|
}).Warn("request failed:")
|
2021-12-23 00:35:47 +00:00
|
|
|
logfile.WithFields(log.Fields{
|
|
|
|
"remote_ip": remoteip,
|
|
|
|
"command": fmt.Sprint(command, args),
|
|
|
|
"error": err.Error(),
|
2021-12-24 00:51:57 +00:00
|
|
|
}).Warn("request failed:")
|
2021-12-19 01:16:27 +00:00
|
|
|
}
|
2021-12-23 00:51:13 +00:00
|
|
|
} else {
|
|
|
|
logfile.WithFields(log.Fields{
|
|
|
|
"remote_ip": remoteip,
|
|
|
|
"command": fmt.Sprint(command, args),
|
2021-12-24 00:51:57 +00:00
|
|
|
}).Info("request succeeded:")
|
2021-12-19 01:16:27 +00:00
|
|
|
}
|
|
|
|
return string(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func showhelp(w http.ResponseWriter, req *http.Request) {
|
|
|
|
fmt.Fprintln(w, "placeholder")
|
|
|
|
}
|
|
|
|
|
|
|
|
func ping(w http.ResponseWriter, req *http.Request) {
|
|
|
|
geturl := strings.Split(req.URL.String(), "/")
|
|
|
|
target := geturl[2]
|
2021-12-24 01:10:42 +00:00
|
|
|
res := runner(req.RemoteAddr, "ping", "-c10", target)
|
|
|
|
if res == "" {
|
|
|
|
fmt.Fprintln(w, http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
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 == "" {
|
2021-12-23 00:44:07 +00:00
|
|
|
fmt.Fprintln(w, http.StatusInternalServerError)
|
2021-12-23 00:51:13 +00:00
|
|
|
} else {
|
2021-12-24 01:10:42 +00:00
|
|
|
fmt.Fprint(w, res)
|
2021-12-23 00:44:07 +00:00
|
|
|
}
|
2021-12-19 01:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
http.HandleFunc("/ping/", ping)
|
2021-12-24 01:10:42 +00:00
|
|
|
http.HandleFunc("/mtr/", mtr)
|
2021-12-19 01:16:27 +00:00
|
|
|
http.HandleFunc("/", showhelp)
|
|
|
|
fmt.Println("Serving on :8000")
|
|
|
|
http.ListenAndServe(":8000", nil)
|
|
|
|
}
|