Skip to content

Commit 4518685

Browse files
committed
lib: remove arti dependency in favor of tor proxy
1 parent a7118a7 commit 4518685

File tree

13 files changed

+396
-3950
lines changed

13 files changed

+396
-3950
lines changed

Cargo.lock

Lines changed: 310 additions & 3572 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,10 @@ tokio = { version = "1", default-features = false, features = [
3131

3232
# Optional dependencies
3333
rusqlite = { version = "0.31.0", features = ["bundled"], optional = true }
34-
arti-client = { version = "0.21.0", features = [
35-
"rustls",
36-
"tokio",
37-
"onion-service-client",
38-
"experimental-api",
39-
], default-features = false, optional = true }
40-
tor-rtcompat = { version = "0.21.0", features = ["tokio"], optional = true }
4134

4235
[features]
4336
default = ["database"]
4437
database = ["rusqlite"]
45-
tor = ["arti-client", "tor-rtcompat"]
4638
filter-control = []
4739

4840
[dev-dependencies]
@@ -73,11 +65,6 @@ path = "example/testnet4.rs"
7365
name = "rescan"
7466
path = "example/rescan.rs"
7567

76-
[[example]]
77-
name = "tor"
78-
path = "example/tor.rs"
79-
required-features = ["tor"]
80-
8168
[[example]]
8269
name = "managed"
8370
path = "example/managed.rs"

example/tor.rs

Lines changed: 0 additions & 111 deletions
This file was deleted.

src/builder.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ use crate::db::error::SqlInitializationError;
1111
#[cfg(feature = "database")]
1212
use crate::db::sqlite::{headers::SqliteHeaderDb, peers::SqlitePeerDb};
1313
use crate::network::dns::{DnsResolver, DNS_RESOLVER_PORT};
14+
use crate::network::ConnectionType;
1415
use crate::{
1516
chain::checkpoints::HeaderCheckpoint,
1617
db::traits::{HeaderStore, PeerStore},
1718
};
18-
use crate::{ConnectionType, FilterSyncPolicy, LogLevel, PeerStoreSizeConfig, TrustedPeer};
19+
use crate::{FilterSyncPolicy, LogLevel, PeerStoreSizeConfig, TrustedPeer};
1920

2021
#[cfg(feature = "database")]
2122
/// The default node returned from the [`NodeBuilder`](crate::core).
@@ -149,12 +150,6 @@ impl NodeBuilder {
149150
self
150151
}
151152

152-
/// Set the desired communication channel. Either directly over TCP or over the Tor network.
153-
pub fn connection_type(mut self, connection_type: ConnectionType) -> Self {
154-
self.config.connection_type = connection_type;
155-
self
156-
}
157-
158153
/// Set the time duration a peer has to respond to a message from the local node.
159154
///
160155
/// ## Note
@@ -193,6 +188,15 @@ impl NodeBuilder {
193188
self
194189
}
195190

191+
/// Route network traffic through a Tor daemon using a Socks5 proxy. Currently, proxies
192+
/// must be reachable by IP address.
193+
pub fn socks5_proxy(mut self, proxy: impl Into<SocketAddr>) -> Self {
194+
let ip_addr = proxy.into();
195+
let connection = ConnectionType::Socks5Proxy(ip_addr);
196+
self.config.connection_type = connection;
197+
self
198+
}
199+
196200
/// Stop the node from downloading and checking compact block filters until an explicit command by the client is made.
197201
/// This is only useful if the scripts to check for may not be known do to some expensive computation, like in a silent
198202
/// payments context.

src/config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use std::{collections::HashSet, path::PathBuf, time::Duration};
33
use bitcoin::ScriptBuf;
44

55
use crate::{
6-
chain::checkpoints::HeaderCheckpoint, network::dns::DnsResolver, ConnectionType,
6+
chain::checkpoints::HeaderCheckpoint,
7+
network::{dns::DnsResolver, ConnectionType},
78
FilterSyncPolicy, LogLevel, PeerStoreSizeConfig, TrustedPeer,
89
};
910

src/lib.rs

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,6 @@ use std::collections::HashSet;
116116

117117
use std::net::{IpAddr, SocketAddr};
118118

119-
#[cfg(feature = "tor")]
120-
pub use arti_client::{TorClient, TorClientConfig};
121-
#[cfg(feature = "tor")]
122-
use tor_rtcompat::PreferredRuntime;
123-
124119
// Re-exports
125120
#[doc(inline)]
126121
pub use chain::checkpoints::{
@@ -320,21 +315,6 @@ impl TrustedPeer {
320315
}
321316
}
322317

323-
/// Create a new peer from a TorV3 service and port.
324-
#[cfg(feature = "tor")]
325-
pub fn new_from_tor_v3(
326-
public_key: [u8; 32],
327-
port: Option<u16>,
328-
services: ServiceFlags,
329-
) -> Self {
330-
let address = AddrV2::TorV3(public_key);
331-
Self {
332-
address,
333-
port,
334-
known_services: services,
335-
}
336-
}
337-
338318
/// Create a new trusted peer using the default port for the network.
339319
pub fn from_ip(ip_addr: impl Into<IpAddr>) -> Self {
340320
let address = match ip_addr.into() {
@@ -362,17 +342,6 @@ impl TrustedPeer {
362342
}
363343
}
364344

365-
/// Create a new peer from a TorV3 service.
366-
#[cfg(feature = "tor")]
367-
pub fn from_tor_v3(public_key: [u8; 32]) -> Self {
368-
let address = AddrV2::TorV3(public_key);
369-
Self {
370-
address,
371-
port: None,
372-
known_services: ServiceFlags::NONE,
373-
}
374-
}
375-
376345
/// The IP address of the trusted peer.
377346
pub fn address(&self) -> AddrV2 {
378347
self.address.clone()
@@ -422,18 +391,6 @@ impl From<SocketAddr> for TrustedPeer {
422391
}
423392
}
424393

425-
/// How to connect to peers on the peer-to-peer network
426-
#[derive(Default, Clone)]
427-
#[non_exhaustive]
428-
pub enum ConnectionType {
429-
/// Version one peer-to-peer connections
430-
#[default]
431-
ClearNet,
432-
/// Connect to peers over Tor
433-
#[cfg(feature = "tor")]
434-
Tor(TorClient<PreferredRuntime>),
435-
}
436-
437394
/// Configure how many peers will be stored.
438395
#[derive(Debug, Default, Clone)]
439396
pub enum PeerStoreSizeConfig {

src/network/mod.rs

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1+
use std::{
2+
net::{IpAddr, SocketAddr},
3+
time::Duration,
4+
};
5+
16
use bitcoin::{
27
consensus::Decodable,
38
io::Read,
4-
p2p::{message::CommandString, Magic},
9+
p2p::{address::AddrV2, message::CommandString, Magic},
510
};
6-
use std::time::Duration;
7-
use tokio::time::Instant;
11+
use tokio::{
12+
io::{AsyncRead, AsyncWrite},
13+
net::TcpStream,
14+
sync::Mutex,
15+
time::Instant,
16+
};
17+
18+
use error::PeerError;
819

920
pub(crate) mod counter;
1021
pub(crate) mod dns;
@@ -16,14 +27,17 @@ pub(crate) mod peer;
1627
pub(crate) mod peer_map;
1728
#[allow(dead_code)]
1829
pub(crate) mod reader;
19-
#[cfg(feature = "tor")]
20-
pub(crate) mod tor;
30+
pub(crate) mod socks;
2131
pub(crate) mod traits;
2232

2333
pub const PROTOCOL_VERSION: u32 = 70016;
2434
pub const KYOTO_VERSION: &str = "0.8.0";
2535
pub const RUST_BITCOIN_VERSION: &str = "0.32.4";
2636
const THIRTY_MINS: u64 = 60 * 30;
37+
const CONNECTION_TIMEOUT: u64 = 2;
38+
39+
pub(crate) type StreamReader = Mutex<Box<dyn AsyncRead + Send + Unpin>>;
40+
pub(crate) type StreamWriter = Mutex<Box<dyn AsyncWrite + Send + Unpin>>;
2741

2842
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2943
pub(crate) struct PeerId(pub(crate) u32);
@@ -86,6 +100,47 @@ impl LastBlockMonitor {
86100
}
87101
}
88102

103+
#[derive(Debug, Clone, Copy, Default)]
104+
pub(crate) enum ConnectionType {
105+
#[default]
106+
ClearNet,
107+
Socks5Proxy(SocketAddr),
108+
}
109+
110+
impl ConnectionType {
111+
pub(crate) fn can_connect(&self, addr: &AddrV2) -> bool {
112+
match &self {
113+
Self::ClearNet => matches!(addr, AddrV2::Ipv4(_) | AddrV2::Ipv6(_)),
114+
Self::Socks5Proxy(_) => matches!(addr, AddrV2::Ipv4(_) | AddrV2::Ipv6(_)),
115+
}
116+
}
117+
118+
pub(crate) async fn connect(
119+
&self,
120+
addr: AddrV2,
121+
port: u16,
122+
) -> Result<(StreamReader, StreamWriter), PeerError> {
123+
let socket_addr = match addr {
124+
AddrV2::Ipv4(ip) => IpAddr::V4(ip),
125+
AddrV2::Ipv6(ip) => IpAddr::V6(ip),
126+
_ => return Err(PeerError::UnreachableSocketAddr),
127+
};
128+
let timeout = tokio::time::timeout(
129+
Duration::from_secs(CONNECTION_TIMEOUT),
130+
TcpStream::connect((socket_addr, port)),
131+
)
132+
.await
133+
.map_err(|_| PeerError::ConnectionFailed)?;
134+
match timeout {
135+
Ok(stream) => {
136+
let (reader, writer) = stream.into_split();
137+
Ok((Mutex::new(Box::new(reader)), Mutex::new(Box::new(writer))))
138+
}
139+
Err(_) => Err(PeerError::ConnectionFailed),
140+
}
141+
}
142+
}
143+
89144
pub(crate) struct V1Header {
90145
magic: Magic,
91146
_command: CommandString,

src/network/parsers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use tokio::io::AsyncReadExt;
88
use crate::prelude::FutureResult;
99

1010
use super::error::PeerReadError;
11-
use super::traits::{MessageParser, StreamReader};
1211
use super::V1Header;
12+
use super::{traits::MessageParser, StreamReader};
1313

1414
const MAX_MESSAGE_BYTES: u32 = 1024 * 1024 * 32;
1515

0 commit comments

Comments
 (0)