adding windows logic

This commit is contained in:
afeiszli 2022-08-23 15:58:02 -04:00
parent c41042ff06
commit 6b41de6563
2 changed files with 17 additions and 3 deletions

View file

@ -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

View file

@ -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)