@@ -10,10 +10,7 @@ use std::pin::Pin;
1010use std:: sync:: Arc ;
1111use std:: time:: Duration ;
1212use tokio:: net:: TcpStream ;
13- use tokio:: {
14- io:: { AsyncRead , AsyncWrite } ,
15- time:: timeout,
16- } ;
13+ use tokio:: time:: timeout;
1714
1815use tokio_rustls:: rustls:: {
1916 client:: danger:: { HandshakeSignatureValid , ServerCertVerified , ServerCertVerifier } ,
@@ -22,6 +19,7 @@ use tokio_rustls::rustls::{
2219} ;
2320use tracing:: { error, info_span, Instrument } ;
2421
22+ use crate :: tor:: * ;
2523use crate :: AppState ;
2624
2725/// wallet2.h has a default timeout of 3 minutes + 30 seconds.
@@ -32,10 +30,6 @@ static TIMEOUT: Duration = Duration::from_secs(3 * 60 + 30).checked_div(2).unwra
3230/// If the main node does not finish within this period, we start a hedged request.
3331static SOFT_TIMEOUT : Duration = TIMEOUT . checked_div ( 2 ) . unwrap ( ) ;
3432
35- /// Trait alias for a stream that can be used with hyper
36- trait HyperStream : AsyncRead + AsyncWrite + Unpin + Send { }
37- impl < T : AsyncRead + AsyncWrite + Unpin + Send > HyperStream for T { }
38-
3933#[ derive( Debug ) ]
4034struct NoCertificateVerification ;
4135
@@ -149,14 +143,7 @@ async fn proxy_to_multiple_nodes(
149143
150144 // Sort nodes to prioritize those with available connections
151145 // Check if we're using Tor for this request
152- let use_tor = match & state. tor_client {
153- Some ( tc)
154- if tc. bootstrap_status ( ) . ready_for_traffic ( ) && !request. clearnet_whitelisted ( ) =>
155- {
156- true
157- }
158- _ => false ,
159- } ;
146+ let use_tor = state. tor_client . ready_for_traffic ( ) && !request. clearnet_whitelisted ( ) ;
160147
161148 // Create a vector of (node, has_connection) pairs
162149 let mut nodes_with_availability = Vec :: new ( ) ;
@@ -321,7 +308,7 @@ async fn proxy_to_multiple_nodes(
321308
322309/// Wraps a stream with TLS if HTTPS is being used
323310async fn maybe_wrap_with_tls (
324- stream : impl AsyncRead + AsyncWrite + Unpin + Send + ' static ,
311+ stream : impl HyperStream + ' static ,
325312 scheme : & str ,
326313 host : & str ,
327314) -> Result < Box < dyn HyperStream > , SingleRequestError > {
@@ -465,14 +452,7 @@ async fn proxy_to_single_node(
465452 tracing:: trace!( "Request is whitelisted, sending over clearnet" ) ;
466453 }
467454
468- let use_tor = match & state. tor_client {
469- Some ( tc)
470- if tc. bootstrap_status ( ) . ready_for_traffic ( ) && !request. clearnet_whitelisted ( ) =>
471- {
472- true
473- }
474- _ => false ,
475- } ;
455+ let use_tor = state. tor_client . ready_for_traffic ( ) && !request. clearnet_whitelisted ( ) ;
476456
477457 let key = ( node. 0 . clone ( ) , node. 1 . clone ( ) , node. 2 , use_tor) ;
478458
@@ -484,24 +464,14 @@ async fn proxy_to_single_node(
484464 let address = ( node. 1 . as_str ( ) , node. 2 ) ;
485465
486466 let maybe_tls_stream = timeout ( TIMEOUT , async {
487- let no_tls_stream: Box < dyn HyperStream > = if use_tor {
488- let tor_client = state. tor_client . as_ref ( ) . ok_or_else ( || {
489- SingleRequestError :: ConnectionError ( "Tor requested but client missing" . into ( ) )
490- } ) ?;
491-
492- let stream = tor_client
493- . connect ( address)
494- . await
495- . map_err ( |e| SingleRequestError :: ConnectionError ( format ! ( "{:?}" , e) ) ) ?;
496-
497- Box :: new ( stream)
498- } else {
499- let stream = TcpStream :: connect ( address)
467+ let no_tls_stream = match use_tor {
468+ true => state. tor_client . connect ( address) . await ,
469+ false => TcpStream :: connect ( address)
500470 . await
501- . map_err ( |e| SingleRequestError :: ConnectionError ( format ! ( "{:?}" , e ) ) ) ? ;
502-
503- Box :: new ( stream )
504- } ;
471+ . map ( |s| Box :: new ( s ) as Box < dyn HyperStream > )
472+ . map_err ( |e| e . into ( ) ) ,
473+ }
474+ . map_err ( |e| SingleRequestError :: ConnectionError ( format ! ( "{:?}" , e ) ) ) ? ;
505475
506476 maybe_wrap_with_tls ( no_tls_stream, & node. 0 , & node. 1 ) . await
507477 } )
0 commit comments