Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NET-1148:add lock for multinet on Win #769

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions functions/mqhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/<NodeID> topic
func NodeUpdate(client mqtt.Client, msg mqtt.Message) {
network := parseNetworkFromTopic(msg.Topic())
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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()
}
}
29 changes: 18 additions & 11 deletions functions/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions wireguard/wireguard_darwin.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing wgMutex for linux close func

Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions wireguard/wireguard_windows.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be good to add mutex for other OS as well

Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Loading