@@ -25,11 +25,7 @@ use url::Url;
25
25
pub use self :: conn:: { ReceivedMessage , RecvError , SendError , SendMessage } ;
26
26
#[ cfg( not( wasm_browser) ) ]
27
27
use crate :: dns:: { DnsError , DnsResolver } ;
28
- use crate :: {
29
- http:: { Protocol , RELAY_PATH } ,
30
- protos:: relay:: SendError as SendRelayError ,
31
- KeyCache ,
32
- } ;
28
+ use crate :: { http:: RELAY_PATH , protos:: relay:: SendError as SendRelayError , KeyCache } ;
33
29
34
30
pub ( crate ) mod conn;
35
31
#[ cfg( not( wasm_browser) ) ]
@@ -125,8 +121,6 @@ pub struct ClientBuilder {
125
121
is_prober : bool ,
126
122
/// Server url.
127
123
url : RelayUrl ,
128
- /// Relay protocol
129
- protocol : Protocol ,
130
124
/// Allow self-signed certificates from relay servers
131
125
#[ cfg( any( test, feature = "test-utils" ) ) ]
132
126
insecure_skip_cert_verify : bool ,
@@ -153,9 +147,6 @@ impl ClientBuilder {
153
147
is_prober : false ,
154
148
url : url. into ( ) ,
155
149
156
- // Resolves to websockets in browsers and relay otherwise
157
- protocol : Protocol :: default ( ) ,
158
-
159
150
#[ cfg( any( test, feature = "test-utils" ) ) ]
160
151
insecure_skip_cert_verify : false ,
161
152
@@ -167,13 +158,6 @@ impl ClientBuilder {
167
158
}
168
159
}
169
160
170
- /// Sets whether to connect to the relay via websockets or not.
171
- /// Set to use non-websocket, normal relaying by default.
172
- pub fn protocol ( mut self , protocol : Protocol ) -> Self {
173
- self . protocol = protocol;
174
- self
175
- }
176
-
177
161
/// Returns if we should prefer ipv6
178
162
/// it replaces the relayhttp.AddressFamilySelector we pass
179
163
/// It provides the hint as to whether in an IPv4-vs-IPv6 race that
@@ -216,41 +200,97 @@ impl ClientBuilder {
216
200
}
217
201
218
202
/// Establishes a new connection to the relay server.
203
+ #[ cfg( not( wasm_browser) ) ]
219
204
pub async fn connect ( & self ) -> Result < Client , ConnectError > {
220
- let ( conn, local_addr) = match self . protocol {
221
- #[ cfg( wasm_browser) ]
222
- Protocol :: Websocket => {
223
- let conn = self . connect_ws ( ) . await ?;
224
- let local_addr = None ;
225
- ( conn, local_addr)
226
- }
227
- #[ cfg( not( wasm_browser) ) ]
228
- Protocol :: Websocket => {
229
- let ( conn, local_addr) = self . connect_ws ( ) . await ?;
230
- ( conn, Some ( local_addr) )
231
- }
232
- #[ cfg( not( wasm_browser) ) ]
233
- Protocol :: Relay => {
234
- let ( conn, local_addr) = self . connect_relay ( ) . await ?;
235
- ( conn, Some ( local_addr) )
205
+ use tls:: MaybeTlsStreamBuilder ;
206
+
207
+ use crate :: protos:: relay:: MAX_FRAME_SIZE ;
208
+
209
+ let mut dial_url = ( * self . url ) . clone ( ) ;
210
+ dial_url. set_path ( RELAY_PATH ) ;
211
+ // The relay URL is exchanged with the http(s) scheme in tickets and similar.
212
+ // We need to use the ws:// or wss:// schemes when connecting with websockets, though.
213
+ dial_url
214
+ . set_scheme ( match self . url . scheme ( ) {
215
+ "http" => "ws" ,
216
+ "ws" => "ws" ,
217
+ _ => "wss" ,
218
+ } )
219
+ . map_err ( |_| {
220
+ InvalidWebsocketUrlSnafu {
221
+ url : dial_url. clone ( ) ,
222
+ }
223
+ . build ( )
224
+ } ) ?;
225
+
226
+ debug ! ( %dial_url, "Dialing relay by websocket" ) ;
227
+
228
+ #[ allow( unused_mut) ]
229
+ let mut builder = MaybeTlsStreamBuilder :: new ( dial_url. clone ( ) , self . dns_resolver . clone ( ) )
230
+ . prefer_ipv6 ( self . prefer_ipv6 ( ) )
231
+ . proxy_url ( self . proxy_url . clone ( ) ) ;
232
+
233
+ #[ cfg( any( test, feature = "test-utils" ) ) ]
234
+ if self . insecure_skip_cert_verify {
235
+ builder = builder. insecure_skip_cert_verify ( self . insecure_skip_cert_verify ) ;
236
+ }
237
+
238
+ let stream = builder. connect ( ) . await ?;
239
+ let local_addr = stream
240
+ . as_ref ( )
241
+ . local_addr ( )
242
+ . map_err ( |_| NoLocalAddrSnafu . build ( ) ) ?;
243
+ let ( conn, response) = tokio_websockets:: ClientBuilder :: new ( )
244
+ . uri ( dial_url. as_str ( ) )
245
+ . map_err ( |_| {
246
+ InvalidRelayUrlSnafu {
247
+ url : dial_url. clone ( ) ,
248
+ }
249
+ . build ( )
250
+ } ) ?
251
+ . limits ( tokio_websockets:: Limits :: default ( ) . max_payload_len ( Some ( MAX_FRAME_SIZE ) ) )
252
+ . connect_on ( stream)
253
+ . await ?;
254
+
255
+ if response. status ( ) != hyper:: StatusCode :: SWITCHING_PROTOCOLS {
256
+ UnexpectedUpgradeStatusSnafu {
257
+ code : response. status ( ) ,
236
258
}
237
- #[ cfg( wasm_browser) ]
238
- Protocol :: Relay => return Err ( RelayProtoNotAvailableSnafu . build ( ) ) ,
239
- } ;
259
+ . fail ( ) ?;
260
+ }
261
+
262
+ let conn = Conn :: new ( conn, self . key_cache . clone ( ) , & self . secret_key ) . await ?;
240
263
241
264
event ! (
242
265
target: "events.net.relay.connected" ,
243
266
Level :: DEBUG ,
244
267
url = %self . url,
245
- protocol = ?self . protocol,
246
268
) ;
247
269
248
270
trace ! ( "connect done" ) ;
249
- Ok ( Client { conn, local_addr } )
271
+
272
+ Ok ( Client {
273
+ conn,
274
+ local_addr : Some ( local_addr) ,
275
+ } )
276
+ }
277
+
278
+ /// Reports whether IPv4 dials should be slightly
279
+ /// delayed to give IPv6 a better chance of winning dial races.
280
+ /// Implementations should only return true if IPv6 is expected
281
+ /// to succeed. (otherwise delaying IPv4 will delay the connection
282
+ /// overall)
283
+ #[ cfg( not( wasm_browser) ) ]
284
+ fn prefer_ipv6 ( & self ) -> bool {
285
+ match self . address_family_selector {
286
+ Some ( ref selector) => selector ( ) ,
287
+ None => false ,
288
+ }
250
289
}
251
290
291
+ /// Establishes a new connection to the relay server.
252
292
#[ cfg( wasm_browser) ]
253
- async fn connect_ws ( & self ) -> Result < Conn , ConnectError > {
293
+ pub async fn connect ( & self ) -> Result < Client , ConnectError > {
254
294
let mut dial_url = ( * self . url ) . clone ( ) ;
255
295
dial_url. set_path ( RELAY_PATH ) ;
256
296
// The relay URL is exchanged with the http(s) scheme in tickets and similar.
@@ -271,9 +311,20 @@ impl ClientBuilder {
271
311
debug ! ( %dial_url, "Dialing relay by websocket" ) ;
272
312
273
313
let ( _, ws_stream) = ws_stream_wasm:: WsMeta :: connect ( dial_url. as_str ( ) , None ) . await ?;
274
- let conn =
275
- Conn :: new_ws_browser ( ws_stream, self . key_cache . clone ( ) , & self . secret_key ) . await ?;
276
- Ok ( conn)
314
+ let conn = Conn :: new ( ws_stream, self . key_cache . clone ( ) , & self . secret_key ) . await ?;
315
+
316
+ event ! (
317
+ target: "events.net.relay.connected" ,
318
+ Level :: DEBUG ,
319
+ url = %self . url,
320
+ ) ;
321
+
322
+ trace ! ( "connect done" ) ;
323
+
324
+ Ok ( Client {
325
+ conn,
326
+ local_addr : None ,
327
+ } )
277
328
}
278
329
}
279
330
0 commit comments