optimize function layout

This commit is contained in:
Nils 2021-12-31 01:07:43 +01:00
parent 164731f741
commit 8bdf07eff7
Signed by: byreqz
GPG Key ID: 396A62D7D436749E

56
main.go
View File

@ -82,13 +82,12 @@ func parseopts(options []string, cmdopts map[string]string) []string{
return opts
}
func ping(w http.ResponseWriter, req *http.Request) {
func prerunner(req *http.Request, cmd string, cmdopts map[string]string, defaultopts []string) string{
geturl := strings.Split(req.URL.String(), "/")
targets := strings.Split(geturl[2], ",")
hosts := validatehosts(targets)
cmdopts := map[string]string{"4": "-4", "6": "-6", "d": "-D", "n": "-n", "v": "-v", "c1": "-c1", "c5": "-c5", "c10": "-c10"}
var opts []string
opts = append(opts, "-c10") // add default options
opts = append(opts, defaultopts...)
if len(geturl) > 3 && len(geturl[3]) > 0 {
options := strings.Split(geturl[3], ",")
opts = append(opts, parseopts(options, cmdopts)...)
@ -98,8 +97,17 @@ func ping(w http.ResponseWriter, req *http.Request) {
for _, host := range hosts {
args = append(args, opts...)
args = append(args, host)
res = fmt.Sprint(res, runner(req.RemoteAddr, "ping", args...), "\n")
res = fmt.Sprint(res, runner(req.RemoteAddr, cmd, args...), "\n")
}
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)
if strings.TrimSpace(res) == "" {
fmt.Fprintln(w, http.StatusInternalServerError)
} else {
@ -108,23 +116,11 @@ func ping(w http.ResponseWriter, req *http.Request) {
}
func mtr(w http.ResponseWriter, req *http.Request) {
geturl := strings.Split(req.URL.String(), "/")
targets := strings.Split(geturl[2], ",")
hosts := validatehosts(targets)
cmd := "mtr"
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"}
var opts []string
opts = append(opts, "-r", "-w", "-c10") // add default options
if len(geturl) > 3 && len(geturl[3]) > 0 {
options := strings.Split(geturl[3], ",")
opts = append(opts, parseopts(options, cmdopts)...)
}
var res string
var args []string
for _, host := range hosts {
args = append(args, opts...)
args = append(args, host)
res = fmt.Sprint(res, runner(req.RemoteAddr, "mtr", args...), "\n")
}
var defaultopts []string
defaultopts = append(defaultopts, "-r", "-w", "-c10")
res := prerunner(req, cmd, cmdopts, defaultopts)
if strings.TrimSpace(res) == "" {
fmt.Fprintln(w, http.StatusInternalServerError)
} else {
@ -133,23 +129,11 @@ func mtr(w http.ResponseWriter, req *http.Request) {
}
func traceroute(w http.ResponseWriter, req *http.Request) {
geturl := strings.Split(req.URL.String(), "/")
targets := strings.Split(geturl[2], ",")
hosts := validatehosts(targets)
cmd := "traceroute"
cmdopts := map[string]string{"4": "-4", "6": "-6", "dnf": "-F", "i": "-I", "t": "-T", "n": "-n", "u": "-U", "ul": "-UL", "d": "-D", "b": "--back"}
var opts []string
//opts = append(opts, "-c10") // no default options for traceroute
if len(geturl) > 3 && len(geturl[3]) > 0 {
options := strings.Split(geturl[3], ",")
opts = append(opts, parseopts(options, cmdopts)...)
}
var res string
var args []string
for _, host := range hosts {
args = append(args, opts...)
args = append(args, host)
res = fmt.Sprint(res, runner(req.RemoteAddr, "traceroute", args...), "\n")
}
var defaultopts []string
//defaultopts = append(defaultopts) // no default options for traceroute
res := prerunner(req, cmd, cmdopts, defaultopts)
if strings.TrimSpace(res) == "" {
fmt.Fprintln(w, http.StatusInternalServerError)
} else {