]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Support ICE servers auth (#920)
authorMarco Vidonis <31407403+marcovidonis@users.noreply.github.com>
Tue, 26 Mar 2024 09:42:08 +0000 (09:42 +0000)
committerGitHub <noreply@github.com>
Tue, 26 Mar 2024 09:42:08 +0000 (20:42 +1100)
* 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
config.go
go.sum
webtorrent/tracker-client.go
webtorrent/transport.go
wstracker.go

index 66abeb847240438dabd4759eb95a50d0255590fa..43a8fc46b4aba313aa8f7a71040275b31e32cf46 100644 (file)
--- 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 {
index e2d0ea1ebec230cce1c7fa50e67ff5fc88f8006d..9eb3444dda0d7b269924099b549e56b23d19fc19 100644 (file)
--- 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 b6e24fc02098b50e8059150a2099830abd0eabba..af15eae5fed617ffe6bbe51c9c637e9f66a6fc3d 100644 (file)
--- 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=
index bc9dab312e2d64acf843f46d423b424e1514ebad..09ea652f234d7fa25c88e00773ae9bf58f72569e 100644 (file)
@@ -43,7 +43,7 @@ type TrackerClient struct {
        pingTicker     *time.Ticker
 
        WebsocketTrackerHttpHeader func() http.Header
-       ICEServers                 []string
+       ICEServers                 []webrtc.ICEServer
 }
 
 func (me *TrackerClient) Stats() TrackerClientStats {
index 856625874c8becc00be22a6ee600d401c260b13a..a8b6a845489e4d4003cc3610bfb3b3fdff000785 100644 (file)
@@ -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 {
index 84af9cbfc18fc0de09a2fafd7199d7dec97f04ca..ff7b4b16007e3cc4cf9764a1b11113a6eb617d15 100644 (file)
@@ -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()) {