Browse Source

changing StunList to slice

afeiszli 2 years ago
parent
commit
bf8a5bbc69
3 changed files with 62 additions and 21 deletions
  1. 0 1
      config/config.go
  2. 19 14
      models/structs.go
  3. 43 6
      servercfg/serverconf.go

+ 0 - 1
config/config.go

@@ -72,7 +72,6 @@ type ServerConfig struct {
 	NetmakerAccountID    string `yaml:"netmaker_account_id"`
 	IsEE                 string `yaml:"is_ee"`
 	StunPort             int    `yaml:"stun_port"`
-	StunList             string `yaml:"stun_list"`
 	Proxy                string `yaml:"proxy"`
 }
 

+ 19 - 14
models/structs.go

@@ -223,20 +223,20 @@ type NodeJoinResponse struct {
 
 // ServerConfig - struct for dealing with the server information for a netclient
 type ServerConfig struct {
-	CoreDNSAddr string `yaml:"corednsaddr"`
-	API         string `yaml:"api"`
-	APIPort     string `yaml:"apiport"`
-	DNSMode     string `yaml:"dnsmode"`
-	Version     string `yaml:"version"`
-	MQPort      string `yaml:"mqport"`
-	MQUserName  string `yaml:"mq_username"`
-	MQPassword  string `yaml:"mq_password"`
-	Server      string `yaml:"server"`
-	Broker      string `yaml:"broker"`
-	Is_EE       bool   `yaml:"isee"`
-	StunPort    int    `yaml:"stun_port"`
-	StunList    string `yaml:"stun_list"`
-	TrafficKey  []byte `yaml:"traffickey"`
+	CoreDNSAddr string       `yaml:"corednsaddr"`
+	API         string       `yaml:"api"`
+	APIPort     string       `yaml:"apiport"`
+	DNSMode     string       `yaml:"dnsmode"`
+	Version     string       `yaml:"version"`
+	MQPort      string       `yaml:"mqport"`
+	MQUserName  string       `yaml:"mq_username"`
+	MQPassword  string       `yaml:"mq_password"`
+	Server      string       `yaml:"server"`
+	Broker      string       `yaml:"broker"`
+	Is_EE       bool         `yaml:"isee"`
+	StunPort    int          `yaml:"stun_port"`
+	StunList    []StunServer `yaml:"stun_list"`
+	TrafficKey  []byte       `yaml:"traffickey"`
 }
 
 // User.NameInCharset - returns if name is in charset below or not
@@ -261,3 +261,8 @@ type JoinData struct {
 	Node Node   `json:"node" yaml:"node"`
 	Key  string `json:"key" yaml:"key"`
 }
+
+type StunServer struct {
+	Domain string `json:"domain" yaml:"domain"`
+	Port   int    `json:"port" yaml:"port"`
+}

+ 43 - 6
servercfg/serverconf.go

@@ -44,7 +44,6 @@ func GetServerConfig() config.ServerConfig {
 	cfg.RestBackend = "off"
 	cfg.NodeID = GetNodeID()
 	cfg.StunPort = GetStunPort()
-	cfg.StunList = GetStunList()
 	cfg.BrokerType = GetBrokerType()
 	cfg.EmqxRestEndpoint = GetEmqxRestEndpoint()
 	if IsRestBackend() {
@@ -178,12 +177,22 @@ func GetAPIPort() string {
 }
 
 // GetStunAddr - gets the stun host address
-func GetStunList() string {
-	stunList := "stun1.netmaker.io:3478,stun2.netmaker.io:3478"
+func GetStunList() []models.StunServer {
+	stunList := []models.StunServer{
+		models.StunServer{
+			Domain: "stun1.netmaker.io",
+			Port:   3478,
+		},
+		models.StunServer{
+			Domain: "stun2.netmaker.io",
+			Port:   3478,
+		},
+	}
 	if os.Getenv("STUN_LIST") != "" {
-		stunList = os.Getenv("STUN_LIST")
-	} else if config.Config.Server.StunList != "" {
-		stunList = config.Config.Server.StunList
+		stuns, err := parseStunList(os.Getenv("STUN_LIST"))
+		if err == nil {
+			stunList = stuns
+		}
 	}
 	return stunList
 }
@@ -604,3 +613,31 @@ func IsProxyEnabled() bool {
 	}
 	return enabled
 }
+
+func parseStunList(stunString string) ([]models.StunServer, error) {
+	var err error
+	stunServers := []models.StunServer{}
+	stuns := strings.Split(stunString, ",")
+	if len(stuns) == 0 {
+		return stunServers, errors.New("no stun servers provided")
+	}
+	for _, stun := range stuns {
+		stunInfo := strings.Split(stun, ":")
+		if len(stunInfo) != 2 {
+			continue
+		}
+		port, err := strconv.Atoi(stunInfo[1])
+		if err != nil || port == 0 {
+			continue
+		}
+		stunServers = append(stunServers, models.StunServer{
+			Domain: stunInfo[0],
+			Port:   port,
+		})
+
+	}
+	if len(stunServers) == 0 {
+		err = errors.New("no stun entries parsable")
+	}
+	return stunServers, err
+}