Improve docker connectivity by using srflx candidates
When deployed via Docker and without using host networking, we'll be
assigned an IP behind NAT. By giving it STUN servers, it should be
connectable even without passing in `--node-ip` explicitly
@@ -2,6 +2,7 @@ package rtc
import (
"errors"
+ "fmt"
"net"
"github.com/pion/ice/v2"
@@ -205,6 +206,16 @@ func NewWebRTCConfig(conf *config.Config, externalIP string) (*WebRTCConfig, err
})
}
+ // use STUN servers for server to support NAT
+ // when deployed in production, we expect UseExternalIP to be used, and ports accessible
+ if !conf.RTC.UseExternalIP {
+ if len(conf.RTC.STUNServers) > 0 {
+ c.ICEServers = []webrtc.ICEServer{iceServerForStunServers(conf.RTC.STUNServers)}
+ } else {
+ c.ICEServers = []webrtc.ICEServer{iceServerForStunServers(config.DefaultStunServers)}
+ }
+ }
+
return &WebRTCConfig{
Configuration: c,
SettingEngine: s,
@@ -223,3 +234,11 @@ func (c *WebRTCConfig) SetBufferFactory(factory *buffer.Factory) {
c.BufferFactory = factory
c.SettingEngine.BufferFactory = factory.GetOrNew
}
+
+func iceServerForStunServers(servers []string) webrtc.ICEServer {
+ iceServer := webrtc.ICEServer{}
+ for _, stunServer := range servers {
+ iceServer.URLs = append(iceServer.URLs, fmt.Sprintf("stun:%s", stunServer))
+ }
+ return iceServer
+}
