client.go | 20 +++++++++++++++++++- config.go | 8 +++++++- go.sum | 2 -- webtorrent/tracker-client.go | 2 +- webtorrent/transport.go | 4 ++-- wstracker.go | 3 ++- diff --git a/client.go b/client.go index 66abeb847240438dabd4759eb95a50d0255590fa..43a8fc46b4aba313aa8f7a71040275b31e32cf46 100644 --- a/client.go +++ b/client.go @@ -35,6 +35,7 @@ "github.com/davecgh/go-spew/spew" "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 @@ } } } + 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 @@ return t.announceRequest(event, infoHash), nil }, 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() @@ -1841,6 +1849,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 diff --git a/config.go b/config.go index e2d0ea1ebec230cce1c7fa50e67ff5fc88f8006d..9eb3444dda0d7b269924099b549e56b23d19fc19 100644 --- a/config.go +++ b/config.go @@ -11,6 +11,7 @@ "github.com/anacrolix/dht/v2" "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 @@ DisableWebseeds bool 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 b6e24fc02098b50e8059150a2099830abd0eabba..af15eae5fed617ffe6bbe51c9c637e9f66a6fc3d 100644 --- a/go.sum +++ b/go.sum @@ -109,8 +109,6 @@ github.com/anacrolix/multiless v0.3.0 h1:5Bu0DZncjE4e06b9r1Ap2tUY4Au0NToBP5RpuEngSis= 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 bc9dab312e2d64acf843f46d423b424e1514ebad..09ea652f234d7fa25c88e00773ae9bf58f72569e 100644 --- a/webtorrent/tracker-client.go +++ b/webtorrent/tracker-client.go @@ -43,7 +43,7 @@ stats TrackerClientStats 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 856625874c8becc00be22a6ee600d401c260b13a..a8b6a845489e4d4003cc3610bfb3b3fdff000785 100644 --- a/webtorrent/transport.go +++ b/webtorrent/transport.go @@ -48,12 +48,12 @@ me.span.End() 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 84af9cbfc18fc0de09a2fafd7199d7dec97f04ca..ff7b4b16007e3cc4cf9764a1b11113a6eb617d15 100644 --- a/wstracker.go +++ b/wstracker.go @@ -11,6 +11,7 @@ "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 @@ clients map[string]*refCountedWebtorrentTrackerClient 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()) {