2022-02-04 08:55:12 +08:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2022-02-06 03:20:50 +08:00
|
|
|
"strings"
|
2022-02-04 08:55:12 +08:00
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2022-02-06 03:20:50 +08:00
|
|
|
|
|
|
|
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)
|
2022-02-06 03:20:50 +08:00
|
|
|
}
|