Browse Source

Merge pull request #708 from gravitl/bugfix_v0.10.0_mac_and_logs

Bugfix v0.10.0 mac and logs
dcarns 3 years ago
parent
commit
d7c7fc70a3

+ 37 - 23
mq/mq.go

@@ -23,6 +23,8 @@ const KEEPALIVE_TIMEOUT = 60 //timeout in seconds
 // MQ_DISCONNECT - disconnects MQ
 const MQ_DISCONNECT = 250
 
+var peer_force_send = 0
+
 // DefaultHandler default message queue handler - only called when GetDebug == true
 func DefaultHandler(client mqtt.Client, msg mqtt.Message) {
 	logger.Log(0, "MQTT Message: Topic: ", string(msg.Topic()), " Message: ", string(msg.Payload()))
@@ -112,10 +114,8 @@ func PublishPeerUpdate(newNode *models.Node) error {
 	for _, node := range networkNodes {
 
 		if node.IsServer == "yes" || node.ID == newNode.ID {
-			log.Println("skipping update on " + node.Name + " : " + node.ID)
 			continue
 		}
-		log.Println("running update on " + node.Name + " : " + node.ID)
 		peerUpdate, err := logic.GetPeerUpdate(&node)
 		if err != nil {
 			logger.Log(1, "error getting peer update for node", node.ID, err.Error())
@@ -244,30 +244,44 @@ func Keepalive(ctx context.Context) {
 		case <-ctx.Done():
 			return
 		case <-time.After(time.Second * KEEPALIVE_TIMEOUT):
-			networks, err := logic.GetNetworks()
-			if err != nil {
-				logger.Log(1, "error retrieving networks for keepalive", err.Error())
-			}
-			for _, network := range networks {
-				serverNode, errN := logic.GetNetworkServerLeader(network.NetID)
-				if errN == nil {
-					serverNode.SetLastCheckIn()
-					logic.UpdateNode(&serverNode, &serverNode)
-					if network.DefaultUDPHolePunch == "yes" {
-						if logic.ShouldPublishPeerPorts(&serverNode) {
-							err = PublishPeerUpdate(&serverNode)
-							if err != nil {
-								logger.Log(1, "error publishing udp port updates for network", network.NetID)
-								logger.Log(1, errN.Error())
-							}
-						}
+			sendPeers()
+		}
+	}
+}
+
+// sendPeers - retrieve networks, send peer ports to all peers
+func sendPeers() {
+	var force bool
+	peer_force_send++
+	if peer_force_send == 5 {
+		force = true
+		peer_force_send = 0
+	}
+	networks, err := logic.GetNetworks()
+	if err != nil {
+		logger.Log(1, "error retrieving networks for keepalive", err.Error())
+	}
+	for _, network := range networks {
+		serverNode, errN := logic.GetNetworkServerLeader(network.NetID)
+		if errN == nil {
+			serverNode.SetLastCheckIn()
+			logic.UpdateNode(&serverNode, &serverNode)
+			if network.DefaultUDPHolePunch == "yes" {
+				if logic.ShouldPublishPeerPorts(&serverNode) || force {
+					if force {
+						logger.Log(2, "sending scheduled peer update (5 min)")
+					}
+					err = PublishPeerUpdate(&serverNode)
+					if err != nil {
+						logger.Log(1, "error publishing udp port updates for network", network.NetID)
+						logger.Log(1, errN.Error())
 					}
-				} else {
-					logger.Log(1, "unable to retrieve leader for network ", network.NetID)
-					logger.Log(1, errN.Error())
-					continue
 				}
 			}
+		} else {
+			logger.Log(1, "unable to retrieve leader for network ", network.NetID)
+			logger.Log(1, errN.Error())
+			continue
 		}
 	}
 }

+ 1 - 1
netclient/daemon/macos.go

@@ -95,7 +95,7 @@ func MacDaemonString(interval string) string {
 	<key>Label</key><string>com.gravitl.netclient</string>
 	<key>ProgramArguments</key>
 		<array>
-			<string>/sbin/netclient</string>
+			<string>/usr/local/bin/netclient</string>
 			<string>daemon</string>
 		</array>
 	<key>StandardOutPath</key><string>/etc/netclient/com.gravitl.netclient.log</string>

+ 48 - 30
netclient/functions/daemon.go

@@ -53,7 +53,7 @@ func read(network, which string) string {
 		if readMessage.LastSeen.IsZero() {
 			return ""
 		}
-		if time.Now().After(readMessage.LastSeen.Add(time.Minute)) { // check if message has been there over a minute
+		if time.Now().After(readMessage.LastSeen.Add(time.Minute * 10)) { // check if message has been there over a minute
 			messageCache.Delete(fmt.Sprintf("%s%s", network, which)) // remove old message if expired
 			return ""
 		}
@@ -132,8 +132,16 @@ func SetupMQTT(cfg *config.ClientConfig, publish bool) mqtt.Client {
 		ncutils.Log("detected broker connection lost, running pull for " + cfg.Node.Network)
 		_, err := Pull(cfg.Node.Network, true)
 		if err != nil {
-			ncutils.Log("could not run pull, please restart daemon or examine network connectivity --- " + err.Error())
+			ncutils.Log("could not run pull, server unreachable: " + err.Error())
+			ncutils.Log("waiting to retry...")
+			/*
+				//Consider putting in logic to restart - daemon may take long time to refresh
+				time.Sleep(time.Minute * 5)
+					ncutils.Log("restarting netclient")
+					daemon.Restart()
+			*/
 		}
+		ncutils.Log("connection re-established with mqtt server")
 	})
 
 	client := mqtt.NewClient(opts)
@@ -173,32 +181,35 @@ func MessageQueue(ctx context.Context, network string) {
 	ncutils.Log("netclient go routine started for " + network)
 	var cfg config.ClientConfig
 	cfg.Network = network
-	var configPath = fmt.Sprintf("%snetconfig-%s", ncutils.GetNetclientPathSpecific(), network)
-	fileInfo, err := os.Stat(configPath)
-	if err != nil {
-		ncutils.Log("could not stat config file: " + configPath)
-	}
-	// speed up UDP rest
-	if time.Now().After(fileInfo.ModTime().Add(time.Minute)) {
-		sleepTime := 2
-		ncutils.Log("pulling latest config for " + cfg.Network)
-		for {
-			_, err := Pull(network, true)
-			if err == nil {
-				break
-			} else {
-				ncutils.PrintLog("error pulling config for "+network+": "+err.Error(), 1)
-			}
-			if sleepTime > 3600 {
-				sleepTime = 3600
+	/*
+			var configPath = fmt.Sprintf("%snetconfig-%s", ncutils.GetNetclientPathSpecific(), network)
+			fileInfo, err := os.Stat(configPath)
+			if err != nil {
+				ncutils.Log("could not stat config file: " + configPath)
 			}
-			ncutils.Log("failed to pull for network " + network)
-			ncutils.Log(fmt.Sprintf("waiting %d seconds to retry...", sleepTime))
-			time.Sleep(time.Second * time.Duration(sleepTime))
-			sleepTime = sleepTime * 2
-		}
-	}
-	time.Sleep(time.Second << 1)
+			// speed up UDP rest
+				if time.Now().After(fileInfo.ModTime().Add(time.Minute)) {
+					sleepTime := 2
+					ncutils.Log("pulling latest config for " + cfg.Network)
+					for {
+						_, err := Pull(network, true)
+						if err == nil {
+							break
+						} else {
+							ncutils.PrintLog("error pulling config for "+network+": "+err.Error(), 1)
+						}
+						if sleepTime > 3600 {
+							sleepTime = 3600
+						}
+						ncutils.Log("failed to pull for network " + network)
+						ncutils.Log(fmt.Sprintf("waiting %d seconds to retry...", sleepTime))
+						time.Sleep(time.Second * time.Duration(sleepTime))
+						sleepTime = sleepTime * 2
+					}
+				}
+
+		time.Sleep(time.Second << 1)
+	*/
 	cfg.ReadConfig()
 	ncutils.Log("daemon started for network: " + network)
 	client := SetupMQTT(&cfg, false)
@@ -365,12 +376,19 @@ func UpdatePeers(client mqtt.Client, msg mqtt.Message) {
 		return
 	}
 	//err = wireguard.SyncWGQuickConf(cfg.Node.Interface, file)
-	err = wireguard.SetPeers(cfg.Node.Interface, cfg.Node.Address, cfg.Node.PersistentKeepalive, peerUpdate.Peers)
+	var iface = cfg.Node.Interface
+	if ncutils.IsMac() {
+		iface, err = local.GetMacIface(cfg.Node.Address)
+		if err != nil {
+			ncutils.Log("error retrieving mac iface: " + err.Error())
+			return
+		}
+	}
+	err = wireguard.SetPeers(iface, cfg.Node.Address, cfg.Node.PersistentKeepalive, peerUpdate.Peers)
 	if err != nil {
-		ncutils.Log("error syncing wg after peer update " + err.Error())
+		ncutils.Log("error syncing wg after peer update: " + err.Error())
 		return
 	}
-	ncutils.Log(fmt.Sprintf("received peer update on network, %s", cfg.Network))
 }
 
 // MonitorKeepalive - checks time last server keepalive received.  If more than 3+ minutes, notify and resubscribe

+ 2 - 2
netclient/ncutils/netclientutils.go

@@ -570,9 +570,9 @@ func CheckWG() {
 			PrintLog(err.Error(), 0)
 			log.Fatal("WireGuard not installed. Please install WireGuard (wireguard-tools) and try again.")
 		}
-		PrintLog("Running with userspace wireguard: "+uspace, 0)
+		PrintLog("running with userspace wireguard: "+uspace, 0)
 	} else if uspace != "wg" {
-		log.Println("running userspace WireGuard with " + uspace)
+		PrintLog("running userspace WireGuard with "+uspace, 0)
 	}
 }
 

+ 1 - 2
netclient/wireguard/common.go

@@ -174,7 +174,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
 		time.Sleep(time.Second >> 2)
 		d, _ = wgclient.Device(deviceiface)
 	}
-	ApplyConf(node, deviceiface, confPath)          // Apply initially
+	ApplyConf(node, ifacename, confPath)            // Apply initially
 	ncutils.PrintLog("waiting for interface...", 1) // ensure interface is created
 	output, _ := ncutils.RunCmd("wg", false)
 	starttime := time.Now()
@@ -355,7 +355,6 @@ func UpdateWgPeers(file string, peers []wgtypes.PeerConfig) error {
 		AllowNonUniqueSections: true,
 		AllowShadows:           true,
 	}
-	ncutils.Log("updating " + file)
 	wireguard, err := ini.LoadSources(options, file)
 	if err != nil {
 		return err