From 1cb42e8f8bf8904bca1cd6a5bff9fc592fe61757 Mon Sep 17 00:00:00 2001 From: cameronts Date: Wed, 27 Jul 2022 16:45:12 -0700 Subject: [PATCH 1/6] Add Public IP Service handling to config and GetPublicIP(). --- netclient/cli_options/flags.go | 7 +++++++ netclient/config/config.go | 7 +++++++ netclient/functions/join.go | 2 +- netclient/functions/mqpublish.go | 2 +- netclient/ncutils/netclientutils.go | 13 +++++++++++-- 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/netclient/cli_options/flags.go b/netclient/cli_options/flags.go index 9830e281..50ec8725 100644 --- a/netclient/cli_options/flags.go +++ b/netclient/cli_options/flags.go @@ -66,6 +66,13 @@ func GetFlags(hostname string) []cli.Flag { Value: "", Usage: "Identifiable name for machine within Netmaker network.", }, + &cli.StringFlag{ + Name: "publicipservice", + Aliases: []string{"ip-service"}, + EnvVars: []string{"NETCLIENT_IP_SERVICE"}, + Value: "", + Usage: "The service to call to obtain the public IP of the machine that is running netclient.", + }, &cli.StringFlag{ Name: "name", EnvVars: []string{"NETCLIENT_NAME"}, diff --git a/netclient/config/config.go b/netclient/config/config.go index 916203e1..e5c31b85 100644 --- a/netclient/config/config.go +++ b/netclient/config/config.go @@ -26,12 +26,18 @@ type ClientConfig struct { Server models.ServerConfig `yaml:"server"` Node models.Node `yaml:"node"` NetworkSettings models.Network `yaml:"networksettings"` + GlobalSettings GlobalSettings `yaml:"globalSettings"` Network string `yaml:"network"` Daemon string `yaml:"daemon"` OperatingSystem string `yaml:"operatingsystem"` AccessKey string `yaml:"accesskey"` } +// GlobalSettings - settings that apply for the netclient across networks +type GlobalSettings struct { + PublicIPService string `yaml:"publicIPService"` +} + // RegisterRequest - struct for registation with netmaker server type RegisterRequest struct { Key ed25519.PrivateKey @@ -231,6 +237,7 @@ func GetCLIConfig(c *cli.Context) (ClientConfig, string, error) { cfg.Server.CoreDNSAddr = c.String("corednsaddr") cfg.Server.API = c.String("apiserver") } + cfg.GlobalSettings.PublicIPService = c.String("publicipservice") cfg.Node.Name = c.String("name") cfg.Node.Interface = c.String("interface") cfg.Node.Password = c.String("password") diff --git a/netclient/functions/join.go b/netclient/functions/join.go index 1d69fcfd..13a4797e 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.Node.Endpoint, err = ncutils.GetPublicIP(cfg.GlobalSettings.PublicIPService) } 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 4f90f2ed..4757db47 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() + extIP, err := ncutils.GetPublicIP(nodeCfg.GlobalSettings.PublicIPService) if err != nil { logger.Log(1, "error encountered checking public ip addresses: ", err.Error()) } diff --git a/netclient/ncutils/netclientutils.go b/netclient/ncutils/netclientutils.go index 72a4bb72..b25bc556 100644 --- a/netclient/ncutils/netclientutils.go +++ b/netclient/ncutils/netclientutils.go @@ -126,12 +126,20 @@ func IsEmptyRecord(err error) bool { } // GetPublicIP - gets public ip -func GetPublicIP() (string, error) { +func GetPublicIP(publicIpService string) (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) + + // prepend the user-specified service so it's checked first + iplist = append([]string{publicIpService}, iplist...) + } - iplist := []string{"https://ip.client.gravitl.com", "https://ifconfig.me", "https://api.ipify.org", "https://ipinfo.io/ip"} endpoint := "" var err error for _, ipserver := range iplist { + logger.Log(3, "Running public IP check with service", ipserver) client := &http.Client{ Timeout: time.Second * 10, } @@ -146,6 +154,7 @@ func GetPublicIP() (string, error) { continue } endpoint = string(bodyBytes) + logger.Log(3, "Public IP address is", endpoint) break } } From 05283eff149b3550860f78183f4a7adb56a3477f Mon Sep 17 00:00:00 2001 From: cameronts Date: Thu, 28 Jul 2022 12:50:53 -0700 Subject: [PATCH 2/6] Change to single variable in ClientConfig (no nested GlobalSettings). --- netclient/config/config.go | 9 ++------- netclient/functions/join.go | 2 +- netclient/functions/mqpublish.go | 2 +- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/netclient/config/config.go b/netclient/config/config.go index e5c31b85..714c40ab 100644 --- a/netclient/config/config.go +++ b/netclient/config/config.go @@ -26,16 +26,11 @@ type ClientConfig struct { Server models.ServerConfig `yaml:"server"` Node models.Node `yaml:"node"` NetworkSettings models.Network `yaml:"networksettings"` - GlobalSettings GlobalSettings `yaml:"globalSettings"` Network string `yaml:"network"` Daemon string `yaml:"daemon"` OperatingSystem string `yaml:"operatingsystem"` AccessKey string `yaml:"accesskey"` -} - -// GlobalSettings - settings that apply for the netclient across networks -type GlobalSettings struct { - PublicIPService string `yaml:"publicIPService"` + PublicIPService string `yaml:"publicipservice"` } // RegisterRequest - struct for registation with netmaker server @@ -237,7 +232,7 @@ func GetCLIConfig(c *cli.Context) (ClientConfig, string, error) { cfg.Server.CoreDNSAddr = c.String("corednsaddr") cfg.Server.API = c.String("apiserver") } - cfg.GlobalSettings.PublicIPService = c.String("publicipservice") + cfg.PublicIPService = c.String("publicipservice") cfg.Node.Name = c.String("name") cfg.Node.Interface = c.String("interface") cfg.Node.Password = c.String("password") diff --git a/netclient/functions/join.go b/netclient/functions/join.go index 13a4797e..2695ff12 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.GlobalSettings.PublicIPService) + cfg.Node.Endpoint, err = ncutils.GetPublicIP(cfg.PublicIPService) } 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 4757db47..a53756e9 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.GlobalSettings.PublicIPService) + extIP, err := ncutils.GetPublicIP(nodeCfg.PublicIPService) if err != nil { logger.Log(1, "error encountered checking public ip addresses: ", err.Error()) } From f656a48f3aa3931d09d6d10db08894a5ac901bfa Mon Sep 17 00:00:00 2001 From: cameronts Date: Thu, 28 Jul 2022 14:33:47 -0700 Subject: [PATCH 3/6] 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 := "" From 3dcce87868c5de4e93409067b73b6421c43a4267 Mon Sep 17 00:00:00 2001 From: cameronts Date: Thu, 28 Jul 2022 14:57:20 -0700 Subject: [PATCH 4/6] Fixed an old reference. --- netclient/functions/daemon.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/netclient/functions/daemon.go b/netclient/functions/daemon.go index 870d9f53..0b6ef9cb 100644 --- a/netclient/functions/daemon.go +++ b/netclient/functions/daemon.go @@ -8,6 +8,7 @@ import ( "crypto/x509" "errors" "fmt" + "github.com/gravitl/netmaker/netclient/global_settings" "log" "os" "os/signal" @@ -98,7 +99,7 @@ func startGoRoutines(wg *sync.WaitGroup) context.CancelFunc { } server := cfg.Server.Server if cfg.PublicIPService != "" { - config.PublicIPServices[server] = cfg.PublicIPService + global_settings.PublicIPServices[server] = cfg.PublicIPService } if !serverSet[server] { // == subscribe to all nodes for each on machine == From 3f0f2c88cae4f12233dddf9b36f12805ccf7081d Mon Sep 17 00:00:00 2001 From: cameronts Date: Fri, 29 Jul 2022 12:10:42 -0700 Subject: [PATCH 5/6] Updated with PR review comments. --- netclient/functions/daemon.go | 5 +++-- netclient/global_settings/globalsettings.go | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/netclient/functions/daemon.go b/netclient/functions/daemon.go index 0b6ef9cb..47b20645 100644 --- a/netclient/functions/daemon.go +++ b/netclient/functions/daemon.go @@ -97,10 +97,11 @@ func startGoRoutines(wg *sync.WaitGroup) context.CancelFunc { if err := wireguard.ApplyConf(&cfg.Node, cfg.Node.Interface, ncutils.GetNetclientPathSpecific()+cfg.Node.Interface+".conf"); err != nil { logger.Log(0, "failed to start ", cfg.Node.Interface, "wg interface", err.Error()) } - server := cfg.Server.Server if cfg.PublicIPService != "" { - global_settings.PublicIPServices[server] = cfg.PublicIPService + global_settings.PublicIPServices[network] = cfg.PublicIPService } + + server := cfg.Server.Server if !serverSet[server] { // == subscribe to all nodes for each on machine == serverSet[server] = true diff --git a/netclient/global_settings/globalsettings.go b/netclient/global_settings/globalsettings.go index 872ea95e..192c884f 100644 --- a/netclient/global_settings/globalsettings.go +++ b/netclient/global_settings/globalsettings.go @@ -2,4 +2,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 +// PublicIPServices - the list of user-specified IP services to use to obtain the node's public IP +var PublicIPServices map[string]string = make(map[string]string) From b9b79ed9f965edf929adb1433e99f22e8f539651 Mon Sep 17 00:00:00 2001 From: cameronts Date: Tue, 2 Aug 2022 16:25:38 -0700 Subject: [PATCH 6/6] Changed order of imports. --- netclient/config/config.go | 2 +- netclient/functions/daemon.go | 2 +- netclient/ncutils/netclientutils.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/netclient/config/config.go b/netclient/config/config.go index a5b3fb74..1fc51545 100644 --- a/netclient/config/config.go +++ b/netclient/config/config.go @@ -6,7 +6,6 @@ import ( "crypto/x509/pkix" "errors" "fmt" - "github.com/gravitl/netmaker/netclient/ncutils" "log" "os" "sync" @@ -14,6 +13,7 @@ import ( "github.com/gravitl/netmaker/logger" "github.com/gravitl/netmaker/models" "github.com/gravitl/netmaker/netclient/global_settings" + "github.com/gravitl/netmaker/netclient/ncutils" "github.com/urfave/cli/v2" "gopkg.in/yaml.v3" ) diff --git a/netclient/functions/daemon.go b/netclient/functions/daemon.go index 47b20645..1d277402 100644 --- a/netclient/functions/daemon.go +++ b/netclient/functions/daemon.go @@ -8,7 +8,6 @@ import ( "crypto/x509" "errors" "fmt" - "github.com/gravitl/netmaker/netclient/global_settings" "log" "os" "os/signal" @@ -23,6 +22,7 @@ import ( "github.com/gravitl/netmaker/netclient/auth" "github.com/gravitl/netmaker/netclient/config" "github.com/gravitl/netmaker/netclient/daemon" + "github.com/gravitl/netmaker/netclient/global_settings" "github.com/gravitl/netmaker/netclient/local" "github.com/gravitl/netmaker/netclient/ncutils" "github.com/gravitl/netmaker/netclient/wireguard" diff --git a/netclient/ncutils/netclientutils.go b/netclient/ncutils/netclientutils.go index 1a0a7f4c..dff41878 100644 --- a/netclient/ncutils/netclientutils.go +++ b/netclient/ncutils/netclientutils.go @@ -6,7 +6,6 @@ import ( "encoding/gob" "errors" "fmt" - "github.com/gravitl/netmaker/netclient/global_settings" "io" "log" "net" @@ -23,6 +22,7 @@ import ( "github.com/c-robinson/iplib" "github.com/gravitl/netmaker/logger" "github.com/gravitl/netmaker/models" + "github.com/gravitl/netmaker/netclient/global_settings" ) var (