@@ -15,11 +15,11 @@ import (
15
15
"github.com/pion/webrtc/v3"
16
16
"golang.org/x/net/proxy"
17
17
"golang.org/x/xerrors"
18
+ "k8s.io/utils/pointer"
18
19
"nhooyr.io/websocket"
19
20
20
21
"cdr.dev/slog"
21
-
22
- "cdr.dev/coder-cli/coder-sdk"
22
+ "coder.com/m/product/coder/pkg/codersdk/legacy"
23
23
)
24
24
25
25
// DialOptions are configurable options for a wsnet connection.
@@ -35,10 +35,11 @@ type DialOptions struct {
35
35
// TURNProxyAuthToken is used to authenticate a TURN proxy request.
36
36
TURNProxyAuthToken string
37
37
38
- // TURNProxyURL is the URL to proxy all TURN data through.
39
- // This URL is sent to the listener during handshake so both
40
- // ends connect to the same TURN endpoint.
41
- TURNProxyURL * url.URL
38
+ // TURNRemoteProxyURL is the URL to proxy listener TURN data through.
39
+ TURNRemoteProxyURL * url.URL
40
+
41
+ // TURNLocalProxyURL is the URL to proxy client TURN data through.
42
+ TURNLocalProxyURL * url.URL
42
43
}
43
44
44
45
// DialWebsocket dials the broker with a WebSocket and negotiates a connection.
@@ -60,7 +61,7 @@ func DialWebsocket(ctx context.Context, broker string, netOpts *DialOptions, wsO
60
61
defer func () {
61
62
_ = resp .Body .Close ()
62
63
}()
63
- return nil , coder .NewHTTPError (resp )
64
+ return nil , legacy .NewHTTPError (resp )
64
65
}
65
66
return nil , fmt .Errorf ("dial websocket: %w" , err )
66
67
}
@@ -91,9 +92,9 @@ func Dial(ctx context.Context, conn net.Conn, options *DialOptions) (*Dialer, er
91
92
}
92
93
93
94
var turnProxy proxy.Dialer
94
- if options .TURNProxyURL != nil {
95
+ if options .TURNLocalProxyURL != nil {
95
96
turnProxy = & turnProxyDialer {
96
- baseURL : options .TURNProxyURL ,
97
+ baseURL : options .TURNLocalProxyURL ,
97
98
token : options .TURNProxyAuthToken ,
98
99
}
99
100
}
@@ -107,7 +108,7 @@ func Dial(ctx context.Context, conn net.Conn, options *DialOptions) (*Dialer, er
107
108
defer func () {
108
109
if err != nil {
109
110
// Wrap our error with some extra details.
110
- err = errWrap {
111
+ err = wrapError {
111
112
err : err ,
112
113
iceServers : rtc .GetConfiguration ().ICEServers ,
113
114
rtc : rtc .ConnectionState (),
@@ -128,8 +129,8 @@ func Dial(ctx context.Context, conn net.Conn, options *DialOptions) (*Dialer, er
128
129
129
130
log .Debug (ctx , "creating control channel" , slog .F ("proto" , controlChannel ))
130
131
ctrl , err := rtc .CreateDataChannel (controlChannel , & webrtc.DataChannelInit {
131
- Protocol : stringPtr (controlChannel ),
132
- Ordered : boolPtr (true ),
132
+ Protocol : pointer . String (controlChannel ),
133
+ Ordered : pointer . Bool (true ),
133
134
})
134
135
if err != nil {
135
136
return nil , fmt .Errorf ("create control channel: %w" , err )
@@ -146,8 +147,8 @@ func Dial(ctx context.Context, conn net.Conn, options *DialOptions) (*Dialer, er
146
147
}
147
148
148
149
var turnProxyURL string
149
- if options .TURNProxyURL != nil {
150
- turnProxyURL = options .TURNProxyURL .String ()
150
+ if options .TURNRemoteProxyURL != nil {
151
+ turnProxyURL = options .TURNRemoteProxyURL .String ()
151
152
}
152
153
153
154
bmsg := BrokerMessage {
@@ -177,7 +178,9 @@ func Dial(ctx context.Context, conn net.Conn, options *DialOptions) (*Dialer, er
177
178
178
179
err = dialer .negotiate (ctx )
179
180
if err != nil {
180
- return nil , xerrors .Errorf ("negotiate rtc connection: %w" , err )
181
+ // Return the dialer since we have tests that verify things are closed
182
+ // if negotiation fails.
183
+ return dialer , xerrors .Errorf ("negotiate rtc connection: %w" , err )
181
184
}
182
185
183
186
return dialer , nil
@@ -290,11 +293,22 @@ func (d *Dialer) negotiate(ctx context.Context) (err error) {
290
293
return fmt .Errorf ("unhandled message: %+v" , msg )
291
294
}
292
295
293
- return <- errCh
296
+ err = <- errCh
297
+ if err != nil {
298
+ return err
299
+ }
300
+
301
+ proto , err := iceProto (d .rtc )
302
+ if err != nil {
303
+ return xerrors .Errorf ("determine ICE connection protocol: %w" , err )
304
+ }
305
+ d .log .Debug (ctx , "connected" , slog .F ("ice_proto" , proto ))
306
+
307
+ return nil
294
308
}
295
309
296
- // ActiveConnections returns the amount of active connections.
297
- // DialContext opens a connection, and close will end it.
310
+ // ActiveConnections returns the amount of active connections. DialContext
311
+ // opens a connection, and close will end it.
298
312
func (d * Dialer ) activeConnections () int {
299
313
stats , ok := d .rtc .GetStats ().GetConnectionStats (d .rtc )
300
314
if ! ok {
@@ -304,6 +318,11 @@ func (d *Dialer) activeConnections() int {
304
318
return int (stats .DataChannelsRequested - stats .DataChannelsClosed ) - 1
305
319
}
306
320
321
+ // Candidates returns the candidate pair that was chosen for the connection.
322
+ func (d * Dialer ) Candidates () (* webrtc.ICECandidatePair , error ) {
323
+ return d .rtc .SCTP ().Transport ().ICETransport ().GetSelectedCandidatePair ()
324
+ }
325
+
307
326
// Close closes the RTC connection.
308
327
// All data channels dialed will be closed.
309
328
func (d * Dialer ) Close () error {
@@ -367,7 +386,7 @@ func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.
367
386
368
387
d .log .Debug (ctx , "opening data channel" )
369
388
dc , err := d .rtc .CreateDataChannel ("proxy" , & webrtc.DataChannelInit {
370
- Ordered : boolPtr (network != "udp" ),
389
+ Ordered : pointer . Bool (network != "udp" ),
371
390
Protocol : & proto ,
372
391
})
373
392
if err != nil {
0 commit comments