From 62307774ba767d2bf22cf3100ee43726699146c6 Mon Sep 17 00:00:00 2001 From: rajarshimaitra Date: Wed, 28 Sep 2022 17:56:15 +0530 Subject: [PATCH 1/2] Update client This PR uses the `proxy` feature of rust-jsonrpc to create a http transport via sock5 proxy. Which can be activated using the `proxy` feature of this library. - Update Cargo.toml to include `proxy` feature. - Add a new `new_with_proxy()` constructor for client. --- client/Cargo.toml | 5 ++++- client/src/client.rs | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/client/Cargo.toml b/client/Cargo.toml index 72ea3c70..b7caac02 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -22,8 +22,11 @@ path = "src/lib.rs" bitcoincore-rpc-json = { version = "0.16.0", path = "../json" } log = "0.4.5" -jsonrpc = "0.13.0" +jsonrpc = { git = "https://github.com/apoelstra/rust-jsonrpc", rev = "7c94adf8aad7d55afad8f890ab1fbc79ecb7abc7"} # Used for deserialization of JSON. serde = "1" serde_json = "1" + +[features] +proxy = ["jsonrpc/proxy"] \ No newline at end of file diff --git a/client/src/client.rs b/client/src/client.rs index d12c4f4b..ce57a303 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -1217,6 +1217,22 @@ impl Client { .map_err(|e| super::error::Error::JsonRpc(e.into())) } + #[cfg(feature = "proxy")] + /// Creates a client to a bitcoind JSON-RPC server via SOCK5 proxy. + pub fn new_with_proxy( + url: &str, + auth: Auth, + proxy_addr: &str, + proxy_auth: Option<(&str, &str)>, + ) -> Result { + let (user, pass) = auth.get_user_pass()?; + jsonrpc::client::Client::http_proxy(url, user, pass, proxy_addr, proxy_auth) + .map(|client| Client { + client, + }) + .map_err(|e| super::error::Error::JsonRpc(e.into())) + } + /// Create a new Client using the given [jsonrpc::Client]. pub fn from_jsonrpc(client: jsonrpc::client::Client) -> Client { Client { From 9a430e094aed1b826d84cd7845349b9bb59b0819 Mon Sep 17 00:00:00 2001 From: rajarshimaitra Date: Wed, 28 Sep 2022 17:59:09 +0530 Subject: [PATCH 2/2] Update integration_test This adds the previously added `proxy` feature into integration test for testing the proxy functionality. --- integration_test/Cargo.toml | 3 +++ integration_test/src/main.rs | 13 +++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/integration_test/Cargo.toml b/integration_test/Cargo.toml index 1ecee4e2..e22fa90c 100644 --- a/integration_test/Cargo.toml +++ b/integration_test/Cargo.toml @@ -9,3 +9,6 @@ bitcoincore-rpc = { path = "../client" } bitcoin = { version = "0.29.0", features = ["serde", "rand"]} lazy_static = "1.4.0" log = "0.4" + +[features] +proxy = ["bitcoincore-rpc/proxy"] diff --git a/integration_test/src/main.rs b/integration_test/src/main.rs index c91cc33d..5e376625 100644 --- a/integration_test/src/main.rs +++ b/integration_test/src/main.rs @@ -25,8 +25,8 @@ use bitcoin::hashes::hex::{FromHex, ToHex}; use bitcoin::hashes::Hash; use bitcoin::secp256k1; use bitcoin::{ - Address, Amount, PackedLockTime, Network, OutPoint, PrivateKey, Script, EcdsaSighashType, SignedAmount, - Sequence, Transaction, TxIn, TxOut, Txid, Witness, + Address, Amount, EcdsaSighashType, Network, OutPoint, PackedLockTime, PrivateKey, Script, + Sequence, SignedAmount, Transaction, TxIn, TxOut, Txid, Witness, }; use bitcoincore_rpc::bitcoincore_rpc_json::{ GetBlockTemplateModes, GetBlockTemplateRules, ScanTxOutRequest, @@ -119,14 +119,23 @@ fn get_auth() -> bitcoincore_rpc::Auth { }; } +#[cfg(feature = "proxy")] +fn get_proxy_url() -> String { + return std::env::var("PROXY_URL").expect("PROXY_URL must be set in proxy feature"); +} + fn main() { log::set_logger(&LOGGER).map(|()| log::set_max_level(log::LevelFilter::max())).unwrap(); let rpc_url = format!("{}/wallet/testwallet", get_rpc_url()); let auth = get_auth(); + #[cfg(not(feature = "proxy"))] let cl = Client::new(&rpc_url, auth).unwrap(); + #[cfg(feature = "proxy")] + let cl = Client::new_with_proxy(&rpc_url, auth, &get_proxy_url(), None).unwrap(); + test_get_network_info(&cl); unsafe { VERSION = cl.version().unwrap() }; println!("Version: {}", version());