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:
+90
-14
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/pion/ice/v2"
|
||||
"github.com/pion/sdp/v3"
|
||||
@@ -28,7 +29,6 @@ type WebRTCConfig struct {
|
||||
Receiver ReceiverConfig
|
||||
BufferFactory *buffer.Factory
|
||||
UDPMux ice.UDPMux
|
||||
UDPMuxConn *net.UDPConn
|
||||
TCPMuxListener *net.TCPListener
|
||||
Publisher DirectionConfig
|
||||
Subscriber DirectionConfig
|
||||
@@ -70,8 +70,17 @@ func NewWebRTCConfig(conf *config.Config, externalIP string) (*WebRTCConfig, err
|
||||
}
|
||||
|
||||
// force it to the node IPs that the user has set
|
||||
if externalIP != "" && (conf.RTC.UseExternalIP || conf.RTC.NodeIP != "") {
|
||||
s.SetNAT1To1IPs([]string{externalIP}, webrtc.ICECandidateTypeHost)
|
||||
if externalIP != "" && (conf.RTC.UseExternalIP || (conf.RTC.NodeIP != "" && !conf.RTC.NodeIPAutoGenerated)) {
|
||||
if conf.RTC.UseExternalIP {
|
||||
ips, err := getNAT1to1IPsForConf(conf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
logger.Debugw("using external IPs", "ips", ips)
|
||||
s.SetNAT1To1IPs(ips, webrtc.ICECandidateTypeHost)
|
||||
} else {
|
||||
s.SetNAT1To1IPs([]string{externalIP}, webrtc.ICECandidateTypeHost)
|
||||
}
|
||||
}
|
||||
|
||||
if rtcConf.PacketBufferSize == 0 {
|
||||
@@ -79,7 +88,6 @@ func NewWebRTCConfig(conf *config.Config, externalIP string) (*WebRTCConfig, err
|
||||
}
|
||||
|
||||
var udpMux ice.UDPMux
|
||||
var udpMuxConn *net.UDPConn
|
||||
var err error
|
||||
networkTypes := make([]webrtc.NetworkType, 0, 4)
|
||||
|
||||
@@ -92,19 +100,16 @@ func NewWebRTCConfig(conf *config.Config, externalIP string) (*WebRTCConfig, err
|
||||
return nil, err
|
||||
}
|
||||
} else if rtcConf.UDPPort != 0 {
|
||||
udpMuxConn, err = net.ListenUDP("udp", &net.UDPAddr{
|
||||
Port: int(rtcConf.UDPPort),
|
||||
})
|
||||
udpMux, err := ice.NewMultiUDPMuxFromPort(
|
||||
int(rtcConf.UDPPort),
|
||||
ice.UDPMuxFromPortWithReadBufferSize(defaultUDPBufferSize),
|
||||
ice.UDPMuxFromPortWithWriteBufferSize(defaultUDPBufferSize),
|
||||
ice.UDPMuxFromPortWithLogger(s.LoggerFactory.NewLogger("udp_mux")),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_ = udpMuxConn.SetReadBuffer(defaultUDPBufferSize)
|
||||
_ = udpMuxConn.SetWriteBuffer(defaultUDPBufferSize)
|
||||
|
||||
udpMux = ice.NewUDPMuxDefault(ice.UDPMuxParams{
|
||||
Logger: s.LoggerFactory.NewLogger("udp_mux"),
|
||||
UDPConn: udpMuxConn,
|
||||
})
|
||||
s.SetICEUDPMux(udpMux)
|
||||
if !conf.Development {
|
||||
checkUDPReadBuffer()
|
||||
@@ -277,7 +282,6 @@ func NewWebRTCConfig(conf *config.Config, externalIP string) (*WebRTCConfig, err
|
||||
PacketBufferSize: rtcConf.PacketBufferSize,
|
||||
},
|
||||
UDPMux: udpMux,
|
||||
UDPMuxConn: udpMuxConn,
|
||||
TCPMuxListener: tcpListener,
|
||||
Publisher: publisherConfig,
|
||||
Subscriber: subscriberConfig,
|
||||
@@ -296,3 +300,75 @@ func iceServerForStunServers(servers []string) webrtc.ICEServer {
|
||||
}
|
||||
return iceServer
|
||||
}
|
||||
|
||||
func getNAT1to1IPsForConf(conf *config.Config) ([]string, error) {
|
||||
stunServers := conf.RTC.STUNServers
|
||||
if len(stunServers) == 0 {
|
||||
stunServers = config.DefaultStunServers
|
||||
}
|
||||
localIPs, err := config.GetLocalIPAddresses()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
type ipmapping struct {
|
||||
externalIP string
|
||||
localIP string
|
||||
}
|
||||
addrCh := make(chan ipmapping, len(localIPs))
|
||||
for _, ip := range localIPs {
|
||||
go func(localIP string) {
|
||||
addr, err := config.GetExternalIP(stunServers, &net.UDPAddr{IP: net.ParseIP(localIP)})
|
||||
if err != nil {
|
||||
logger.Infow("failed to get external ip", "local", localIP, "err", err)
|
||||
return
|
||||
}
|
||||
addrCh <- ipmapping{externalIP: addr, localIP: localIP}
|
||||
}(ip)
|
||||
}
|
||||
|
||||
var firstResloved bool
|
||||
natMapping := make(map[string]string)
|
||||
timeout := time.NewTimer(5 * time.Second)
|
||||
|
||||
done:
|
||||
for {
|
||||
select {
|
||||
case mapping := <-addrCh:
|
||||
if !firstResloved {
|
||||
firstResloved = true
|
||||
timeout.Reset(1 * time.Second)
|
||||
}
|
||||
if local, ok := natMapping[mapping.externalIP]; ok {
|
||||
logger.Infow("external ip already solved, ignore duplicate",
|
||||
"external", mapping.externalIP,
|
||||
"local", local,
|
||||
"ignore", mapping.localIP)
|
||||
} else {
|
||||
natMapping[mapping.externalIP] = mapping.localIP
|
||||
}
|
||||
|
||||
case <-timeout.C:
|
||||
break done
|
||||
}
|
||||
}
|
||||
|
||||
// mapping unresolved local ip to itself
|
||||
for _, local := range localIPs {
|
||||
var found bool
|
||||
for _, localIPMapping := range natMapping {
|
||||
if local == localIPMapping {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
natMapping[local] = local
|
||||
}
|
||||
}
|
||||
|
||||
nat1to1IPs := make([]string, 0, len(natMapping))
|
||||
for external, local := range natMapping {
|
||||
nat1to1IPs = append(nat1to1IPs, fmt.Sprintf("%s/%s", external, local))
|
||||
}
|
||||
return nat1to1IPs, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user