|
@@ -28,6 +28,7 @@ import (
|
|
// == Message Caches ==
|
|
// == Message Caches ==
|
|
var keepalive = new(sync.Map)
|
|
var keepalive = new(sync.Map)
|
|
var messageCache = new(sync.Map)
|
|
var messageCache = new(sync.Map)
|
|
|
|
+var networkcontext = new(sync.Map)
|
|
|
|
|
|
const lastNodeUpdate = "lnu"
|
|
const lastNodeUpdate = "lnu"
|
|
const lastPeerUpdate = "lpu"
|
|
const lastPeerUpdate = "lpu"
|
|
@@ -65,21 +66,26 @@ func read(network, which string) string {
|
|
|
|
|
|
// Daemon runs netclient daemon from command line
|
|
// Daemon runs netclient daemon from command line
|
|
func Daemon() error {
|
|
func Daemon() error {
|
|
- ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
networks, err := ncutils.GetSystemNetworks()
|
|
networks, err := ncutils.GetSystemNetworks()
|
|
if err != nil {
|
|
if err != nil {
|
|
- cancel()
|
|
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
for _, network := range networks {
|
|
for _, network := range networks {
|
|
|
|
+ ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
+ networkcontext.Store(network, cancel)
|
|
go MessageQueue(ctx, network)
|
|
go MessageQueue(ctx, network)
|
|
}
|
|
}
|
|
quit := make(chan os.Signal, 1)
|
|
quit := make(chan os.Signal, 1)
|
|
signal.Notify(quit, syscall.SIGTERM, os.Interrupt)
|
|
signal.Notify(quit, syscall.SIGTERM, os.Interrupt)
|
|
<-quit
|
|
<-quit
|
|
- cancel()
|
|
|
|
|
|
+ for _, network := range networks {
|
|
|
|
+ if cancel, ok := networkcontext.Load(network); ok {
|
|
|
|
+ cancel.(context.CancelFunc)()
|
|
|
|
+ }
|
|
|
|
+ }
|
|
ncutils.Log("all done")
|
|
ncutils.Log("all done")
|
|
return nil
|
|
return nil
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
// SetupMQTT creates a connection to broker and return client
|
|
// SetupMQTT creates a connection to broker and return client
|
|
@@ -87,7 +93,6 @@ func SetupMQTT(cfg *config.ClientConfig) mqtt.Client {
|
|
opts := mqtt.NewClientOptions()
|
|
opts := mqtt.NewClientOptions()
|
|
server := getServerAddress(cfg)
|
|
server := getServerAddress(cfg)
|
|
opts.AddBroker(server + ":1883")
|
|
opts.AddBroker(server + ":1883")
|
|
-
|
|
|
|
opts.SetDefaultPublishHandler(All)
|
|
opts.SetDefaultPublishHandler(All)
|
|
client := mqtt.NewClient(opts)
|
|
client := mqtt.NewClient(opts)
|
|
tperiod := time.Now().Add(12 * time.Second)
|
|
tperiod := time.Now().Add(12 * time.Second)
|
|
@@ -197,10 +202,18 @@ func MessageQueue(ctx context.Context, network string) {
|
|
ncutils.Log("leader not defined for network " + cfg.Node.Network)
|
|
ncutils.Log("leader not defined for network " + cfg.Node.Network)
|
|
}
|
|
}
|
|
defer client.Disconnect(250)
|
|
defer client.Disconnect(250)
|
|
- go MonitorKeepalive(ctx, client, &cfg)
|
|
|
|
- go Checkin(ctx, &cfg, network)
|
|
|
|
|
|
+ wg := &sync.WaitGroup{}
|
|
|
|
+ wg.Add(2)
|
|
|
|
+ keepalivectx, keepalivecancel := context.WithCancel(context.Background())
|
|
|
|
+ go MonitorKeepalive(keepalivectx, wg, client, &cfg)
|
|
|
|
+ checkinctx, checkincancel := context.WithCancel(context.Background())
|
|
|
|
+ go Checkin(checkinctx, wg, &cfg, network)
|
|
<-ctx.Done()
|
|
<-ctx.Done()
|
|
- ncutils.Log("shutting down daemon")
|
|
|
|
|
|
+ keepalivecancel()
|
|
|
|
+ checkincancel()
|
|
|
|
+ ncutils.Log("shutting down message queue for network " + network)
|
|
|
|
+ wg.Wait()
|
|
|
|
+ ncutils.Log("shutdown complete")
|
|
}
|
|
}
|
|
|
|
|
|
// All -- mqtt message hander for all ('#') topics
|
|
// All -- mqtt message hander for all ('#') topics
|
|
@@ -254,9 +267,13 @@ func NodeUpdate(client mqtt.Client, msg mqtt.Message) {
|
|
cfg.Node = newNode
|
|
cfg.Node = newNode
|
|
switch newNode.Action {
|
|
switch newNode.Action {
|
|
case models.NODE_DELETE:
|
|
case models.NODE_DELETE:
|
|
- if token := client.Unsubscribe(fmt.Sprintf("update/%s/%s", newNode.Network, newNode.ID), fmt.Sprintf("peers/%s/%s", newNode.Network, newNode.ID)); token.Wait() && token.Error() != nil {
|
|
|
|
- ncutils.PrintLog("error unsubscribing during node deletion", 1)
|
|
|
|
|
|
+ if cancel, ok := networkcontext.Load(newNode.Network); ok {
|
|
|
|
+ ncutils.Log("cancelling message queue context for " + newNode.Network)
|
|
|
|
+ cancel.(context.CancelFunc)()
|
|
|
|
+ } else {
|
|
|
|
+ ncutils.Log("failed to kill go routines for network " + newNode.Network)
|
|
}
|
|
}
|
|
|
|
+ ncutils.Log("deleting configuration files")
|
|
if err := WipeLocal(cfg.Network); err != nil {
|
|
if err := WipeLocal(cfg.Network); err != nil {
|
|
ncutils.PrintLog("error deleting local instance: "+err.Error(), 1)
|
|
ncutils.PrintLog("error deleting local instance: "+err.Error(), 1)
|
|
ncutils.PrintLog("Please perform manual clean up", 1)
|
|
ncutils.PrintLog("Please perform manual clean up", 1)
|
|
@@ -270,11 +287,13 @@ func NodeUpdate(client mqtt.Client, msg mqtt.Message) {
|
|
}
|
|
}
|
|
return
|
|
return
|
|
case models.NODE_UPDATE_KEY:
|
|
case models.NODE_UPDATE_KEY:
|
|
|
|
+ ncutils.Log("delete recieved")
|
|
if err := UpdateKeys(&cfg, client); err != nil {
|
|
if err := UpdateKeys(&cfg, client); err != nil {
|
|
ncutils.PrintLog("err updating wireguard keys: "+err.Error(), 1)
|
|
ncutils.PrintLog("err updating wireguard keys: "+err.Error(), 1)
|
|
}
|
|
}
|
|
ifaceDelta = true
|
|
ifaceDelta = true
|
|
case models.NODE_NOOP:
|
|
case models.NODE_NOOP:
|
|
|
|
+ ncutils.Log("noop recieved")
|
|
default:
|
|
default:
|
|
}
|
|
}
|
|
//Save new config
|
|
//Save new config
|
|
@@ -295,7 +314,7 @@ func NodeUpdate(client mqtt.Client, msg mqtt.Message) {
|
|
}
|
|
}
|
|
if ifaceDelta {
|
|
if ifaceDelta {
|
|
ncutils.Log("applying WG conf to " + file)
|
|
ncutils.Log("applying WG conf to " + file)
|
|
- err = wireguard.ApplyWGQuickConf(file, cfg.Node.Interface)
|
|
|
|
|
|
+ err = wireguard.ApplyConf(&cfg.Node, cfg.Node.Interface, file)
|
|
if err != nil {
|
|
if err != nil {
|
|
ncutils.Log("error restarting wg after node update " + err.Error())
|
|
ncutils.Log("error restarting wg after node update " + err.Error())
|
|
return
|
|
return
|
|
@@ -367,10 +386,12 @@ func UpdatePeers(client mqtt.Client, msg mqtt.Message) {
|
|
}
|
|
}
|
|
|
|
|
|
// MonitorKeepalive - checks time last server keepalive received. If more than 3+ minutes, notify and resubscribe
|
|
// 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) {
|
|
|
|
|
|
+func MonitorKeepalive(ctx context.Context, wg *sync.WaitGroup, client mqtt.Client, cfg *config.ClientConfig) {
|
|
|
|
+ defer wg.Done()
|
|
for {
|
|
for {
|
|
select {
|
|
select {
|
|
case <-ctx.Done():
|
|
case <-ctx.Done():
|
|
|
|
+ ncutils.Log("cancel recieved, monitor keepalive exiting")
|
|
return
|
|
return
|
|
case <-time.After(time.Second * 150):
|
|
case <-time.After(time.Second * 150):
|
|
var keepalivetime time.Time
|
|
var keepalivetime time.Time
|
|
@@ -464,7 +485,8 @@ func UpdateKeys(cfg *config.ClientConfig, client mqtt.Client) error {
|
|
|
|
|
|
// Checkin -- go routine that checks for public or local ip changes, publishes changes
|
|
// Checkin -- go routine that checks for public or local ip changes, publishes changes
|
|
// if there are no updates, simply "pings" the server as a checkin
|
|
// if there are no updates, simply "pings" the server as a checkin
|
|
-func Checkin(ctx context.Context, cfg *config.ClientConfig, network string) {
|
|
|
|
|
|
+func Checkin(ctx context.Context, wg *sync.WaitGroup, cfg *config.ClientConfig, network string) {
|
|
|
|
+ defer wg.Done()
|
|
for {
|
|
for {
|
|
select {
|
|
select {
|
|
case <-ctx.Done():
|
|
case <-ctx.Done():
|