Skip to content

Commit

Permalink
Add proxy support (#272)
Browse files Browse the repository at this point in the history
* Add proxy support
  • Loading branch information
davidcaseria authored Aug 7, 2024
1 parent 6adf326 commit 0945b35
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ uuid = { version = "1", features = ["v4"] }
lightning-invoice = { version = "0.31", features = ["serde"] }
home = "0.5.9"
rand = "0.8.5"
url = "2.3"

[profile]

Expand Down
1 change: 1 addition & 0 deletions crates/cdk-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ nostr-sdk = { version = "0.33.0", default-features = false, features = [
"nip04",
"nip44"
]}
url.workspace = true
12 changes: 10 additions & 2 deletions crates/cdk-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ use anyhow::{bail, Result};
use bip39::Mnemonic;
use cdk::cdk_database;
use cdk::cdk_database::WalletDatabase;
use cdk::wallet::client::HttpClient;
use cdk::wallet::{MultiMintWallet, Wallet};
use cdk_redb::WalletRedbDatabase;
use cdk_sqlite::WalletSqliteDatabase;
use clap::{Parser, Subcommand};
use rand::Rng;
use tracing::Level;
use tracing_subscriber::EnvFilter;
use url::Url;

mod sub_commands;

Expand All @@ -35,6 +37,9 @@ struct Cli {
/// Logging level
#[arg(short, long, default_value = "error")]
log_level: Level,
/// NWS Proxy
#[arg(short, long)]
proxy: Option<Url>,
#[command(subcommand)]
command: Commands,
}
Expand Down Expand Up @@ -130,13 +135,16 @@ async fn main() -> Result<()> {
let mints = localstore.get_mints().await?;

for (mint, _) in mints {
let wallet = Wallet::new(
let mut wallet = Wallet::new(
&mint.to_string(),
cdk::nuts::CurrencyUnit::Sat,
localstore.clone(),
&mnemonic.to_seed_normalized(""),
None,
);
if let Some(proxy_url) = args.proxy.as_ref() {
wallet.set_client(HttpClient::with_proxy(proxy_url.clone(), None, true)?);
}

wallets.push(wallet);
}
Expand Down Expand Up @@ -167,7 +175,7 @@ async fn main() -> Result<()> {
sub_commands::check_spent::check_spent(&multi_mint_wallet).await
}
Commands::MintInfo(sub_command_args) => {
sub_commands::mint_info::mint_info(sub_command_args).await
sub_commands::mint_info::mint_info(args.proxy, sub_command_args).await
}
Commands::Mint(sub_command_args) => {
sub_commands::mint::mint(
Expand Down
8 changes: 6 additions & 2 deletions crates/cdk-cli/src/sub_commands/mint_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ use anyhow::Result;
use cdk::url::UncheckedUrl;
use cdk::HttpClient;
use clap::Args;
use url::Url;

#[derive(Args)]
pub struct MintInfoSubcommand {
/// Cashu Token
mint_url: UncheckedUrl,
}

pub async fn mint_info(sub_command_args: &MintInfoSubcommand) -> Result<()> {
let client = HttpClient::default();
pub async fn mint_info(proxy: Option<Url>, sub_command_args: &MintInfoSubcommand) -> Result<()> {
let client = match proxy {
Some(proxy) => HttpClient::with_proxy(proxy, None, true)?,
None => HttpClient::new(),
};

let info = client
.get_mint_info(sub_command_args.mint_url.clone().try_into()?)
Expand Down
3 changes: 2 additions & 1 deletion crates/cdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ bitcoin = { workspace = true, features = [
ciborium = { version = "0.2.2", default-features = false, features = ["std"] }
lightning-invoice.workspace = true
once_cell = "1.19"
regex = "1"
reqwest = { version = "0.12", default-features = false, features = [
"json",
"rustls-tls",
Expand All @@ -40,7 +41,7 @@ serde_with = "3.4"
tracing.workspace = true
thiserror.workspace = true
futures = { workspace = true, optional = true }
url = "2.3"
url.workspace = true
uuid.workspace = true

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
29 changes: 29 additions & 0 deletions crates/cdk/src/wallet/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,35 @@ impl HttpClient {
}
}

#[cfg(not(target_arch = "wasm32"))]
/// Create new [`HttpClient`] with a proxy for specific TLDs.
/// Specifying `None` for `host_matcher` will use the proxy for all requests.
pub fn with_proxy(
proxy: Url,
host_matcher: Option<&str>,
accept_invalid_certs: bool,
) -> Result<Self, Error> {
let regex = host_matcher
.map(regex::Regex::new)
.transpose()
.map_err(|e| Error::Custom(e.to_string()))?;
let client = reqwest::Client::builder()
.proxy(reqwest::Proxy::custom(move |url| {
if let Some(matcher) = regex.as_ref() {
if let Some(host) = url.host_str() {
if matcher.is_match(host) {
return Some(proxy.clone());
}
}
}
None
}))
.danger_accept_invalid_certs(accept_invalid_certs) // Allow self-signed certs
.build()?;

Ok(Self { inner: client })
}

/// Get Active Mint Keys [NUT-01]
#[instrument(skip(self), fields(mint_url = %mint_url))]
pub async fn get_mint_keys(&self, mint_url: Url) -> Result<Vec<KeySet>, Error> {
Expand Down
5 changes: 5 additions & 0 deletions crates/cdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ impl Wallet {
}
}

/// Change HTTP client
pub fn set_client(&mut self, client: HttpClient) {
self.client = client;
}

/// Fee required for proof set
#[instrument(skip_all)]
pub async fn get_proofs_fee(&self, proofs: &Proofs) -> Result<Amount, Error> {
Expand Down

0 comments on commit 0945b35

Please sign in to comment.