Support unsafe_routes on Darwin (#139)

* Support unsafe_routes on darwin

* fix formatting in tun_darwin.go (spaces to tabs)
This commit is contained in:
Chad Harp 2020-01-06 12:09:56 -06:00 committed by Nathan Brown
parent e9b0498b21
commit 4e0da13180
1 changed files with 13 additions and 8 deletions

View File

@ -13,6 +13,7 @@ type Tun struct {
Device string
Cidr *net.IPNet
MTU int
UnsafeRoutes []route
*water.Interface
}
@ -21,13 +22,11 @@ func newTun(deviceName string, cidr *net.IPNet, defaultMTU int, routes []route,
if len(routes) > 0 {
return nil, fmt.Errorf("Route MTU not supported in Darwin")
}
if len(unsafeRoutes) > 0 {
return nil, fmt.Errorf("unsafeRoutes not supported in Darwin")
}
// NOTE: You cannot set the deviceName under Darwin, so you must check tun.Device after calling .Activate()
return &Tun{
Cidr: cidr,
MTU: defaultMTU,
UnsafeRoutes: unsafeRoutes,
}, nil
}
@ -52,6 +51,12 @@ func (c *Tun) Activate() error {
if err = exec.Command("ifconfig", c.Device, "mtu", strconv.Itoa(c.MTU)).Run(); err != nil {
return fmt.Errorf("failed to run 'ifconfig': %s", err)
}
// Unsafe path routes
for _, r := range c.UnsafeRoutes {
if err = exec.Command("route", "-n", "add", "-net", r.route.String(), "-interface", c.Device).Run(); err != nil {
return fmt.Errorf("failed to run 'route add' for unsafe_route %s: %s", r.route.String(), err)
}
}
return nil
}