Browse Source

Merge pull request #663 from gravitl/feature_v0.10.0_windows_adapt

edited windows to new daemon
dcarns 3 years ago
parent
commit
147cc18be0
4 changed files with 28 additions and 22 deletions
  1. 2 0
      netclient/daemon/windows.go
  2. 4 8
      netclient/main.go
  3. 10 5
      netclient/ncwindows/windows.go
  4. 12 9
      netclient/wireguard/unix.go

+ 2 - 0
netclient/daemon/windows.go

@@ -34,6 +34,7 @@ func SetupWindowsDaemon() error {
 	return nil
 	return nil
 }
 }
 
 
+// RestartWindowsDaemon - restarts windows service
 func RestartWindowsDaemon() {
 func RestartWindowsDaemon() {
 	StopWindowsDaemon()
 	StopWindowsDaemon()
 	// start daemon, will not restart or start another
 	// start daemon, will not restart or start another
@@ -59,6 +60,7 @@ func writeServiceConfig() error {
 <name>Netclient</name>
 <name>Netclient</name>
 <description>Connects Windows nodes to one or more Netmaker networks.</description>
 <description>Connects Windows nodes to one or more Netmaker networks.</description>
 <executable>%v</executable>
 <executable>%v</executable>
+<arguments>daemon</arguments>
 <log mode="roll"></log>
 <log mode="roll"></log>
 </service>
 </service>
 `, strings.Replace(ncutils.GetNetclientPathSpecific()+"netclient.exe", `\\`, `\`, -1))
 `, strings.Replace(ncutils.GetNetclientPathSpecific()+"netclient.exe", `\\`, `\`, -1))

+ 4 - 8
netclient/main.go

@@ -8,7 +8,6 @@ import (
 	"runtime/debug"
 	"runtime/debug"
 
 
 	"github.com/gravitl/netmaker/netclient/cli_options"
 	"github.com/gravitl/netmaker/netclient/cli_options"
-	"github.com/gravitl/netmaker/netclient/command"
 	"github.com/gravitl/netmaker/netclient/ncutils"
 	"github.com/gravitl/netmaker/netclient/ncutils"
 	"github.com/gravitl/netmaker/netclient/ncwindows"
 	"github.com/gravitl/netmaker/netclient/ncwindows"
 	"github.com/urfave/cli/v2"
 	"github.com/urfave/cli/v2"
@@ -31,13 +30,10 @@ func main() {
 		ncutils.CheckUID()
 		ncutils.CheckUID()
 		ncutils.CheckWG()
 		ncutils.CheckWG()
 	}
 	}
-	if len(os.Args) == 1 && ncutils.IsWindows() {
-		command.RunUserspaceDaemon()
-	} else {
-		err := app.Run(os.Args)
-		if err != nil {
-			log.Fatal(err)
-		}
+
+	err := app.Run(os.Args)
+	if err != nil {
+		log.Fatal(err)
 	}
 	}
 }
 }
 
 

+ 10 - 5
netclient/ncwindows/windows.go

@@ -18,17 +18,22 @@ func InitWindows() {
 	if wdErr != nil {
 	if wdErr != nil {
 		log.Fatal("failed to get current directory..")
 		log.Fatal("failed to get current directory..")
 	}
 	}
-	_, dataNetclientErr := os.Stat(ncutils.GetNetclientPathSpecific() + "netclient.exe")
-	_, currentNetclientErr := os.Stat(wdPath + "\\netclient.exe")
 
 
-	if os.IsNotExist(dataNetclientErr) { // check and see if netclient.exe is in appdata
+	dataPath := ncutils.GetNetclientPathSpecific() + "netclient.exe"
+	currentPath := wdPath + "\\netclient.exe"
+	_, dataNetclientErr := os.Stat(dataPath)
+	_, currentNetclientErr := os.Stat(currentPath)
+
+	if currentPath == dataPath && currentNetclientErr == nil {
+		ncutils.Log("netclient.exe is in proper location, " + currentPath)
+	} else if os.IsNotExist(dataNetclientErr) { // check and see if netclient.exe is in appdata
 		if currentNetclientErr == nil { // copy it if it exists locally
 		if currentNetclientErr == nil { // copy it if it exists locally
-			input, err := os.ReadFile(wdPath + "\\netclient.exe")
+			input, err := os.ReadFile(currentPath)
 			if err != nil {
 			if err != nil {
 				log.Println("failed to find netclient.exe")
 				log.Println("failed to find netclient.exe")
 				return
 				return
 			}
 			}
-			if err = os.WriteFile(ncutils.GetNetclientPathSpecific()+"netclient.exe", input, 0600); err != nil {
+			if err = os.WriteFile(dataPath, input, 0700); err != nil {
 				log.Println("failed to copy netclient.exe to", ncutils.GetNetclientPath())
 				log.Println("failed to copy netclient.exe to", ncutils.GetNetclientPath())
 				return
 				return
 			}
 			}

+ 12 - 9
netclient/wireguard/unix.go

@@ -53,17 +53,20 @@ func SetWGKeyConfig(network string, serveraddr string) error {
 
 
 // ApplyWGQuickConf - applies wg-quick commands if os supports
 // ApplyWGQuickConf - applies wg-quick commands if os supports
 func ApplyWGQuickConf(confPath string, ifacename string) error {
 func ApplyWGQuickConf(confPath string, ifacename string) error {
-	_, err := os.Stat(confPath)
-	if err != nil {
-		ncutils.Log(confPath + " does not exist " + err.Error())
+	if ncutils.IsWindows() {
+		return ApplyWindowsConf(confPath)
+	} else {
+		_, err := os.Stat(confPath)
+		if err != nil {
+			ncutils.Log(confPath + " does not exist " + err.Error())
+			return err
+		}
+		if ncutils.IfaceExists(ifacename) {
+			ncutils.RunCmd("wg-quick down "+confPath, true)
+		}
+		_, err = ncutils.RunCmd("wg-quick up "+confPath, true)
 		return err
 		return err
 	}
 	}
-	if ncutils.IfaceExists(ifacename) {
-		ncutils.RunCmd("wg-quick down "+confPath, true)
-	}
-	_, err = ncutils.RunCmd("wg-quick up "+confPath, true)
-
-	return err
 }
 }
 
 
 // ApplyMacOSConf - applies system commands similar to wg-quick using golang for MacOS
 // ApplyMacOSConf - applies system commands similar to wg-quick using golang for MacOS