netmaker/mq/mq.go

302 lines
8.9 KiB
Go
Raw Normal View History

2022-01-13 05:23:34 +08:00
package mq
import (
"context"
2022-01-16 07:16:44 +08:00
"encoding/json"
2022-01-13 05:23:34 +08:00
"errors"
2022-01-29 04:33:30 +08:00
"fmt"
"log"
2022-01-13 05:23:34 +08:00
"strings"
"time"
2022-01-13 05:23:34 +08:00
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/logic"
"github.com/gravitl/netmaker/models"
2022-02-09 04:05:37 +08:00
"github.com/gravitl/netmaker/netclient/ncutils"
2022-01-19 06:32:12 +08:00
"github.com/gravitl/netmaker/servercfg"
2022-01-13 05:23:34 +08:00
)
2022-02-09 08:13:58 +08:00
// KEEPALIVE_TIMEOUT - time in seconds for timeout
2022-02-02 03:01:13 +08:00
const KEEPALIVE_TIMEOUT = 60 //timeout in seconds
2022-02-09 08:13:58 +08:00
// MQ_DISCONNECT - disconnects MQ
const MQ_DISCONNECT = 250
2022-02-09 10:49:47 +08:00
var peer_force_send = 0
2022-01-26 07:15:27 +08:00
// DefaultHandler default message queue handler - only called when GetDebug == true
2022-02-09 08:13:58 +08:00
func DefaultHandler(client mqtt.Client, msg mqtt.Message) {
2022-01-26 07:15:27 +08:00
logger.Log(0, "MQTT Message: Topic: ", string(msg.Topic()), " Message: ", string(msg.Payload()))
2022-01-13 05:23:34 +08:00
}
// Ping message Handler -- handles ping topic from client nodes
2022-02-09 08:13:58 +08:00
func Ping(client mqtt.Client, msg mqtt.Message) {
2022-01-26 07:15:27 +08:00
logger.Log(0, "Ping Handler: ", msg.Topic())
2022-01-13 05:23:34 +08:00
go func() {
2022-01-15 04:35:15 +08:00
id, err := GetID(msg.Topic())
2022-01-13 05:23:34 +08:00
if err != nil {
logger.Log(0, "error getting node.ID sent on ping topic ")
return
}
2022-01-15 04:35:15 +08:00
node, err := logic.GetNodeByID(id)
2022-01-13 05:23:34 +08:00
if err != nil {
2022-01-26 07:15:27 +08:00
logger.Log(0, "mq-ping error getting node: ", err.Error())
2022-01-15 04:35:15 +08:00
record, err := database.FetchRecord(database.NODES_TABLE_NAME, id)
2022-01-13 05:23:34 +08:00
if err != nil {
logger.Log(0, "error reading database ", err.Error())
return
}
logger.Log(0, "record from database")
logger.Log(0, record)
return
}
2022-01-30 04:11:21 +08:00
_, decryptErr := decryptMsg(&node, msg.Payload())
2022-01-29 04:33:30 +08:00
if decryptErr != nil {
logger.Log(0, "error updating node ", node.ID, err.Error())
return
}
2022-01-13 05:23:34 +08:00
node.SetLastCheckIn()
2022-01-19 22:44:35 +08:00
if err := logic.UpdateNode(&node, &node); err != nil {
2022-01-26 07:15:27 +08:00
logger.Log(0, "error updating node ", err.Error())
2022-01-19 06:32:12 +08:00
}
2022-01-30 04:11:21 +08:00
logger.Log(3, "ping processed for node", node.ID)
2022-01-13 05:23:34 +08:00
// --TODO --set client version once feature is implemented.
//node.SetClientVersion(msg.Payload())
}()
}
// UpdateNode message Handler -- handles updates from client nodes
2022-02-09 08:13:58 +08:00
func UpdateNode(client mqtt.Client, msg mqtt.Message) {
2022-01-13 05:23:34 +08:00
go func() {
2022-01-15 04:35:15 +08:00
id, err := GetID(msg.Topic())
2022-01-13 05:23:34 +08:00
if err != nil {
2022-01-26 07:15:27 +08:00
logger.Log(1, "error getting node.ID sent on ", msg.Topic(), err.Error())
return
}
2022-01-29 04:33:30 +08:00
currentNode, err := logic.GetNodeByID(id)
if err != nil {
logger.Log(1, "error getting node ", id, err.Error())
return
}
2022-01-30 04:02:37 +08:00
decrypted, decryptErr := decryptMsg(&currentNode, msg.Payload())
2022-01-29 04:33:30 +08:00
if decryptErr != nil {
logger.Log(1, "failed to decrypt message for node ", id, decryptErr.Error())
return
}
2022-01-26 07:15:27 +08:00
logger.Log(1, "Update Node Handler", id)
var newNode models.Node
2022-01-29 04:33:30 +08:00
if err := json.Unmarshal(decrypted, &newNode); err != nil {
2022-01-26 07:15:27 +08:00
logger.Log(1, "error unmarshaling payload ", err.Error())
2022-01-13 05:23:34 +08:00
return
}
if err := logic.UpdateNode(&currentNode, &newNode); err != nil {
2022-01-26 07:15:27 +08:00
logger.Log(1, "error saving node", err.Error())
2022-01-19 06:32:12 +08:00
}
2022-01-31 09:52:56 +08:00
if err := PublishPeerUpdate(&newNode); err != nil {
logger.Log(1, "error publishing peer update ", err.Error())
return
}
2022-01-27 05:06:45 +08:00
logger.Log(1, "no need to update peers")
2022-01-13 05:23:34 +08:00
}()
}
// PublishPeerUpdate --- deterines and publishes a peer update to all the peers of a node
2022-01-29 04:33:30 +08:00
func PublishPeerUpdate(newNode *models.Node) error {
2022-02-06 02:26:12 +08:00
if !servercfg.IsMessageQueueBackend() {
return nil
}
networkNodes, err := logic.GetNetworkNodes(newNode.Network)
2022-01-13 05:23:34 +08:00
if err != nil {
2022-01-26 07:15:27 +08:00
logger.Log(1, "err getting Network Nodes", err.Error())
2022-01-16 07:16:44 +08:00
return err
}
2022-01-19 22:44:35 +08:00
for _, node := range networkNodes {
2022-01-31 08:58:20 +08:00
2022-02-01 05:15:10 +08:00
if node.IsServer == "yes" || node.ID == newNode.ID {
2022-01-31 08:58:20 +08:00
continue
}
peerUpdate, err := logic.GetPeerUpdate(&node)
if err != nil {
2022-01-31 00:51:39 +08:00
logger.Log(1, "error getting peer update for node", node.ID, err.Error())
continue
2022-01-19 22:44:35 +08:00
}
2022-01-19 06:32:12 +08:00
data, err := json.Marshal(&peerUpdate)
2022-01-16 07:16:44 +08:00
if err != nil {
2022-01-29 04:33:30 +08:00
logger.Log(2, "error marshaling peer update for node", node.ID, err.Error())
continue
2022-01-16 07:16:44 +08:00
}
2022-01-29 22:14:18 +08:00
if err = publish(&node, fmt.Sprintf("peers/%s/%s", node.Network, node.ID), data); err != nil {
2022-01-29 04:33:30 +08:00
logger.Log(1, "failed to publish peer update for node", node.ID)
2022-01-31 09:07:50 +08:00
} else {
2022-02-02 03:01:13 +08:00
logger.Log(1, fmt.Sprintf("sent peer update for node %s on network: %s ", node.Name, node.Network))
2022-01-13 05:23:34 +08:00
}
}
return nil
2022-01-13 05:23:34 +08:00
}
2022-02-07 02:06:07 +08:00
// PublishPeerUpdate --- deterines and publishes a peer update to all the peers of a node
2022-02-07 02:27:24 +08:00
func PublishExtPeerUpdate(node *models.Node) error {
var err error
if logic.IsLocalServer(node) {
if err = logic.ServerUpdate(node, false); err != nil {
2022-02-07 02:06:07 +08:00
logger.Log(1, "server node:", node.ID, "failed to update peers with ext clients")
return err
} else {
return nil
}
}
if !servercfg.IsMessageQueueBackend() {
return nil
}
2022-02-07 02:27:24 +08:00
peerUpdate, err := logic.GetPeerUpdate(node)
2022-02-07 02:06:07 +08:00
if err != nil {
return err
}
data, err := json.Marshal(&peerUpdate)
if err != nil {
return err
}
2022-02-07 02:27:24 +08:00
return publish(node, fmt.Sprintf("peers/%s/%s", node.Network, node.ID), data)
2022-02-07 02:06:07 +08:00
}
// GetID -- decodes a message queue topic and returns the embedded node.ID
2022-01-13 05:23:34 +08:00
func GetID(topic string) (string, error) {
parts := strings.Split(topic, "/")
count := len(parts)
if count == 1 {
return "", errors.New("invalid topic")
}
2022-01-15 04:35:15 +08:00
//the last part of the topic will be the node.ID
2022-01-15 06:22:11 +08:00
return parts[count-1], nil
2022-01-13 05:23:34 +08:00
}
2022-01-19 06:32:12 +08:00
2022-01-29 04:33:30 +08:00
// NodeUpdate -- publishes a node update
func NodeUpdate(node *models.Node) error {
2022-01-29 04:33:30 +08:00
if !servercfg.IsMessageQueueBackend() {
return nil
}
2022-01-22 22:04:11 +08:00
logger.Log(3, "publishing node update to "+node.Name)
data, err := json.Marshal(node)
if err != nil {
2022-01-26 07:15:27 +08:00
logger.Log(2, "error marshalling node update ", err.Error())
return err
}
2022-01-29 22:14:18 +08:00
if err = publish(node, fmt.Sprintf("update/%s/%s", node.Network, node.ID), data); err != nil {
2022-01-29 04:33:30 +08:00
logger.Log(2, "error publishing node update to peer ", node.ID, err.Error())
return err
}
return nil
}
// SetupMQTT creates a connection to broker and return client
2022-02-09 08:13:58 +08:00
func SetupMQTT(publish bool) mqtt.Client {
2022-01-19 06:32:12 +08:00
opts := mqtt.NewClientOptions()
2022-02-09 08:13:58 +08:00
opts.AddBroker(servercfg.GetMessageQueueEndpoint())
2022-02-09 04:05:37 +08:00
id := ncutils.MakeRandomString(23)
opts.ClientID = id
2022-02-09 08:13:58 +08:00
opts.SetAutoReconnect(true)
opts.SetConnectRetry(true)
opts.SetConnectRetryInterval(time.Second << 2)
opts.SetKeepAlive(time.Minute)
opts.SetWriteTimeout(time.Minute)
opts.SetOnConnectHandler(func(client mqtt.Client) {
if !publish {
if servercfg.GetDebug() {
if token := client.Subscribe("#", 2, mqtt.MessageHandler(DefaultHandler)); token.Wait() && token.Error() != nil {
client.Disconnect(240)
logger.Log(0, "default subscription failed")
}
}
if token := client.Subscribe("ping/#", 2, mqtt.MessageHandler(Ping)); token.Wait() && token.Error() != nil {
client.Disconnect(240)
logger.Log(0, "ping subscription failed")
}
if token := client.Subscribe("update/#", 0, mqtt.MessageHandler(UpdateNode)); token.Wait() && token.Error() != nil {
client.Disconnect(240)
logger.Log(0, "node update subscription failed")
}
opts.SetOrderMatters(true)
opts.SetResumeSubs(true)
}
})
2022-01-28 04:52:31 +08:00
client := mqtt.NewClient(opts)
2022-01-28 04:46:45 +08:00
tperiod := time.Now().Add(10 * time.Second)
for {
if token := client.Connect(); token.Wait() && token.Error() != nil {
logger.Log(2, "unable to connect to broker, retrying ...")
2022-01-28 04:52:31 +08:00
if time.Now().After(tperiod) {
2022-01-28 04:46:45 +08:00
log.Fatal(0, "could not connect to broker, exiting ...", token.Error())
}
} else {
break
}
time.Sleep(2 * time.Second)
2022-01-19 06:32:12 +08:00
}
return client
2022-01-19 06:32:12 +08:00
}
// Keepalive -- periodically pings all nodes to let them know server is still alive and doing well
func Keepalive(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case <-time.After(time.Second * KEEPALIVE_TIMEOUT):
2022-02-09 10:34:38 +08:00
sendPeers()
}
}
}
// sendPeers - retrieve networks, send peer ports to all peers
func sendPeers() {
var force bool
2022-02-09 10:49:47 +08:00
peer_force_send++
if peer_force_send == 5 {
2022-02-09 10:34:38 +08:00
force = true
2022-02-09 10:49:47 +08:00
peer_force_send = 0
2022-02-09 10:34:38 +08:00
}
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())
2022-02-01 06:13:19 +08:00
}
}
}
2022-02-09 10:34:38 +08:00
} else {
logger.Log(1, "unable to retrieve leader for network ", network.NetID)
logger.Log(1, errN.Error())
continue
}
}
}
2022-02-09 08:13:58 +08:00
// func publishServerKeepalive(client mqtt.Client, network *models.Network) {
// nodes, err := logic.GetNetworkNodes(network.NetID)
// if err != nil {
// return
// }
// for _, node := range nodes {
// if token := client.Publish(fmt.Sprintf("serverkeepalive/%s/%s", network.NetID, node.ID), 0, false, servercfg.GetVersion()); token.Wait() && token.Error() != nil {
// logger.Log(1, "error publishing server keepalive for network", network.NetID, token.Error().Error())
// } else {
// logger.Log(2, "keepalive sent for network/node", network.NetID, node.ID)
// }
// }
// }