Skip to content

Commit 27bb7ff

Browse files
fix(monero-rpc-pool): only try to bypass unforced Tor
Bypassing Tor on TorBackend::Socks breaks everything, because /all/ traffic needs to go through the proxy (normal connect() is broken on Tails) Bypassing Tor on TorBackend::Torsocks is misleading, because the log implies the request would be performed over clearnet (it won't be), but doesn't otherwise impact anything, since Torsocks is the same as clearnet from the program's POV
1 parent d7d113a commit 27bb7ff

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

monero-rpc-pool/src/proxy.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ pub async fn proxy_handler(State(state): State<AppState>, request: Request) -> R
130130
}
131131
}
132132

133+
/// Check if we're using Tor for this request
134+
///
135+
/// Use Tor if:
136+
/// 1. the environment can *only* route clearnet traffic over Tor
137+
/// 2. it's enabled, ready, and the request didn't ask to be routed over clearnet
138+
fn use_tor_for_request(state: &AppState, request: &CloneableRequest) -> bool {
139+
state.tor_client.masquerade_clearnet()
140+
|| (state.tor_client.ready_for_traffic() && !request.clearnet_whitelisted())
141+
}
142+
133143
/// Given a Vec of nodes, proxy the given request to multiple nodes until we get a successful response
134144
async fn proxy_to_multiple_nodes(
135145
state: &AppState,
@@ -141,8 +151,7 @@ async fn proxy_to_multiple_nodes(
141151
}
142152

143153
// Sort nodes to prioritize those with available connections
144-
// Check if we're using Tor for this request
145-
let use_tor = state.tor_client.ready_for_traffic() && !request.clearnet_whitelisted();
154+
let use_tor = use_tor_for_request(state, &request);
146155

147156
// Create a vector of (node, has_connection) pairs
148157
let mut nodes_with_availability = Vec::new();
@@ -447,12 +456,11 @@ async fn proxy_to_single_node(
447456
) -> Result<Response, SingleRequestError> {
448457
use crate::connection_pool::GuardedSender;
449458

450-
if request.clearnet_whitelisted() {
459+
let use_tor = use_tor_for_request(state, &request);
460+
if !use_tor && request.clearnet_whitelisted() {
451461
tracing::trace!("Request is whitelisted, sending over clearnet");
452462
}
453463

454-
let use_tor = state.tor_client.ready_for_traffic() && !request.clearnet_whitelisted();
455-
456464
let key = (node.0.clone(), node.1.clone(), node.2, use_tor);
457465

458466
// Try to reuse an idle HTTP connection first.

monero-rpc-pool/src/tor.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ impl<T: AsyncRead + AsyncWrite + Unpin + Send> HyperStream for T {}
1111
pub trait TorBackendRpc {
1212
fn is_some(&self) -> bool;
1313
fn ready_for_traffic(&self) -> bool;
14+
fn masquerade_clearnet(&self) -> bool;
1415
async fn connect(&self, address: (&str, u16)) -> anyhow::Result<Box<dyn HyperStream>>;
1516
}
1617
impl TorBackendRpc for TorBackend {
@@ -26,6 +27,13 @@ impl TorBackendRpc for TorBackend {
2627
}
2728
}
2829

30+
fn masquerade_clearnet(&self) -> bool {
31+
match self {
32+
TorBackend::Arti(..) | TorBackend::None => false,
33+
TorBackend::Socks(..) => true,
34+
}
35+
}
36+
2937
async fn connect(&self, address: (&str, u16)) -> anyhow::Result<Box<dyn HyperStream>> {
3038
match self {
3139
TorBackend::Arti(tor_client) => Ok(Box::new(tor_client.connect(address).await?)),

0 commit comments

Comments
 (0)