-
-
Notifications
You must be signed in to change notification settings - Fork 625
/
transport.go
265 lines (239 loc) · 7.08 KB
/
transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package webtorrent
import (
"context"
"expvar"
"fmt"
"io"
"sync"
"time"
"github.com/anacrolix/log"
"github.com/anacrolix/missinggo/v2/pproffd"
"github.com/pion/datachannel"
"github.com/pion/webrtc/v3"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
const (
dataChannelLabel = "webrtc-datachannel"
)
var (
metrics = expvar.NewMap("webtorrent")
api = func() *webrtc.API {
// Enable the detach API (since it's non-standard but more idiomatic).
s.DetachDataChannels()
return webrtc.NewAPI(webrtc.WithSettingEngine(s))
}()
newPeerConnectionMu sync.Mutex
)
type wrappedPeerConnection struct {
*webrtc.PeerConnection
closeMu sync.Mutex
pproffd.CloseWrapper
span trace.Span
ctx context.Context
}
func (me *wrappedPeerConnection) Close() error {
me.closeMu.Lock()
defer me.closeMu.Unlock()
err := me.CloseWrapper.Close()
me.span.End()
return err
}
func newPeerConnection(logger log.Logger, iceServers []string) (*wrappedPeerConnection, error) {
newPeerConnectionMu.Lock()
defer newPeerConnectionMu.Unlock()
ctx, span := otel.Tracer(tracerName).Start(context.Background(), "PeerConnection")
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)
span.End()
return nil, err
}
wpc := &wrappedPeerConnection{
PeerConnection: pc,
CloseWrapper: pproffd.NewCloseWrapper(pc),
ctx: ctx,
span: span,
}
// If the state change handler intends to call Close, it should call it on the wrapper.
wpc.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
logger.Levelf(log.Warning, "webrtc PeerConnection state changed to %v", state)
span.AddEvent("connection state changed", trace.WithAttributes(attribute.String("state", state.String())))
})
return wpc, nil
}
func setAndGatherLocalDescription(peerConnection *wrappedPeerConnection, sdp webrtc.SessionDescription) (_ webrtc.SessionDescription, err error) {
gatherComplete := webrtc.GatheringCompletePromise(peerConnection.PeerConnection)
peerConnection.span.AddEvent("setting local description")
err = peerConnection.SetLocalDescription(sdp)
if err != nil {
err = fmt.Errorf("setting local description: %w", err)
return
}
<-gatherComplete
peerConnection.span.AddEvent("gathering complete")
return *peerConnection.LocalDescription(), nil
}
// newOffer creates a transport and returns a WebRTC offer to be announced. See
// https://github.com/pion/webrtc/blob/master/examples/data-channels/jsfiddle/main.go for what this is modelled on.
func (tc *TrackerClient) newOffer(
logger log.Logger,
offerId string,
infoHash [20]byte,
) (
peerConnection *wrappedPeerConnection,
dataChannel *webrtc.DataChannel,
offer webrtc.SessionDescription,
err error,
) {
peerConnection, err = newPeerConnection(logger, tc.ICEServers)
if err != nil {
return
}
peerConnection.span.SetAttributes(attribute.String(webrtcConnTypeKey, "offer"))
dataChannel, err = peerConnection.CreateDataChannel(dataChannelLabel, nil)
if err != nil {
err = fmt.Errorf("creating data channel: %w", err)
peerConnection.Close()
}
initDataChannel(dataChannel, peerConnection, func(dc datachannel.ReadWriteCloser, dcCtx context.Context, dcSpan trace.Span) {
metrics.Add("outbound offers answered with datachannel", 1)
tc.mu.Lock()
tc.stats.ConvertedOutboundConns++
tc.mu.Unlock()
tc.OnConn(dc, DataChannelContext{
OfferId: offerId,
LocalOffered: true,
InfoHash: infoHash,
peerConnection: peerConnection,
Context: dcCtx,
Span: dcSpan,
})
})
offer, err = peerConnection.CreateOffer(nil)
if err != nil {
dataChannel.Close()
peerConnection.Close()
return
}
offer, err = setAndGatherLocalDescription(peerConnection, offer)
if err != nil {
dataChannel.Close()
peerConnection.Close()
}
return
}
type onDetachedDataChannelFunc func(detached datachannel.ReadWriteCloser, ctx context.Context, span trace.Span)
func (tc *TrackerClient) initAnsweringPeerConnection(
peerConn *wrappedPeerConnection,
offerContext offerContext,
) (answer webrtc.SessionDescription, err error) {
peerConn.span.SetAttributes(attribute.String(webrtcConnTypeKey, "answer"))
timer := time.AfterFunc(30*time.Second, func() {
peerConn.span.SetStatus(codes.Error, "answer timeout")
metrics.Add("answering peer connections timed out", 1)
peerConn.Close()
})
peerConn.OnDataChannel(func(d *webrtc.DataChannel) {
initDataChannel(d, peerConn, func(detached datachannel.ReadWriteCloser, ctx context.Context, span trace.Span) {
timer.Stop()
metrics.Add("answering peer connection conversions", 1)
tc.mu.Lock()
tc.stats.ConvertedInboundConns++
tc.mu.Unlock()
tc.OnConn(detached, DataChannelContext{
OfferId: offerContext.Id,
LocalOffered: false,
InfoHash: offerContext.InfoHash,
peerConnection: peerConn,
Context: ctx,
Span: span,
})
})
})
err = peerConn.SetRemoteDescription(offerContext.SessDesc)
if err != nil {
return
}
answer, err = peerConn.CreateAnswer(nil)
if err != nil {
return
}
answer, err = setAndGatherLocalDescription(peerConn, answer)
return
}
// newAnsweringPeerConnection creates a transport from a WebRTC offer and returns a WebRTC answer to be announced.
func (tc *TrackerClient) newAnsweringPeerConnection(
offerContext offerContext,
) (
peerConn *wrappedPeerConnection, answer webrtc.SessionDescription, err error,
) {
peerConn, err = newPeerConnection(tc.Logger, tc.ICEServers)
if err != nil {
err = fmt.Errorf("failed to create new connection: %w", err)
return
}
answer, err = tc.initAnsweringPeerConnection(peerConn, offerContext)
if err != nil {
peerConn.span.RecordError(err)
peerConn.Close()
}
return
}
type datachannelReadWriter interface {
datachannel.Reader
datachannel.Writer
io.Reader
io.Writer
}
type ioCloserFunc func() error
func (me ioCloserFunc) Close() error {
return me()
}
func initDataChannel(
dc *webrtc.DataChannel,
pc *wrappedPeerConnection,
onOpen onDetachedDataChannelFunc,
) {
var span trace.Span
dc.OnClose(func() {
span.End()
})
dc.OnOpen(func() {
pc.span.AddEvent("data channel opened")
var ctx context.Context
ctx, span = otel.Tracer(tracerName).Start(pc.ctx, "DataChannel")
raw, err := dc.Detach()
if err != nil {
// This shouldn't happen if the API is configured correctly, and we call from OnOpen.
panic(err)
}
onOpen(hookDataChannelCloser(raw, pc, span, dc), ctx, span)
})
}
// Hooks the datachannel's Close to Close the owning PeerConnection. The datachannel takes ownership
// and responsibility for the PeerConnection.
func hookDataChannelCloser(
dcrwc datachannel.ReadWriteCloser,
pc *wrappedPeerConnection,
dataChannelSpan trace.Span,
originalDataChannel *webrtc.DataChannel,
) datachannel.ReadWriteCloser {
return struct {
datachannelReadWriter
io.Closer
}{
dcrwc,
ioCloserFunc(func() error {
dcrwc.Close()
pc.Close()
originalDataChannel.Close()
dataChannelSpan.End()
return nil
}),
}
}