From 4e0da13180a07da4df31f91dea98237ff3bd0322 Mon Sep 17 00:00:00 2001 From: Chad Harp Date: Mon, 6 Jan 2020 12:09:56 -0600 Subject: [PATCH] Support unsafe_routes on Darwin (#139) * Support unsafe_routes on darwin * fix formatting in tun_darwin.go (spaces to tabs) --- tun_darwin.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/tun_darwin.go b/tun_darwin.go index 907add1..0cc9339 100644 --- a/tun_darwin.go +++ b/tun_darwin.go @@ -10,9 +10,10 @@ import ( ) type Tun struct { - Device string - Cidr *net.IPNet - MTU int + 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, + 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 }