mirror of
https://github.com/byReqz/probehost2.git
synced 2025-07-01 10:30:48 +00:00
Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
3b4e96509b
|
|||
a0e42d369a
|
|||
871fe76df5
|
|||
2b8510c5ee
|
|||
955f1253af
|
82
main.go
82
main.go
@ -1,16 +1,16 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
|
||||||
"net/http"
|
|
||||||
"net"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
flag "github.com/spf13/pflag"
|
flag "github.com/spf13/pflag"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var logstdout = log.New()
|
var logstdout = log.New()
|
||||||
@ -92,20 +92,38 @@ func runner(remoteip string, command string, args... string) string{
|
|||||||
return string(cmd)
|
return string(cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func validatehosts(hosts []string) []string{
|
func validatehosts(hosts []string) ([]string, []string) {
|
||||||
var valid []string
|
var validhosts []string
|
||||||
|
var validports []string
|
||||||
for _, host := range hosts {
|
for _, host := range hosts {
|
||||||
|
split := strings.Split(host, "_")
|
||||||
|
host = split[0]
|
||||||
if hostparse := net.ParseIP(host); hostparse != nil {
|
if hostparse := net.ParseIP(host); hostparse != nil {
|
||||||
if (net.IP.IsPrivate(hostparse) || net.IP.IsLoopback(hostparse)) && allowprivate {
|
if (net.IP.IsPrivate(hostparse) || net.IP.IsLoopback(hostparse)) && allowprivate {
|
||||||
valid = append(valid, host)
|
validhosts = append(validhosts, host)
|
||||||
} else if !(net.IP.IsPrivate(hostparse) || net.IP.IsLoopback(hostparse)) {
|
} else if !(net.IP.IsPrivate(hostparse) || net.IP.IsLoopback(hostparse)) {
|
||||||
valid = append(valid, host)
|
validhosts = append(validhosts, host)
|
||||||
}
|
}
|
||||||
} else if _, err := net.LookupIP(host); err == nil {
|
} else if _, err := net.LookupIP(host); err == nil {
|
||||||
valid = append(valid, host)
|
validhosts = append(validhosts, host)
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var port string
|
||||||
|
if len(split) > 1 {
|
||||||
|
port = split[1]
|
||||||
|
_, err := strconv.Atoi(port) // validate if port is just an int
|
||||||
|
if err == nil {
|
||||||
|
validports = append(validports, port)
|
||||||
|
} else {
|
||||||
|
validports = append(validports, "0")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
validports = append(validports, "0")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return valid
|
return validhosts, validports
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseopts(options []string, cmdopts map[string]string) []string {
|
func parseopts(options []string, cmdopts map[string]string) []string {
|
||||||
@ -119,7 +137,7 @@ func parseopts(options []string, cmdopts map[string]string) []string{
|
|||||||
func prerunner(req *http.Request, cmd string, cmdopts map[string]string, defaultopts []string) string {
|
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, ports := validatehosts(targets)
|
||||||
var opts []string
|
var opts []string
|
||||||
opts = append(opts, defaultopts...)
|
opts = append(opts, defaultopts...)
|
||||||
if len(geturl) > 3 && len(geturl[3]) > 0 {
|
if len(geturl) > 3 && len(geturl[3]) > 0 {
|
||||||
@ -134,10 +152,13 @@ func prerunner(req *http.Request, cmd string, cmdopts map[string]string, default
|
|||||||
} else {
|
} else {
|
||||||
remoteaddr = req.RemoteAddr
|
remoteaddr = req.RemoteAddr
|
||||||
}
|
}
|
||||||
for _, host := range hosts {
|
for i, host := range hosts {
|
||||||
args = append(args, opts...)
|
runargs := append(args, opts...)
|
||||||
args = append(args, host)
|
if ports[i] != "0" && cmd == "nping" {
|
||||||
res = fmt.Sprint(res, runner(remoteaddr, cmd, args...), "\n")
|
runargs = append(runargs, "-p"+ports[i])
|
||||||
|
}
|
||||||
|
runargs = append(runargs, host)
|
||||||
|
res = fmt.Sprint(res, runner(remoteaddr, cmd, runargs...), "\n")
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
@ -152,9 +173,9 @@ func ping(w http.ResponseWriter, req *http.Request) {
|
|||||||
defaultopts = append(defaultopts, "-c10")
|
defaultopts = append(defaultopts, "-c10")
|
||||||
res := prerunner(req, cmd, cmdopts, defaultopts)
|
res := prerunner(req, cmd, cmdopts, defaultopts)
|
||||||
if strings.TrimSpace(res) == "" {
|
if strings.TrimSpace(res) == "" {
|
||||||
fmt.Fprintln(w, http.StatusInternalServerError)
|
http.Error(w, "500: Internal Server Error", http.StatusInternalServerError)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprint(w, strings.TrimSpace(res), "\n")
|
_, _ = fmt.Fprint(w, strings.TrimSpace(res), "\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,9 +189,9 @@ func mtr(w http.ResponseWriter, req *http.Request) {
|
|||||||
defaultopts = append(defaultopts, "-r", "-w", "-c10")
|
defaultopts = append(defaultopts, "-r", "-w", "-c10")
|
||||||
res := prerunner(req, cmd, cmdopts, defaultopts)
|
res := prerunner(req, cmd, cmdopts, defaultopts)
|
||||||
if strings.TrimSpace(res) == "" {
|
if strings.TrimSpace(res) == "" {
|
||||||
fmt.Fprintln(w, http.StatusInternalServerError)
|
http.Error(w, "500: Internal Server Error", http.StatusInternalServerError)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprint(w, strings.TrimSpace(res), "\n")
|
_, _ = fmt.Fprint(w, strings.TrimSpace(res), "\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -184,9 +205,25 @@ func traceroute(w http.ResponseWriter, req *http.Request) {
|
|||||||
//defaultopts = append(defaultopts) // no default options for traceroute
|
//defaultopts = append(defaultopts) // no default options for traceroute
|
||||||
res := prerunner(req, cmd, cmdopts, defaultopts)
|
res := prerunner(req, cmd, cmdopts, defaultopts)
|
||||||
if strings.TrimSpace(res) == "" {
|
if strings.TrimSpace(res) == "" {
|
||||||
fmt.Fprintln(w, http.StatusInternalServerError)
|
http.Error(w, "500: Internal Server Error", http.StatusInternalServerError)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprint(w, strings.TrimSpace(res), "\n")
|
_, _ = fmt.Fprint(w, strings.TrimSpace(res), "\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func nping(w http.ResponseWriter, req *http.Request) {
|
||||||
|
cmd := "nping"
|
||||||
|
cmdopts := map[string]string{
|
||||||
|
"4": "-4", "6": "-6", "u": "--udp", "t": "--tcp-connect", "v": "-v", "c1": "-c1", "c3": "-c3", "c5": "-c5",
|
||||||
|
"force4": "-4", "force6": "-6", "udp": "--udp", "tcp": "--tcp-connect", "verbose": "-v", "count1": "-c1", "count3": "-c3", "count5": "-c5",
|
||||||
|
}
|
||||||
|
var defaultopts []string
|
||||||
|
defaultopts = append(defaultopts, "-c3")
|
||||||
|
res := prerunner(req, cmd, cmdopts, defaultopts)
|
||||||
|
if strings.TrimSpace(res) == "" {
|
||||||
|
http.Error(w, "500: Internal Server Error", http.StatusInternalServerError)
|
||||||
|
} else {
|
||||||
|
_, _ = fmt.Fprint(w, strings.TrimSpace(res), "\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,7 +232,8 @@ func main() {
|
|||||||
http.HandleFunc("/mtr/", mtr)
|
http.HandleFunc("/mtr/", mtr)
|
||||||
http.HandleFunc("/tracert/", traceroute)
|
http.HandleFunc("/tracert/", traceroute)
|
||||||
http.HandleFunc("/traceroute/", traceroute)
|
http.HandleFunc("/traceroute/", traceroute)
|
||||||
|
http.HandleFunc("/nping/", nping)
|
||||||
logstdout.Info("Serving on :", listenport)
|
logstdout.Info("Serving on :", listenport)
|
||||||
logfile.Info("Serving on :", listenport)
|
logfile.Info("Serving on :", listenport)
|
||||||
http.ListenAndServe(fmt.Sprint(":", listenport), nil)
|
_ = http.ListenAndServe(fmt.Sprint(":", listenport), nil)
|
||||||
}
|
}
|
Reference in New Issue
Block a user