Browse Source

windows updates now working

afeiszli 3 years ago
parent
commit
9a72825534

+ 2 - 2
netclient/ncutils/netclientutils.go

@@ -40,7 +40,7 @@ const LINUX_APP_DATA_PATH = "/etc/netclient"
 const WINDOWS_APP_DATA_PATH = "C:\\ProgramData\\Netclient"
 
 // WINDOWS_APP_DATA_PATH - windows path
-const WINDOWS_WG_DATA_PATH = "C:\\Program Files\\WireGuard\\Data\\Configurations"
+const WINDOWS_WG_DPAPI_PATH = "C:\\Program Files\\WireGuard\\Data\\Configurations"
 
 // WINDOWS_SVC_NAME - service name
 const WINDOWS_SVC_NAME = "netclient"
@@ -322,7 +322,7 @@ func GetNetclientPathSpecific() string {
 // GetNetclientPathSpecific - gets specific netclient config path
 func GetWGPathSpecific() string {
 	if IsWindows() {
-		return WINDOWS_WG_DATA_PATH + "\\"
+		return WINDOWS_APP_DATA_PATH + "\\"
 	} else {
 		return "/etc/wireguard/"
 	}

BIN
netclient/netclient.syso


+ 15 - 8
netclient/wireguard/common.go

@@ -202,6 +202,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
 			}
 		}
 		if syncconf {
+			log.Println("syncing conf")
 			err = SyncWGQuickConf(ifacename, confPath)
 		} else {
 			d, _ := wgclient.Device(deviceiface)
@@ -210,19 +211,22 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
 				time.Sleep(time.Second >> 2)
 				d, _ = wgclient.Device(deviceiface)
 			}
-			err = ApplyConf(confPath)
-			if err != nil {
-				ncutils.PrintLog("failed to create wireguard interface", 1)
-				return err
-			}
-			if ncutils.IsWindows() {
+			if !ncutils.IsWindows() {
+				err = ApplyConf(confPath)
+				if err != nil {
+					ncutils.PrintLog("failed to create wireguard interface", 1)
+					return err
+				}
+			} else {
 				var output string
 				starttime := time.Now()
+				RemoveConf(ifacename, false)
+				time.Sleep(time.Second >> 2)
 				ncutils.PrintLog("waiting for interface...", 1)
 				for !strings.Contains(output, ifacename) && !(time.Now().After(starttime.Add(time.Duration(10) * time.Second))) {
 					output, _ = ncutils.RunCmd("wg", false)
-					time.Sleep(time.Second >> 1)
 					err = ApplyConf(confPath)
+					time.Sleep(time.Second)
 				}
 				if !strings.Contains(output, ifacename) {
 					return errors.New("could not create wg interface for " + ifacename)
@@ -232,7 +236,9 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
 					log.Println(err.Error())
 					return err
 				}
-				_, _ = ncutils.RunCmd("route add "+ip+" mask "+mask+" "+node.Address, true)
+				ncutils.RunCmd("route add "+ip+" mask "+mask+" "+node.Address, true)
+				time.Sleep(time.Second >> 2)
+				ncutils.RunCmd("route change "+ip+" mask "+mask+" "+node.Address, true)
 			}
 		}
 	} else {
@@ -348,6 +354,7 @@ func RemoveConf(iface string, printlog bool) error {
 	var err error
 	switch os {
 	case "windows":
+
 		err = RemoveWindowsConf(iface, printlog)
 	default:
 		confPath := ncutils.GetNetclientPathSpecific() + iface + ".conf"

+ 29 - 1
netclient/wireguard/windows.go

@@ -2,11 +2,23 @@ package wireguard
 
 import (
 	"fmt"
+	"os"
+	"strings"
+	"time"
 
 	"github.com/gravitl/netmaker/netclient/ncutils"
 )
 
+// ApplyWindowsConf - applies the WireGuard configuration file on Windows
 func ApplyWindowsConf(confPath string) error {
+	pathStrings := strings.Split(confPath, ncutils.GetWGPathSpecific())
+	if len(pathStrings) == 2 {
+		copyConfPath := fmt.Sprintf("%s\\%s", ncutils.WINDOWS_WG_DPAPI_PATH, pathStrings[1])
+		err := ncutils.Copy(confPath, copyConfPath)
+		if err != nil {
+			ncutils.PrintLog(err.Error(), 1)
+		}
+	}
 	var commandLine = fmt.Sprintf(`wireguard.exe /installtunnelservice "%s"`, confPath)
 	if _, err := ncutils.RunCmdFormatted(commandLine, false); err != nil {
 		return err
@@ -14,9 +26,25 @@ func ApplyWindowsConf(confPath string) error {
 	return nil
 }
 
+// RemoveWindowsConf - removes the WireGuard configuration file on Windows and dpapi file
 func RemoveWindowsConf(ifacename string, printlog bool) error {
 	if _, err := ncutils.RunCmd("wireguard.exe /uninstalltunnelservice "+ifacename, printlog); err != nil {
-		return err
+		ncutils.PrintLog(err.Error(), 1)
+	}
+	dpapipath := fmt.Sprintf("%s\\%s.conf.dpapi", ncutils.WINDOWS_WG_DPAPI_PATH, ifacename)
+	confpath := fmt.Sprintf("%s\\%s.conf", ncutils.WINDOWS_WG_DPAPI_PATH, ifacename)
+	if ncutils.FileExists(confpath) {
+		err := os.Remove(confpath)
+		if err != nil {
+			ncutils.PrintLog(err.Error(), 1)
+		}
+	}
+	time.Sleep(time.Second >> 2)
+	if ncutils.FileExists(dpapipath) {
+		err := os.Remove(dpapipath)
+		if err != nil {
+			ncutils.PrintLog(err.Error(), 1)
+		}
 	}
 	return nil
 }