convert file to use spaces

This commit is contained in:
Nils 2021-12-24 14:50:49 +01:00
parent 016dd7662a
commit 8b1b28b274
Signed by: byreqz
GPG Key ID: 396A62D7D436749E

165
main.go
View File

@ -1,111 +1,112 @@
package main package main
import ( import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"net/http" "net/http"
"net" "net"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
var logstdout = log.New() var logstdout = log.New()
var logfile = log.New() var logfile = log.New()
func init() { func init() {
logstdout.SetFormatter(&log.TextFormatter{ logstdout.SetFormatter(&log.TextFormatter{
FullTimestamp: true}) FullTimestamp: true})
logstdout.SetOutput(os.Stdout) logstdout.SetOutput(os.Stdout)
logstdout.SetLevel(log.WarnLevel) logstdout.SetLevel(log.InfoLevel)
logpath, err := os.OpenFile("probehost2.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0660) logpath, err := os.OpenFile("probehost2.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0660)
if err != nil { if err != nil {
logstdout.Fatal("Failed to initialize the logfile: ", err.Error()) logstdout.Fatal("Failed to initialize the logfile: ", err.Error())
} }
logfile.SetLevel(log.InfoLevel) logfile.SetLevel(log.InfoLevel)
logfile.SetOutput(logpath) logfile.SetOutput(logpath)
logfile.Info("probehost2 initialized") logfile.Info("probehost2 initialized")
} }
func runner(remoteip string, command string, args... string) string{ func runner(remoteip string, command string, args... string) string{
logfile.WithFields(log.Fields{ logfile.WithFields(log.Fields{
"remote_ip": remoteip, "remote_ip": remoteip,
"command": fmt.Sprint(command, args), "command": fmt.Sprint(command, args),
}).Info("request initiated:") }).Info("request initiated:")
cmd, err := exec.Command(command, args...).Output() cmd, err := exec.Command(command, args...).Output()
if err != nil { if err != nil {
if ! strings.Contains(err.Error(), "1") { // dont exit if error code is 1 if ! strings.Contains(err.Error(), "1") { // dont exit if error code is 1
logstdout.WithFields(log.Fields{ logstdout.WithFields(log.Fields{
"remote_ip": remoteip, "remote_ip": remoteip,
"command": fmt.Sprint(command, args), "command": fmt.Sprint(command, args),
"error": err.Error(), "error": err.Error(),
}).Warn("request failed:") }).Warn("request failed:")
logfile.WithFields(log.Fields{ logfile.WithFields(log.Fields{
"remote_ip": remoteip, "remote_ip": remoteip,
"command": fmt.Sprint(command, args), "command": fmt.Sprint(command, args),
"error": err.Error(), "error": err.Error(),
}).Warn("request failed:") }).Warn("request failed:")
} }
} else { } else {
logfile.WithFields(log.Fields{ logfile.WithFields(log.Fields{
"remote_ip": remoteip, "remote_ip": remoteip,
"command": fmt.Sprint(command, args), "command": fmt.Sprint(command, args),
}).Info("request succeeded:") }).Info("request succeeded:")
} }
return string(cmd) return string(cmd)
} }
func showhelp(w http.ResponseWriter, req *http.Request) { func showhelp(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, "placeholder") fmt.Fprintln(w, "placeholder")
} }
func validatehosts(hosts []string) []string{ func validatehosts(hosts []string) []string{
var valid []string var valid []string
for _, host := range hosts { for _, host := range hosts {
if net.ParseIP(host) != nil { if net.ParseIP(host) != nil {
valid = append(valid, host) valid = append(valid, host)
} else if _, err := net.LookupIP(host); err == nil { } else if _, err := net.LookupIP(host); err == nil {
valid = append(valid, host) valid = append(valid, host)
} }
} }
return valid return valid
} }
func ping(w http.ResponseWriter, req *http.Request) { func ping(w http.ResponseWriter, req *http.Request) {
geturl := strings.Split(req.URL.String(), "/") geturl := strings.Split(req.URL.String(), "/")
targets := strings.Split(geturl[2], ",") targets := strings.Split(geturl[2], ",")
hosts := validatehosts(targets) hosts := validatehosts(targets)
var res string var res string
for _, host := range hosts { for _, host := range hosts {
res = fmt.Sprint(res, runner(req.RemoteAddr, "ping", "-c10", host), "\n") res = fmt.Sprint(res, runner(req.RemoteAddr, "ping", "-c10", host), "\n")
} }
if res == "" { if res == "" {
fmt.Fprintln(w, http.StatusInternalServerError) fmt.Fprintln(w, http.StatusInternalServerError)
} else { } else {
fmt.Fprint(w, strings.TrimSpace(res), "\n") fmt.Fprint(w, strings.TrimSpace(res), "\n")
} }
} }
func mtr(w http.ResponseWriter, req *http.Request) { func mtr(w http.ResponseWriter, req *http.Request) {
geturl := strings.Split(req.URL.String(), "/") geturl := strings.Split(req.URL.String(), "/")
targets := strings.Split(geturl[2], ",") targets := strings.Split(geturl[2], ",")
hosts := validatehosts(targets) hosts := validatehosts(targets)
var res string var res string
for _, host := range hosts { for _, host := range hosts {
res = fmt.Sprint(res, runner(req.RemoteAddr, "mtr", "-c10", "-w", host), "\n") res = fmt.Sprint(res, runner(req.RemoteAddr, "mtr", "-c10", "-w", host), "\n")
} }
if res == "" { if res == "" {
fmt.Fprintln(w, http.StatusInternalServerError) fmt.Fprintln(w, http.StatusInternalServerError)
} else { } else {
fmt.Fprint(w, strings.TrimSpace(res), "\n") fmt.Fprint(w, strings.TrimSpace(res), "\n")
} }
} }
func main() { func main() {
http.HandleFunc("/ping/", ping) http.HandleFunc("/ping/", ping)
http.HandleFunc("/mtr/", mtr) http.HandleFunc("/mtr/", mtr)
http.HandleFunc("/", showhelp) http.HandleFunc("/", showhelp)
fmt.Println("Serving on :8000") logstdout.Info("Serving on :8000")
http.ListenAndServe(":8000", nil) logfile.Info("Serving on :8000")
http.ListenAndServe(":8000", nil)
} }