Skip to content

Commit 0d8efcb

Browse files
committed
Add Socks5Proxy wrapper type
Useful for the `local` constructor which uses the typical proxy hosted by the Tor daemon.
1 parent fae3dc0 commit 0d8efcb

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

examples/bitcoin.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ async fn main() {
3232
))
3333
// Add some initial peers
3434
.add_peers(seeds.into_iter().map(From::from))
35+
// Connections over Tor are supported by Socks5 proxy
36+
// .socks5_proxy(bip157::Socks5Proxy::local())
3537
// Create the node and client
3638
.build();
3739
// Run the node on a separate task

src/builder.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
use std::net::SocketAddr;
21
use std::{path::PathBuf, time::Duration};
32

43
use bitcoin::Network;
54

65
use super::{client::Client, node::Node};
76
use crate::chain::ChainState;
87
use crate::network::ConnectionType;
9-
use crate::TrustedPeer;
108
use crate::{Config, FilterType};
9+
use crate::{Socks5Proxy, TrustedPeer};
1110

1211
const MIN_PEERS: u8 = 1;
1312
const MAX_PEERS: u8 = 15;
@@ -125,7 +124,7 @@ impl Builder {
125124

126125
/// Route network traffic through a Tor daemon using a Socks5 proxy. Currently, proxies
127126
/// must be reachable by IP address.
128-
pub fn socks5_proxy(mut self, proxy: impl Into<SocketAddr>) -> Self {
127+
pub fn socks5_proxy(mut self, proxy: impl Into<Socks5Proxy>) -> Self {
129128
let ip_addr = proxy.into();
130129
let connection = ConnectionType::Socks5Proxy(ip_addr);
131130
self.config.connection_type = connection;

src/lib.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub mod node;
6262

6363
use chain::Filter;
6464

65-
use std::net::{IpAddr, SocketAddr};
65+
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
6666
use std::path::PathBuf;
6767

6868
// Re-exports
@@ -294,6 +294,26 @@ impl From<SocketAddr> for TrustedPeer {
294294
}
295295
}
296296

297+
/// Route network traffic through a Socks5 proxy, typically used by a Tor daemon.
298+
#[derive(Debug, Clone)]
299+
pub struct Socks5Proxy(SocketAddr);
300+
301+
impl Socks5Proxy {
302+
/// Connect to the default local Socks5 proxy hosted at `127.0.0.1:9050`.
303+
pub const fn local() -> Self {
304+
Socks5Proxy(SocketAddr::new(
305+
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
306+
9050,
307+
))
308+
}
309+
}
310+
311+
impl From<SocketAddr> for Socks5Proxy {
312+
fn from(value: SocketAddr) -> Self {
313+
Self(value)
314+
}
315+
}
316+
297317
#[derive(Debug, Clone, Copy)]
298318
enum NodeState {
299319
// We are behind on block headers according to our peers.

src/network/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{
22
collections::{HashMap, HashSet},
33
fs::{self, File},
4-
net::{IpAddr, SocketAddr},
4+
net::IpAddr,
55
path::PathBuf,
66
time::Duration,
77
};
@@ -28,6 +28,8 @@ use tokio::{net::TcpStream, time::Instant};
2828

2929
use error::PeerError;
3030

31+
use crate::Socks5Proxy;
32+
3133
pub(crate) mod dns;
3234
pub(crate) mod error;
3335
pub(crate) mod inbound;
@@ -130,11 +132,11 @@ impl LastBlockMonitor {
130132
}
131133
}
132134

133-
#[derive(Debug, Clone, Copy, Default)]
135+
#[derive(Debug, Clone, Default)]
134136
pub(crate) enum ConnectionType {
135137
#[default]
136138
ClearNet,
137-
Socks5Proxy(SocketAddr),
139+
Socks5Proxy(Socks5Proxy),
138140
}
139141

140142
impl ConnectionType {
@@ -177,7 +179,7 @@ impl ConnectionType {
177179
_ => return Err(PeerError::UnreachableSocketAddr),
178180
};
179181
let socks5_timeout =
180-
tokio::time::timeout(handshake_timeout, create_socks5(*proxy, addr, port))
182+
tokio::time::timeout(handshake_timeout, create_socks5(proxy.0, addr, port))
181183
.await
182184
.map_err(|_| PeerError::ConnectionFailed)?;
183185
let tcp_stream = socks5_timeout.map_err(PeerError::Socks5)?;

0 commit comments

Comments
 (0)