Browse Source

removing peer cache

afeiszli 3 years ago
parent
commit
03a30b6bbc

+ 20 - 0
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
+}

+ 7 - 0
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")

+ 4 - 0
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)

+ 7 - 0
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") {

+ 8 - 7
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
 }
 

+ 1 - 1
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 {

+ 7 - 0
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