diff --git a/netclient/daemon/common.go b/netclient/daemon/common.go index 22ff6c62..cce26e9b 100644 --- a/netclient/daemon/common.go +++ b/netclient/daemon/common.go @@ -3,6 +3,7 @@ package daemon import ( "errors" "runtime" + "time" "github.com/gravitl/netmaker/netclient/config" ) @@ -29,3 +30,22 @@ func InstallDaemon(cfg config.ClientConfig) error { } return err } + +func Restart() error { + os := runtime.GOOS + var err error + + time.Sleep(time.Second) + + switch os { + case "windows": + RestartWindowsDaemon() + case "darwin": + RestartLaunchD() + case "linux": + RestartSystemD() + default: + err = errors.New("this os is not yet supported for daemon mode. Run join cmd with flag '--daemon off'") + } + return err +} diff --git a/netclient/daemon/macos.go b/netclient/daemon/macos.go index 520aef92..b14dee85 100644 --- a/netclient/daemon/macos.go +++ b/netclient/daemon/macos.go @@ -5,6 +5,7 @@ import ( "log" "os" "path/filepath" + "time" "github.com/gravitl/netmaker/netclient/ncutils" ) @@ -54,6 +55,12 @@ func CleanupMac() { os.Remove(EXEC_DIR + "netclient") } +func RestartLaunchD() { + ncutils.RunCmd("launchctl unload /Library/LaunchDaemons/"+MAC_SERVICE_NAME+".plist", true) + time.Sleep(time.Second >> 2) + ncutils.RunCmd("launchctl load /Library/LaunchDaemons/"+MAC_SERVICE_NAME+".plist", true) +} + // CreateMacService - Creates the mac service file for LaunchDaemons func CreateMacService(servicename string, interval string) error { _, err := os.Stat("/Library/LaunchDaemons") diff --git a/netclient/daemon/systemd.go b/netclient/daemon/systemd.go index 93f33f9d..7e9f4a74 100644 --- a/netclient/daemon/systemd.go +++ b/netclient/daemon/systemd.go @@ -73,6 +73,10 @@ WantedBy=multi-user.target return nil } +func RestartSystemD() { + _, _ = ncutils.RunCmd("systemctl start netclient.service", true) +} + func CleanupLinux() { if err := os.RemoveAll(ncutils.GetNetclientPath()); err != nil { ncutils.PrintLog("Removing netclient configs: "+err.Error(), 1) diff --git a/netclient/daemon/windows.go b/netclient/daemon/windows.go index 799bd853..266f520c 100644 --- a/netclient/daemon/windows.go +++ b/netclient/daemon/windows.go @@ -34,6 +34,13 @@ func SetupWindowsDaemon() error { return nil } +func RestartWindowsDaemon() { + StopWindowsDaemon() + // start daemon, will not restart or start another + ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe start`, false) + ncutils.Log(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1) + `winsw.exe start`) +} + // CleanupWindows - cleans up windows files func CleanupWindows() { if !ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.xml") { diff --git a/netclient/functions/common.go b/netclient/functions/common.go index 6d08d6fb..6dbdb2f9 100644 --- a/netclient/functions/common.go +++ b/netclient/functions/common.go @@ -43,20 +43,21 @@ func getPrivateAddr() (string, error) { var local string conn, err := net.Dial("udp", "8.8.8.8:80") - if err != nil { - log.Fatal(err) - } - defer conn.Close() + if err == nil { + defer conn.Close() - localAddr := conn.LocalAddr().(*net.UDPAddr) - localIP := localAddr.IP - local = localIP.String() + localAddr := conn.LocalAddr().(*net.UDPAddr) + localIP := localAddr.IP + local = localIP.String() + } if local == "" { local, err = getPrivateAddrBackup() } + if local == "" { err = errors.New("could not find local ip") } + return local, err } diff --git a/netclient/functions/daemon.go b/netclient/functions/daemon.go index 210ebe11..0282d8cb 100644 --- a/netclient/functions/daemon.go +++ b/netclient/functions/daemon.go @@ -357,7 +357,7 @@ func MonitorKeepalive(ctx context.Context, client mqtt.Client, cfg *config.Clien ncutils.Log("unable to parse timestamp " + keepalivetime.String()) continue } - if time.Since(keepalivetime) > time.Second*200 { // more than 3+ minutes + if time.Since(keepalivetime) > time.Second*120 { // more than 2+ minutes ncutils.Log("server keepalive not recieved recently, resubscribe to message queue") err := Resubscribe(client, cfg) if err != nil { diff --git a/netclient/functions/join.go b/netclient/functions/join.go index c0ffc5fc..55c12ad7 100644 --- a/netclient/functions/join.go +++ b/netclient/functions/join.go @@ -69,6 +69,11 @@ func JoinNetwork(cfg config.ClientConfig, privateKey string) error { if cfg.Node.LocalRange != "" && cfg.Node.LocalAddress == "" { log.Println("local vpn, getting local address from range: " + cfg.Node.LocalRange) cfg.Node.LocalAddress = getLocalIP(cfg.Node) + } else if cfg.Node.LocalAddress == "" { + intIP, err := getPrivateAddr() + if err == nil { + cfg.Node.LocalAddress = intIP + } } // set endpoint if blank. set to local if local net, retrieve from function if not @@ -233,6 +238,8 @@ func JoinNetwork(cfg config.ClientConfig, privateKey string) error { } if err != nil { return err + } else { + daemon.Restart() } return err