mirror of
				https://github.com/gravitl/netmaker.git
				synced 2025-11-01 00:49:04 +08:00 
			
		
		
		
	Merge pull request #1432 from gravitl/feature_v0.14.7_netclientaddvarforpublicipservice
Netclient: Add Public IP Service handling to config and GetPublicIP().
This commit is contained in:
		
						commit
						40c4a7842c
					
				
					 5 changed files with 36 additions and 1 deletions
				
			
		|  | @ -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"}, | ||||||
|  |  | ||||||
|  | @ -12,6 +12,7 @@ import ( | ||||||
| 
 | 
 | ||||||
| 	"github.com/gravitl/netmaker/logger" | 	"github.com/gravitl/netmaker/logger" | ||||||
| 	"github.com/gravitl/netmaker/models" | 	"github.com/gravitl/netmaker/models" | ||||||
|  | 	"github.com/gravitl/netmaker/netclient/global_settings" | ||||||
| 	"github.com/gravitl/netmaker/netclient/ncutils" | 	"github.com/gravitl/netmaker/netclient/ncutils" | ||||||
| 	"github.com/urfave/cli/v2" | 	"github.com/urfave/cli/v2" | ||||||
| 	"gopkg.in/yaml.v3" | 	"gopkg.in/yaml.v3" | ||||||
|  | @ -30,6 +31,7 @@ type ClientConfig struct { | ||||||
| 	Daemon          string              `yaml:"daemon"` | 	Daemon          string              `yaml:"daemon"` | ||||||
| 	OperatingSystem string              `yaml:"operatingsystem"` | 	OperatingSystem string              `yaml:"operatingsystem"` | ||||||
| 	AccessKey       string              `yaml:"accesskey"` | 	AccessKey       string              `yaml:"accesskey"` | ||||||
|  | 	PublicIPService string              `yaml:"publicipservice"` | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // RegisterRequest - struct for registation with netmaker server | // RegisterRequest - struct for registation with netmaker server | ||||||
|  | @ -231,6 +233,10 @@ 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.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.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") | ||||||
|  |  | ||||||
|  | @ -22,6 +22,7 @@ import ( | ||||||
| 	"github.com/gravitl/netmaker/netclient/auth" | 	"github.com/gravitl/netmaker/netclient/auth" | ||||||
| 	"github.com/gravitl/netmaker/netclient/config" | 	"github.com/gravitl/netmaker/netclient/config" | ||||||
| 	"github.com/gravitl/netmaker/netclient/daemon" | 	"github.com/gravitl/netmaker/netclient/daemon" | ||||||
|  | 	"github.com/gravitl/netmaker/netclient/global_settings" | ||||||
| 	"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" | ||||||
|  | @ -96,6 +97,10 @@ func startGoRoutines(wg *sync.WaitGroup) context.CancelFunc { | ||||||
| 		if err := wireguard.ApplyConf(&cfg.Node, cfg.Node.Interface, ncutils.GetNetclientPathSpecific()+cfg.Node.Interface+".conf"); err != nil { | 		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()) | 			logger.Log(0, "failed to start ", cfg.Node.Interface, "wg interface", err.Error()) | ||||||
| 		} | 		} | ||||||
|  | 		if cfg.PublicIPService != "" { | ||||||
|  | 			global_settings.PublicIPServices[network] = cfg.PublicIPService | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
| 		server := cfg.Server.Server | 		server := cfg.Server.Server | ||||||
| 		if !serverSet[server] { | 		if !serverSet[server] { | ||||||
| 			// == subscribe to all nodes for each on machine == | 			// == subscribe to all nodes for each on machine == | ||||||
|  |  | ||||||
							
								
								
									
										6
									
								
								netclient/global_settings/globalsettings.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								netclient/global_settings/globalsettings.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,6 @@ | ||||||
|  | package global_settings | ||||||
|  | 
 | ||||||
|  | // globalsettings - settings that are global in nature.  Avoids circular dependencies between config loading and usage. | ||||||
|  | 
 | ||||||
|  | // 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) | ||||||
|  | @ -22,6 +22,7 @@ import ( | ||||||
| 	"github.com/c-robinson/iplib" | 	"github.com/c-robinson/iplib" | ||||||
| 	"github.com/gravitl/netmaker/logger" | 	"github.com/gravitl/netmaker/logger" | ||||||
| 	"github.com/gravitl/netmaker/models" | 	"github.com/gravitl/netmaker/models" | ||||||
|  | 	"github.com/gravitl/netmaker/netclient/global_settings" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| var ( | var ( | ||||||
|  | @ -128,10 +129,19 @@ func IsEmptyRecord(err error) bool { | ||||||
| // GetPublicIP - gets public ip | // GetPublicIP - gets public ip | ||||||
| func GetPublicIP() (string, error) { | func GetPublicIP() (string, error) { | ||||||
| 
 | 
 | ||||||
| 	iplist := []string{"https://ip.client.gravitl.com", "https://ifconfig.me", "https://api.ipify.org", "https://ipinfo.io/ip"} | 	iplist := []string{"https://ip.server.gravitl.com", "https://ifconfig.me", "https://api.ipify.org", "https://ipinfo.io/ip"} | ||||||
|  | 
 | ||||||
|  | 	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{ipService}, iplist...) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	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 +156,7 @@ func GetPublicIP() (string, error) { | ||||||
| 				continue | 				continue | ||||||
| 			} | 			} | ||||||
| 			endpoint = string(bodyBytes) | 			endpoint = string(bodyBytes) | ||||||
|  | 			logger.Log(3, "Public IP address is", endpoint) | ||||||
| 			break | 			break | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue