@@ -13,7 +13,9 @@ use std::time::Duration;
1313
1414use bitcoin:: secp256k1:: PublicKey ;
1515use lightning:: ln:: msgs:: SocketAddress ;
16+ use lightning:: sign:: RandomBytes ;
1617
18+ use crate :: config:: TorConfig ;
1719use crate :: logger:: { log_error, log_info, LdkLogger } ;
1820use crate :: types:: PeerManager ;
1921use crate :: Error ;
@@ -25,16 +27,23 @@ where
2527 pending_connections :
2628 Mutex < HashMap < PublicKey , Vec < tokio:: sync:: oneshot:: Sender < Result < ( ) , Error > > > > > ,
2729 peer_manager : Arc < PeerManager > ,
30+ tor_proxy_config : Option < TorConfig > ,
31+ tor_proxy_rng : Arc < RandomBytes > ,
2832 logger : L ,
2933}
3034
3135impl < L : Deref + Clone + Sync + Send > ConnectionManager < L >
3236where
3337 L :: Target : LdkLogger ,
3438{
35- pub ( crate ) fn new ( peer_manager : Arc < PeerManager > , logger : L ) -> Self {
39+ pub ( crate ) fn new (
40+ peer_manager : Arc < PeerManager > , tor_proxy_config : Option < TorConfig > ,
41+ ephemeral_random_data : [ u8 ; 32 ] , logger : L ,
42+ ) -> Self {
3643 let pending_connections = Mutex :: new ( HashMap :: new ( ) ) ;
37- Self { pending_connections, peer_manager, logger }
44+ let tor_proxy_rng = Arc :: new ( RandomBytes :: new ( ephemeral_random_data) ) ;
45+
46+ Self { pending_connections, peer_manager, tor_proxy_config, tor_proxy_rng, logger }
3847 }
3948
4049 pub ( crate ) async fn connect_peer_if_necessary (
@@ -64,27 +73,157 @@ where
6473
6574 log_info ! ( self . logger, "Connecting to peer: {}@{}" , node_id, addr) ;
6675
67- let socket_addr = addr
68- . to_socket_addrs ( )
69- . map_err ( |e| {
70- log_error ! ( self . logger, "Failed to resolve network address {}: {}" , addr, e) ;
71- self . propagate_result_to_subscribers ( & node_id, Err ( Error :: InvalidSocketAddress ) ) ;
72- Error :: InvalidSocketAddress
73- } ) ?
74- . next ( )
75- . ok_or_else ( || {
76- log_error ! ( self . logger, "Failed to resolve network address {}" , addr) ;
76+ let res = match addr {
77+ SocketAddress :: OnionV2 ( old_onion_addr) => {
78+ log_error ! (
79+ self . logger,
80+ "Failed to resolve network address {:?}: Resolution of OnionV2 addresses is currently unsupported." ,
81+ old_onion_addr
82+ ) ;
7783 self . propagate_result_to_subscribers ( & node_id, Err ( Error :: InvalidSocketAddress ) ) ;
78- Error :: InvalidSocketAddress
79- } ) ?;
84+ return Err ( Error :: InvalidSocketAddress ) ;
85+ } ,
86+ SocketAddress :: OnionV3 { .. } => {
87+ let proxy_config = self . tor_proxy_config . as_ref ( ) . ok_or_else ( || {
88+ log_error ! (
89+ self . logger,
90+ "Failed to resolve network address {:?}: Tor usage is not configured." ,
91+ addr
92+ ) ;
93+ self . propagate_result_to_subscribers (
94+ & node_id,
95+ Err ( Error :: InvalidSocketAddress ) ,
96+ ) ;
97+ Error :: InvalidSocketAddress
98+ } ) ?;
99+ let proxy_addr = proxy_config
100+ . proxy_address
101+ . to_socket_addrs ( )
102+ . map_err ( |e| {
103+ log_error ! (
104+ self . logger,
105+ "Failed to resolve Tor proxy network address {}: {}" ,
106+ proxy_config. proxy_address,
107+ e
108+ ) ;
109+ self . propagate_result_to_subscribers (
110+ & node_id,
111+ Err ( Error :: InvalidSocketAddress ) ,
112+ ) ;
113+ Error :: InvalidSocketAddress
114+ } ) ?
115+ . next ( )
116+ . ok_or_else ( || {
117+ log_error ! (
118+ self . logger,
119+ "Failed to resolve Tor proxy network address {}" ,
120+ proxy_config. proxy_address
121+ ) ;
122+ self . propagate_result_to_subscribers (
123+ & node_id,
124+ Err ( Error :: InvalidSocketAddress ) ,
125+ ) ;
126+ Error :: InvalidSocketAddress
127+ } ) ?;
128+ let connection_future = lightning_net_tokio:: tor_connect_outbound (
129+ Arc :: clone ( & self . peer_manager ) ,
130+ node_id,
131+ addr. clone ( ) ,
132+ proxy_addr,
133+ Arc :: clone ( & self . tor_proxy_rng ) ,
134+ ) ;
135+ self . await_connection ( connection_future, node_id, addr) . await
136+ } ,
137+ _ => {
138+ let socket_addr = addr
139+ . to_socket_addrs ( )
140+ . map_err ( |e| {
141+ log_error ! (
142+ self . logger,
143+ "Failed to resolve network address {}: {}" ,
144+ addr,
145+ e
146+ ) ;
147+ self . propagate_result_to_subscribers (
148+ & node_id,
149+ Err ( Error :: InvalidSocketAddress ) ,
150+ ) ;
151+ Error :: InvalidSocketAddress
152+ } ) ?
153+ . next ( )
154+ . ok_or_else ( || {
155+ log_error ! ( self . logger, "Failed to resolve network address {}" , addr) ;
156+ self . propagate_result_to_subscribers (
157+ & node_id,
158+ Err ( Error :: InvalidSocketAddress ) ,
159+ ) ;
160+ Error :: InvalidSocketAddress
161+ } ) ?;
162+ match & self . tor_proxy_config {
163+ None | Some ( TorConfig { proxy_all_outbound : false , .. } ) => {
164+ let connection_future = lightning_net_tokio:: connect_outbound (
165+ Arc :: clone ( & self . peer_manager ) ,
166+ node_id,
167+ socket_addr,
168+ ) ;
169+ self . await_connection ( connection_future, node_id, addr) . await
170+ } ,
171+ Some ( proxy_config) => {
172+ let proxy_addr = proxy_config
173+ . proxy_address
174+ . to_socket_addrs ( )
175+ . map_err ( |e| {
176+ log_error ! (
177+ self . logger,
178+ "Failed to resolve Tor proxy network address {}: {}" ,
179+ proxy_config. proxy_address,
180+ e
181+ ) ;
182+ self . propagate_result_to_subscribers (
183+ & node_id,
184+ Err ( Error :: InvalidSocketAddress ) ,
185+ ) ;
186+ Error :: InvalidSocketAddress
187+ } ) ?
188+ . next ( )
189+ . ok_or_else ( || {
190+ log_error ! (
191+ self . logger,
192+ "Failed to resolve Tor proxy network address {}" ,
193+ proxy_config. proxy_address
194+ ) ;
195+ self . propagate_result_to_subscribers (
196+ & node_id,
197+ Err ( Error :: InvalidSocketAddress ) ,
198+ ) ;
199+ Error :: InvalidSocketAddress
200+ } ) ?;
201+ let connection_future = lightning_net_tokio:: tor_connect_outbound (
202+ Arc :: clone ( & self . peer_manager ) ,
203+ node_id,
204+ addr. clone ( ) ,
205+ proxy_addr,
206+ Arc :: clone ( & self . tor_proxy_rng ) ,
207+ ) ;
208+ self . await_connection ( connection_future, node_id, addr) . await
209+ } ,
210+ }
211+ } ,
212+ } ;
213+
214+ self . propagate_result_to_subscribers ( & node_id, res) ;
80215
81- let connection_future = lightning_net_tokio:: connect_outbound (
82- Arc :: clone ( & self . peer_manager ) ,
83- node_id,
84- socket_addr,
85- ) ;
216+ res
217+ }
86218
87- let res = match connection_future. await {
219+ async fn await_connection < F , CF > (
220+ & self , connection_future : F , node_id : PublicKey , addr : SocketAddress ,
221+ ) -> Result < ( ) , Error >
222+ where
223+ F : std:: future:: Future < Output = Option < CF > > ,
224+ CF : std:: future:: Future < Output = ( ) > ,
225+ {
226+ match connection_future. await {
88227 Some ( connection_closed_future) => {
89228 let mut connection_closed_future = Box :: pin ( connection_closed_future) ;
90229 loop {
@@ -106,11 +245,7 @@ where
106245 log_error ! ( self . logger, "Failed to connect to peer: {}@{}" , node_id, addr) ;
107246 Err ( Error :: ConnectionFailed )
108247 } ,
109- } ;
110-
111- self . propagate_result_to_subscribers ( & node_id, res) ;
112-
113- res
248+ }
114249 }
115250
116251 fn register_or_subscribe_pending_connection (
0 commit comments