From ec8eb76b42f7719790af50750f94e9ce93331c18 Mon Sep 17 00:00:00 2001 From: Pascal Fischer Date: Fri, 6 Oct 2023 16:32:30 +0200 Subject: [PATCH] small refactor for better code quality in swift --- client/internal/engine.go | 8 +++++--- client/internal/routemanager/manager.go | 4 ++-- client/internal/routemanager/notifier.go | 16 ++++++---------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/client/internal/engine.go b/client/internal/engine.go index 6444e6d37..0868eece1 100644 --- a/client/internal/engine.go +++ b/client/internal/engine.go @@ -214,7 +214,9 @@ func (e *Engine) Start() error { } } - e.routeManager = routemanager.NewManager(e.ctx, e.config.WgPrivateKey.PublicKey().String(), e.wgInterface, e.statusRecorder, routes, wgAddr) + log.Debugf("Initial routes contain %d routes", len(routes)) + e.routeManager = routemanager.NewManager(e.ctx, e.config.WgPrivateKey.PublicKey().String(), e.wgInterface, e.statusRecorder, routes) + e.mobileDep.RouteListener.SetInterfaceIP(wgAddr) e.routeManager.SetRouteChangeListener(e.mobileDep.RouteListener) switch runtime.GOOS { @@ -225,8 +227,8 @@ func (e *Engine) Start() error { }) case "ios": err = e.wgInterface.CreateOniOS(e.mobileDep.FileDescriptor) - log.Debugf("wireguardAddress: %s", wgAddr) - e.mobileDep.RouteListener.OnNewRouteSetting("", "100.127.93.142/16") + log.Debugf("sending initial route range %s to iOS", strings.Join(e.routeManager.InitialRouteRange(), ",")) + e.mobileDep.RouteListener.OnNewRouteSetting(strings.Join(e.routeManager.InitialRouteRange(), ",")) default: err = e.wgInterface.Create() } diff --git a/client/internal/routemanager/manager.go b/client/internal/routemanager/manager.go index 1be182d0a..2f8482dbf 100644 --- a/client/internal/routemanager/manager.go +++ b/client/internal/routemanager/manager.go @@ -35,7 +35,7 @@ type DefaultManager struct { } // NewManager returns a new route manager -func NewManager(ctx context.Context, pubKey string, wgInterface *iface.WGIface, statusRecorder *peer.Status, initialRoutes []*route.Route, wgAddr string) *DefaultManager { +func NewManager(ctx context.Context, pubKey string, wgInterface *iface.WGIface, statusRecorder *peer.Status, initialRoutes []*route.Route) *DefaultManager { srvRouter, err := newServerRouter(ctx, wgInterface) if err != nil { log.Errorf("server router is not supported: %s", err) @@ -50,7 +50,7 @@ func NewManager(ctx context.Context, pubKey string, wgInterface *iface.WGIface, statusRecorder: statusRecorder, wgInterface: wgInterface, pubKey: pubKey, - notifier: newNotifier(wgAddr), + notifier: newNotifier(), } log.Debug("initializing route manager") diff --git a/client/internal/routemanager/notifier.go b/client/internal/routemanager/notifier.go index aa2be7606..7da4f920b 100644 --- a/client/internal/routemanager/notifier.go +++ b/client/internal/routemanager/notifier.go @@ -13,12 +13,11 @@ import ( // RouteListener is a callback interface for mobile system type RouteListener interface { // OnNewRouteSetting invoke when new route setting has been arrived - OnNewRouteSetting(string, string) + OnNewRouteSetting(string) + SetInterfaceIP(string) } type notifier struct { - // ownIPAddr is the ip address of the netbird interface including the netmask - ownIPAddr string initialRouteRangers []string routeRangers []string @@ -26,11 +25,8 @@ type notifier struct { routeListenerMux sync.Mutex } -func newNotifier(ip string) *notifier { - log.Debugf("creating notifier with own ip: %s", ip) - return ¬ifier{ - ownIPAddr: ip, - } +func newNotifier() *notifier { + return ¬ifier{} } func (n *notifier) setListener(listener RouteListener) { @@ -77,8 +73,8 @@ func (n *notifier) notify() { } go func(l RouteListener) { - log.Debugf("notifying route listener with route ranges: %s and own ip: %s", strings.Join(n.routeRangers, ","), n.ownIPAddr) - l.OnNewRouteSetting(strings.Join(n.routeRangers, ","), n.ownIPAddr) + log.Debugf("notifying route listener with route ranges: %s", strings.Join(n.routeRangers, ",")) + l.OnNewRouteSetting(strings.Join(n.routeRangers, ",")) }(n.routeListener) }