diff --git a/go.mod b/go.mod index bc87434..6c976a6 100644 --- a/go.mod +++ b/go.mod @@ -6,3 +6,10 @@ require ( github.com/gorilla/mux v1.8.1 gopkg.in/yaml.v3 v3.0.1 ) + +require ( + github.com/digineo/go-logwrap v0.0.0-20181106161722-a178c58ea3f0 // indirect + github.com/digineo/go-ping v1.1.0 // indirect + golang.org/x/net v0.24.0 // indirect + golang.org/x/sys v0.19.0 // indirect +) diff --git a/go.sum b/go.sum index 3e12cf5..5753375 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,13 @@ +github.com/digineo/go-logwrap v0.0.0-20181106161722-a178c58ea3f0 h1:OT/LKmj81wMymnWXaKaKBR9n1vPlu+GC0VVKaZP6kzs= +github.com/digineo/go-logwrap v0.0.0-20181106161722-a178c58ea3f0/go.mod h1:DmqdumeAKGQNU5E8MN0ruT5ZGx8l/WbAsMbXCXcSEts= +github.com/digineo/go-ping v1.1.0 h1:HXZPBw8/Zk+tFuHrHejBTLopcEkqK4FNn1ocqKo6xhw= +github.com/digineo/go-ping v1.1.0/go.mod h1:rVhwm0cbn6i20vX/MBmo4OoxOvAW/6JiIf+2Oln8n0M= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= +golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= +golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= +golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= +golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/minipam.go b/minipam.go index 8044c6f..1dfd046 100644 --- a/minipam.go +++ b/minipam.go @@ -4,6 +4,7 @@ import ( "crypto/tls" _ "embed" "encoding/json" + "github.com/digineo/go-ping" "github.com/gorilla/mux" "gopkg.in/yaml.v3" "log" @@ -11,7 +12,6 @@ import ( "net/http" "net/netip" "os" - "os/exec" "sync" "time" ) @@ -50,6 +50,7 @@ var p PersistenceT //go:embed index.html var indexhtml []byte +var pinger *ping.Pinger func main() { @@ -80,6 +81,12 @@ func main() { if p.Subnets == nil { p.Subnets = make(map[string]SubnetT) } + + pinger, err = ping.New("0.0.0.0", "") + if err != nil { + log.Fatalf("Failed to create pinger: %s", err) + } + go scanner() r := mux.NewRouter() r.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { @@ -107,16 +114,9 @@ func main() { } -func ping(addr string) bool { - cmd := exec.Command("ping", "-n", "-W", "0.2", "-c", "1", addr) - cmd.Stdin = nil - cmd.Stdout = nil - cmd.Stderr = nil - err := cmd.Run() - if err == nil { - return true - } - return false +func PingFunc(addr net.IPAddr) bool { + _, err := pinger.PingAttempts(&addr, 200*time.Millisecond, 2) + return err == nil } func scanner() { @@ -159,7 +159,8 @@ func scanner() { persistenceSubnet.TotalAddresses++ //fmt.Println(addr.String()) persistenceSubnet.HostList = append(persistenceSubnet.HostList, addr.String()) - pingstate := ping(addr.String()) + x := net.IPAddr{IP: net.ParseIP(addr.String())} + pingstate := PingFunc(x) host, ok := persistenceSubnet.Hosts[addr.String()] if pingstate { persistenceSubnet.UsedAddresses++