tuntap: add routing gateway options

This commit is contained in:
ginuerzh 2020-01-04 15:55:42 +08:00
parent 267885f1b4
commit 4d818442d7
5 changed files with 90 additions and 29 deletions

View File

@ -441,6 +441,7 @@ func (r *route) GenRouters() ([]router, error) {
Addr: node.Get("net"),
MTU: node.GetInt("mtu"),
Routes: strings.Split(node.Get("route"), ","),
Gateway: node.Get("gw"),
}
handler = gost.TunHandler(node.Remote, gost.TunConfigHandlerOption(cfg))
case "tap":
@ -449,6 +450,7 @@ func (r *route) GenRouters() ([]router, error) {
Addr: node.Get("net"),
MTU: node.GetInt("mtu"),
Routes: strings.Split(node.Get("route"), ","),
Gateway: node.Get("gw"),
}
handler = gost.TapHandler(node.Remote, gost.TapConfigHandlerOption(cfg))
default:

View File

@ -42,6 +42,7 @@ type TunConfig struct {
Addr string
MTU int
Routes []string
Gateway string
}
type tunRouteKey [16]byte
@ -355,7 +356,7 @@ func etherType(et waterutil.Ethertype) string {
if s, ok := mEtherTypes[et]; ok {
return s
}
return "unknown"
return fmt.Sprintf("unknown(%v)", et)
}
type TapConfig struct {
@ -363,6 +364,7 @@ type TapConfig struct {
Addr string
MTU int
Routes []string
Gateway string
}
type tapRouteKey [6]byte

View File

@ -57,7 +57,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.Routes...); err != nil {
return
}
@ -120,7 +120,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
}
@ -136,16 +136,30 @@ 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, routes ...string) error {
for _, route := range routes {
if route == "" {
continue
}
cmd := fmt.Sprintf("ip route add %s dev %s", route, ifName)
log.Logf("[%s] %s", ifType, cmd)
log.Logf("[tun] %s", cmd)
if err := netlink.AddRoute(route, "", "", ifName); err != nil {
return fmt.Errorf("%s: %v", cmd, err)
}
}
return nil
}
func addTapRoutes(ifName string, gw string, routes ...string) error {
for _, route := range routes {
if route == "" {
continue
}
cmd := fmt.Sprintf("ip route add %s via %s dev %s", route, gw, ifName)
log.Logf("[tap] %s", cmd)
if err := netlink.AddRoute(route, "", gw, ifName); err != nil {
return fmt.Errorf("%s: %v", cmd, err)
}
}
return nil
}

View File

@ -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.Routes...); err != nil {
return
}
@ -80,7 +80,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
}
@ -96,13 +96,31 @@ 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, routes ...string) error {
for _, route := range routes {
if route == "" {
continue
}
cmd := fmt.Sprintf("route add -net %s -interface %s", route, ifName)
log.Logf("[%s] %s", ifType, cmd)
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
}
cmd := fmt.Sprintf("route add -net %s dev %s", route, ifName)
if gw != "" {
cmd += " gw " + 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)

View File

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