netmaker/netclient/local/routes_windows.go

78 lines
2.4 KiB
Go
Raw Normal View History

2022-02-04 08:55:12 +08:00
package local
import (
2022-08-23 12:33:48 +08:00
"fmt"
2022-02-04 08:55:12 +08:00
"net"
2022-08-23 12:33:48 +08:00
"regexp"
"strings"
2022-02-04 08:55:12 +08:00
"time"
"github.com/gravitl/netmaker/netclient/ncutils"
)
2022-08-23 12:33:48 +08:00
// GetDefaultRoute - Gets the default route (ip and interface) on a windows machine
2022-08-23 04:44:04 +08:00
func GetDefaultRoute() (string, string, error) {
var ipaddr string
var iface string
var err error
2022-08-23 12:33:48 +08:00
var outLine string
2022-08-24 03:58:02 +08:00
output, err := ncutils.RunCmd("netstat -rn", false)
2022-08-23 12:33:48 +08:00
if err != nil {
return ipaddr, iface, err
}
2022-08-24 03:58:02 +08:00
var startLook bool
2022-08-23 12:33:48 +08:00
for _, line := range strings.Split(strings.TrimSuffix(output, "\n"), "\n") {
2022-08-24 03:58:02 +08:00
if strings.Contains(line, "Active Routes:") {
startLook = true
}
if startLook && strings.Contains(line, "0.0.0.0") {
2022-08-23 12:33:48 +08:00
outLine = line
break
}
}
if outLine == "" {
return ipaddr, iface, fmt.Errorf("could not find default gateway")
}
space := regexp.MustCompile(`\s+`)
outputSlice := strings.Split(strings.TrimSpace(space.ReplaceAllString(outLine, " ")), " ")
2022-08-24 03:58:02 +08:00
ipaddr = outputSlice[len(outputSlice)-2]
2022-08-23 12:33:48 +08:00
if err = ncutils.CheckIPAddress(ipaddr); err != nil {
return ipaddr, iface, fmt.Errorf("invalid output for ip address check: " + err.Error())
}
iface = "irrelevant"
return ipaddr, iface, err
2022-08-23 04:44:04 +08:00
}
2022-02-04 08:55:12 +08:00
func setRoute(iface string, addr *net.IPNet, address string) error {
var err error
2022-02-07 00:06:04 +08:00
_, err = ncutils.RunCmd("route ADD "+addr.String()+" "+address, false)
2022-02-04 08:55:12 +08:00
time.Sleep(time.Second >> 2)
2022-02-07 00:06:04 +08:00
ncutils.RunCmd("route CHANGE "+addr.IP.String()+" MASK "+addr.Mask.String()+" "+address, false)
2022-02-04 08:55:12 +08:00
return err
}
2022-08-24 03:58:02 +08:00
// SetExplicitRoute - sets route via explicit ip address
func SetExplicitRoute(iface string, destination *net.IPNet, gateway string) error {
var err error
_, err = ncutils.RunCmd("route ADD "+destination.String()+" "+gateway, false)
time.Sleep(time.Second >> 2)
ncutils.RunCmd("route CHANGE "+destination.IP.String()+" MASK "+destination.Mask.String()+" "+gateway, false)
return err
}
2022-02-04 08:55:12 +08:00
func deleteRoute(iface string, addr *net.IPNet, address string) error {
var err error
2022-02-07 00:06:04 +08:00
_, err = ncutils.RunCmd("route DELETE "+addr.IP.String()+" MASK "+addr.Mask.String()+" "+address, false)
2022-02-04 08:55:12 +08:00
return err
}
func setCidr(iface, address string, addr *net.IPNet) {
2022-02-07 00:06:04 +08:00
ncutils.RunCmd("route ADD "+addr.String()+" "+address, false)
time.Sleep(time.Second >> 2)
2022-02-07 00:06:04 +08:00
ncutils.RunCmd("route CHANGE "+addr.IP.String()+" MASK "+addr.Mask.String()+" "+address, false)
2022-02-06 04:00:26 +08:00
}
2022-02-06 05:09:23 +08:00
func removeCidr(iface string, addr *net.IPNet, address string) {
2022-02-07 00:06:04 +08:00
ncutils.RunCmd("route DELETE "+addr.String(), false)
}