Browse Source

adding windows logic

afeiszli 3 years ago
parent
commit
6b41de6563
2 changed files with 17 additions and 3 deletions
  1. 1 0
      netclient/local/routes_linux.go
  2. 16 3
      netclient/local/routes_windows.go

+ 1 - 0
netclient/local/routes_linux.go

@@ -44,6 +44,7 @@ func setRoute(iface string, addr *net.IPNet, address string) error {
 	return err
 }
 
+// SetExplicitRoute - sets route via explicit ip address
 func SetExplicitRoute(iface string, destination *net.IPNet, gateway string) error {
 	_, err := ncutils.RunCmd(fmt.Sprintf("ip route add %s via %s dev %s", destination.String(), gateway, iface), false)
 	return err

+ 16 - 3
netclient/local/routes_windows.go

@@ -16,12 +16,16 @@ func GetDefaultRoute() (string, string, error) {
 	var iface string
 	var err error
 	var outLine string
-	output, err := ncutils.RunCmd("netsh interface ipv4 show route", false)
+	output, err := ncutils.RunCmd("netstat -rn", false)
 	if err != nil {
 		return ipaddr, iface, err
 	}
+	var startLook bool
 	for _, line := range strings.Split(strings.TrimSuffix(output, "\n"), "\n") {
-		if strings.Contains(line, "0.0.0.0/0") {
+		if strings.Contains(line, "Active Routes:") {
+			startLook = true
+		}
+		if startLook && strings.Contains(line, "0.0.0.0") {
 			outLine = line
 			break
 		}
@@ -31,7 +35,7 @@ func GetDefaultRoute() (string, string, error) {
 	}
 	space := regexp.MustCompile(`\s+`)
 	outputSlice := strings.Split(strings.TrimSpace(space.ReplaceAllString(outLine, " ")), " ")
-	ipaddr = outputSlice[len(outputSlice)-1]
+	ipaddr = outputSlice[len(outputSlice)-2]
 	if err = ncutils.CheckIPAddress(ipaddr); err != nil {
 		return ipaddr, iface, fmt.Errorf("invalid output for ip address check: " + err.Error())
 	}
@@ -47,6 +51,15 @@ func setRoute(iface string, addr *net.IPNet, address string) error {
 	return err
 }
 
+// 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
+}
+
 func deleteRoute(iface string, addr *net.IPNet, address string) error {
 	var err error
 	_, err = ncutils.RunCmd("route DELETE "+addr.IP.String()+" MASK "+addr.Mask.String()+" "+address, false)