mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-05 16:57:51 +08:00
refactor mq connect
This commit is contained in:
@@ -2,6 +2,8 @@ package functions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/ed25519"
|
||||||
|
"crypto/rand"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"errors"
|
"errors"
|
||||||
@@ -22,6 +24,7 @@ import (
|
|||||||
"github.com/gravitl/netmaker/netclient/daemon"
|
"github.com/gravitl/netmaker/netclient/daemon"
|
||||||
"github.com/gravitl/netmaker/netclient/ncutils"
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
||||||
"github.com/gravitl/netmaker/netclient/wireguard"
|
"github.com/gravitl/netmaker/netclient/wireguard"
|
||||||
|
ssl "github.com/gravitl/netmaker/tls"
|
||||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -38,8 +41,7 @@ type cachedMessage struct {
|
|||||||
|
|
||||||
// Daemon runs netclient daemon from command line
|
// Daemon runs netclient daemon from command line
|
||||||
func Daemon() error {
|
func Daemon() error {
|
||||||
var exists = struct{}{}
|
serverSet := make(map[string]config.ClientConfig)
|
||||||
serverSet := make(map[string]struct{})
|
|
||||||
// == initial pull of all networks ==
|
// == initial pull of all networks ==
|
||||||
networks, _ := ncutils.GetSystemNetworks()
|
networks, _ := ncutils.GetSystemNetworks()
|
||||||
if len(networks) == 0 {
|
if len(networks) == 0 {
|
||||||
@@ -50,7 +52,7 @@ func Daemon() error {
|
|||||||
cfg := config.ClientConfig{}
|
cfg := config.ClientConfig{}
|
||||||
cfg.Network = network
|
cfg.Network = network
|
||||||
cfg.ReadConfig()
|
cfg.ReadConfig()
|
||||||
serverSet[cfg.Server.Server] = exists
|
serverSet[cfg.Server.Server] = cfg
|
||||||
//temporary code --- remove in version v0.13.0
|
//temporary code --- remove in version v0.13.0
|
||||||
removeHostDNS(network, ncutils.IsWindows())
|
removeHostDNS(network, ncutils.IsWindows())
|
||||||
// end of code to be removed in version v0.13.0
|
// end of code to be removed in version v0.13.0
|
||||||
@@ -58,11 +60,11 @@ func Daemon() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// == subscribe to all nodes for each on machine ==
|
// == subscribe to all nodes for each on machine ==
|
||||||
for server := range serverSet {
|
for server, config := range serverSet {
|
||||||
logger.Log(1, "started daemon for server ", server)
|
logger.Log(1, "started daemon for server ", server)
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
networkcontext.Store(server, cancel)
|
networkcontext.Store(server, cancel)
|
||||||
go messageQueue(ctx, server)
|
go messageQueue(ctx, &config)
|
||||||
}
|
}
|
||||||
|
|
||||||
// == add waitgroup and cancel for checkin routine ==
|
// == add waitgroup and cancel for checkin routine ==
|
||||||
@@ -110,15 +112,18 @@ func UpdateKeys(nodeCfg *config.ClientConfig, client mqtt.Client) error {
|
|||||||
|
|
||||||
// PingServer -- checks if server is reachable
|
// PingServer -- checks if server is reachable
|
||||||
func PingServer(cfg *config.ClientConfig) error {
|
func PingServer(cfg *config.ClientConfig) error {
|
||||||
pinger, err := ping.NewPinger(cfg.Server.Server)
|
//pinger, err := ping.NewPinger(cfg.Server.Server)
|
||||||
|
pinger, err := ping.NewPinger("broker.nusak.ca")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
log.Println("pinging server ", cfg.Server.Server)
|
||||||
pinger.Timeout = 2 * time.Second
|
pinger.Timeout = 2 * time.Second
|
||||||
|
pinger.Count = 3
|
||||||
pinger.Run()
|
pinger.Run()
|
||||||
stats := pinger.Statistics()
|
stats := pinger.Statistics()
|
||||||
if stats.PacketLoss == 100 {
|
if stats.PacketLoss == 100 {
|
||||||
return errors.New("ping error")
|
return errors.New("ping error " + fmt.Sprintf("%f", stats.PacketLoss))
|
||||||
}
|
}
|
||||||
logger.Log(3, "ping of server", cfg.Server.Server, "was successful")
|
logger.Log(3, "ping of server", cfg.Server.Server, "was successful")
|
||||||
return nil
|
return nil
|
||||||
@@ -168,12 +173,12 @@ func unsubscribeNode(client mqtt.Client, nodeCfg *config.ClientConfig) {
|
|||||||
|
|
||||||
// sets up Message Queue and subsribes/publishes updates to/from server
|
// sets up Message Queue and subsribes/publishes updates to/from server
|
||||||
// the client should subscribe to ALL nodes that exist on server locally
|
// the client should subscribe to ALL nodes that exist on server locally
|
||||||
func messageQueue(ctx context.Context, server string) {
|
func messageQueue(ctx context.Context, cfg *config.ClientConfig) {
|
||||||
logger.Log(0, "netclient daemon started for server: ", server)
|
logger.Log(0, "netclient daemon started for server: ", cfg.Server.Server)
|
||||||
client := setupMQTT(nil, server, false)
|
client := setupMQTT(cfg, false)
|
||||||
defer client.Disconnect(250)
|
defer client.Disconnect(250)
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
logger.Log(0, "shutting down daemon for server ", server)
|
logger.Log(0, "shutting down daemon for server ", cfg.Server.Server)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTLSConf sets up tls configuration to connect to broker securely
|
// NewTLSConf sets up tls configuration to connect to broker securely
|
||||||
@@ -204,11 +209,9 @@ func NewTLSConfig(server string) *tls.Config {
|
|||||||
|
|
||||||
// setupMQTT creates a connection to broker and returns client
|
// setupMQTT creates a connection to broker and returns client
|
||||||
// this function is primarily used to create a connection to publish to the broker
|
// this function is primarily used to create a connection to publish to the broker
|
||||||
func setupMQTT(cfg *config.ClientConfig, server string, publish bool) mqtt.Client {
|
func setupMQTT(cfg *config.ClientConfig, publish bool) mqtt.Client {
|
||||||
opts := mqtt.NewClientOptions()
|
opts := mqtt.NewClientOptions()
|
||||||
if cfg != nil {
|
server := cfg.Server.Server
|
||||||
server = cfg.Server.Server
|
|
||||||
}
|
|
||||||
opts.AddBroker("ssl://" + server + ":8883") // TODO get the appropriate port of the comms mq server
|
opts.AddBroker("ssl://" + server + ":8883") // TODO get the appropriate port of the comms mq server
|
||||||
opts.SetTLSConfig(NewTLSConfig(server))
|
opts.SetTLSConfig(NewTLSConfig(server))
|
||||||
opts.SetClientID(ncutils.MakeRandomString(23))
|
opts.SetClientID(ncutils.MakeRandomString(23))
|
||||||
@@ -236,44 +239,30 @@ func setupMQTT(cfg *config.ClientConfig, server string, publish bool) mqtt.Clien
|
|||||||
opts.SetOrderMatters(true)
|
opts.SetOrderMatters(true)
|
||||||
opts.SetResumeSubs(true)
|
opts.SetResumeSubs(true)
|
||||||
opts.SetConnectionLostHandler(func(c mqtt.Client, e error) {
|
opts.SetConnectionLostHandler(func(c mqtt.Client, e error) {
|
||||||
logger.Log(0, "detected broker connection lost, running pull for ", cfg.Node.Network)
|
logger.Log(0, "detected broker connection lost for", cfg.Server.Server)
|
||||||
_, err := Pull(cfg.Node.Network, true)
|
|
||||||
if err != nil {
|
|
||||||
logger.Log(0, "could not run pull, server unreachable: ", err.Error())
|
|
||||||
logger.Log(0, "waiting to retry...")
|
|
||||||
}
|
|
||||||
logger.Log(0, "connection re-established with mqtt server")
|
|
||||||
})
|
})
|
||||||
client := mqtt.NewClient(opts)
|
client := mqtt.NewClient(opts)
|
||||||
|
for token := client.Connect(); !token.WaitTimeout(30*time.Second) || token.Error() != nil; token = client.Connect() {
|
||||||
tperiod := time.Now().Add(12 * time.Second)
|
|
||||||
for {
|
|
||||||
//if after 12 seconds, try a pull on the last try
|
|
||||||
if time.Now().After(tperiod) {
|
|
||||||
logger.Log(0, "running pull for ", cfg.Node.Network)
|
|
||||||
_, err := Pull(cfg.Node.Network, true)
|
|
||||||
if err != nil {
|
|
||||||
logger.Log(0, "could not run pull, exiting ", cfg.Node.Network, " setup: ", err.Error())
|
|
||||||
return client
|
|
||||||
}
|
|
||||||
time.Sleep(time.Second)
|
|
||||||
}
|
|
||||||
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
|
||||||
|
|
||||||
logger.Log(0, "unable to connect to broker, retrying ...")
|
logger.Log(0, "unable to connect to broker, retrying ...")
|
||||||
if time.Now().After(tperiod) {
|
var err error
|
||||||
logger.Log(0, "could not connect to broker, exiting ", cfg.Node.Network, " setup: ", token.Error().Error())
|
if token.Error() == nil {
|
||||||
if strings.Contains(token.Error().Error(), "connectex") || strings.Contains(token.Error().Error(), "i/o timeout") {
|
err = errors.New("connect timeout")
|
||||||
logger.Log(0, "connection issue detected.. pulling and restarting daemon")
|
} else {
|
||||||
Pull(cfg.Node.Network, true)
|
err = token.Error()
|
||||||
|
}
|
||||||
|
logger.Log(0, "could not connect to broker", cfg.Server.Server, err.Error())
|
||||||
|
if strings.Contains(err.Error(), "connectex") || strings.Contains(err.Error(), "connect timeout") {
|
||||||
|
logger.Log(0, "connection issue detected.. attempt connection with new certs")
|
||||||
|
key, err := ssl.ReadKey(ncutils.GetNetclientPath() + ncutils.GetSeparator() + "client.key")
|
||||||
|
if err != nil {
|
||||||
|
_, *key, err = ed25519.GenerateKey(rand.Reader)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("could not generate new key")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
RegisterWithServer(key, cfg)
|
||||||
daemon.Restart()
|
daemon.Restart()
|
||||||
}
|
}
|
||||||
return client
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
time.Sleep(2 * time.Second)
|
|
||||||
}
|
}
|
||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
@@ -76,7 +76,7 @@ func Checkin(ctx context.Context, wg *sync.WaitGroup) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := PingServer(&nodeCfg); err != nil {
|
if err := PingServer(&nodeCfg); err != nil {
|
||||||
logger.Log(0, "could not ping server for , ", nodeCfg.Network, "\n", err.Error())
|
logger.Log(0, "could not ping server for", nodeCfg.Network, nodeCfg.Server.Server+"\n", err.Error())
|
||||||
} else {
|
} else {
|
||||||
Hello(&nodeCfg)
|
Hello(&nodeCfg)
|
||||||
}
|
}
|
||||||
@@ -128,17 +128,25 @@ func publish(nodeCfg *config.ClientConfig, dest string, msg []byte, qos byte) er
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
client := setupMQTT(nodeCfg, "", true)
|
client := setupMQTT(nodeCfg, true)
|
||||||
defer client.Disconnect(250)
|
defer client.Disconnect(250)
|
||||||
encrypted, err := ncutils.Chunk(msg, serverPubKey, trafficPrivKey)
|
encrypted, err := ncutils.Chunk(msg, serverPubKey, trafficPrivKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if token := client.Publish(dest, qos, false, encrypted); token.Wait() && token.Error() != nil {
|
if token := client.Publish(dest, qos, false, encrypted); !token.WaitTimeout(30*time.Second) || token.Error() != nil {
|
||||||
|
logger.Log(0, "could not connect to broker at "+nodeCfg.Server.Server+":8883")
|
||||||
|
var err error
|
||||||
|
if token.Error() == nil {
|
||||||
|
err = errors.New("connection timeout")
|
||||||
|
} else {
|
||||||
|
err = token.Error()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
return token.Error()
|
return token.Error()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user