From 1830d852ac651c36d62de1dd9f3c68eefd5f7049 Mon Sep 17 00:00:00 2001 From: Marco Vidonis <31407403+marcovidonis@users.noreply.github.com> Date: Tue, 26 Mar 2024 09:42:08 +0000 Subject: [PATCH] Support ICE servers auth (#920) * change ICEServers type in config to Pion-defined type This allows specifying credentials for access to servers, in particular TURN servers. * add legacy support for ICEServers as []string --- client.go | 20 +++++++++++++++++++- config.go | 8 +++++++- go.sum | 2 -- webtorrent/tracker-client.go | 2 +- webtorrent/transport.go | 4 ++-- wstracker.go | 3 ++- 6 files changed, 31 insertions(+), 8 deletions(-) diff --git a/client.go b/client.go index 66abeb84..43a8fc46 100644 --- a/client.go +++ b/client.go @@ -35,6 +35,7 @@ import ( "github.com/dustin/go-humanize" gbtree "github.com/google/btree" "github.com/pion/datachannel" + "github.com/pion/webrtc/v3" "github.com/anacrolix/torrent/bencode" "github.com/anacrolix/torrent/internal/check" @@ -310,6 +311,13 @@ func NewClient(cfg *ClientConfig) (cl *Client, err error) { } } + var ICEServers []webrtc.ICEServer + if cl.config.ICEServerList != nil { + ICEServers = cl.config.ICEServerList + } else if cl.config.ICEServers != nil { + ICEServers = []webrtc.ICEServer{{URLs: cl.config.ICEServers}} + } + cl.websocketTrackers = websocketTrackers{ PeerId: cl.peerID, Logger: cl.logger, @@ -328,7 +336,7 @@ func NewClient(cfg *ClientConfig) (cl *Client, err error) { }, Proxy: cl.config.HTTPProxy, WebsocketTrackerHttpHeader: cl.config.WebsocketTrackerHttpHeader, - ICEServers: cl.config.ICEServers, + ICEServers: ICEServers, DialContext: cl.config.TrackerDialContext, OnConn: func(dc datachannel.ReadWriteCloser, dcc webtorrent.DataChannelContext) { cl.lock() @@ -1843,6 +1851,16 @@ func (cl *Client) String() string { return fmt.Sprintf("<%[1]T %[1]p>", cl) } +func (cl *Client) ICEServers() []webrtc.ICEServer { + var ICEServers []webrtc.ICEServer + if cl.config.ICEServerList != nil { + ICEServers = cl.config.ICEServerList + } else if cl.config.ICEServers != nil { + ICEServers = []webrtc.ICEServer{{URLs: cl.config.ICEServers}} + } + return ICEServers +} + // Returns connection-level aggregate connStats at the Client level. See the comment on // TorrentStats.ConnStats. func (cl *Client) ConnStats() ConnStats { diff --git a/config.go b/config.go index e2d0ea1e..9eb3444d 100644 --- a/config.go +++ b/config.go @@ -11,6 +11,7 @@ import ( "github.com/anacrolix/dht/v2/krpc" "github.com/anacrolix/log" "github.com/anacrolix/missinggo/v2" + "github.com/pion/webrtc/v3" "golang.org/x/time/rate" "github.com/anacrolix/torrent/iplist" @@ -183,8 +184,13 @@ type ClientConfig struct { Callbacks Callbacks - // ICEServers defines a slice describing servers available to be used by + // ICEServerList defines a slice describing servers available to be used by // ICE, such as STUN and TURN servers. + ICEServerList []webrtc.ICEServer + + // Deprecated. ICEServers does not support server authentication and therefore + // it cannot be used with most TURN servers. Use ICEServerList instead. + // ICEServers is kept for legacy support. ICEServers []string DialRateLimiter *rate.Limiter diff --git a/go.sum b/go.sum index b6e24fc0..af15eae5 100644 --- a/go.sum +++ b/go.sum @@ -109,8 +109,6 @@ github.com/anacrolix/multiless v0.3.0 h1:5Bu0DZncjE4e06b9r1Ap2tUY4Au0NToBP5RpuEn github.com/anacrolix/multiless v0.3.0/go.mod h1:TrCLEZfIDbMVfLoQt5tOoiBS/uq4y8+ojuEVVvTNPX4= github.com/anacrolix/possum/go v0.1.1-0.20240309232535-7d660fa365f8 h1:XDKUI9RHyhyfGXVXb/4N+l5kGo5jQITrrbF7EZPLuak= github.com/anacrolix/possum/go v0.1.1-0.20240309232535-7d660fa365f8/go.mod h1:pw5HEMBSiL+otYzHe4q5jGaVuy5unl+Mt4Bx6SDemW8= -github.com/anacrolix/squirrel v0.6.0 h1:ovfWW42wcGzrVYYI9s56pEYzfeTwtXxCCvSd+KwvUEA= -github.com/anacrolix/squirrel v0.6.0/go.mod h1:60vdNPUbK1jYWePp39Wqn9whHm12Yb9JEuwOXzLMDuY= github.com/anacrolix/squirrel v0.6.4 h1:K6ABRMCms0xwpEIdY3kAaDBUqiUeUYCKLKI0yHTr9IQ= github.com/anacrolix/squirrel v0.6.4/go.mod h1:0kFVjOLMOKVOet6ja2ac1vTOrqVbLj2zy2Fjp7+dkE8= github.com/anacrolix/stm v0.2.0/go.mod h1:zoVQRvSiGjGoTmbM0vSLIiaKjWtNPeTvXUSdJQA4hsg= diff --git a/webtorrent/tracker-client.go b/webtorrent/tracker-client.go index bc9dab31..09ea652f 100644 --- a/webtorrent/tracker-client.go +++ b/webtorrent/tracker-client.go @@ -43,7 +43,7 @@ type TrackerClient struct { pingTicker *time.Ticker WebsocketTrackerHttpHeader func() http.Header - ICEServers []string + ICEServers []webrtc.ICEServer } func (me *TrackerClient) Stats() TrackerClientStats { diff --git a/webtorrent/transport.go b/webtorrent/transport.go index 85662587..a8b6a845 100644 --- a/webtorrent/transport.go +++ b/webtorrent/transport.go @@ -48,12 +48,12 @@ func (me *wrappedPeerConnection) Close() error { return err } -func newPeerConnection(logger log.Logger, iceServers []string) (*wrappedPeerConnection, error) { +func newPeerConnection(logger log.Logger, iceServers []webrtc.ICEServer) (*wrappedPeerConnection, error) { newPeerConnectionMu.Lock() defer newPeerConnectionMu.Unlock() ctx, span := otel.Tracer(tracerName).Start(context.Background(), "PeerConnection") - pcConfig := webrtc.Configuration{ICEServers: []webrtc.ICEServer{{URLs: iceServers}}} + pcConfig := webrtc.Configuration{ICEServers: iceServers} pc, err := api.NewPeerConnection(pcConfig) if err != nil { diff --git a/wstracker.go b/wstracker.go index 84af9cbf..ff7b4b16 100644 --- a/wstracker.go +++ b/wstracker.go @@ -11,6 +11,7 @@ import ( "github.com/anacrolix/log" "github.com/gorilla/websocket" "github.com/pion/datachannel" + "github.com/pion/webrtc/v3" "github.com/anacrolix/torrent/tracker" httpTracker "github.com/anacrolix/torrent/tracker/http" @@ -45,7 +46,7 @@ type websocketTrackers struct { Proxy httpTracker.ProxyFunc DialContext func(ctx context.Context, network, addr string) (net.Conn, error) WebsocketTrackerHttpHeader func() netHttp.Header - ICEServers []string + ICEServers []webrtc.ICEServer } func (me *websocketTrackers) Get(url string, infoHash [20]byte) (*webtorrent.TrackerClient, func()) { -- 2.44.0