mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-06 11:56:39 +08:00
updating net check function on macos
This commit is contained in:
parent
a0586ea6d2
commit
06811235c9
1 changed files with 23 additions and 12 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
//"github.com/davecgh/go-spew/spew"
|
//"github.com/davecgh/go-spew/spew"
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -58,23 +59,33 @@ func IsWGInstalled() bool {
|
||||||
return strings.Contains(out, "Available subcommand")
|
return strings.Contains(out, "Available subcommand")
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetMacIface(addr string) (string, error) {
|
func GetMacIface(ipstring string) (string, error) {
|
||||||
out, err := ncutils.RunCmd("route get "+addr, false)
|
var wgiface string
|
||||||
var iface string
|
_, checknet, err := net.ParseCIDR(ipstring + "/24")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return iface, errors.New(string(out))
|
return wgiface, errors.New("could not parse ip " + ipstring)
|
||||||
}
|
}
|
||||||
for _, line := range strings.Split(strings.TrimSuffix(string(out), "\n"), "\n") {
|
ifaces, err := net.Interfaces()
|
||||||
if strings.Contains(line, "interface: ") {
|
if err != nil {
|
||||||
iface = getLineAfter(string(out), "interface: ")
|
return wgiface, err
|
||||||
iface = strings.Split(iface, "\n")[0]
|
}
|
||||||
break
|
for _, iface := range ifaces {
|
||||||
|
addrs, err := iface.Addrs()
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, addr := range addrs {
|
||||||
|
ip := addr.(*net.IPNet).IP
|
||||||
|
if checknet.Contains(ip) {
|
||||||
|
wgiface = iface.Name
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if iface == "" {
|
if wgiface == "" {
|
||||||
err = errors.New("could not find iface for ip addr " + addr)
|
err = errors.New("could not find iface for network " + ipstring)
|
||||||
}
|
}
|
||||||
return iface, err
|
return wgiface, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func getLineAfter(value string, a string) string {
|
func getLineAfter(value string, a string) string {
|
||||||
|
|
Loading…
Add table
Reference in a new issue