mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-07 01:36:23 +08:00
adding gateway routes for peers
This commit is contained in:
@@ -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 {
|
||||
|
Reference in New Issue
Block a user