2022-02-03 09:46:25 +08:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
//"github.com/davecgh/go-spew/spew"
|
|
|
|
|
2022-02-03 11:00:59 +08:00
|
|
|
"fmt"
|
2022-02-03 09:46:25 +08:00
|
|
|
"net"
|
2022-02-06 03:26:19 +08:00
|
|
|
"strings"
|
2022-02-03 09:46:25 +08:00
|
|
|
|
|
|
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
|
|
|
)
|
|
|
|
|
2022-02-04 08:55:12 +08:00
|
|
|
func setRoute(iface string, addr *net.IPNet, address string) error {
|
2022-02-06 03:26:19 +08:00
|
|
|
out, err := ncutils.RunCmd(fmt.Sprintf("ip route get %s", addr.IP.String()), false)
|
|
|
|
if err != nil || !strings.Contains(out, iface) {
|
2022-02-06 04:38:50 +08:00
|
|
|
_, err = ncutils.RunCmd(fmt.Sprintf("ip route add %s dev %s", addr.String(), iface), false)
|
2022-02-06 03:26:19 +08:00
|
|
|
}
|
2022-02-03 09:46:25 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-04 08:55:12 +08:00
|
|
|
func deleteRoute(iface string, addr *net.IPNet, address string) error {
|
2022-02-03 09:46:25 +08:00
|
|
|
var err error
|
2022-02-06 03:26:19 +08:00
|
|
|
out, _ := ncutils.RunCmd(fmt.Sprintf("ip route get %s", addr.IP.String()), false)
|
|
|
|
if strings.Contains(out, iface) {
|
|
|
|
_, err = ncutils.RunCmd(fmt.Sprintf("ip route del %s dev %s", addr.String(), iface), false)
|
|
|
|
}
|
2022-02-03 09:46:25 +08:00
|
|
|
return err
|
|
|
|
}
|
2022-02-06 03:20:50 +08:00
|
|
|
|
|
|
|
func setCidr(iface, address string, addr *net.IPNet) {
|
|
|
|
ncutils.RunCmd("ip -4 route add "+addr.String()+" dev "+iface, false)
|
|
|
|
}
|
2022-02-06 04:00:26 +08:00
|
|
|
|
|
|
|
func removeCidr(iface string, addr *net.IPNet, address string) {
|
|
|
|
ncutils.RunCmd("ip route delete "+addr.String()+" dev "+iface, false)
|
|
|
|
}
|