Skip to content

Commit adf445e

Browse files
committed
Add SOCKS5 proxy support to lightning-net-tokio
New function connect_outbound_via_socks5() mirrors connect_outbound() but routes the TCP connection through a SOCKS5 proxy via tokio-socks. The existing connect_outbound() is untouched. Gated behind an optional "socks" feature flag so downstream crates that don't need proxy support pay zero cost. ldk-node will use this to route peer connections through Tor or similar SOCKS5 proxies.
1 parent 070b31d commit adf445e

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

lightning-net-tokio/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ edition = "2021"
1515
all-features = true
1616
rustdoc-args = ["--cfg", "docsrs"]
1717

18+
[features]
19+
socks = ["tokio-socks"]
20+
1821
[dependencies]
1922
bitcoin = "0.32.2"
2023
lightning = { version = "0.2.0", path = "../lightning" }
2124
tokio = { version = "1.35", features = [ "rt", "sync", "net", "time" ] }
25+
tokio-socks = { version = "0.5", optional = true }
2226

2327
[dev-dependencies]
2428
tokio = { version = "1.35", features = [ "macros", "rt", "rt-multi-thread", "sync", "net", "time" ] }

lightning-net-tokio/src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,33 @@ where
470470
}
471471
}
472472

473+
/// Like [`connect_outbound`], but routes the TCP connection through a SOCKS5 proxy.
474+
///
475+
/// `proxy_addr` is the address of the SOCKS5 proxy (e.g. `127.0.0.1:1080`).
476+
/// The proxy connects to `addr` on our behalf, then we hand the resulting stream to
477+
/// [`setup_outbound`].
478+
///
479+
/// Available only when the `socks` feature is enabled.
480+
#[cfg(feature = "socks")]
481+
#[cfg_attr(docsrs, doc(cfg(feature = "socks")))]
482+
pub async fn connect_outbound_via_socks5<PM: Deref + 'static + Send + Sync + Clone>(
483+
peer_manager: PM, their_node_id: PublicKey, addr: SocketAddr, proxy_addr: SocketAddr,
484+
) -> Option<impl std::future::Future<Output = ()>>
485+
where
486+
PM::Target: APeerManager<Descriptor = SocketDescriptor>,
487+
{
488+
let connect_fut = async {
489+
tokio_socks::tcp::Socks5Stream::connect(proxy_addr, addr)
490+
.await
491+
.map(|s| s.into_inner().into_std().unwrap())
492+
};
493+
if let Ok(Ok(stream)) = time::timeout(Duration::from_secs(10), connect_fut).await {
494+
Some(setup_outbound(peer_manager, their_node_id, stream))
495+
} else {
496+
None
497+
}
498+
}
499+
473500
const SOCK_WAKER_VTABLE: task::RawWakerVTable = task::RawWakerVTable::new(
474501
clone_socket_waker,
475502
wake_socket_waker,

0 commit comments

Comments
 (0)