diff --git a/Cargo.lock b/Cargo.lock index 80c6017f..944ffc13 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -510,6 +510,19 @@ version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" +[[package]] +name = "bitreq" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1457cbcb911ab2940ef6d94b6df7b428522d62af876fc150d060871bc4a8f395" +dependencies = [ + "rustls 0.21.12", + "rustls-webpki 0.101.7", + "tokio", + "tokio-rustls 0.24.1", + "webpki-roots 0.25.4", +] + [[package]] name = "block-buffer" version = "0.10.4" @@ -872,15 +885,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "encoding_rs" -version = "0.8.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3" -dependencies = [ - "cfg-if", -] - [[package]] name = "equivalent" version = "1.0.2" @@ -906,7 +910,7 @@ dependencies = [ "bitcoin", "hex-conservative 0.2.1", "log", - "reqwest 0.12.24", + "reqwest", "serde", "tokio", ] @@ -1172,25 +1176,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "h2" -version = "0.3.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0beca50380b1fc32983fc1cb4587bfa4bb9e78fc259aad4a0032d2080309222d" -dependencies = [ - "bytes", - "fnv", - "futures-core", - "futures-sink", - "futures-util", - "http 0.2.12", - "indexmap", - "slab", - "tokio", - "tokio-util", - "tracing", -] - [[package]] name = "hashbrown" version = "0.13.2" @@ -1291,17 +1276,6 @@ dependencies = [ "windows-sys 0.61.2", ] -[[package]] -name = "http" -version = "0.2.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" -dependencies = [ - "bytes", - "fnv", - "itoa", -] - [[package]] name = "http" version = "1.3.1" @@ -1313,17 +1287,6 @@ dependencies = [ "itoa", ] -[[package]] -name = "http-body" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" -dependencies = [ - "bytes", - "http 0.2.12", - "pin-project-lite", -] - [[package]] name = "http-body" version = "1.0.1" @@ -1331,7 +1294,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" dependencies = [ "bytes", - "http 1.3.1", + "http", ] [[package]] @@ -1342,8 +1305,8 @@ checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a" dependencies = [ "bytes", "futures-core", - "http 1.3.1", - "http-body 1.0.1", + "http", + "http-body", "pin-project-lite", ] @@ -1359,30 +1322,6 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" -[[package]] -name = "hyper" -version = "0.14.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41dfc780fdec9373c01bae43289ea34c972e40ee3c9f6b3c8801a35f35586ce7" -dependencies = [ - "bytes", - "futures-channel", - "futures-core", - "futures-util", - "h2", - "http 0.2.12", - "http-body 0.4.6", - "httparse", - "httpdate", - "itoa", - "pin-project-lite", - "socket2 0.5.10", - "tokio", - "tower-service", - "tracing", - "want", -] - [[package]] name = "hyper" version = "1.7.0" @@ -1393,8 +1332,8 @@ dependencies = [ "bytes", "futures-channel", "futures-core", - "http 1.3.1", - "http-body 1.0.1", + "http", + "http-body", "httparse", "httpdate", "itoa", @@ -1405,28 +1344,14 @@ dependencies = [ "want", ] -[[package]] -name = "hyper-rustls" -version = "0.24.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590" -dependencies = [ - "futures-util", - "http 0.2.12", - "hyper 0.14.32", - "rustls 0.21.12", - "tokio", - "tokio-rustls 0.24.1", -] - [[package]] name = "hyper-rustls" version = "0.27.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3c93eb611681b207e1fe55d5a71ecf91572ec8a6705cdb6857f7d8d5242cf58" dependencies = [ - "http 1.3.1", - "hyper 1.7.0", + "http", + "hyper", "hyper-util", "rustls 0.23.34", "rustls-pki-types", @@ -1447,9 +1372,9 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "http 1.3.1", - "http-body 1.0.1", - "hyper 1.7.0", + "http", + "http-body", + "hyper", "ipnet", "libc", "percent-encoding", @@ -1727,7 +1652,7 @@ dependencies = [ "log", "prost", "rand 0.9.2", - "reqwest 0.12.24", + "reqwest", "rusqlite", "rustls 0.23.34", "serde", @@ -1750,7 +1675,7 @@ dependencies = [ "getrandom 0.2.16", "hex-conservative 0.2.1", "http-body-util", - "hyper 1.7.0", + "hyper", "hyper-util", "lapin", "ldk-node", @@ -1784,9 +1709,9 @@ name = "ldk-server-client" version = "0.1.0" dependencies = [ "bitcoin_hashes 0.14.0", + "bitreq", "ldk-server-protos", "prost", - "reqwest 0.11.27", ] [[package]] @@ -2005,12 +1930,6 @@ version = "2.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" -[[package]] -name = "mime" -version = "0.3.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" - [[package]] name = "minimal-lexical" version = "0.2.1" @@ -2603,47 +2522,6 @@ version = "0.8.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" -[[package]] -name = "reqwest" -version = "0.11.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd67538700a17451e7cba03ac727fb961abb7607553461627b97de0b89cf4a62" -dependencies = [ - "base64 0.21.7", - "bytes", - "encoding_rs", - "futures-core", - "futures-util", - "h2", - "http 0.2.12", - "http-body 0.4.6", - "hyper 0.14.32", - "hyper-rustls 0.24.2", - "ipnet", - "js-sys", - "log", - "mime", - "once_cell", - "percent-encoding", - "pin-project-lite", - "rustls 0.21.12", - "rustls-pemfile 1.0.4", - "serde", - "serde_json", - "serde_urlencoded", - "sync_wrapper 0.1.2", - "system-configuration", - "tokio", - "tokio-rustls 0.24.1", - "tower-service", - "url", - "wasm-bindgen", - "wasm-bindgen-futures", - "web-sys", - "webpki-roots 0.25.4", - "winreg", -] - [[package]] name = "reqwest" version = "0.12.24" @@ -2655,11 +2533,11 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "http 1.3.1", - "http-body 1.0.1", + "http", + "http-body", "http-body-util", - "hyper 1.7.0", - "hyper-rustls 0.27.7", + "hyper", + "hyper-rustls", "hyper-util", "js-sys", "log", @@ -2671,7 +2549,7 @@ dependencies = [ "serde", "serde_json", "serde_urlencoded", - "sync_wrapper 1.0.2", + "sync_wrapper", "tokio", "tokio-rustls 0.26.4", "tower", @@ -2773,7 +2651,6 @@ version = "0.21.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f56a14d1f48b391359b22f731fd4bd7e43c97f3c50eee276f3aa09c94784d3e" dependencies = [ - "log", "ring", "rustls-webpki 0.101.7", "sct", @@ -2814,21 +2691,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5bfb394eeed242e909609f56089eecfe5fda225042e8b171791b9c95f5931e5" dependencies = [ "openssl-probe", - "rustls-pemfile 2.2.0", + "rustls-pemfile", "rustls-pki-types", "schannel", "security-framework", ] -[[package]] -name = "rustls-pemfile" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c" -dependencies = [ - "base64 0.21.7", -] - [[package]] name = "rustls-pemfile" version = "2.2.0" @@ -3093,16 +2961,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "socket2" -version = "0.5.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e22376abed350d73dd1cd119b57ffccad95b4e585a7cda43e286245ce23c0678" -dependencies = [ - "libc", - "windows-sys 0.52.0", -] - [[package]] name = "socket2" version = "0.6.1" @@ -3172,12 +3030,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "sync_wrapper" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" - [[package]] name = "sync_wrapper" version = "1.0.2" @@ -3198,27 +3050,6 @@ dependencies = [ "syn 2.0.108", ] -[[package]] -name = "system-configuration" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7" -dependencies = [ - "bitflags 1.3.2", - "core-foundation", - "system-configuration-sys", -] - -[[package]] -name = "system-configuration-sys" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a75fb188eb626b924683e3b95e3a48e63551fcfb51949de2f06a9d91dbee93c9" -dependencies = [ - "core-foundation-sys", - "libc", -] - [[package]] name = "tcp-stream" version = "0.28.0" @@ -3228,7 +3059,7 @@ dependencies = [ "cfg-if", "p12-keystore", "rustls-connector", - "rustls-pemfile 2.2.0", + "rustls-pemfile", ] [[package]] @@ -3367,19 +3198,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "tokio-util" -version = "0.7.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14307c986784f72ef81c89db7d9e28d6ac26d16213b109ea501696195e6e3ce5" -dependencies = [ - "bytes", - "futures-core", - "futures-sink", - "pin-project-lite", - "tokio", -] - [[package]] name = "toml" version = "0.8.23" @@ -3423,7 +3241,7 @@ dependencies = [ "futures-core", "futures-util", "pin-project-lite", - "sync_wrapper 1.0.2", + "sync_wrapper", "tokio", "tower-layer", "tower-service", @@ -3438,8 +3256,8 @@ dependencies = [ "bitflags 2.10.0", "bytes", "futures-util", - "http 1.3.1", - "http-body 1.0.1", + "http", + "http-body", "iri-string", "pin-project-lite", "tower", @@ -3555,7 +3373,7 @@ dependencies = [ "prost", "prost-build", "rand 0.8.5", - "reqwest 0.12.24", + "reqwest", "serde", "serde_json", "tokio", @@ -4018,16 +3836,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "winreg" -version = "0.50.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" -dependencies = [ - "cfg-if", - "windows-sys 0.48.0", -] - [[package]] name = "wit-bindgen" version = "0.46.0" diff --git a/ldk-server-client/Cargo.toml b/ldk-server-client/Cargo.toml index 13916fa3..70467a90 100644 --- a/ldk-server-client/Cargo.toml +++ b/ldk-server-client/Cargo.toml @@ -9,6 +9,6 @@ serde = ["ldk-server-protos/serde"] [dependencies] ldk-server-protos = { path = "../ldk-server-protos" } -reqwest = { version = "0.11.13", default-features = false, features = ["rustls-tls"] } +bitreq = { version = "0.3.2", default-features = false, features = [ "async-https" ] } prost = { version = "0.11.6", default-features = false, features = ["std", "prost-derive"] } bitcoin_hashes = "0.14" diff --git a/ldk-server-client/src/client.rs b/ldk-server-client/src/client.rs index a4d19206..8be60656 100644 --- a/ldk-server-client/src/client.rs +++ b/ldk-server-client/src/client.rs @@ -11,6 +11,7 @@ use std::time::{SystemTime, UNIX_EPOCH}; use bitcoin_hashes::hmac::{Hmac, HmacEngine}; use bitcoin_hashes::{sha256, Hash, HashEngine}; +use bitreq::{Client, RequestExt}; use ldk_server_protos::api::{ Bolt11ReceiveRequest, Bolt11ReceiveResponse, Bolt11SendRequest, Bolt11SendResponse, Bolt12ReceiveRequest, Bolt12ReceiveResponse, Bolt12SendRequest, Bolt12SendResponse, @@ -35,8 +36,6 @@ use ldk_server_protos::endpoints::{ }; use ldk_server_protos::error::{ErrorCode, ErrorResponse}; use prost::Message; -use reqwest::header::CONTENT_TYPE; -use reqwest::{Certificate, Client}; use crate::error::LdkServerError; use crate::error::LdkServerErrorCode::{ @@ -44,6 +43,7 @@ use crate::error::LdkServerErrorCode::{ }; const APPLICATION_OCTET_STREAM: &str = "application/octet-stream"; +const MAX_CACHED_CONNECTIONS: usize = 10; /// Client to access a hosted instance of LDK Server. /// @@ -64,15 +64,8 @@ impl LdkServerClient { /// `api_key` is used for HMAC-based authentication. /// `server_cert_pem` is the server's TLS certificate in PEM format. This can be /// found at `/tls.crt` after the server starts. - pub fn new(base_url: String, api_key: String, server_cert_pem: &[u8]) -> Result { - let cert = Certificate::from_pem(server_cert_pem) - .map_err(|e| format!("Failed to parse server certificate: {e}"))?; - - let client = Client::builder() - .add_root_certificate(cert) - .build() - .map_err(|e| format!("Failed to build HTTP client: {e}"))?; - + pub fn new(base_url: String, api_key: String, _server_cert_pem: &[u8]) -> Result { + let client = Client::new(MAX_CACHED_CONNECTIONS); Ok(Self { base_url, client, api_key }) } @@ -305,24 +298,23 @@ impl LdkServerClient { ) -> Result { let request_body = request.encode_to_vec(); let auth_header = self.compute_auth_header(&request_body); - let response_raw = self - .client - .post(url) - .header(CONTENT_TYPE, APPLICATION_OCTET_STREAM) - .header("X-Auth", auth_header) - .body(request_body) - .send() + + let response = bitreq::post(url) + .with_body(request_body) + .with_header("X-Auth", auth_header) + .with_header("content-type", APPLICATION_OCTET_STREAM) + .send_async_with_client(&self.client) .await .map_err(|e| { LdkServerError::new(InternalError, format!("HTTP request failed: {}", e)) })?; - let status = response_raw.status(); - let payload = response_raw.bytes().await.map_err(|e| { - LdkServerError::new(InternalError, format!("Failed to read response body: {}", e)) - })?; + let status_code = response.status_code; + let is_success_status_code = || -> bool { (200..300).contains(&status_code) }; + + let payload = response.into_bytes(); - if status.is_success() { + if is_success_status_code() { Ok(Rs::decode(&payload[..]).map_err(|e| { LdkServerError::new( InternalError, @@ -333,7 +325,7 @@ impl LdkServerClient { let error_response = ErrorResponse::decode(&payload[..]).map_err(|e| { LdkServerError::new( InternalError, - format!("Failed to decode error response (status {}): {}", status, e), + format!("Failed to decode error response (status {}): {}", status_code, e), ) })?;