Skip to content

Commit 3442ce3

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

File tree

13 files changed

+382
-3945
lines changed

13 files changed

+382
-3945
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 & 8 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]

example/tor.rs

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

src/builder.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
chain::checkpoints::HeaderCheckpoint,
1616
db::traits::{HeaderStore, PeerStore},
1717
};
18-
use crate::{ConnectionType, FilterSyncPolicy, LogLevel, PeerStoreSizeConfig, TrustedPeer};
18+
use crate::{FilterSyncPolicy, LogLevel, PeerStoreSizeConfig, TrustedPeer};
1919

2020
#[cfg(feature = "database")]
2121
/// The default node returned from the [`NodeBuilder`](crate::core).
@@ -149,12 +149,6 @@ impl NodeBuilder {
149149
self
150150
}
151151

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-
158152
/// Set the time duration a peer has to respond to a message from the local node.
159153
///
160154
/// ## Note

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: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1+
use std::{net::IpAddr, time::Duration};
2+
13
use bitcoin::{
24
consensus::Decodable,
35
io::Read,
4-
p2p::{message::CommandString, Magic},
6+
p2p::{address::AddrV2, message::CommandString, Magic},
7+
};
8+
use tokio::{
9+
io::{AsyncRead, AsyncWrite},
10+
net::TcpStream,
11+
sync::Mutex,
12+
time::Instant,
513
};
6-
use std::time::Duration;
7-
use tokio::time::Instant;
14+
15+
use error::PeerError;
816

917
pub(crate) mod counter;
1018
pub(crate) mod dns;
@@ -16,14 +24,17 @@ pub(crate) mod peer;
1624
pub(crate) mod peer_map;
1725
#[allow(dead_code)]
1826
pub(crate) mod reader;
19-
#[cfg(feature = "tor")]
20-
pub(crate) mod tor;
27+
pub(crate) mod socks;
2128
pub(crate) mod traits;
2229

2330
pub const PROTOCOL_VERSION: u32 = 70016;
2431
pub const KYOTO_VERSION: &str = "0.8.0";
2532
pub const RUST_BITCOIN_VERSION: &str = "0.32.4";
2633
const THIRTY_MINS: u64 = 60 * 30;
34+
const CONNECTION_TIMEOUT: u64 = 2;
35+
36+
pub(crate) type StreamReader = Mutex<Box<dyn AsyncRead + Send + Unpin>>;
37+
pub(crate) type StreamWriter = Mutex<Box<dyn AsyncWrite + Send + Unpin>>;
2738

2839
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2940
pub(crate) struct PeerId(pub(crate) u32);
@@ -86,6 +97,47 @@ impl LastBlockMonitor {
8697
}
8798
}
8899

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

src/network/peer.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,11 @@ use crate::{
2323
};
2424

2525
use super::{
26-
counter::MessageCounter,
27-
error::PeerError,
28-
parsers::V1MessageParser,
29-
reader::Reader,
30-
traits::{MessageGenerator, StreamReader, StreamWriter},
31-
PeerId, PeerTimeoutConfig,
26+
counter::MessageCounter, error::PeerError, parsers::V1MessageParser, reader::Reader,
27+
traits::MessageGenerator, PeerId, PeerTimeoutConfig, StreamReader, StreamWriter,
3228
};
3329

34-
#[cfg(not(feature = "tor"))]
3530
use super::outbound_messages::V2OutboundMessage;
36-
#[cfg(not(feature = "tor"))]
3731
use super::parsers::V2MessageParser;
3832

3933
const MESSAGE_TIMEOUT: u64 = 2;
@@ -88,7 +82,6 @@ impl Peer {
8882
let writer = lock.deref_mut();
8983

9084
// If a peer signals for V2 we will use it, otherwise just use plaintext.
91-
#[cfg(not(feature = "tor"))]
9285
let (message_mutex, mut peer_reader) = if self.services.has(ServiceFlags::P2P_V2) {
9386
let mut lock = reader.lock().await;
9487
let read_lock = lock.deref_mut();
@@ -118,15 +111,6 @@ impl Peer {
118111
(message_mutex, reader)
119112
};
120113

121-
// V2 handshakes fail frequently over Tor and messages are encrypted over relays anyway.
122-
#[cfg(feature = "tor")]
123-
let (message_mutex, mut peer_reader) = {
124-
let outbound_messages = V1OutboundMessage::new(self.network);
125-
let message_mutex: MutexMessageGenerator = Mutex::new(Box::new(outbound_messages));
126-
let reader = Reader::new(V1MessageParser::new(reader, self.network), tx);
127-
(message_mutex, reader)
128-
};
129-
130114
let mut message_lock = message_mutex.lock().await;
131115
let outbound_messages = message_lock.deref_mut();
132116
let message = outbound_messages.version_message(None)?;

0 commit comments

Comments
 (0)