Browse Source

adding windows GetDefaultRoute

afeiszli 3 years ago
parent
commit
878e3d848e

+ 1 - 8
netclient/local/routes_darwin.go

@@ -32,7 +32,7 @@ func GetDefaultRoute() (string, string, error) {
 		return ipaddr, iface, fmt.Errorf("could not find default gateway")
 		return ipaddr, iface, fmt.Errorf("could not find default gateway")
 	}
 	}
 	ipaddr = outputSlice[1]
 	ipaddr = outputSlice[1]
-	if err = checkIPAddress(ipaddr); err != nil {
+	if err = ncutils.CheckIPAddress(ipaddr); err != nil {
 		return ipaddr, iface, err
 		return ipaddr, iface, err
 	}
 	}
 	iface = outputSlice[3]
 	iface = outputSlice[3]
@@ -40,13 +40,6 @@ func GetDefaultRoute() (string, string, error) {
 	return ipaddr, iface, err
 	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
 // 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
 // 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 {
 func setRoute(iface string, addr *net.IPNet, address string) error {

+ 26 - 3
netclient/local/routes_windows.go

@@ -1,19 +1,42 @@
 package local
 package local
 
 
 import (
 import (
+	"fmt"
 	"net"
 	"net"
+	"regexp"
+	"strings"
 	"time"
 	"time"
 
 
 	"github.com/gravitl/netmaker/netclient/ncutils"
 	"github.com/gravitl/netmaker/netclient/ncutils"
 )
 )
 
 
-// GetDefaultRoute - Gets the default route (ip and interface) on a linux machine
+// GetDefaultRoute - Gets the default route (ip and interface) on a windows machine
 func GetDefaultRoute() (string, string, error) {
 func GetDefaultRoute() (string, string, error) {
 	var ipaddr string
 	var ipaddr string
 	var iface string
 	var iface string
 	var err error
 	var err error
-
-	return ipaddr, iface, fmt.Errorf("not written yet on windows")
+	var outLine string
+	output, err := ncutils.RunCmd("netsh interface ipv4 show route", false)
+	if err != nil {
+		return ipaddr, iface, err
+	}
+	for _, line := range strings.Split(strings.TrimSuffix(output, "\n"), "\n") {
+		if strings.Contains(line, "0.0.0.0/0") {
+			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, " ")), " ")
+	ipaddr = outputSlice[len(outputSlice)-1]
+	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
 }
 }
 
 
 func setRoute(iface string, addr *net.IPNet, address string) error {
 func setRoute(iface string, addr *net.IPNet, address string) error {

+ 7 - 0
netclient/ncutils/netclientutils.go

@@ -358,6 +358,13 @@ func GetNetclientPathSpecific() string {
 	}
 	}
 }
 }
 
 
+func CheckIPAddress(ip string) error {
+	if net.ParseIP(ip) == nil {
+		return fmt.Errorf("ip address %s is invalid", ip)
+	}
+	return nil
+}
+
 // GetNewIface - Gets the name of the real interface created on Mac
 // GetNewIface - Gets the name of the real interface created on Mac
 func GetNewIface(dir string) (string, error) {
 func GetNewIface(dir string) (string, error) {
 	files, _ := os.ReadDir(dir)
 	files, _ := os.ReadDir(dir)