diff --git a/netclient/functions/daemon.go b/netclient/functions/daemon.go index edd97cf5..a35b10c5 100644 --- a/netclient/functions/daemon.go +++ b/netclient/functions/daemon.go @@ -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 { diff --git a/netclient/functions/mqpublish.go b/netclient/functions/mqpublish.go index 5f22b5b6..baaa70dc 100644 --- a/netclient/functions/mqpublish.go +++ b/netclient/functions/mqpublish.go @@ -22,9 +22,6 @@ import ( // pubNetworks hold the currently publishable networks var pubNetworks []string -// EMPTY_BROKER_ERR is the error to return if no broker address is provided -var EMPTY_BROKER_ERR = "error: broker address is blank" - // 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 func Checkin(ctx context.Context, wg *sync.WaitGroup) { @@ -170,7 +167,7 @@ func checkCertExpiry(cfg *config.ClientConfig) error { func checkBroker(broker string, port string) error { if broker == "" { - return errors.New(EMPTY_BROKER_ERR) + return errors.New("error: broker address is blank") } _, err := net.LookupIP(broker) if err != nil {