mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-10 07:05:28 +08:00
changing StunList to slice
This commit is contained in:
parent
42bb53cc12
commit
bf8a5bbc69
3 changed files with 62 additions and 21 deletions
|
@ -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"`
|
||||
}
|
||||
|
||||
|
|
|
@ -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"`
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue