support multiple STUN servers

This commit is contained in:
David Zhao
2020-10-08 00:12:57 -07:00
parent 0a4ce6e742
commit 32af882b99
2 changed files with 11 additions and 4 deletions
+5 -3
View File
@@ -15,8 +15,8 @@ type Config struct {
MultiNode bool `yaml:"multi_node"` MultiNode bool `yaml:"multi_node"`
Development bool `yaml:"development"` Development bool `yaml:"development"`
// Stun server // STUN
StunServer string `yaml:"stun_server"` StunServers []string `yaml:"stun_servers"`
} }
func NewConfig(confString string) (*Config, error) { func NewConfig(confString string) (*Config, error) {
@@ -26,7 +26,9 @@ func NewConfig(confString string) (*Config, error) {
RTCPort: 7881, RTCPort: 7881,
UDPRangeStart: 10000, UDPRangeStart: 10000,
UDPRangeEnd: 11000, UDPRangeEnd: 11000,
StunServer: "stun.l.google.com:19302", StunServers: []string{
"stun.l.google.com:19302",
},
} }
if confString != "" { if confString != "" {
yaml.Unmarshal([]byte(confString), conf) yaml.Unmarshal([]byte(confString), conf)
+6 -1
View File
@@ -1,6 +1,8 @@
package node package node
import ( import (
"errors"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/google/wire" "github.com/google/wire"
"github.com/pion/stun" "github.com/pion/stun"
@@ -45,7 +47,10 @@ func NewLocalNode(conf *config.Config) (*Node, error) {
} }
func (n *Node) DiscoverNetworkInfo() error { func (n *Node) DiscoverNetworkInfo() error {
c, err := stun.Dial("udp", n.config.StunServer) if len(n.config.StunServers) == 0 {
return errors.New("STUN servers are required but not defined")
}
c, err := stun.Dial("udp", n.config.StunServers[0])
if err != nil { if err != nil {
return err return err
} }