2022-01-13 05:23:34 +08:00
|
|
|
package mq
|
|
|
|
|
|
|
|
import (
|
2022-01-27 02:42:52 +08:00
|
|
|
"context"
|
2022-09-30 21:01:57 +08:00
|
|
|
"fmt"
|
2022-01-27 02:42:52 +08:00
|
|
|
"time"
|
2022-01-13 05:23:34 +08:00
|
|
|
|
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
|
|
"github.com/gravitl/netmaker/logger"
|
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
|
2022-01-27 02:42:52 +08:00
|
|
|
const MQ_DISCONNECT = 250
|
|
|
|
|
2022-05-16 21:38:47 +08:00
|
|
|
// MQ_TIMEOUT - timeout for MQ
|
|
|
|
const MQ_TIMEOUT = 30
|
|
|
|
|
2022-02-09 10:49:47 +08:00
|
|
|
var peer_force_send = 0
|
|
|
|
|
2022-08-30 02:08:01 +08:00
|
|
|
var mqclient mqtt.Client
|
|
|
|
|
2022-09-30 02:29:18 +08:00
|
|
|
// SetUpAdminClient - sets up admin client for the MQ
|
2022-09-26 19:27:10 +08:00
|
|
|
func SetUpAdminClient() {
|
2022-09-16 18:10:42 +08:00
|
|
|
opts := mqtt.NewClientOptions()
|
2022-09-26 19:27:10 +08:00
|
|
|
setMqOptions(mqAdminUserName, servercfg.GetMqAdminPassword(), opts)
|
|
|
|
mqAdminClient = mqtt.NewClient(opts)
|
|
|
|
opts.SetOnConnectHandler(func(client mqtt.Client) {
|
2022-09-30 21:01:57 +08:00
|
|
|
if token := client.Subscribe(dynamicSecSubTopic, 2, mqtt.MessageHandler(watchDynSecTopic)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
|
2022-09-26 19:27:10 +08:00
|
|
|
client.Disconnect(240)
|
2022-09-30 21:01:57 +08:00
|
|
|
logger.Log(0, fmt.Sprintf("Dynamic security client subscription failed: %v ", token.Error()))
|
2022-09-26 19:27:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
opts.SetOrderMatters(true)
|
|
|
|
opts.SetResumeSubs(true)
|
|
|
|
})
|
2022-09-16 18:10:42 +08:00
|
|
|
tperiod := time.Now().Add(10 * time.Second)
|
|
|
|
for {
|
2022-09-26 19:27:10 +08:00
|
|
|
if token := mqAdminClient.Connect(); !token.WaitTimeout(MQ_TIMEOUT*time.Second) || token.Error() != nil {
|
|
|
|
logger.Log(2, "Admin: unable to connect to broker, retrying ...")
|
2022-09-16 18:10:42 +08:00
|
|
|
if time.Now().After(tperiod) {
|
|
|
|
if token.Error() == nil {
|
2022-09-26 19:27:10 +08:00
|
|
|
logger.FatalLog("Admin: could not connect to broker, token timeout, exiting ...")
|
2022-09-16 18:10:42 +08:00
|
|
|
} else {
|
2022-09-26 19:27:10 +08:00
|
|
|
logger.FatalLog("Admin: could not connect to broker, exiting ...", token.Error().Error())
|
2022-09-16 18:10:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
}
|
2022-09-26 19:27:10 +08:00
|
|
|
|
2022-09-16 18:10:42 +08:00
|
|
|
}
|
|
|
|
|
2022-09-26 19:27:10 +08:00
|
|
|
func setMqOptions(user, password string, opts *mqtt.ClientOptions) {
|
2022-09-14 02:03:39 +08:00
|
|
|
broker, _ := servercfg.GetMessageQueueEndpoint()
|
2022-07-08 03:38:13 +08:00
|
|
|
opts.AddBroker(broker)
|
2022-02-09 04:05:37 +08:00
|
|
|
id := ncutils.MakeRandomString(23)
|
|
|
|
opts.ClientID = id
|
2022-09-26 19:27:10 +08:00
|
|
|
opts.SetUsername(user)
|
|
|
|
opts.SetPassword(password)
|
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)
|
2022-09-26 19:27:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetupMQTT creates a connection to broker and return client
|
|
|
|
func SetupMQTT() {
|
|
|
|
opts := mqtt.NewClientOptions()
|
|
|
|
setMqOptions(mqNetmakerServerUserName, servercfg.GetMqAdminPassword(), opts)
|
2022-02-09 08:13:58 +08:00
|
|
|
opts.SetOnConnectHandler(func(client mqtt.Client) {
|
2022-08-30 02:08:01 +08:00
|
|
|
if token := client.Subscribe("ping/#", 2, mqtt.MessageHandler(Ping)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
|
|
|
|
client.Disconnect(240)
|
|
|
|
logger.Log(0, "ping subscription failed")
|
2022-02-09 08:13:58 +08:00
|
|
|
}
|
2022-08-30 02:08:01 +08:00
|
|
|
if token := client.Subscribe("update/#", 0, mqtt.MessageHandler(UpdateNode)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
|
|
|
|
client.Disconnect(240)
|
|
|
|
logger.Log(0, "node update subscription failed")
|
|
|
|
}
|
|
|
|
if token := client.Subscribe("signal/#", 0, mqtt.MessageHandler(ClientPeerUpdate)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
|
|
|
|
client.Disconnect(240)
|
|
|
|
logger.Log(0, "node client subscription failed")
|
|
|
|
}
|
2022-09-14 03:25:56 +08:00
|
|
|
if token := client.Subscribe("metrics/#", 0, mqtt.MessageHandler(UpdateMetrics)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
|
2022-09-14 18:29:22 +08:00
|
|
|
client.Disconnect(240)
|
2022-09-14 03:25:56 +08:00
|
|
|
logger.Log(0, "node metrics subscription failed")
|
2022-09-14 18:29:22 +08:00
|
|
|
}
|
2022-08-30 02:08:01 +08:00
|
|
|
|
|
|
|
opts.SetOrderMatters(true)
|
|
|
|
opts.SetResumeSubs(true)
|
2022-02-09 08:13:58 +08:00
|
|
|
})
|
2022-08-30 02:29:28 +08:00
|
|
|
mqclient = mqtt.NewClient(opts)
|
2022-01-28 04:46:45 +08:00
|
|
|
tperiod := time.Now().Add(10 * time.Second)
|
|
|
|
for {
|
2022-08-30 02:08:01 +08:00
|
|
|
if token := mqclient.Connect(); !token.WaitTimeout(MQ_TIMEOUT*time.Second) || token.Error() != nil {
|
2022-01-28 04:46:45 +08:00
|
|
|
logger.Log(2, "unable to connect to broker, retrying ...")
|
2022-01-28 04:52:31 +08:00
|
|
|
if time.Now().After(tperiod) {
|
2022-05-16 21:38:47 +08:00
|
|
|
if token.Error() == nil {
|
2022-07-06 04:27:17 +08:00
|
|
|
logger.FatalLog("could not connect to broker, token timeout, exiting ...")
|
2022-05-16 21:38:47 +08:00
|
|
|
} else {
|
2022-07-06 04:27:17 +08:00
|
|
|
logger.FatalLog("could not connect to broker, exiting ...", token.Error().Error())
|
2022-05-16 21:38:47 +08:00
|
|
|
}
|
2022-01-28 04:46:45 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(2 * time.Second)
|
2022-01-19 06:32:12 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-27 02:42:52 +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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|