Refine nat 1to1 mapping setting (#1094)

Now only set mapping when user_external_ip enabled or node_ip is
explicitly set. If multiple local address resolved to same external
ip, only the first one will be mapped to external, avoid candidate
conflict between different clients.
This commit is contained in:
cnderrauber
2022-10-17 16:11:52 +08:00
committed by GitHub
parent 4161768530
commit 759e3bb1f2
4 changed files with 114 additions and 31 deletions
+13 -11
View File
@@ -54,17 +54,18 @@ type Config struct {
}
type RTCConfig struct {
UDPPort uint32 `yaml:"udp_port,omitempty"`
TCPPort uint32 `yaml:"tcp_port,omitempty"`
ICEPortRangeStart uint32 `yaml:"port_range_start,omitempty"`
ICEPortRangeEnd uint32 `yaml:"port_range_end,omitempty"`
NodeIP string `yaml:"node_ip,omitempty"`
STUNServers []string `yaml:"stun_servers,omitempty"`
TURNServers []TURNServer `yaml:"turn_servers,omitempty"`
UseExternalIP bool `yaml:"use_external_ip"`
UseICELite bool `yaml:"use_ice_lite,omitempty"`
Interfaces InterfacesConfig `yaml:"interfaces"`
IPs IPsConfig `yaml:"ips"`
UDPPort uint32 `yaml:"udp_port,omitempty"`
TCPPort uint32 `yaml:"tcp_port,omitempty"`
ICEPortRangeStart uint32 `yaml:"port_range_start,omitempty"`
ICEPortRangeEnd uint32 `yaml:"port_range_end,omitempty"`
NodeIP string `yaml:"node_ip,omitempty"`
NodeIPAutoGenerated bool `yaml:"-"`
STUNServers []string `yaml:"stun_servers,omitempty"`
TURNServers []TURNServer `yaml:"turn_servers,omitempty"`
UseExternalIP bool `yaml:"use_external_ip"`
UseICELite bool `yaml:"use_ice_lite,omitempty"`
Interfaces InterfacesConfig `yaml:"interfaces"`
IPs IPsConfig `yaml:"ips"`
// Number of packets to buffer for NACK
PacketBufferSize int `yaml:"packet_buffer_size,omitempty"`
@@ -313,6 +314,7 @@ func NewConfig(confString string, strictMode bool, c *cli.Context) (*Config, err
if err != nil {
return nil, err
}
conf.RTC.NodeIPAutoGenerated = true
}
if conf.LogLevel != "" {
+11 -3
View File
@@ -19,7 +19,7 @@ func (conf *Config) determineIP() (string, error) {
var err error
for i := 0; i < 3; i++ {
var ip string
ip, err = GetExternalIP(stunServers)
ip, err = GetExternalIP(stunServers, nil)
if err == nil {
return ip, nil
} else {
@@ -79,11 +79,19 @@ func GetLocalIPAddresses() ([]string, error) {
return nil, fmt.Errorf("could not find local IP address")
}
func GetExternalIP(stunServers []string) (string, error) {
// GetExternalIP return external IP for localAddr from stun server. If localAddr is nil, a local address is chosen automatically.
func GetExternalIP(stunServers []string, localAddr net.Addr) (string, error) {
if len(stunServers) == 0 {
return "", errors.New("STUN servers are required but not defined")
}
c, err := stun.Dial("udp4", stunServers[0])
dialer := &net.Dialer{
LocalAddr: localAddr,
}
conn, err := dialer.Dial("udp4", stunServers[0])
if err != nil {
return "", err
}
c, err := stun.NewClient(conn)
if err != nil {
return "", err
}