mirror of
https://github.com/gravitl/netmaker.git
synced 2025-11-02 13:04:11 +08:00
resub on server ping timeout; may not work in multi server enviornment
This commit is contained in:
@@ -22,7 +22,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// ServerKeepalive - stores time of last server keepalive message
|
// ServerKeepalive - stores time of last server keepalive message
|
||||||
var KeepaliveReceived time.Time
|
var keepalive chan string = make(chan string)
|
||||||
var messageCache = make(map[string]string, 20)
|
var messageCache = make(map[string]string, 20)
|
||||||
|
|
||||||
const lastNodeUpdate = "lnu"
|
const lastNodeUpdate = "lnu"
|
||||||
@@ -83,7 +83,6 @@ func MessageQueue(ctx context.Context, network string) {
|
|||||||
cfg.Network = network
|
cfg.Network = network
|
||||||
cfg.ReadConfig()
|
cfg.ReadConfig()
|
||||||
ncutils.Log("daemon started for network:" + network)
|
ncutils.Log("daemon started for network:" + network)
|
||||||
KeepaliveReceived = time.Now()
|
|
||||||
client := SetupMQTT(&cfg)
|
client := SetupMQTT(&cfg)
|
||||||
if cfg.DebugOn {
|
if cfg.DebugOn {
|
||||||
if token := client.Subscribe("#", 0, nil); token.Wait() && token.Error() != nil {
|
if token := client.Subscribe("#", 0, nil); token.Wait() && token.Error() != nil {
|
||||||
@@ -110,6 +109,7 @@ func MessageQueue(ctx context.Context, network string) {
|
|||||||
ncutils.Log("subscribed to server keepalives")
|
ncutils.Log("subscribed to server keepalives")
|
||||||
}
|
}
|
||||||
defer client.Disconnect(250)
|
defer client.Disconnect(250)
|
||||||
|
go MonitorKeepalive(ctx, client, &cfg)
|
||||||
go Checkin(ctx, &cfg, network)
|
go Checkin(ctx, &cfg, network)
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
ncutils.Log("shutting down daemon")
|
ncutils.Log("shutting down daemon")
|
||||||
@@ -227,8 +227,11 @@ func UpdatePeers(client mqtt.Client, msg mqtt.Message) {
|
|||||||
cfg.ReadConfig()
|
cfg.ReadConfig()
|
||||||
var shouldReSub = shouldResub(cfg.Node.NetworkSettings.DefaultServerAddrs, peerUpdate.ServerAddrs)
|
var shouldReSub = shouldResub(cfg.Node.NetworkSettings.DefaultServerAddrs, peerUpdate.ServerAddrs)
|
||||||
if shouldReSub {
|
if shouldReSub {
|
||||||
|
if err := config.ModConfig(&cfg.Node); err == nil {
|
||||||
Resubscribe(client, &cfg)
|
Resubscribe(client, &cfg)
|
||||||
cfg.Node.NetworkSettings.DefaultServerAddrs = peerUpdate.ServerAddrs
|
cfg.Node.NetworkSettings.DefaultServerAddrs = peerUpdate.ServerAddrs
|
||||||
|
} else {
|
||||||
|
ncutils.Log("resub required but mod config failed")
|
||||||
}
|
}
|
||||||
file := ncutils.GetNetclientPathSpecific() + cfg.Node.Interface + ".conf"
|
file := ncutils.GetNetclientPathSpecific() + cfg.Node.Interface + ".conf"
|
||||||
err = wireguard.UpdateWgPeers(file, peerUpdate.Peers)
|
err = wireguard.UpdateWgPeers(file, peerUpdate.Peers)
|
||||||
@@ -242,23 +245,35 @@ func UpdatePeers(client mqtt.Client, msg mqtt.Message) {
|
|||||||
ncutils.Log("error restarting wg after peer update " + err.Error())
|
ncutils.Log("error restarting wg after peer update " + err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MonitorKeepalive - checks time last server keepalive received. If more than 3+ minutes, notify and resubscribe
|
||||||
|
func MonitorKeepalive(ctx context.Context, client mqtt.Client, cfg *config.ClientConfig) {
|
||||||
|
lastUpdate := time.Now()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-time.After(time.Second * 150):
|
||||||
|
if time.Since(lastUpdate) < time.Second*200 { // more than 3+ minutes
|
||||||
|
Resubscribe(client, cfg)
|
||||||
|
}
|
||||||
|
case <-keepalive:
|
||||||
|
lastUpdate = time.Now()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ServerKeepAlive -- handler to react to keepalive messages published by server
|
// ServerKeepAlive -- handler to react to keepalive messages published by server
|
||||||
func ServerKeepAlive(client mqtt.Client, msg mqtt.Message) {
|
func ServerKeepAlive(client mqtt.Client, msg mqtt.Message) {
|
||||||
if time.Now().Sub(KeepaliveReceived) < time.Second*200 { // more than 3+ minutes
|
serverid := string(msg.Payload())
|
||||||
|
keepalive <- serverid
|
||||||
KeepaliveReceived = time.Now()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ncutils.Log("server keepalive not recieved in last 3 minutes")
|
|
||||||
///do other stuff
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resubscribe --- handles resubscribing if needed
|
// Resubscribe --- handles resubscribing if needed
|
||||||
func Resubscribe(client mqtt.Client, cfg *config.ClientConfig) error {
|
func Resubscribe(client mqtt.Client, cfg *config.ClientConfig) error {
|
||||||
if err := config.ModConfig(&cfg.Node); err == nil {
|
|
||||||
ncutils.Log("resubbing on network " + cfg.Node.Network)
|
ncutils.Log("resubbing on network " + cfg.Node.Network)
|
||||||
client.Disconnect(250)
|
client.Disconnect(250)
|
||||||
client = SetupMQTT(cfg)
|
client = SetupMQTT(cfg)
|
||||||
@@ -273,10 +288,6 @@ func Resubscribe(client mqtt.Client, cfg *config.ClientConfig) error {
|
|||||||
}
|
}
|
||||||
ncutils.Log("finished re subbing")
|
ncutils.Log("finished re subbing")
|
||||||
return nil
|
return nil
|
||||||
} else {
|
|
||||||
ncutils.Log("could not mod config when re-subbing")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateKeys -- updates private key and returns new publickey
|
// UpdateKeys -- updates private key and returns new publickey
|
||||||
|
|||||||
Reference in New Issue
Block a user