probehost2/main.go

112 lines
2.9 KiB
Go
Raw Normal View History

2021-12-19 01:16:27 +00:00
package main
import (
2021-12-24 13:50:49 +00:00
"fmt"
"os"
"os/exec"
"strings"
"net/http"
"net"
2021-12-23 00:35:47 +00:00
2021-12-24 13:50:49 +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 13:50:49 +00:00
logstdout.SetFormatter(&log.TextFormatter{
FullTimestamp: true})
logstdout.SetOutput(os.Stdout)
logstdout.SetLevel(log.InfoLevel)
2021-12-23 00:35:47 +00:00
2021-12-24 13:50:49 +00:00
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")
2021-12-23 00:35:47 +00:00
}
func runner(remoteip string, command string, args... string) string{
2021-12-24 13:50:49 +00:00
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
logstdout.WithFields(log.Fields{
"remote_ip": remoteip,
"command": fmt.Sprint(command, args),
"error": err.Error(),
}).Warn("request failed:")
logfile.WithFields(log.Fields{
"remote_ip": remoteip,
"command": fmt.Sprint(command, args),
"error": err.Error(),
}).Warn("request failed:")
}
} else {
logfile.WithFields(log.Fields{
"remote_ip": remoteip,
"command": fmt.Sprint(command, args),
}).Info("request succeeded:")
}
return string(cmd)
2021-12-19 01:16:27 +00:00
}
func showhelp(w http.ResponseWriter, req *http.Request) {
2021-12-24 13:50:49 +00:00
fmt.Fprintln(w, "placeholder")
2021-12-19 01:16:27 +00:00
}
func validatehosts(hosts []string) []string{
2021-12-24 13:50:49 +00:00
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
}
2021-12-19 01:16:27 +00:00
func ping(w http.ResponseWriter, req *http.Request) {
2021-12-24 13:50:49 +00:00
geturl := strings.Split(req.URL.String(), "/")
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, strings.TrimSpace(res), "\n")
}
2021-12-24 01:10:42 +00:00
}
func mtr(w http.ResponseWriter, req *http.Request) {
2021-12-24 13:50:49 +00:00
geturl := strings.Split(req.URL.String(), "/")
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, strings.TrimSpace(res), "\n")
}
2021-12-19 01:16:27 +00:00
}
func main() {
2021-12-24 13:50:49 +00:00
http.HandleFunc("/ping/", ping)
http.HandleFunc("/mtr/", mtr)
http.HandleFunc("/", showhelp)
logstdout.Info("Serving on :8000")
logfile.Info("Serving on :8000")
http.ListenAndServe(":8000", nil)
2021-12-19 01:16:27 +00:00
}