Browse Source

removing windows installer portions

afeiszli 3 years ago
parent
commit
bc1eb17bad

+ 1 - 0
.gitignore

@@ -4,6 +4,7 @@ netmaker-arm64
 netmaker-32
 netmaker-amd64
 netclient/netclient
+netclient/netclient.syso
 netclient/build
 netclient/build/
 !netclient/build/netclient.service

+ 0 - 62
netclient/auth/auth.go

@@ -7,68 +7,6 @@ import (
 	//    "os"
 )
 
-// SetJWT func will used to create the JWT while signing in and signing out
-//func SetJWT(client nodepb.NodeServiceClient, network string) (context.Context, error) {
-//	home := ncutils.GetNetclientPathSpecific()
-//	tokentext, err := os.ReadFile(home + "nettoken-" + network)
-//	if err != nil {
-//		err = AutoLogin(client, network)
-//		if err != nil {
-//			return nil, status.Errorf(codes.Unauthenticated, fmt.Sprintf("Something went wrong with Auto Login: %v", err))
-//		}
-//		tokentext, err = ncutils.GetFileWithRetry(home+"nettoken-"+network, 1)
-//		if err != nil {
-//			return nil, status.Errorf(codes.Unauthenticated, fmt.Sprintf("Something went wrong: %v", err))
-//		}
-//	}
-//	token := string(tokentext)
-//
-//	// Anything linked to this variable will transmit request headers.
-//	md := metadata.New(map[string]string{"authorization": token})
-//	ctx := context.Background()
-//	ctx = metadata.NewOutgoingContext(ctx, md)
-//	return ctx, nil
-//}
-
-// AutoLogin - auto logins whenever client needs to request from server
-//func AutoLogin(client nodepb.NodeServiceClient, network string) error {
-//	home := ncutils.GetNetclientPathSpecific()
-//	cfg, err := config.ReadConfig(network)
-//	if err != nil {
-//		return err
-//	}
-//	pass, err := RetrieveSecret(network)
-//	if err != nil {
-//		return err
-//	}
-//	node := models.Node{
-//		Password:   pass,
-//		MacAddress: cfg.Node.MacAddress,
-//		ID:         cfg.Node.ID,
-//		Network:    network,
-//	}
-//	data, err := json.Marshal(&node)
-//	if err != nil {
-//		return nil
-//	}
-//
-//	login := &nodepb.Object{
-//		Data: string(data),
-//		Type: nodepb.NODE_TYPE,
-//	}
-//	// RPC call
-//	res, err := client.Login(context.TODO(), login)
-//	if err != nil {
-//		return err
-//	}
-//	tokenstring := []byte(res.Data)
-//	err = os.WriteFile(home+"nettoken-"+network, tokenstring, 0600)
-//	if err != nil {
-//		return err
-//	}
-//	return err
-//}
-
 // StoreSecret - stores auth secret locally
 func StoreSecret(key string, network string) error {
 	d1 := []byte(key)

+ 1 - 7
netclient/command/commands.go

@@ -49,13 +49,7 @@ func Join(cfg *config.ClientConfig, privateKey string) error {
 		return err
 	}
 	logger.Log(1, "joined ", cfg.Network)
-	/*
-		if ncutils.IsWindows() {
-			logger.Log("setting up WireGuard app", 0)
-			time.Sleep(time.Second >> 1)
-			functions.Pull(cfg.Network, true)
-		}
-	*/
+
 	return err
 }
 

+ 1 - 1
netclient/daemon/common.go

@@ -59,7 +59,7 @@ func Stop() error {
 
 	switch os {
 	case "windows":
-		StopWindowsDaemon()
+		RunWinSWCMD("stop")
 	case "darwin":
 		StopLaunchD()
 	case "linux":

+ 36 - 77
netclient/daemon/windows.go

@@ -5,6 +5,7 @@ import (
 	"log"
 	"os"
 	"strings"
+	"time"
 
 	"github.com/gravitl/netmaker/logger"
 	"github.com/gravitl/netmaker/netclient/ncutils"
@@ -27,19 +28,19 @@ func SetupWindowsDaemon() error {
 		}
 		logger.Log(0, "finished daemon setup")
 	}
-	// install daemon, will not overwrite
-	ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe install`, false)
-	// start daemon, will not restart or start another
-	ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe start`, false)
-	logger.Log(0, strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe start`)
+	//get exact formatted commands
+	RunWinSWCMD("install")
+	time.Sleep(1)
+	RunWinSWCMD("start")
+
 	return nil
 }
 
 // RestartWindowsDaemon - restarts windows service
 func RestartWindowsDaemon() {
-	StopWindowsDaemon()
-	// start daemon, will not restart or start another
-	ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe start`, false)
+	RunWinSWCMD("stop")
+	time.Sleep(1)
+	RunWinSWCMD("start")
 }
 
 // CleanupWindows - cleans up windows files
@@ -47,8 +48,8 @@ func CleanupWindows() {
 	if !ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.xml") {
 		writeServiceConfig()
 	}
-	StopWindowsDaemon()
-	RemoveWindowsDaemon()
+	RunWinSWCMD("stop")
+	RunWinSWCMD("uninstall")
 	os.RemoveAll(ncutils.GetNetclientPath())
 	log.Println("Netclient on Windows, uninstalled")
 }
@@ -74,73 +75,31 @@ func writeServiceConfig() error {
 	return nil
 }
 
-// == Daemon ==
+// RunWinSWCMD - Run a command with the winsw.exe tool (start, stop, install, uninstall)
+func RunWinSWCMD(command string) {
 
-// StopWindowsDaemon - stops the Windows daemon
-func StopWindowsDaemon() {
-	logger.Log(0, "stopping Windows, Netclient daemon")
-	// stop daemon, will not overwrite
-	ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe stop`, true)
-}
+	// check if command allowed
+	allowedCommands := map[string]bool{
+		"start":     true,
+		"stop":      true,
+		"install":   true,
+		"uninstall": true,
+	}
+	if !allowedCommands[command] {
+		logger.Log(0, "command "+command+" unsupported by winsw")
+		return
+	}
 
-// RemoveWindowsDaemon - removes the Windows daemon
-func RemoveWindowsDaemon() {
-	// uninstall daemon, will not restart or start another
-	ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe uninstall`, true)
-	logger.Log(0, "uninstalled Windows, Netclient daemon")
+	// format command
+	dirPath := strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)
+	winCmd := fmt.Sprintf(`"%swinsw.exe" "%s"`, dirPath, command)
+	logger.Log(0, "running "+command+" of Windows Netclient daemon")
+
+	// run command and log for success/failure
+	out, err := ncutils.RunCmdFormatted(winCmd, true)
+	if err != nil {
+		logger.Log(0, "error with "+command+" of Windows Netclient daemon: "+err.Error()+" : "+out)
+	} else {
+		logger.Log(0, "successfully ran "+command+" of Windows Netclient daemon")
+	}
 }
-
-// func copyWinswOver() error {
-
-// 	input, err := ioutil.ReadFile(".\\winsw.exe")
-// 	if err != nil {
-// 		logger.Log(0, "failed to find winsw.exe")
-// 		return err
-// 	}
-// 	if err = ioutil.WriteFile(ncutils.GetNetclientPathSpecific()+"winsw.exe", input, 0644); err != nil {
-// 		logger.Log(0, "failed to copy winsw.exe to " + ncutils.GetNetclientPath())
-// 		return err
-// 	}
-// 	if err = os.Remove(".\\winsw.exe"); err != nil {
-// 		logger.Log(0, "failed to cleanup local winsw.exe, feel free to delete it")
-// 		return err
-// 	}
-// 	logger.Log(0, "finished copying winsw.exe")
-// 	return nil
-// }
-
-// func downloadWinsw() error {
-// 	fullURLFile := "https://github.com/winsw/winsw/releases/download/v2.11.0/WinSW-x64.exe"
-// 	fileName := "winsw.exe"
-
-// 	// Create the file
-// 	file, err := os.Create(fileName)
-// 	if err != nil {
-// 		logger.Log(0, "could not create file on OS for Winsw")
-// 		return err
-// 	}
-// 	defer file.Close()
-
-// 	client := http.Client{
-// 		CheckRedirect: func(r *http.Request, via []*http.Request) error {
-// 			r.URL.Opaque = r.URL.Path
-// 			return nil
-// 		},
-// 	}
-// 	// Put content on file
-// 	logger.Log(0, "downloading service tool...")
-// 	resp, err := client.Get(fullURLFile)
-// 	if err != nil {
-// 		logger.Log(0, "could not GET Winsw")
-// 		return err
-// 	}
-// 	defer resp.Body.Close()
-
-// 	_, err = io.Copy(file, resp.Body)
-// 	if err != nil {
-// 		logger.Log(0, "could not mount winsw.exe")
-// 		return err
-// 	}
-// 	logger.Log(0, "finished downloading Winsw")
-// 	return nil
-// }

+ 1 - 1
netclient/ncutils/netclientutils.go

@@ -42,7 +42,7 @@ const NO_DB_RECORDS = "could not find any records"
 const LINUX_APP_DATA_PATH = "/etc/netclient"
 
 // WINDOWS_APP_DATA_PATH - windows path
-const WINDOWS_APP_DATA_PATH = "C:\\ProgramData\\Netclient"
+const WINDOWS_APP_DATA_PATH = "C:\\Program Files (x86)\\Netclient"
 
 // WINDOWS_APP_DATA_PATH - windows path
 //const WINDOWS_WG_DPAPI_PATH = "C:\\Program Files\\WireGuard\\Data\\Configurations"