mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-03 18:35:01 +08:00
adding windows GetDefaultRoute
This commit is contained in:
parent
4d9a07bf00
commit
878e3d848e
3 changed files with 34 additions and 11 deletions
|
@ -32,7 +32,7 @@ func GetDefaultRoute() (string, string, error) {
|
|||
return ipaddr, iface, fmt.Errorf("could not find default gateway")
|
||||
}
|
||||
ipaddr = outputSlice[1]
|
||||
if err = checkIPAddress(ipaddr); err != nil {
|
||||
if err = ncutils.CheckIPAddress(ipaddr); err != nil {
|
||||
return ipaddr, iface, err
|
||||
}
|
||||
iface = outputSlice[3]
|
||||
|
@ -40,13 +40,6 @@ func GetDefaultRoute() (string, string, error) {
|
|||
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
|
||||
// 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 {
|
||||
|
|
|
@ -1,19 +1,42 @@
|
|||
package local
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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) {
|
||||
var ipaddr string
|
||||
var iface string
|
||||
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 {
|
||||
|
|
|
@ -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
|
||||
func GetNewIface(dir string) (string, error) {
|
||||
files, _ := os.ReadDir(dir)
|
||||
|
|
Loading…
Add table
Reference in a new issue