mirror of
https://github.com/byReqz/probehost2.git
synced 2024-11-21 22:33:14 +00:00
optimize function layout
This commit is contained in:
parent
164731f741
commit
8bdf07eff7
56
main.go
56
main.go
@ -82,13 +82,12 @@ func parseopts(options []string, cmdopts map[string]string) []string{
|
|||||||
return opts
|
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(), "/")
|
geturl := strings.Split(req.URL.String(), "/")
|
||||||
targets := strings.Split(geturl[2], ",")
|
targets := strings.Split(geturl[2], ",")
|
||||||
hosts := validatehosts(targets)
|
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
|
var opts []string
|
||||||
opts = append(opts, "-c10") // add default options
|
opts = append(opts, defaultopts...)
|
||||||
if len(geturl) > 3 && len(geturl[3]) > 0 {
|
if len(geturl) > 3 && len(geturl[3]) > 0 {
|
||||||
options := strings.Split(geturl[3], ",")
|
options := strings.Split(geturl[3], ",")
|
||||||
opts = append(opts, parseopts(options, cmdopts)...)
|
opts = append(opts, parseopts(options, cmdopts)...)
|
||||||
@ -98,8 +97,17 @@ func ping(w http.ResponseWriter, req *http.Request) {
|
|||||||
for _, host := range hosts {
|
for _, host := range hosts {
|
||||||
args = append(args, opts...)
|
args = append(args, opts...)
|
||||||
args = append(args, host)
|
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) == "" {
|
if strings.TrimSpace(res) == "" {
|
||||||
fmt.Fprintln(w, http.StatusInternalServerError)
|
fmt.Fprintln(w, http.StatusInternalServerError)
|
||||||
} else {
|
} else {
|
||||||
@ -108,23 +116,11 @@ func ping(w http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func mtr(w http.ResponseWriter, req *http.Request) {
|
func mtr(w http.ResponseWriter, req *http.Request) {
|
||||||
geturl := strings.Split(req.URL.String(), "/")
|
cmd := "mtr"
|
||||||
targets := strings.Split(geturl[2], ",")
|
|
||||||
hosts := validatehosts(targets)
|
|
||||||
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"}
|
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
|
var defaultopts []string
|
||||||
opts = append(opts, "-r", "-w", "-c10") // add default options
|
defaultopts = append(defaultopts, "-r", "-w", "-c10")
|
||||||
if len(geturl) > 3 && len(geturl[3]) > 0 {
|
res := prerunner(req, cmd, cmdopts, defaultopts)
|
||||||
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")
|
|
||||||
}
|
|
||||||
if strings.TrimSpace(res) == "" {
|
if strings.TrimSpace(res) == "" {
|
||||||
fmt.Fprintln(w, http.StatusInternalServerError)
|
fmt.Fprintln(w, http.StatusInternalServerError)
|
||||||
} else {
|
} else {
|
||||||
@ -133,23 +129,11 @@ func mtr(w http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func traceroute(w http.ResponseWriter, req *http.Request) {
|
func traceroute(w http.ResponseWriter, req *http.Request) {
|
||||||
geturl := strings.Split(req.URL.String(), "/")
|
cmd := "traceroute"
|
||||||
targets := strings.Split(geturl[2], ",")
|
|
||||||
hosts := validatehosts(targets)
|
|
||||||
cmdopts := map[string]string{"4": "-4", "6": "-6", "dnf": "-F", "i": "-I", "t": "-T", "n": "-n", "u": "-U", "ul": "-UL", "d": "-D", "b": "--back"}
|
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
|
var defaultopts []string
|
||||||
//opts = append(opts, "-c10") // no default options for traceroute
|
//defaultopts = append(defaultopts) // no default options for traceroute
|
||||||
if len(geturl) > 3 && len(geturl[3]) > 0 {
|
res := prerunner(req, cmd, cmdopts, defaultopts)
|
||||||
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")
|
|
||||||
}
|
|
||||||
if strings.TrimSpace(res) == "" {
|
if strings.TrimSpace(res) == "" {
|
||||||
fmt.Fprintln(w, http.StatusInternalServerError)
|
fmt.Fprintln(w, http.StatusInternalServerError)
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user