|
|
|
@ -38,7 +38,7 @@ func createTun(cfg TunConfig) (conn net.Conn, itf *net.Interface, err error) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
if err = addRoutes("tun", ifce.Name(), cfg.Routes...); err != nil { |
|
|
|
if err = addTunRoutes(ifce.Name(), cfg.Gateway, cfg.Routes...); err != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
@ -82,7 +82,7 @@ func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
if err = addRoutes("tap", ifce.Name(), cfg.Routes...); err != nil { |
|
|
|
if err = addTapRoutes(ifce.Name(), cfg.Gateway, cfg.Routes...); err != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
@ -98,7 +98,7 @@ func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
func addRoutes(ifType, ifName string, routes ...string) error { |
|
|
|
func addTunRoutes(ifName string, gw string, routes ...string) error { |
|
|
|
for _, route := range routes { |
|
|
|
if route == "" { |
|
|
|
continue |
|
|
|
@ -108,7 +108,32 @@ func addRoutes(ifType, ifName string, routes ...string) error { |
|
|
|
|
|
|
|
cmd := fmt.Sprintf("netsh interface ip add route prefix=%s interface=%s store=active", |
|
|
|
route, ifName) |
|
|
|
log.Logf("[%s] %s", ifType, cmd) |
|
|
|
if gw != "" { |
|
|
|
cmd += " nexthop=" + gw |
|
|
|
} |
|
|
|
log.Logf("[tun] %s", cmd) |
|
|
|
args := strings.Split(cmd, " ") |
|
|
|
if er := exec.Command(args[0], args[1:]...).Run(); er != nil { |
|
|
|
return fmt.Errorf("%s: %v", cmd, er) |
|
|
|
} |
|
|
|
} |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
func addTapRoutes(ifName string, gw string, routes ...string) error { |
|
|
|
for _, route := range routes { |
|
|
|
if route == "" { |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
|
deleteRoute(ifName, route) |
|
|
|
|
|
|
|
cmd := fmt.Sprintf("netsh interface ip add route prefix=%s interface=%s store=active", |
|
|
|
route, ifName) |
|
|
|
if gw != "" { |
|
|
|
cmd += " nexthop=" + gw |
|
|
|
} |
|
|
|
log.Logf("[tap] %s", cmd) |
|
|
|
args := strings.Split(cmd, " ") |
|
|
|
if er := exec.Command(args[0], args[1:]...).Run(); er != nil { |
|
|
|
return fmt.Errorf("%s: %v", cmd, er) |
|
|
|
|