fixed some bugs around join..

This commit is contained in:
0xdcarns 2022-02-18 20:21:03 -05:00
parent c89ad8d3ce
commit 7bf716429f
3 changed files with 52 additions and 49 deletions

View file

@ -7,16 +7,11 @@ import (
"github.com/gravitl/netmaker/netclient/daemon"
"github.com/gravitl/netmaker/netclient/functions"
"github.com/gravitl/netmaker/netclient/ncutils"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
// JoinCommsNetwork -- Join the message queue comms network
func JoinCommsNetwork(cfg config.ClientConfig) error {
key, err := wgtypes.GeneratePrivateKey()
if err != nil {
return err
}
if err := functions.JoinNetwork(cfg, key.PublicKey().String()); err != nil {
if err := functions.JoinNetwork(cfg, "", true); err != nil {
return err
}
return nil
@ -28,14 +23,18 @@ func Join(cfg config.ClientConfig, privateKey string) error {
//check if comms network exists
var commsCfg config.ClientConfig
commsCfg.Network = cfg.Server.CommsNetwork
commsCfg.Node.Network = cfg.Server.CommsNetwork
commsCfg.Server.AccessKey = cfg.Server.AccessKey
commsCfg.Server.GRPCAddress = cfg.Server.GRPCAddress
commsCfg.Server.GRPCSSL = cfg.Server.GRPCSSL
commsCfg.Server.CoreDNSAddr = cfg.Server.CoreDNSAddr
commsCfg.ReadConfig()
if commsCfg.Node.Name == "" {
if err := JoinCommsNetwork(commsCfg); err != nil {
ncutils.Log("could not join comms network " + err.Error())
return err
}
}
//ensure comms network is reachable
} else { // check if comms is currently reachable
if err := functions.PingServer(&commsCfg); err != nil {
if err := functions.LeaveNetwork(commsCfg.Network); err != nil {
ncutils.Log("could not leave comms network " + err.Error())
@ -46,8 +45,10 @@ func Join(cfg config.ClientConfig, privateKey string) error {
return err
}
}
}
//join network
err = functions.JoinNetwork(cfg, privateKey)
err = functions.JoinNetwork(cfg, privateKey, false)
if err != nil && !cfg.DebugOn {
if !strings.Contains(err.Error(), "ALREADY_INSTALLED") {
ncutils.PrintLog("error installing: "+err.Error(), 1)

View file

@ -126,7 +126,16 @@ func setupMQTT(publish bool, networkName string) mqtt.Client {
opts.SetWriteTimeout(time.Minute)
opts.SetOnConnectHandler(func(client mqtt.Client) {
if !publish {
SetSubscriptions(client, cfg)
networks, err := ncutils.GetSystemNetworks()
if err != nil {
ncutils.Log("error retriving networks " + err.Error())
}
for _, network := range networks {
var currConf config.ClientConfig
currConf.Network = network
currConf.ReadConfig()
SetSubscriptions(client, &currConf)
}
}
})
opts.SetOrderMatters(true)
@ -182,14 +191,6 @@ func SetSubscriptions(client mqtt.Client, cfg *config.ClientConfig) {
}
ncutils.Log("subscribed to all topics for debugging purposes")
}
networks, err := ncutils.GetSystemNetworks()
if err != nil {
ncutils.Log("error retriving networks " + err.Error())
}
for _, network := range networks {
var cfg config.ClientConfig
cfg.Network = network
cfg.ReadConfig()
if token := client.Subscribe(fmt.Sprintf("update/%s/%s", cfg.Node.Network, cfg.Node.ID), 0, mqtt.MessageHandler(NodeUpdate)); token.Wait() && token.Error() != nil {
ncutils.Log(token.Error().Error())
@ -206,7 +207,6 @@ func SetSubscriptions(client mqtt.Client, cfg *config.ClientConfig) {
ncutils.Log(fmt.Sprintf("subscribed to peer updates for node %s peers/%s/%s", cfg.Node.Name, cfg.Node.Network, cfg.Node.ID))
}
}
}
// publishes a message to server to update peers on this peer's behalf
func publishSignal(cfg *config.ClientConfig, signal byte) error {

View file

@ -25,7 +25,7 @@ import (
)
// JoinNetwork - helps a client join a network
func JoinNetwork(cfg config.ClientConfig, privateKey string) error {
func JoinNetwork(cfg config.ClientConfig, privateKey string, iscomms bool) error {
if cfg.Node.Network == "" {
return errors.New("no network provided")
}
@ -250,6 +250,7 @@ func JoinNetwork(cfg config.ClientConfig, privateKey string) error {
}
}
if !iscomms {
if cfg.Daemon != "off" {
err = daemon.InstallDaemon(cfg)
}
@ -258,8 +259,9 @@ func JoinNetwork(cfg config.ClientConfig, privateKey string) error {
} else {
daemon.Restart()
}
}
return err
return nil
}
// format name appropriately. Set to blank on failure