2
2
//!
3
3
//! Based on tailscale/derp/derphttp/derphttp_client.go
4
4
5
+ #[ cfg( not( wasm_browser) ) ]
6
+ use std:: { future:: Future , net:: IpAddr } ;
5
7
use std:: {
6
- future:: Future ,
7
- net:: { IpAddr , SocketAddr } ,
8
+ net:: SocketAddr ,
8
9
pin:: Pin ,
9
10
sync:: Arc ,
10
11
task:: { self , Poll } ,
11
12
} ;
12
13
13
- use anyhow:: { anyhow, bail, Context , Result } ;
14
+ #[ cfg( not( wasm_browser) ) ]
15
+ use anyhow:: Context ;
16
+ use anyhow:: { anyhow, bail, Result } ;
17
+ #[ cfg( not( wasm_browser) ) ]
14
18
use bytes:: Bytes ;
15
19
use conn:: Conn ;
20
+ #[ cfg( not( wasm_browser) ) ]
16
21
use data_encoding:: BASE64URL ;
17
22
use futures_lite:: Stream ;
18
23
use futures_util:: {
@@ -21,33 +26,41 @@ use futures_util::{
21
26
} ;
22
27
#[ cfg( not( wasm_browser) ) ]
23
28
use hickory_resolver:: TokioResolver as DnsResolver ;
29
+ #[ cfg( not( wasm_browser) ) ]
24
30
use http_body_util:: Empty ;
31
+ #[ cfg( not( wasm_browser) ) ]
25
32
use hyper:: {
26
33
body:: Incoming ,
27
34
header:: { HOST , UPGRADE } ,
28
35
upgrade:: Parts ,
29
36
Request ,
30
37
} ;
31
38
use iroh_base:: { RelayUrl , SecretKey } ;
39
+ #[ cfg( not( wasm_browser) ) ]
32
40
use rustls:: client:: Resumption ;
33
41
#[ cfg( not( wasm_browser) ) ]
34
42
use streams:: { downcast_upgrade, MaybeTlsStream , ProxyStream } ;
43
+ #[ cfg( not( wasm_browser) ) ]
35
44
use tokio:: io:: { AsyncRead , AsyncWrite } ;
36
45
#[ cfg( any( test, feature = "test-utils" ) ) ]
37
46
use tracing:: warn;
38
- use tracing:: { debug, error, event, info_span, trace, Instrument , Level } ;
47
+ use tracing:: { debug, event, trace, Level } ;
48
+ #[ cfg( not( wasm_browser) ) ]
49
+ use tracing:: { error, info_span, Instrument } ;
39
50
use url:: Url ;
40
51
41
52
pub use self :: conn:: { ConnSendError , ReceivedMessage , SendMessage } ;
53
+ #[ cfg( not( wasm_browser) ) ]
54
+ use crate :: defaults:: timeouts:: * ;
42
55
use crate :: {
43
- defaults:: timeouts:: * ,
44
56
http:: { Protocol , RELAY_PATH } ,
45
57
KeyCache ,
46
58
} ;
47
59
48
60
pub ( crate ) mod conn;
49
61
#[ cfg( not( wasm_browser) ) ]
50
62
pub ( crate ) mod streams;
63
+ #[ cfg( not( wasm_browser) ) ]
51
64
mod util;
52
65
53
66
#[ cfg( wasm_browser) ]
@@ -73,6 +86,7 @@ pub struct ClientBuilder {
73
86
/// The secret key of this client.
74
87
secret_key : SecretKey ,
75
88
/// The DNS resolver to use.
89
+ #[ cfg_attr( wasm_browser, allow( unused) ) ]
76
90
dns_resolver : DnsResolver ,
77
91
/// Cache for public keys of remote nodes.
78
92
key_cache : KeyCache ,
@@ -152,35 +166,6 @@ impl ClientBuilder {
152
166
153
167
/// Establishes a new connection to the relay server.
154
168
pub async fn connect ( & self ) -> Result < Client > {
155
- let roots = rustls:: RootCertStore {
156
- roots : webpki_roots:: TLS_SERVER_ROOTS . to_vec ( ) ,
157
- } ;
158
- let mut config = rustls:: client:: ClientConfig :: builder_with_provider ( Arc :: new (
159
- rustls:: crypto:: ring:: default_provider ( ) ,
160
- ) )
161
- . with_safe_default_protocol_versions ( )
162
- . expect ( "protocols supported by ring" )
163
- . with_root_certificates ( roots)
164
- . with_no_client_auth ( ) ;
165
- #[ cfg( any( test, feature = "test-utils" ) ) ]
166
- if self . insecure_skip_cert_verify {
167
- warn ! ( "Insecure config: SSL certificates from relay servers not verified" ) ;
168
- config
169
- . dangerous ( )
170
- . set_certificate_verifier ( Arc :: new ( NoCertVerifier ) ) ;
171
- }
172
- config. resumption = Resumption :: default ( ) ;
173
- let tls_connector: tokio_rustls:: TlsConnector = Arc :: new ( config) . into ( ) ;
174
-
175
- let ( conn, local_addr) = self . connect_0 ( tls_connector) . await ?;
176
-
177
- Ok ( Client { conn, local_addr } )
178
- }
179
-
180
- async fn connect_0 (
181
- & self ,
182
- tls_connector : tokio_rustls:: TlsConnector ,
183
- ) -> Result < ( Conn , Option < SocketAddr > ) > {
184
169
let ( conn, local_addr) = match self . protocol {
185
170
Protocol :: Websocket => {
186
171
let conn = self . connect_ws ( ) . await ?;
@@ -189,7 +174,7 @@ impl ClientBuilder {
189
174
}
190
175
#[ cfg( not( wasm_browser) ) ]
191
176
Protocol :: Relay => {
192
- let ( conn, local_addr) = self . connect_relay ( tls_connector ) . await ?;
177
+ let ( conn, local_addr) = self . connect_relay ( ) . await ?;
193
178
( conn, Some ( local_addr) )
194
179
}
195
180
#[ cfg( wasm_browser) ]
@@ -205,8 +190,8 @@ impl ClientBuilder {
205
190
protocol = ?self . protocol,
206
191
) ;
207
192
208
- trace ! ( "connect_0 done" ) ;
209
- Ok ( ( conn, local_addr) )
193
+ trace ! ( "connect done" ) ;
194
+ Ok ( Client { conn, local_addr } )
210
195
}
211
196
212
197
async fn connect_ws ( & self ) -> Result < Conn > {
@@ -239,10 +224,27 @@ impl ClientBuilder {
239
224
// Non-browser code
240
225
#[ cfg( not( wasm_browser) ) ]
241
226
impl ClientBuilder {
242
- async fn connect_relay (
243
- & self ,
244
- tls_connector : tokio_rustls:: TlsConnector ,
245
- ) -> Result < ( Conn , SocketAddr ) > {
227
+ async fn connect_relay ( & self ) -> Result < ( Conn , SocketAddr ) > {
228
+ let roots = rustls:: RootCertStore {
229
+ roots : webpki_roots:: TLS_SERVER_ROOTS . to_vec ( ) ,
230
+ } ;
231
+ let mut config = rustls:: client:: ClientConfig :: builder_with_provider ( Arc :: new (
232
+ rustls:: crypto:: ring:: default_provider ( ) ,
233
+ ) )
234
+ . with_safe_default_protocol_versions ( )
235
+ . expect ( "protocols supported by ring" )
236
+ . with_root_certificates ( roots)
237
+ . with_no_client_auth ( ) ;
238
+ #[ cfg( any( test, feature = "test-utils" ) ) ]
239
+ if self . insecure_skip_cert_verify {
240
+ warn ! ( "Insecure config: SSL certificates from relay servers not verified" ) ;
241
+ config
242
+ . dangerous ( )
243
+ . set_certificate_verifier ( Arc :: new ( NoCertVerifier ) ) ;
244
+ }
245
+ config. resumption = Resumption :: default ( ) ;
246
+ let tls_connector: tokio_rustls:: TlsConnector = Arc :: new ( config) . into ( ) ;
247
+
246
248
let url = self . url . clone ( ) ;
247
249
let tcp_stream = self . dial_url ( & tls_connector) . await ?;
248
250
@@ -605,6 +607,7 @@ pub fn make_dangerous_client_config() -> rustls::ClientConfig {
605
607
. with_no_client_auth ( )
606
608
}
607
609
610
+ #[ cfg( not( wasm_browser) ) ]
608
611
fn host_header_value ( relay_url : RelayUrl ) -> Result < String > {
609
612
// grab the host, turns e.g. https://example.com:8080/xyz -> example.com.
610
613
let relay_url_host = relay_url. host_str ( ) . context ( "Invalid URL" ) ?;
@@ -711,6 +714,7 @@ impl rustls::client::danger::ServerCertVerifier for NoCertVerifier {
711
714
}
712
715
}
713
716
717
+ #[ cfg( not( wasm_browser) ) ]
714
718
fn url_port ( url : & Url ) -> Option < u16 > {
715
719
if let Some ( port) = url. port ( ) {
716
720
return Some ( port) ;
0 commit comments