Add WebRTC ICE servers config (#824)

* add ICEServers config

* remove unnecessary check
This commit is contained in:
Marco Vidonis 2023-05-27 11:27:24 +01:00 committed by GitHub
parent 1c1f47555b
commit e7087fde56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 5 deletions

View File

@ -301,6 +301,7 @@ func NewClient(cfg *ClientConfig) (cl *Client, err error) {
},
Proxy: cl.config.HTTPProxy,
WebsocketTrackerHttpHeader: cl.config.WebsocketTrackerHttpHeader,
ICEServers: cl.config.ICEServers,
DialContext: cl.config.TrackerDialContext,
OnConn: func(dc datachannel.ReadWriteCloser, dcc webtorrent.DataChannelContext) {
cl.lock()

View File

@ -181,6 +181,10 @@ type ClientConfig struct {
Callbacks Callbacks
// ICEServers defines a slice describing servers available to be used by
// ICE, such as STUN and TURN servers.
ICEServers []string
DialRateLimiter *rate.Limiter
}

View File

@ -43,6 +43,7 @@ type TrackerClient struct {
pingTicker *time.Ticker
WebsocketTrackerHttpHeader func() http.Header
ICEServers []string
}
func (me *TrackerClient) Stats() TrackerClientStats {

View File

@ -29,7 +29,6 @@ var (
s.DetachDataChannels()
return webrtc.NewAPI(webrtc.WithSettingEngine(s))
}()
config = webrtc.Configuration{ICEServers: []webrtc.ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}}}
newPeerConnectionMu sync.Mutex
)
@ -49,11 +48,14 @@ func (me *wrappedPeerConnection) Close() error {
return err
}
func newPeerConnection(logger log.Logger) (*wrappedPeerConnection, error) {
func newPeerConnection(logger log.Logger, iceServers []string) (*wrappedPeerConnection, error) {
newPeerConnectionMu.Lock()
defer newPeerConnectionMu.Unlock()
ctx, span := otel.Tracer(tracerName).Start(context.Background(), "PeerConnection")
pc, err := api.NewPeerConnection(config)
pcConfig := webrtc.Configuration{ICEServers: []webrtc.ICEServer{{URLs: iceServers}}}
pc, err := api.NewPeerConnection(pcConfig)
if err != nil {
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
@ -99,7 +101,7 @@ func (tc *TrackerClient) newOffer(
offer webrtc.SessionDescription,
err error,
) {
peerConnection, err = newPeerConnection(logger)
peerConnection, err = newPeerConnection(logger, tc.ICEServers)
if err != nil {
return
}
@ -191,7 +193,7 @@ func (tc *TrackerClient) newAnsweringPeerConnection(
) (
peerConn *wrappedPeerConnection, answer webrtc.SessionDescription, err error,
) {
peerConn, err = newPeerConnection(tc.Logger)
peerConn, err = newPeerConnection(tc.Logger, tc.ICEServers)
if err != nil {
err = fmt.Errorf("failed to create new connection: %w", err)
return

View File

@ -45,6 +45,7 @@ type websocketTrackers struct {
Proxy httpTracker.ProxyFunc
DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
WebsocketTrackerHttpHeader func() netHttp.Header
ICEServers []string
}
func (me *websocketTrackers) Get(url string, infoHash [20]byte) (*webtorrent.TrackerClient, func()) {
@ -64,6 +65,7 @@ func (me *websocketTrackers) Get(url string, infoHash [20]byte) (*webtorrent.Tra
return fmt.Sprintf("tracker client for %q: %v", url, m)
}),
WebsocketTrackerHttpHeader: me.WebsocketTrackerHttpHeader,
ICEServers: me.ICEServers,
},
}
value.TrackerClient.Start(func(err error) {