@@ -38,8 +38,11 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION");
38
38
/// Default channel size.
39
39
const DEFAULT_CHANNEL_SIZE : usize = 4096 ;
40
40
41
- /// Timeout for proxied TCP connections
42
- pub const TCP_SERVER_TIMEOUT : u64 = 30 ;
41
+ /// Timeout for local TCP server.
42
+ pub const LOCAL_TCP_TIMEOUT : u64 = 5 ;
43
+
44
+ /// Timeout for remote TCP server.
45
+ pub const REMOTE_TCP_TIMEOUT : u64 = 30 ;
43
46
44
47
/// Public result type error type used by the crate.
45
48
pub use crate :: error:: Error ;
@@ -154,6 +157,24 @@ impl PProxy {
154
157
}
155
158
}
156
159
160
+ async fn dial_tunnel (
161
+ & mut self ,
162
+ proxy_addr : SocketAddr ,
163
+ peer_id : PeerId ,
164
+ tunnel_id : TunnelId ,
165
+ ) -> Result < ( ) > {
166
+ let stream = tcp_connect_with_timeout ( proxy_addr, LOCAL_TCP_TIMEOUT ) . await ?;
167
+
168
+ let mut tunnel = Tunnel :: new ( peer_id, tunnel_id, self . command_tx . clone ( ) ) ;
169
+ let ( tunnel_tx, tunnel_rx) = mpsc:: channel ( 1024 ) ;
170
+ tunnel. listen ( stream, tunnel_rx) . await ?;
171
+
172
+ self . inbound_tunnels . insert ( ( peer_id, tunnel_id) , tunnel) ;
173
+ self . tunnel_txs . insert ( ( peer_id, tunnel_id) , tunnel_tx) ;
174
+
175
+ Ok ( ( ) )
176
+ }
177
+
157
178
async fn handle_p2p_server_event ( & mut self , event : P2pServerEvent ) -> Result < ( ) > {
158
179
tracing:: debug!( "received P2pServerEvent: {:?}" , event) ;
159
180
#[ allow( clippy:: single_match) ]
@@ -182,18 +203,18 @@ impl PProxy {
182
203
. parse ( )
183
204
. map_err ( |_| Error :: TunnelIdParseError ( msg. tunnel_id ) ) ?;
184
205
185
- let stream = tcp_connect_with_timeout ( proxy_addr, 60 ) . await ? ;
186
- let mut tunnel = Tunnel :: new ( peer , tunnel_id , self . command_tx . clone ( ) ) ;
187
- let ( tunnel_tx , tunnel_rx ) = mpsc :: channel ( 1024 ) ;
188
- tunnel . listen ( stream , tunnel_rx ) . await ? ;
189
-
190
- self . inbound_tunnels . insert ( ( peer , tunnel_id ) , tunnel ) ;
191
- self . tunnel_txs . insert ( ( peer , tunnel_id ) , tunnel_tx ) ;
206
+ let data = match self . dial_tunnel ( proxy_addr, peer , tunnel_id ) . await {
207
+ Ok ( _ ) => None ,
208
+ Err ( e ) => {
209
+ tracing :: warn! ( "failed to dial tunnel: {:?}" , e ) ;
210
+ Some ( e . to_string ( ) . into_bytes ( ) )
211
+ }
212
+ } ;
192
213
193
214
let response = proto:: Tunnel {
194
215
tunnel_id : tunnel_id. to_string ( ) ,
195
216
command : proto:: TunnelCommand :: ConnectResp . into ( ) ,
196
- data : None ,
217
+ data,
197
218
} ;
198
219
199
220
self . p2p_server
@@ -254,8 +275,14 @@ impl PProxy {
254
275
) )
255
276
} ) ?;
256
277
257
- tx. send ( Ok ( PProxyCommandResponse :: SendConnectCommand { } ) )
258
- . map_err ( |_| Error :: EssentialTaskClosed ) ?;
278
+ match msg. data {
279
+ None => tx. send ( Ok ( PProxyCommandResponse :: SendConnectCommand { } ) ) ,
280
+ Some ( data) => tx. send ( Err ( Error :: TunnelDialFailed (
281
+ String :: from_utf8 ( data)
282
+ . unwrap_or ( "Unknown (decode failed)" . to_string ( ) ) ,
283
+ ) ) ) ,
284
+ }
285
+ . map_err ( |_| Error :: EssentialTaskClosed ) ?;
259
286
}
260
287
261
288
_ => {
0 commit comments