|
@@ -16,11 +16,12 @@ import (
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
|
)
|
|
)
|
|
|
|
|
|
-//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())
|
|
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 {
|
|
@@ -34,7 +35,7 @@ func Daemon() error {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-//SetupMQTT creates a connection to broker and return client
|
|
|
|
|
|
+// SetupMQTT creates a connection to broker and return client
|
|
func SetupMQTT(cfg config.ClientConfig) mqtt.Client {
|
|
func SetupMQTT(cfg config.ClientConfig) mqtt.Client {
|
|
opts := mqtt.NewClientOptions()
|
|
opts := mqtt.NewClientOptions()
|
|
ncutils.Log("setting broker to " + cfg.Server.CoreDNSAddr + ":1883")
|
|
ncutils.Log("setting broker to " + cfg.Server.CoreDNSAddr + ":1883")
|
|
@@ -47,7 +48,7 @@ func SetupMQTT(cfg config.ClientConfig) mqtt.Client {
|
|
return client
|
|
return client
|
|
}
|
|
}
|
|
|
|
|
|
-//Netclient sets up Message Queue and subsribes/publishes updates to/from server
|
|
|
|
|
|
+// Netclient sets up Message Queue and subsribes/publishes updates to/from server
|
|
func Netclient(ctx context.Context, network string) {
|
|
func Netclient(ctx context.Context, network string) {
|
|
ncutils.Log("netclient go routine started for " + network)
|
|
ncutils.Log("netclient go routine started for " + network)
|
|
var cfg config.ClientConfig
|
|
var cfg config.ClientConfig
|
|
@@ -55,7 +56,7 @@ func Netclient(ctx context.Context, network string) {
|
|
cfg.ReadConfig()
|
|
cfg.ReadConfig()
|
|
//fix NodeID to remove ### so NodeID can be used as message topic
|
|
//fix NodeID to remove ### so NodeID can be used as message topic
|
|
//remove with GRA-73
|
|
//remove with GRA-73
|
|
- cfg.Node.ID = strings.ReplaceAll(cfg.Node.ID, "###", "-")
|
|
|
|
|
|
+ cfg.Node.ID = strings.Replace(cfg.Node.ID, "###", "-", 1)
|
|
ncutils.Log("daemon started for network:" + network)
|
|
ncutils.Log("daemon started for network:" + network)
|
|
client := SetupMQTT(cfg)
|
|
client := SetupMQTT(cfg)
|
|
if token := client.Subscribe("#", 0, nil); token.Wait() && token.Error() != nil {
|
|
if token := client.Subscribe("#", 0, nil); token.Wait() && token.Error() != nil {
|
|
@@ -69,32 +70,30 @@ func Netclient(ctx context.Context, network string) {
|
|
go Metrics(ctx, cfg, network)
|
|
go Metrics(ctx, cfg, network)
|
|
<-ctx.Done()
|
|
<-ctx.Done()
|
|
ncutils.Log("shutting down daemon")
|
|
ncutils.Log("shutting down daemon")
|
|
- return
|
|
|
|
- ncutils.Log("netclient go routine ended for " + network)
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-//All -- mqtt message hander for all ('#') topics
|
|
|
|
|
|
+// All -- mqtt message hander for all ('#') topics
|
|
var All mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
|
|
var All mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
|
|
ncutils.Log("Topic: " + string(msg.Topic()))
|
|
ncutils.Log("Topic: " + string(msg.Topic()))
|
|
ncutils.Log("Message: " + string(msg.Payload()))
|
|
ncutils.Log("Message: " + string(msg.Payload()))
|
|
}
|
|
}
|
|
|
|
|
|
-//NodeUpdate -- mqtt message handler for /update/<NodeID> topic
|
|
|
|
|
|
+// NodeUpdate -- mqtt message handler for /update/<NodeID> topic
|
|
var NodeUpdate mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
|
|
var NodeUpdate mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
|
|
ncutils.Log("received message to update node " + string(msg.Payload()))
|
|
ncutils.Log("received message to update node " + string(msg.Payload()))
|
|
}
|
|
}
|
|
|
|
|
|
-//UpdatePeers -- mqtt message handler for /update/peers/<NodeID> topic
|
|
|
|
|
|
+// UpdatePeers -- mqtt message handler for /update/peers/<NodeID> topic
|
|
var UpdatePeers mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
|
|
var UpdatePeers mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
|
|
ncutils.Log("received message to update peers " + string(msg.Payload()))
|
|
ncutils.Log("received message to update peers " + string(msg.Payload()))
|
|
}
|
|
}
|
|
|
|
|
|
-//UpdateKeys -- mqtt message handler for /update/keys/<NodeID> topic
|
|
|
|
|
|
+// UpdateKeys -- mqtt message handler for /update/keys/<NodeID> topic
|
|
var UpdateKeys mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
|
|
var UpdateKeys mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
|
|
ncutils.Log("received message to update keys " + string(msg.Payload()))
|
|
ncutils.Log("received message to update keys " + string(msg.Payload()))
|
|
}
|
|
}
|
|
|
|
|
|
-//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, cfg config.ClientConfig, network string) {
|
|
for {
|
|
for {
|
|
@@ -138,7 +137,7 @@ func Checkin(ctx context.Context, cfg config.ClientConfig, network string) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-//UpdateEndpoint -- publishes an endpoint update to broker
|
|
|
|
|
|
+// UpdateEndpoint -- publishes an endpoint update to broker
|
|
func UpdateEndpoint(cfg config.ClientConfig, network, ip string) {
|
|
func UpdateEndpoint(cfg config.ClientConfig, network, ip string) {
|
|
ncutils.Log("Updating endpoint")
|
|
ncutils.Log("Updating endpoint")
|
|
client := SetupMQTT(cfg)
|
|
client := SetupMQTT(cfg)
|
|
@@ -148,7 +147,7 @@ func UpdateEndpoint(cfg config.ClientConfig, network, ip string) {
|
|
client.Disconnect(250)
|
|
client.Disconnect(250)
|
|
}
|
|
}
|
|
|
|
|
|
-//UpdateLocalAddress -- publishes a local address update to broker
|
|
|
|
|
|
+// UpdateLocalAddress -- publishes a local address update to broker
|
|
func UpdateLocalAddress(cfg config.ClientConfig, network, ip string) {
|
|
func UpdateLocalAddress(cfg config.ClientConfig, network, ip string) {
|
|
ncutils.Log("Updating local address")
|
|
ncutils.Log("Updating local address")
|
|
client := SetupMQTT(cfg)
|
|
client := SetupMQTT(cfg)
|
|
@@ -158,7 +157,7 @@ func UpdateLocalAddress(cfg config.ClientConfig, network, ip string) {
|
|
client.Disconnect(250)
|
|
client.Disconnect(250)
|
|
}
|
|
}
|
|
|
|
|
|
-//Hello -- ping the broker to let server know node is alive and doing fine
|
|
|
|
|
|
+// Hello -- ping the broker to let server know node is alive and doing fine
|
|
func Hello(cfg config.ClientConfig, network string) {
|
|
func Hello(cfg config.ClientConfig, network string) {
|
|
client := SetupMQTT(cfg)
|
|
client := SetupMQTT(cfg)
|
|
if token := client.Publish("ping/"+cfg.Node.ID, 0, false, "hello world!"); token.Wait() && token.Error() != nil {
|
|
if token := client.Publish("ping/"+cfg.Node.ID, 0, false, "hello world!"); token.Wait() && token.Error() != nil {
|
|
@@ -167,7 +166,7 @@ func Hello(cfg config.ClientConfig, network string) {
|
|
client.Disconnect(250)
|
|
client.Disconnect(250)
|
|
}
|
|
}
|
|
|
|
|
|
-//Metics -- go routine that collects wireguard metrics and publishes to broker
|
|
|
|
|
|
+// Metics -- go routine that collects wireguard metrics and publishes to broker
|
|
func Metrics(ctx context.Context, cfg config.ClientConfig, network string) {
|
|
func Metrics(ctx context.Context, cfg config.ClientConfig, network string) {
|
|
for {
|
|
for {
|
|
select {
|
|
select {
|