netmaker/netclient/local/routes_darwin.go

87 lines
2.5 KiB
Go
Raw Normal View History

2022-02-04 08:55:12 +08:00
package local
import (
2022-08-24 20:10:36 +08:00
"fmt"
2022-05-27 05:39:18 +08:00
"github.com/c-robinson/iplib"
2022-04-26 01:22:56 +08:00
"github.com/gravitl/netmaker/logger"
2022-02-04 08:55:12 +08:00
"github.com/gravitl/netmaker/netclient/ncutils"
2022-08-24 20:10:36 +08:00
"net"
"regexp"
"strings"
2022-02-04 08:55:12 +08:00
)
2022-08-23 04:44:04 +08:00
// 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]
2022-08-23 12:33:48 +08:00
if err = ncutils.CheckIPAddress(ipaddr); err != nil {
2022-08-23 04:44:04 +08:00
return ipaddr, iface, err
}
iface = outputSlice[3]
return ipaddr, iface, err
}
2022-02-04 08:55:12 +08:00
// 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 {
var err error
var out string
var inetx = "inet"
if strings.Contains(addr.IP.String(), ":") {
inetx = "inet6"
}
2022-02-06 04:00:26 +08:00
out, err = ncutils.RunCmd("route -n get -"+inetx+" "+addr.IP.String(), false)
2022-02-04 08:55:12 +08:00
if err != nil {
return err
}
if !(strings.Contains(out, iface)) {
2022-02-06 04:00:26 +08:00
_, err = ncutils.RunCmd("route -q -n add -"+inetx+" "+addr.String()+" -interface "+iface, false)
2022-02-04 08:55:12 +08:00
}
return err
}
2022-08-24 20:10:36 +08:00
// SetExplicitRoute - sets route via explicit ip address
func SetExplicitRoute(iface string, destination *net.IPNet, gateway string) error {
return setRoute(iface, destination, gateway)
}
2022-02-04 08:55:12 +08:00
func deleteRoute(iface string, addr *net.IPNet, address string) error {
var err error
2022-02-06 04:00:26 +08:00
_, err = ncutils.RunCmd("route -q -n delete "+addr.String(), false)
2022-02-04 08:55:12 +08:00
return err
}
func setCidr(iface, address string, addr *net.IPNet) {
2022-05-27 05:39:18 +08:00
if iplib.Version(addr.IP) == 4 {
2022-04-20 04:18:03 +08:00
ncutils.RunCmd("route -q -n add -net "+addr.String()+" "+address, false)
2022-05-27 05:39:18 +08:00
} else if iplib.Version(addr.IP) == 6 {
2022-04-20 04:18:03 +08:00
ncutils.RunCmd("route -A inet6 -q -n add -net "+addr.String()+" "+address, false)
} else {
logger.Log(1, "could not parse address: "+addr.String())
}
2022-02-06 04:00:26 +08:00
}
func removeCidr(iface string, addr *net.IPNet, address string) {
ncutils.RunCmd("route -q -n delete "+addr.String()+" -interface "+iface, false)
}