adding gateway routes for peers

This commit is contained in:
afeiszli
2022-08-22 16:44:04 -04:00
parent ec40ea78d3
commit 4d9a07bf00
7 changed files with 159 additions and 7 deletions

View File

@@ -9,6 +9,44 @@ import (
"github.com/gravitl/netmaker/netclient/ncutils"
)
// GetDefaultRoute - Gets the default route (ip and interface) on a mac machine
func GetDefaultRoute() (string, string, error) {
var ipaddr string
var iface string
var err error
var outLine string
output, err := ncutils.RunCmd("netstat -nr", false)
for _, line := range strings.Split(strings.TrimSuffix(output, "\n"), "\n") {
if strings.Contains(line, "default") {
outLine = line
break
}
}
space := regexp.MustCompile(`\s+`)
outFormatted := space.ReplaceAllString(outLine, " ")
if err != nil {
return ipaddr, iface, err
}
outputSlice := strings.Split(string(outFormatted), " ")
if !strings.Contains(outputSlice[0], "default") {
return ipaddr, iface, fmt.Errorf("could not find default gateway")
}
ipaddr = outputSlice[1]
if err = checkIPAddress(ipaddr); err != nil {
return ipaddr, iface, err
}
iface = outputSlice[3]
return ipaddr, iface, err
}
func checkIPAddress(ip string) error {
if net.ParseIP(ip) == nil {
return fmt.Errorf("IP Address: %s - Invalid", ip)
}
return nil
}
// route -n add -net 10.0.0.0/8 192.168.0.254
// networksetup -setadditionalroutes Ethernet 192.168.1.0 255.255.255.0 10.0.0.2 persistent
func setRoute(iface string, addr *net.IPNet, address string) error {