mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-02 15:46:11 +08:00
moving around some logic
This commit is contained in:
@@ -25,7 +25,6 @@ import (
|
||||
"github.com/gravitl/netmaker/netclient/local"
|
||||
"github.com/gravitl/netmaker/netclient/ncutils"
|
||||
"github.com/gravitl/netmaker/netclient/wireguard"
|
||||
"github.com/gravitl/netmaker/servercfg"
|
||||
ssl "github.com/gravitl/netmaker/tls"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
)
|
||||
@@ -209,8 +208,12 @@ func NewTLSConfig(server string) *tls.Config {
|
||||
// this function is primarily used to create a connection to publish to the broker
|
||||
func setupMQTT(cfg *config.ClientConfig, publish bool) (mqtt.Client, error) {
|
||||
opts := mqtt.NewClientOptions()
|
||||
if cfg.Server.Server == "" || cfg.Server.BrokerPort == "" {
|
||||
reRegisterWithServer(cfg)
|
||||
}
|
||||
server := cfg.Server.Server
|
||||
opts.AddBroker("ssl://" + server + ":" + servercfg.GetMQPort())
|
||||
port := cfg.Server.BrokerPort
|
||||
opts.AddBroker("ssl://" + server + ":" + port)
|
||||
opts.SetTLSConfig(NewTLSConfig(server))
|
||||
opts.SetClientID(ncutils.MakeRandomString(23))
|
||||
opts.SetDefaultPublishHandler(All)
|
||||
@@ -248,25 +251,30 @@ func setupMQTT(cfg *config.ClientConfig, publish bool) (mqtt.Client, error) {
|
||||
} else {
|
||||
err = token.Error()
|
||||
}
|
||||
if err = checkBroker(cfg.Server.Server, cfg.Server.BrokerPort); err != nil &&
|
||||
(strings.Contains(err.Error(), "connectex") ||
|
||||
strings.Contains(err.Error(), "connect timeout")) ||
|
||||
strings.Contains(err.Error(), EMPTY_BROKER_ERR) {
|
||||
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()
|
||||
if err := checkBroker(cfg.Server.Server, cfg.Server.BrokerPort); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
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") {
|
||||
reRegisterWithServer(cfg)
|
||||
}
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func reRegisterWithServer(cfg *config.ClientConfig) {
|
||||
logger.Log(0, "connection issue detected.. attempt connection with new certs and broker information")
|
||||
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()
|
||||
}
|
||||
|
||||
// publishes a message to server to update peers on this peer's behalf
|
||||
func publishSignal(nodeCfg *config.ClientConfig, signal byte) error {
|
||||
if err := publish(nodeCfg, fmt.Sprintf("signal/%s", nodeCfg.Node.ID), []byte{signal}, 1); err != nil {
|
||||
|
Reference in New Issue
Block a user