Browse Source

fixing windows remove conf

afeiszli 3 years ago
parent
commit
3b37532eaa
3 changed files with 33 additions and 21 deletions
  1. 0 0
      netclient/netclient.syso
  2. 28 15
      netclient/wireguard/common.go
  3. 5 6
      netclient/wireguard/noquick.go

+ 0 - 0
netclient/windowsdata/resource/netclient.syso → netclient/netclient.syso


+ 28 - 15
netclient/wireguard/common.go

@@ -166,21 +166,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
 		}
 	}
 	// ensure you clear any existing interface first
-	d, _ := wgclient.Device(deviceiface)
-	startTime := time.Now()
-	for d != nil && d.Name == deviceiface {
-		if err = RemoveConf(deviceiface, false); err != nil { // remove interface first
-			if strings.Contains(err.Error(), "does not exist") {
-				err = nil
-				break
-			}
-		}
-		time.Sleep(time.Second >> 2)
-		d, _ = wgclient.Device(deviceiface)
-		if time.Now().After(startTime.Add(time.Second << 4)) {
-			break
-		}
-	}
+	RemoveConfGraceful(deviceiface)
 	ApplyConf(node, ifacename, confPath)            // Apply initially
 	ncutils.PrintLog("waiting for interface...", 1) // ensure interface is created
 	output, _ := ncutils.RunCmd("wg", false)
@@ -301,6 +287,7 @@ func ApplyConf(node *models.Node, ifacename string, confPath string) error {
 	var err error
 	switch os {
 	case "windows":
+		RemoveConfGraceful(ifacename)
 		ApplyWindowsConf(confPath)
 	case "darwin":
 		ApplyMacOSConf(node, ifacename, confPath)
@@ -478,3 +465,29 @@ func UpdatePrivateKey(file, privateKey string) error {
 	}
 	return nil
 }
+
+// RemoveConfGraceful - Run remove conf and wait for it to actually be gone before proceeding
+func RemoveConfGraceful(ifacename string) {
+	// ensure you clear any existing interface first
+	wgclient, err := wgctrl.New()
+	if err != nil {
+		ncutils.PrintLog("could not create wgclient", 0)
+		return
+	}
+	defer wgclient.Close()
+	d, _ := wgclient.Device(ifacename)
+	startTime := time.Now()
+	for d != nil && d.Name == ifacename {
+		if err = RemoveConf(ifacename, false); err != nil { // remove interface first
+			if strings.Contains(err.Error(), "does not exist") {
+				err = nil
+				break
+			}
+		}
+		time.Sleep(time.Second >> 2)
+		d, _ = wgclient.Device(ifacename)
+		if time.Now().After(startTime.Add(time.Second << 4)) {
+			break
+		}
+	}
+}

+ 5 - 6
netclient/wireguard/noquick.go

@@ -7,7 +7,6 @@ import (
 	"strconv"
 	"strings"
 
-	"github.com/gravitl/netmaker/logger"
 	"github.com/gravitl/netmaker/models"
 	"github.com/gravitl/netmaker/netclient/config"
 	"github.com/gravitl/netmaker/netclient/ncutils"
@@ -72,7 +71,7 @@ func ApplyWithoutWGQuick(node *models.Node, ifacename string, confPath string) e
 		}
 	}
 	if _, err := ncutils.RunCmd(ipExec+" link set down dev "+ifacename, false); err != nil {
-		logger.Log(2, "attempted to remove interface before editing")
+		ncutils.PrintLog("attempted to remove interface before editing", 1)
 		return err
 	}
 	if node.PostDown != "" {
@@ -81,7 +80,7 @@ func ApplyWithoutWGQuick(node *models.Node, ifacename string, confPath string) e
 	}
 	// set MTU of node interface
 	if _, err := ncutils.RunCmd(ipExec+" link set mtu "+strconv.Itoa(int(node.MTU))+" up dev "+ifacename, true); err != nil {
-		logger.Log(2, "failed to create interface with mtu", strconv.Itoa(int(node.MTU)), "-", ifacename)
+		ncutils.PrintLog("failed to create interface with mtu "+strconv.Itoa(int(node.MTU))+"-"+ifacename, 1)
 		return err
 	}
 	if node.PostUp != "" {
@@ -89,7 +88,7 @@ func ApplyWithoutWGQuick(node *models.Node, ifacename string, confPath string) e
 		_ = ncutils.RunCmds(runcmds, true)
 	}
 	if node.Address6 != "" && node.IsDualStack == "yes" {
-		logger.Log(1, "adding address:", node.Address6)
+		ncutils.PrintLog("adding address: "+node.Address6, 1)
 		_, _ = ncutils.RunCmd(ipExec+" address add dev "+ifacename+" "+node.Address6+"/64", true)
 	}
 	return nil
@@ -104,8 +103,8 @@ func RemoveWithoutWGQuick(ifacename string) error {
 	out, err := ncutils.RunCmd(ipExec+" link del "+ifacename, false)
 	dontprint := strings.Contains(out, "does not exist") || strings.Contains(out, "Cannot find device")
 	if err != nil && !dontprint {
-		logger.Log(1, "error running command:", ipExec, "link del", ifacename)
-		logger.Log(1, out)
+		ncutils.PrintLog("error running command: "+ipExec+" link del "+ifacename, 1)
+		ncutils.PrintLog(out, 1)
 	}
 	network := strings.ReplaceAll(ifacename, "nm-", "")
 	nodeconf, err := config.ReadConfig(network)