@@ -14,8 +14,9 @@ use std::time::Duration;
1414use bitcoin:: secp256k1:: PublicKey ;
1515use lightning:: ln:: msgs:: SocketAddress ;
1616
17+ use crate :: config:: TorConfig ;
1718use crate :: logger:: { log_error, log_info, LdkLogger } ;
18- use crate :: types:: PeerManager ;
19+ use crate :: types:: { KeysManager , PeerManager } ;
1920use crate :: Error ;
2021
2122pub ( crate ) struct ConnectionManager < L : Deref + Clone + Sync + Send >
@@ -25,16 +26,22 @@ where
2526 pending_connections :
2627 Mutex < HashMap < PublicKey , Vec < tokio:: sync:: oneshot:: Sender < Result < ( ) , Error > > > > > ,
2728 peer_manager : Arc < PeerManager > ,
29+ tor_proxy_config : Option < TorConfig > ,
30+ keys_manager : Arc < KeysManager > ,
2831 logger : L ,
2932}
3033
3134impl < L : Deref + Clone + Sync + Send > ConnectionManager < L >
3235where
3336 L :: Target : LdkLogger ,
3437{
35- pub ( crate ) fn new ( peer_manager : Arc < PeerManager > , logger : L ) -> Self {
38+ pub ( crate ) fn new (
39+ peer_manager : Arc < PeerManager > , tor_proxy_config : Option < TorConfig > ,
40+ keys_manager : Arc < KeysManager > , logger : L ,
41+ ) -> Self {
3642 let pending_connections = Mutex :: new ( HashMap :: new ( ) ) ;
37- Self { pending_connections, peer_manager, logger }
43+
44+ Self { pending_connections, peer_manager, tor_proxy_config, keys_manager, logger }
3845 }
3946
4047 pub ( crate ) async fn connect_peer_if_necessary (
@@ -64,27 +71,114 @@ where
6471
6572 log_info ! ( self . logger, "Connecting to peer: {}@{}" , node_id, addr) ;
6673
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) ;
74+ let res = match addr {
75+ SocketAddress :: OnionV2 ( old_onion_addr) => {
76+ log_error ! (
77+ self . logger,
78+ "Failed to resolve network address {:?}: Resolution of OnionV2 addresses is currently unsupported." ,
79+ old_onion_addr
80+ ) ;
7781 self . propagate_result_to_subscribers ( & node_id, Err ( Error :: InvalidSocketAddress ) ) ;
78- Error :: InvalidSocketAddress
79- } ) ?;
82+ return Err ( Error :: InvalidSocketAddress ) ;
83+ } ,
84+ SocketAddress :: OnionV3 { .. } => {
85+ let proxy_config = self . tor_proxy_config . as_ref ( ) . ok_or_else ( || {
86+ log_error ! (
87+ self . logger,
88+ "Failed to resolve network address {:?}: Tor usage is not configured." ,
89+ addr
90+ ) ;
91+ self . propagate_result_to_subscribers (
92+ & node_id,
93+ Err ( Error :: InvalidSocketAddress ) ,
94+ ) ;
95+ Error :: InvalidSocketAddress
96+ } ) ?;
97+ let proxy_addr = proxy_config
98+ . proxy_address
99+ . to_socket_addrs ( )
100+ . map_err ( |e| {
101+ log_error ! (
102+ self . logger,
103+ "Failed to resolve Tor proxy network address {}: {}" ,
104+ proxy_config. proxy_address,
105+ e
106+ ) ;
107+ self . propagate_result_to_subscribers (
108+ & node_id,
109+ Err ( Error :: InvalidSocketAddress ) ,
110+ ) ;
111+ Error :: InvalidSocketAddress
112+ } ) ?
113+ . next ( )
114+ . ok_or_else ( || {
115+ log_error ! (
116+ self . logger,
117+ "Failed to resolve Tor proxy network address {}" ,
118+ proxy_config. proxy_address
119+ ) ;
120+ self . propagate_result_to_subscribers (
121+ & node_id,
122+ Err ( Error :: InvalidSocketAddress ) ,
123+ ) ;
124+ Error :: InvalidSocketAddress
125+ } ) ?;
126+ let connection_future = lightning_net_tokio:: tor_connect_outbound (
127+ Arc :: clone ( & self . peer_manager ) ,
128+ node_id,
129+ addr. clone ( ) ,
130+ proxy_addr,
131+ Arc :: clone ( & self . keys_manager ) ,
132+ ) ;
133+ self . await_connection ( connection_future, node_id, addr) . await
134+ } ,
135+ _ => {
136+ let socket_addr = addr
137+ . to_socket_addrs ( )
138+ . map_err ( |e| {
139+ log_error ! (
140+ self . logger,
141+ "Failed to resolve network address {}: {}" ,
142+ addr,
143+ e
144+ ) ;
145+ self . propagate_result_to_subscribers (
146+ & node_id,
147+ Err ( Error :: InvalidSocketAddress ) ,
148+ ) ;
149+ Error :: InvalidSocketAddress
150+ } ) ?
151+ . next ( )
152+ . ok_or_else ( || {
153+ log_error ! ( self . logger, "Failed to resolve network address {}" , addr) ;
154+ self . propagate_result_to_subscribers (
155+ & node_id,
156+ Err ( Error :: InvalidSocketAddress ) ,
157+ ) ;
158+ Error :: InvalidSocketAddress
159+ } ) ?;
160+ let connection_future = lightning_net_tokio:: connect_outbound (
161+ Arc :: clone ( & self . peer_manager ) ,
162+ node_id,
163+ socket_addr,
164+ ) ;
165+ self . await_connection ( connection_future, node_id, addr) . await
166+ } ,
167+ } ;
80168
81- let connection_future = lightning_net_tokio:: connect_outbound (
82- Arc :: clone ( & self . peer_manager ) ,
83- node_id,
84- socket_addr,
85- ) ;
169+ self . propagate_result_to_subscribers ( & node_id, res) ;
86170
87- let res = match connection_future. await {
171+ res
172+ }
173+
174+ async fn await_connection < F , CF > (
175+ & self , connection_future : F , node_id : PublicKey , addr : SocketAddress ,
176+ ) -> Result < ( ) , Error >
177+ where
178+ F : std:: future:: Future < Output = Option < CF > > ,
179+ CF : std:: future:: Future < Output = ( ) > ,
180+ {
181+ match connection_future. await {
88182 Some ( connection_closed_future) => {
89183 let mut connection_closed_future = Box :: pin ( connection_closed_future) ;
90184 loop {
@@ -106,11 +200,7 @@ where
106200 log_error ! ( self . logger, "Failed to connect to peer: {}@{}" , node_id, addr) ;
107201 Err ( Error :: ConnectionFailed )
108202 } ,
109- } ;
110-
111- self . propagate_result_to_subscribers ( & node_id, res) ;
112-
113- res
203+ }
114204 }
115205
116206 fn register_or_subscribe_pending_connection (
0 commit comments