From f656a48f3aa3931d09d6d10db08894a5ac901bfa Mon Sep 17 00:00:00 2001 From: cameronts Date: Thu, 28 Jul 2022 14:33:47 -0700 Subject: [PATCH] Move public IP services handling to a map-based approach to work for daemon (multiple network configs) and CLI-based setting of the IP services. --- netclient/config/config.go | 6 +++++- netclient/functions/daemon.go | 3 +++ netclient/functions/join.go | 2 +- netclient/functions/mqpublish.go | 2 +- netclient/global_settings/globalsettings.go | 5 +++++ netclient/ncutils/netclientutils.go | 10 ++++++---- 6 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 netclient/global_settings/globalsettings.go diff --git a/netclient/config/config.go b/netclient/config/config.go index 714c40ab..a5b3fb74 100644 --- a/netclient/config/config.go +++ b/netclient/config/config.go @@ -6,13 +6,14 @@ import ( "crypto/x509/pkix" "errors" "fmt" + "github.com/gravitl/netmaker/netclient/ncutils" "log" "os" "sync" "github.com/gravitl/netmaker/logger" "github.com/gravitl/netmaker/models" - "github.com/gravitl/netmaker/netclient/ncutils" + "github.com/gravitl/netmaker/netclient/global_settings" "github.com/urfave/cli/v2" "gopkg.in/yaml.v3" ) @@ -233,6 +234,9 @@ func GetCLIConfig(c *cli.Context) (ClientConfig, string, error) { cfg.Server.API = c.String("apiserver") } cfg.PublicIPService = c.String("publicipservice") + // populate the map as we're not running as a daemon so won't be building the map otherwise + // (and the map will be used by GetPublicIP()). + global_settings.PublicIPServices[cfg.Network] = cfg.PublicIPService cfg.Node.Name = c.String("name") cfg.Node.Interface = c.String("interface") cfg.Node.Password = c.String("password") diff --git a/netclient/functions/daemon.go b/netclient/functions/daemon.go index ee71bbc1..870d9f53 100644 --- a/netclient/functions/daemon.go +++ b/netclient/functions/daemon.go @@ -97,6 +97,9 @@ func startGoRoutines(wg *sync.WaitGroup) context.CancelFunc { logger.Log(0, "failed to start ", cfg.Node.Interface, "wg interface", err.Error()) } server := cfg.Server.Server + if cfg.PublicIPService != "" { + config.PublicIPServices[server] = cfg.PublicIPService + } if !serverSet[server] { // == subscribe to all nodes for each on machine == serverSet[server] = true diff --git a/netclient/functions/join.go b/netclient/functions/join.go index 2695ff12..1d69fcfd 100644 --- a/netclient/functions/join.go +++ b/netclient/functions/join.go @@ -85,7 +85,7 @@ func JoinNetwork(cfg *config.ClientConfig, privateKey string) error { if cfg.Node.IsLocal == "yes" && cfg.Node.LocalAddress != "" { cfg.Node.Endpoint = cfg.Node.LocalAddress } else { - cfg.Node.Endpoint, err = ncutils.GetPublicIP(cfg.PublicIPService) + cfg.Node.Endpoint, err = ncutils.GetPublicIP() } if err != nil || cfg.Node.Endpoint == "" { logger.Log(0, "network:", cfg.Network, "error setting cfg.Node.Endpoint.") diff --git a/netclient/functions/mqpublish.go b/netclient/functions/mqpublish.go index a53756e9..4f90f2ed 100644 --- a/netclient/functions/mqpublish.go +++ b/netclient/functions/mqpublish.go @@ -44,7 +44,7 @@ func checkin() { nodeCfg.Network = network nodeCfg.ReadConfig() if nodeCfg.Node.IsStatic != "yes" { - extIP, err := ncutils.GetPublicIP(nodeCfg.PublicIPService) + extIP, err := ncutils.GetPublicIP() if err != nil { logger.Log(1, "error encountered checking public ip addresses: ", err.Error()) } diff --git a/netclient/global_settings/globalsettings.go b/netclient/global_settings/globalsettings.go new file mode 100644 index 00000000..872ea95e --- /dev/null +++ b/netclient/global_settings/globalsettings.go @@ -0,0 +1,5 @@ +package global_settings + +// globalsettings - settings that are global in nature. Avoids circular dependencies between config loading and usage. + +var PublicIPServices map[string]string \ No newline at end of file diff --git a/netclient/ncutils/netclientutils.go b/netclient/ncutils/netclientutils.go index b25bc556..1a0a7f4c 100644 --- a/netclient/ncutils/netclientutils.go +++ b/netclient/ncutils/netclientutils.go @@ -6,6 +6,7 @@ import ( "encoding/gob" "errors" "fmt" + "github.com/gravitl/netmaker/netclient/global_settings" "io" "log" "net" @@ -126,14 +127,15 @@ func IsEmptyRecord(err error) bool { } // GetPublicIP - gets public ip -func GetPublicIP(publicIpService string) (string, error) { +func GetPublicIP() (string, error) { iplist := []string{"https://ip.server.gravitl.com", "https://ifconfig.me", "https://api.ipify.org", "https://ipinfo.io/ip"} - if publicIpService != "" { - logger.Log(3, "User (config file) provided public IP service is", publicIpService) + + for network, ipService := range global_settings.PublicIPServices { + logger.Log(3, "User provided public IP service defined for network", network, "is", ipService) // prepend the user-specified service so it's checked first - iplist = append([]string{publicIpService}, iplist...) + iplist = append([]string{ipService}, iplist...) } endpoint := ""