From 4fb6775c0f3f07d824c4da846f47d65d4ede466e Mon Sep 17 00:00:00 2001 From: Max Ma Date: Wed, 17 Apr 2024 23:33:56 +0200 Subject: [PATCH 1/3] add lock for multinet on Win --- wireguard/wireguard_windows.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/wireguard/wireguard_windows.go b/wireguard/wireguard_windows.go index 5326f7cd..970a6a53 100644 --- a/wireguard/wireguard_windows.go +++ b/wireguard/wireguard_windows.go @@ -358,6 +358,8 @@ func restoreInternetGwV4() (err error) { // NCIface.Close - closes the managed WireGuard interface func (nc *NCIface) Close() { + wgMutex.Lock() + defer wgMutex.Unlock() err := nc.Iface.Close() if err != nil { logger.Log(0, "error closing netclient interface -", err.Error()) From adfdf2ff095b0cdcb6bfa0267a2c51413b991f4f Mon Sep 17 00:00:00 2001 From: Max Ma Date: Wed, 24 Apr 2024 13:50:15 +0200 Subject: [PATCH 2/3] add lock for multinet on darwin --- wireguard/wireguard_darwin.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/wireguard/wireguard_darwin.go b/wireguard/wireguard_darwin.go index a6e24cdd..50f21800 100644 --- a/wireguard/wireguard_darwin.go +++ b/wireguard/wireguard_darwin.go @@ -93,6 +93,8 @@ func (nc *NCIface) SetMTU() error { } func (nc *NCIface) Close() { + wgMutex.Lock() + defer wgMutex.Unlock() err := nc.Iface.Close() if err == nil { sockPath := "/var/run/wireguard/" + nc.Name + ".sock" From 4f56482d5eafd026e14a4e62fc3f291c5e07a81d Mon Sep 17 00:00:00 2001 From: Max Ma Date: Thu, 25 Apr 2024 10:41:02 +0200 Subject: [PATCH 3/3] add lock for reset interface --- functions/mqhandlers.go | 41 ++++++++++++++++++++--------------------- functions/uninstall.go | 29 ++++++++++++++++++----------- 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/functions/mqhandlers.go b/functions/mqhandlers.go index 2a3e9d0c..e97a56f2 100644 --- a/functions/mqhandlers.go +++ b/functions/mqhandlers.go @@ -35,6 +35,8 @@ var All mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) { slog.Info("default message handler -- received message but not handling", "topic", msg.Topic()) } +var mNMutex = sync.Mutex{} // used to mutex functions of the interface + // NodeUpdate -- mqtt message handler for /update/ topic func NodeUpdate(client mqtt.Client, msg mqtt.Message) { network := parseNetworkFromTopic(msg.Topic()) @@ -326,17 +328,23 @@ func HostUpdate(client mqtt.Client, msg mqtt.Message) { return } if resetInterface { - nc := wireguard.GetInterface() - nc.Close() - nc = wireguard.NewNCIface(config.Netclient(), config.GetNodes()) - nc.Create() - if err := nc.Configure(); err != nil { - slog.Error("could not configure netmaker interface", "error", err) - return - } - if err = wireguard.SetPeers(false); err != nil { - slog.Error("failed to set peers", err) - } + resetInterfaceFunc() + } +} + +func resetInterfaceFunc() { + mNMutex.Lock() + defer mNMutex.Unlock() + nc := wireguard.GetInterface() + nc.Close() + nc = wireguard.NewNCIface(config.Netclient(), config.GetNodes()) + nc.Create() + if err := nc.Configure(); err != nil { + slog.Error("could not configure netmaker interface", "error", err) + return + } + if err := wireguard.SetPeers(false); err != nil { + slog.Error("failed to set peers", err) } } @@ -596,15 +604,6 @@ func mqFallbackPull(pullResponse models.HostPull, resetInterface, replacePeers b handleFwUpdate(serverName, &pullResponse.FwUpdate) if resetInterface { - nc := wireguard.GetInterface() - nc.Close() - nc = wireguard.NewNCIface(config.Netclient(), config.GetNodes()) - nc.Create() - if err := nc.Configure(); err != nil { - slog.Error("could not configure netmaker interface", "error", err) - return - } - _ = wireguard.SetPeers(false) - slog.Info("mqfallback reset interface") + resetInterfaceFunc() } } diff --git a/functions/uninstall.go b/functions/uninstall.go index fdd9ceeb..494be896 100644 --- a/functions/uninstall.go +++ b/functions/uninstall.go @@ -64,17 +64,7 @@ func LeaveNetwork(network string, isDaemon bool) ([]error, error) { } // re-configure interface if daemon is calling leave if isDaemon { - nc := wireguard.GetInterface() - nc.Close() - nc = wireguard.NewNCIface(config.Netclient(), config.GetNodes()) - nc.Create() - if err := nc.Configure(); err != nil { - faults = append(faults, fmt.Errorf("failed to configure interface during node removal - %v", err.Error())) - } else { - if err = wireguard.SetPeers(true); err != nil { - faults = append(faults, fmt.Errorf("issue setting peers after node removal - %v", err.Error())) - } - } + faults = resetInterfaceUninstall(faults) } else { // was called from CLI so restart daemon if err := daemon.Restart(); err != nil { faults = append(faults, fmt.Errorf("could not restart daemon after leave - %v", err.Error())) @@ -87,6 +77,23 @@ func LeaveNetwork(network string, isDaemon bool) ([]error, error) { return faults, nil } +func resetInterfaceUninstall(faults []error) []error { + mNMutex.Lock() + defer mNMutex.Unlock() + nc := wireguard.GetInterface() + nc.Close() + nc = wireguard.NewNCIface(config.Netclient(), config.GetNodes()) + nc.Create() + if err := nc.Configure(); err != nil { + faults = append(faults, fmt.Errorf("failed to configure interface during node removal - %v", err.Error())) + } else { + if err = wireguard.SetPeers(true); err != nil { + faults = append(faults, fmt.Errorf("issue setting peers after node removal - %v", err.Error())) + } + } + return faults +} + func deleteNodeFromServer(node *config.Node) error { server := config.GetServer(node.Server) if server == nil {