Skip to content

Commit

Permalink
Support ICE servers auth (#920)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
marcovidonis authored Mar 26, 2024
1 parent d17250b commit 1830d85
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
20 changes: 19 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion webtorrent/tracker-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type TrackerClient struct {
pingTicker *time.Ticker

WebsocketTrackerHttpHeader func() http.Header
ICEServers []string
ICEServers []webrtc.ICEServer
}

func (me *TrackerClient) Stats() TrackerClientStats {
Expand Down
4 changes: 2 additions & 2 deletions webtorrent/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion wstracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit 1830d85

Please sign in to comment.