diff --git a/.gitignore b/.gitignore index a1c4c024..5bd27c12 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ netmaker-arm64 netmaker-32 netmaker-amd64 netclient/netclient +netclient/netclient.syso netclient/build netclient/build/ !netclient/build/netclient.service diff --git a/netclient/auth/auth.go b/netclient/auth/auth.go index 8747fcea..2f1423ae 100644 --- a/netclient/auth/auth.go +++ b/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) diff --git a/netclient/command/commands.go b/netclient/command/commands.go index 65138b31..10662d58 100644 --- a/netclient/command/commands.go +++ b/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 } diff --git a/netclient/daemon/common.go b/netclient/daemon/common.go index 6a7d0982..9572f1c5 100644 --- a/netclient/daemon/common.go +++ b/netclient/daemon/common.go @@ -59,7 +59,7 @@ func Stop() error { switch os { case "windows": - StopWindowsDaemon() + RunWinSWCMD("stop") case "darwin": StopLaunchD() case "linux": diff --git a/netclient/daemon/windows.go b/netclient/daemon/windows.go index fc357fac..3ed1db17 100644 --- a/netclient/daemon/windows.go +++ b/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 + } + + // 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") + } } - -// 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") -} - -// 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 -// } diff --git a/netclient/ncutils/netclientutils.go b/netclient/ncutils/netclientutils.go index 2616c775..30cb8b82 100644 --- a/netclient/ncutils/netclientutils.go +++ b/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"