moving around some logic

This commit is contained in:
afeiszli
2022-05-30 12:54:30 -04:00
parent 0865a535c7
commit bad2a0faea
2 changed files with 25 additions and 20 deletions

View File

@@ -25,7 +25,6 @@ import (
"github.com/gravitl/netmaker/netclient/local" "github.com/gravitl/netmaker/netclient/local"
"github.com/gravitl/netmaker/netclient/ncutils" "github.com/gravitl/netmaker/netclient/ncutils"
"github.com/gravitl/netmaker/netclient/wireguard" "github.com/gravitl/netmaker/netclient/wireguard"
"github.com/gravitl/netmaker/servercfg"
ssl "github.com/gravitl/netmaker/tls" ssl "github.com/gravitl/netmaker/tls"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" "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 // this function is primarily used to create a connection to publish to the broker
func setupMQTT(cfg *config.ClientConfig, publish bool) (mqtt.Client, error) { func setupMQTT(cfg *config.ClientConfig, publish bool) (mqtt.Client, error) {
opts := mqtt.NewClientOptions() opts := mqtt.NewClientOptions()
if cfg.Server.Server == "" || cfg.Server.BrokerPort == "" {
reRegisterWithServer(cfg)
}
server := cfg.Server.Server server := cfg.Server.Server
opts.AddBroker("ssl://" + server + ":" + servercfg.GetMQPort()) port := cfg.Server.BrokerPort
opts.AddBroker("ssl://" + server + ":" + port)
opts.SetTLSConfig(NewTLSConfig(server)) opts.SetTLSConfig(NewTLSConfig(server))
opts.SetClientID(ncutils.MakeRandomString(23)) opts.SetClientID(ncutils.MakeRandomString(23))
opts.SetDefaultPublishHandler(All) opts.SetDefaultPublishHandler(All)
@@ -248,25 +251,30 @@ func setupMQTT(cfg *config.ClientConfig, publish bool) (mqtt.Client, error) {
} else { } else {
err = token.Error() err = token.Error()
} }
if err = checkBroker(cfg.Server.Server, cfg.Server.BrokerPort); err != nil && if err := checkBroker(cfg.Server.Server, cfg.Server.BrokerPort); err != nil {
(strings.Contains(err.Error(), "connectex") || return nil, err
strings.Contains(err.Error(), "connect timeout")) || }
strings.Contains(err.Error(), EMPTY_BROKER_ERR) { logger.Log(0, "could not connect to broker", cfg.Server.Server, err.Error())
logger.Log(0, "connection issue detected.. attempt connection with new certs") if strings.Contains(err.Error(), "connectex") || strings.Contains(err.Error(), "connect timeout") {
key, err := ssl.ReadKey(ncutils.GetNetclientPath() + ncutils.GetSeparator() + "client.key") reRegisterWithServer(cfg)
if err != nil {
_, *key, err = ed25519.GenerateKey(rand.Reader)
if err != nil {
log.Fatal("could not generate new key")
}
}
RegisterWithServer(key, cfg)
daemon.Restart()
} }
} }
return client, nil 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 // publishes a message to server to update peers on this peer's behalf
func publishSignal(nodeCfg *config.ClientConfig, signal byte) error { func publishSignal(nodeCfg *config.ClientConfig, signal byte) error {
if err := publish(nodeCfg, fmt.Sprintf("signal/%s", nodeCfg.Node.ID), []byte{signal}, 1); err != nil { if err := publish(nodeCfg, fmt.Sprintf("signal/%s", nodeCfg.Node.ID), []byte{signal}, 1); err != nil {

View File

@@ -22,9 +22,6 @@ import (
// pubNetworks hold the currently publishable networks // pubNetworks hold the currently publishable networks
var pubNetworks []string 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 // 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 // if there are no updates, simply "pings" the server as a checkin
func Checkin(ctx context.Context, wg *sync.WaitGroup) { 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 { func checkBroker(broker string, port string) error {
if broker == "" { if broker == "" {
return errors.New(EMPTY_BROKER_ERR) return errors.New("error: broker address is blank")
} }
_, err := net.LookupIP(broker) _, err := net.LookupIP(broker)
if err != nil { if err != nil {