Browse Source

Add Public IP Service handling to config and GetPublicIP().

cameronts 3 years ago
parent
commit
1cb42e8f8b

+ 7 - 0
netclient/cli_options/flags.go

@@ -66,6 +66,13 @@ func GetFlags(hostname string) []cli.Flag {
 			Value:   "",
 			Value:   "",
 			Usage:   "Identifiable name for machine within Netmaker network.",
 			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{
 		&cli.StringFlag{
 			Name:    "name",
 			Name:    "name",
 			EnvVars: []string{"NETCLIENT_NAME"},
 			EnvVars: []string{"NETCLIENT_NAME"},

+ 7 - 0
netclient/config/config.go

@@ -26,12 +26,18 @@ type ClientConfig struct {
 	Server          models.ServerConfig `yaml:"server"`
 	Server          models.ServerConfig `yaml:"server"`
 	Node            models.Node         `yaml:"node"`
 	Node            models.Node         `yaml:"node"`
 	NetworkSettings models.Network      `yaml:"networksettings"`
 	NetworkSettings models.Network      `yaml:"networksettings"`
+	GlobalSettings  GlobalSettings      `yaml:"globalSettings"`
 	Network         string              `yaml:"network"`
 	Network         string              `yaml:"network"`
 	Daemon          string              `yaml:"daemon"`
 	Daemon          string              `yaml:"daemon"`
 	OperatingSystem string              `yaml:"operatingsystem"`
 	OperatingSystem string              `yaml:"operatingsystem"`
 	AccessKey       string              `yaml:"accesskey"`
 	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
 // RegisterRequest - struct for registation with netmaker server
 type RegisterRequest struct {
 type RegisterRequest struct {
 	Key        ed25519.PrivateKey
 	Key        ed25519.PrivateKey
@@ -231,6 +237,7 @@ func GetCLIConfig(c *cli.Context) (ClientConfig, string, error) {
 		cfg.Server.CoreDNSAddr = c.String("corednsaddr")
 		cfg.Server.CoreDNSAddr = c.String("corednsaddr")
 		cfg.Server.API = c.String("apiserver")
 		cfg.Server.API = c.String("apiserver")
 	}
 	}
+	cfg.GlobalSettings.PublicIPService = c.String("publicipservice")
 	cfg.Node.Name = c.String("name")
 	cfg.Node.Name = c.String("name")
 	cfg.Node.Interface = c.String("interface")
 	cfg.Node.Interface = c.String("interface")
 	cfg.Node.Password = c.String("password")
 	cfg.Node.Password = c.String("password")

+ 1 - 1
netclient/functions/join.go

@@ -85,7 +85,7 @@ func JoinNetwork(cfg *config.ClientConfig, privateKey string) error {
 		if cfg.Node.IsLocal == "yes" && cfg.Node.LocalAddress != "" {
 		if cfg.Node.IsLocal == "yes" && cfg.Node.LocalAddress != "" {
 			cfg.Node.Endpoint = cfg.Node.LocalAddress
 			cfg.Node.Endpoint = cfg.Node.LocalAddress
 		} else {
 		} else {
-			cfg.Node.Endpoint, err = ncutils.GetPublicIP()
+			cfg.Node.Endpoint, err = ncutils.GetPublicIP(cfg.GlobalSettings.PublicIPService)
 		}
 		}
 		if err != nil || cfg.Node.Endpoint == "" {
 		if err != nil || cfg.Node.Endpoint == "" {
 			logger.Log(0, "network:", cfg.Network, "error setting cfg.Node.Endpoint.")
 			logger.Log(0, "network:", cfg.Network, "error setting cfg.Node.Endpoint.")

+ 1 - 1
netclient/functions/mqpublish.go

@@ -44,7 +44,7 @@ func checkin() {
 		nodeCfg.Network = network
 		nodeCfg.Network = network
 		nodeCfg.ReadConfig()
 		nodeCfg.ReadConfig()
 		if nodeCfg.Node.IsStatic != "yes" {
 		if nodeCfg.Node.IsStatic != "yes" {
-			extIP, err := ncutils.GetPublicIP()
+			extIP, err := ncutils.GetPublicIP(nodeCfg.GlobalSettings.PublicIPService)
 			if err != nil {
 			if err != nil {
 				logger.Log(1, "error encountered checking public ip addresses: ", err.Error())
 				logger.Log(1, "error encountered checking public ip addresses: ", err.Error())
 			}
 			}

+ 11 - 2
netclient/ncutils/netclientutils.go

@@ -126,12 +126,20 @@ func IsEmptyRecord(err error) bool {
 }
 }
 
 
 // GetPublicIP - gets public ip
 // 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 := ""
 	endpoint := ""
 	var err error
 	var err error
 	for _, ipserver := range iplist {
 	for _, ipserver := range iplist {
+		logger.Log(3, "Running public IP check with service", ipserver)
 		client := &http.Client{
 		client := &http.Client{
 			Timeout: time.Second * 10,
 			Timeout: time.Second * 10,
 		}
 		}
@@ -146,6 +154,7 @@ func GetPublicIP() (string, error) {
 				continue
 				continue
 			}
 			}
 			endpoint = string(bodyBytes)
 			endpoint = string(bodyBytes)
+			logger.Log(3, "Public IP address is", endpoint)
 			break
 			break
 		}
 		}
 	}
 	}