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.

This commit is contained in:
cameronts 2022-07-28 14:33:47 -07:00
parent 05283eff14
commit f656a48f3a
6 changed files with 21 additions and 7 deletions

View file

@ -6,13 +6,14 @@ import (
"crypto/x509/pkix" "crypto/x509/pkix"
"errors" "errors"
"fmt" "fmt"
"github.com/gravitl/netmaker/netclient/ncutils"
"log" "log"
"os" "os"
"sync" "sync"
"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/ncutils" "github.com/gravitl/netmaker/netclient/global_settings"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -233,6 +234,9 @@ func GetCLIConfig(c *cli.Context) (ClientConfig, string, error) {
cfg.Server.API = c.String("apiserver") cfg.Server.API = c.String("apiserver")
} }
cfg.PublicIPService = c.String("publicipservice") 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")

View file

@ -97,6 +97,9 @@ func startGoRoutines(wg *sync.WaitGroup) context.CancelFunc {
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())
} }
server := cfg.Server.Server server := cfg.Server.Server
if cfg.PublicIPService != "" {
config.PublicIPServices[server] = cfg.PublicIPService
}
if !serverSet[server] { if !serverSet[server] {
// == subscribe to all nodes for each on machine == // == subscribe to all nodes for each on machine ==
serverSet[server] = true serverSet[server] = true

View file

@ -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.PublicIPService) cfg.Node.Endpoint, err = ncutils.GetPublicIP()
} }
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.")

View file

@ -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(nodeCfg.PublicIPService) extIP, err := ncutils.GetPublicIP()
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())
} }

View file

@ -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

View file

@ -6,6 +6,7 @@ import (
"encoding/gob" "encoding/gob"
"errors" "errors"
"fmt" "fmt"
"github.com/gravitl/netmaker/netclient/global_settings"
"io" "io"
"log" "log"
"net" "net"
@ -126,14 +127,15 @@ func IsEmptyRecord(err error) bool {
} }
// GetPublicIP - gets public ip // 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"} 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 // prepend the user-specified service so it's checked first
iplist = append([]string{publicIpService}, iplist...) iplist = append([]string{ipService}, iplist...)
} }
endpoint := "" endpoint := ""