probehost2/main.go

151 lines
4.5 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-24 14:32:27 +00:00
"flag"
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()
2021-12-24 14:32:27 +00:00
var listenport int
2021-12-23 00:35:47 +00:00
func init() {
2021-12-28 02:54:31 +00:00
var logfilepath string
2021-12-24 14:32:27 +00:00
flag.StringVar(&logfilepath, "logfilepath", "probehost2.log", "sets the output file for the log")
flag.IntVar(&listenport, "port", 8000, "sets the port to listen on")
flag.Parse()
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 14:32:27 +00:00
logpath, err := os.OpenFile(logfilepath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0660)
2021-12-24 13:50:49 +00:00
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 {
2021-12-30 01:58:33 +00:00
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:")
2021-12-24 13:50:49 +00:00
} 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 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-30 01:58:33 +00:00
func parseopts(options []string, cmdopts map[string]string) []string{
var opts []string
for _, opt := range options {
opts = append(opts, cmdopts[opt])
}
return opts
}
2021-12-31 00:07:43 +00:00
func prerunner(req *http.Request, cmd string, cmdopts map[string]string, defaultopts []string) string{
2021-12-24 13:50:49 +00:00
geturl := strings.Split(req.URL.String(), "/")
targets := strings.Split(geturl[2], ",")
hosts := validatehosts(targets)
2021-12-30 01:58:33 +00:00
var opts []string
2021-12-31 00:07:43 +00:00
opts = append(opts, defaultopts...)
2021-12-30 23:37:27 +00:00
if len(geturl) > 3 && len(geturl[3]) > 0 {
2021-12-30 01:58:33 +00:00
options := strings.Split(geturl[3], ",")
opts = append(opts, parseopts(options, cmdopts)...)
}
2021-12-24 13:50:49 +00:00
var res string
2021-12-30 01:58:33 +00:00
var args []string
2021-12-24 13:50:49 +00:00
for _, host := range hosts {
2021-12-30 01:58:33 +00:00
args = append(args, opts...)
args = append(args, host)
2021-12-31 00:07:43 +00:00
res = fmt.Sprint(res, runner(req.RemoteAddr, cmd, args...), "\n")
2021-12-24 13:50:49 +00:00
}
2021-12-31 00:07:43 +00:00
return res
}
func ping(w http.ResponseWriter, req *http.Request) {
cmd := "ping"
cmdopts := map[string]string{"4": "-4", "6": "-6", "d": "-D", "n": "-n", "v": "-v", "c1": "-c1", "c5": "-c5", "c10": "-c10"}
var defaultopts []string
defaultopts = append(defaultopts, "-c10")
res := prerunner(req, cmd, cmdopts, defaultopts)
2021-12-30 01:58:33 +00:00
if strings.TrimSpace(res) == "" {
2021-12-24 13:50:49 +00:00
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-31 00:07:43 +00:00
cmd := "mtr"
2021-12-30 01:58:33 +00:00
cmdopts := map[string]string{"4": "-4", "6": "-6", "u": "-u", "t": "-T", "e": "-e", "x": "-x", "n": "-n", "b": "-b", "z": "-z", "c1": "-c1", "c5": "-c5", "c10": "-c10"}
2021-12-31 00:07:43 +00:00
var defaultopts []string
defaultopts = append(defaultopts, "-r", "-w", "-c10")
res := prerunner(req, cmd, cmdopts, defaultopts)
2021-12-30 01:58:33 +00:00
if strings.TrimSpace(res) == "" {
2021-12-24 13:50:49 +00:00
fmt.Fprintln(w, http.StatusInternalServerError)
} else {
fmt.Fprint(w, strings.TrimSpace(res), "\n")
}
2021-12-19 01:16:27 +00:00
}
2021-12-28 03:02:54 +00:00
func traceroute(w http.ResponseWriter, req *http.Request) {
2021-12-31 00:07:43 +00:00
cmd := "traceroute"
2021-12-30 01:58:33 +00:00
cmdopts := map[string]string{"4": "-4", "6": "-6", "dnf": "-F", "i": "-I", "t": "-T", "n": "-n", "u": "-U", "ul": "-UL", "d": "-D", "b": "--back"}
2021-12-31 00:07:43 +00:00
var defaultopts []string
//defaultopts = append(defaultopts) // no default options for traceroute
res := prerunner(req, cmd, cmdopts, defaultopts)
2021-12-30 01:58:33 +00:00
if strings.TrimSpace(res) == "" {
2021-12-28 03:02:54 +00:00
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)
2021-12-28 03:02:54 +00:00
http.HandleFunc("/tracert/", traceroute)
2021-12-24 14:32:27 +00:00
logstdout.Info("Serving on :", listenport)
logfile.Info("Serving on :", listenport)
http.ListenAndServe(fmt.Sprint(":", listenport), nil)
2021-12-19 01:16:27 +00:00
}