From b0b755b08b4aaa0d11b4f2dadac0e2cd8698c4c4 Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Fri, 10 May 2024 14:45:02 +0200 Subject: [PATCH] User Ip as next hop for default routes on darwin --- client/internal/routemanager/manager.go | 1 + client/internal/routemanager/systemops.go | 10 ++++++---- client/internal/routemanager/systemops_darwin.go | 2 ++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/client/internal/routemanager/manager.go b/client/internal/routemanager/manager.go index 47549f74d..24d7e9c3e 100644 --- a/client/internal/routemanager/manager.go +++ b/client/internal/routemanager/manager.go @@ -53,6 +53,7 @@ type DefaultManager struct { } func NewManager(ctx context.Context, pubKey string, wgInterface *iface.WGIface, statusRecorder *peer.Status, initialRoutes []*route.Route) *DefaultManager { + tunIP = netip.MustParseAddr(wgInterface.Address().IP.String()) mCTX, cancel := context.WithCancel(ctx) dm := &DefaultManager{ ctx: mCTX, diff --git a/client/internal/routemanager/systemops.go b/client/internal/routemanager/systemops.go index bc506411c..c7baead48 100644 --- a/client/internal/routemanager/systemops.go +++ b/client/internal/routemanager/systemops.go @@ -28,6 +28,8 @@ var splitDefaultv6_2 = netip.PrefixFrom(netip.AddrFrom16([16]byte{0x80}), 1) var ErrRouteNotFound = errors.New("route not found") var ErrRouteNotAllowed = errors.New("route not allowed") +var tunIP netip.Addr + // TODO: fix: for default our wg address now appears as the default gw func addRouteForCurrentDefaultGateway(prefix netip.Prefix) error { addr := netip.IPv4Unspecified() @@ -196,10 +198,10 @@ func addRouteToNonVPNIntf(prefix netip.Prefix, vpnIntf *iface.WGIface, initialNe // in two /1 prefixes to avoid replacing the existing default route func genericAddVPNRoute(prefix netip.Prefix, intf *net.Interface) error { if prefix == defaultv4 { - if err := addToRouteTable(splitDefaultv4_1, netip.Addr{}, intf); err != nil { + if err := addToRouteTable(splitDefaultv4_1, tunIP, intf); err != nil { return err } - if err := addToRouteTable(splitDefaultv4_2, netip.Addr{}, intf); err != nil { + if err := addToRouteTable(splitDefaultv4_2, tunIP, intf); err != nil { if err2 := removeFromRouteTable(splitDefaultv4_1, netip.Addr{}, intf); err2 != nil { log.Warnf("Failed to rollback route addition: %s", err2) } @@ -266,10 +268,10 @@ func addNonExistingRoute(prefix netip.Prefix, intf *net.Interface) error { func genericRemoveVPNRoute(prefix netip.Prefix, intf *net.Interface) error { if prefix == defaultv4 { var result *multierror.Error - if err := removeFromRouteTable(splitDefaultv4_1, netip.Addr{}, intf); err != nil { + if err := removeFromRouteTable(splitDefaultv4_1, tunIP, intf); err != nil { result = multierror.Append(result, err) } - if err := removeFromRouteTable(splitDefaultv4_2, netip.Addr{}, intf); err != nil { + if err := removeFromRouteTable(splitDefaultv4_2, tunIP, intf); err != nil { result = multierror.Append(result, err) } diff --git a/client/internal/routemanager/systemops_darwin.go b/client/internal/routemanager/systemops_darwin.go index 017dc6c28..794c4a170 100644 --- a/client/internal/routemanager/systemops_darwin.go +++ b/client/internal/routemanager/systemops_darwin.go @@ -52,8 +52,10 @@ func routeCmd(action string, prefix netip.Prefix, nexthop netip.Addr, intf *net. args := []string{"-n", action, inet, network} if nexthop.IsValid() { + log.Debugf("route %s %s via %s", action, prefix, nexthop) args = append(args, nexthop.Unmap().String()) } else if intf != nil { + log.Debugf("route %s %s via %s, iptun: %s", action, prefix, intf.Name, tunIP) args = append(args, "-interface", intf.Name) }