Browse Source

added better handling of wg ifaces on windows + fix connect issue

0xdcarns 2 years ago
parent
commit
3b1ff49344

+ 5 - 1
mq/handlers.go

@@ -97,7 +97,11 @@ func UpdateNode(client mqtt.Client, msg mqtt.Message) {
 			logger.Log(1, "error saving node", err.Error())
 			logger.Log(1, "error saving node", err.Error())
 			return
 			return
 		}
 		}
-		updateNodePeers(&currentNode)
+		if ifaceDelta { // reduce number of unneeded updates, by only sending on iface changes
+			if err = PublishPeerUpdate(&currentNode, true); err != nil {
+				logger.Log(0, "error updating peers when node", currentNode.Name, currentNode.ID, "informed the server of an interface change", err.Error())
+			}
+		}
 		logger.Log(1, "updated node", id, newNode.Name)
 		logger.Log(1, "updated node", id, newNode.Name)
 	}()
 	}()
 }
 }

+ 0 - 6
netclient/functions/mqhandlers.go

@@ -129,9 +129,6 @@ func NodeUpdate(client mqtt.Client, msg mqtt.Message) {
 		wireguard.UpdateKeepAlive(file, newNode.PersistentKeepalive)
 		wireguard.UpdateKeepAlive(file, newNode.PersistentKeepalive)
 	}
 	}
 	logger.Log(0, "applying WG conf to "+file)
 	logger.Log(0, "applying WG conf to "+file)
-	if ncutils.IsWindows() {
-		wireguard.RemoveConfGraceful(nodeCfg.Node.Interface)
-	}
 	err = wireguard.ApplyConf(&nodeCfg.Node, nodeCfg.Node.Interface, file)
 	err = wireguard.ApplyConf(&nodeCfg.Node, nodeCfg.Node.Interface, file)
 	if err != nil {
 	if err != nil {
 		logger.Log(0, "error restarting wg after node update -", err.Error())
 		logger.Log(0, "error restarting wg after node update -", err.Error())
@@ -227,9 +224,6 @@ func UpdatePeers(client mqtt.Client, msg mqtt.Message) {
 		if err := config.ModNodeConfig(&cfg.Node); err != nil {
 		if err := config.ModNodeConfig(&cfg.Node); err != nil {
 			logger.Log(0, "failed to save internet gateway", err.Error())
 			logger.Log(0, "failed to save internet gateway", err.Error())
 		}
 		}
-		if ncutils.IsWindows() {
-			wireguard.RemoveConfGraceful(cfg.Node.Interface)
-		}
 		if err := wireguard.ApplyConf(&cfg.Node, cfg.Node.Interface, file); err != nil {
 		if err := wireguard.ApplyConf(&cfg.Node, cfg.Node.Interface, file); err != nil {
 			logger.Log(0, "error applying internet gateway", err.Error())
 			logger.Log(0, "error applying internet gateway", err.Error())
 		}
 		}

+ 1 - 1
netclient/wireguard/common.go

@@ -277,7 +277,7 @@ func ApplyConf(node *models.Node, ifacename string, confPath string) error {
 	var err error
 	var err error
 	switch os {
 	switch os {
 	case "windows":
 	case "windows":
-		ApplyWindowsConf(confPath, isConnected)
+		ApplyWindowsConf(confPath, ifacename, isConnected)
 	case "nowgquick":
 	case "nowgquick":
 		ApplyWithoutWGQuick(node, ifacename, confPath, isConnected)
 		ApplyWithoutWGQuick(node, ifacename, confPath, isConnected)
 	default:
 	default:

+ 1 - 1
netclient/wireguard/unix.go

@@ -11,7 +11,7 @@ import (
 // ApplyWGQuickConf - applies wg-quick commands if os supports
 // ApplyWGQuickConf - applies wg-quick commands if os supports
 func ApplyWGQuickConf(confPath, ifacename string, isConnected bool) error {
 func ApplyWGQuickConf(confPath, ifacename string, isConnected bool) error {
 	if ncutils.IsWindows() {
 	if ncutils.IsWindows() {
-		return ApplyWindowsConf(confPath, isConnected)
+		return ApplyWindowsConf(confPath, ifacename, isConnected)
 	} else {
 	} else {
 		_, err := os.Stat(confPath)
 		_, err := os.Stat(confPath)
 		if err != nil {
 		if err != nil {

+ 3 - 1
netclient/wireguard/windows.go

@@ -8,7 +8,9 @@ import (
 )
 )
 
 
 // ApplyWindowsConf - applies the WireGuard configuration file on Windows
 // ApplyWindowsConf - applies the WireGuard configuration file on Windows
-func ApplyWindowsConf(confPath string, isConnected bool) error {
+func ApplyWindowsConf(confPath, iface string, isConnected bool) error {
+	RemoveConfGraceful(iface) // have to remove gracefully before applying windows conf
+
 	if !isConnected {
 	if !isConnected {
 		return nil
 		return nil
 	}
 	}