From df8ab4dd75ec2ed98b28db7d329819ce53a3c3a1 Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Thu, 11 Aug 2022 12:52:35 +0200 Subject: [PATCH] Feat: Add proxy arg to client creation Allowing the user to specify a proxy, is useful to allow him to connect to a server through tor, analyze the traffic, or go through any number of anonymising services. --- Cargo.toml | 2 +- src/lib.rs | 24 ++++++++++++++++++------ tests/rpc.rs | 2 +- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 69ed6d2..2d6026b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ hex = "0.4" http = "0.2" jsonrpc-core = "18" monero = { version = "0.17", features = ["serde"] } -reqwest = { version = "0.11", features = ["json"] } +reqwest = { version = "0.11", features = ["json", "socks"] } serde = { version = "1", features = ["derive"] } serde_json = "1" tracing = "0.1" diff --git a/src/lib.rs b/src/lib.rs index 08bfb73..88cdf1a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -83,12 +83,24 @@ pub struct LwsRpcClient { } impl LwsRpcClient { - pub fn new(addr: String) -> Self { - Self { - inner: CallerWrapper(Arc::new(RemoteCaller { - http_client: reqwest::ClientBuilder::new().build().unwrap(), - addr, - })), + pub fn new(addr: String, proxy: Option) -> Self { + if let Some(proxy_address) = proxy { + Self { + inner: CallerWrapper(Arc::new(RemoteCaller { + http_client: reqwest::Client::builder() + .proxy(reqwest::Proxy::all(proxy_address).unwrap()) + .build() + .unwrap(), + addr, + })), + } + } else { + Self { + inner: CallerWrapper(Arc::new(RemoteCaller { + http_client: reqwest::ClientBuilder::new().build().unwrap(), + addr, + })), + } } } diff --git a/tests/rpc.rs b/tests/rpc.rs index f5f7a85..394e28c 100644 --- a/tests/rpc.rs +++ b/tests/rpc.rs @@ -83,6 +83,6 @@ async fn setup_monero() -> ( regtest.generate_blocks(100, address).await.unwrap(); let dhost = env::var("MONERO_DAEMON_HOST").unwrap_or_else(|_| "localhost".into()); - let lws_client = monero_lws::LwsRpcClient::new(format!("http://{}:38884", dhost)); + let lws_client = monero_lws::LwsRpcClient::new(format!("http://{}:38884", dhost), None); (address, viewkey, lws_client, regtest) }