mirror of
https://gitlab.com/famedly/conduit.git
synced 2024-12-26 21:04:40 +01:00
use pre-constructed client for well-known requests also
This commit is contained in:
parent
f8d1c1a8af
commit
1059f35fdc
4 changed files with 21 additions and 34 deletions
3
Cargo.lock
generated
3
Cargo.lock
generated
|
@ -1950,8 +1950,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "reqwest"
|
name = "reqwest"
|
||||||
version = "0.11.9"
|
version = "0.11.9"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "git+https://github.com/niuhuan/reqwest?branch=dns-resolver-fn#57b7cf4feb921573dfafad7d34b9ac6e44ead0bd"
|
||||||
checksum = "87f242f1488a539a79bac6dbe7c8609ae43b7914b7736210f239a37cccb32525"
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64 0.13.0",
|
"base64 0.13.0",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
|
|
@ -48,7 +48,7 @@ rand = "0.8.4"
|
||||||
# Used to hash passwords
|
# Used to hash passwords
|
||||||
rust-argon2 = "0.8.3"
|
rust-argon2 = "0.8.3"
|
||||||
# Used to send requests
|
# Used to send requests
|
||||||
reqwest = { version = "0.11.4", default-features = false, features = ["rustls-tls", "socks"] }
|
reqwest = { version = "0.11.4", default-features = false, features = ["rustls-tls", "socks"], git = "https://github.com/niuhuan/reqwest", branch = "dns-resolver-fn" }
|
||||||
# Used for conduit::Error type
|
# Used for conduit::Error type
|
||||||
thiserror = "1.0.28"
|
thiserror = "1.0.28"
|
||||||
# Used to generate thumbnails for images
|
# Used to generate thumbnails for images
|
||||||
|
|
|
@ -10,7 +10,7 @@ use std::{
|
||||||
collections::{BTreeMap, HashMap},
|
collections::{BTreeMap, HashMap},
|
||||||
fs,
|
fs,
|
||||||
future::Future,
|
future::Future,
|
||||||
net::IpAddr,
|
net::{IpAddr, SocketAddr},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
sync::{Arc, Mutex, RwLock},
|
sync::{Arc, Mutex, RwLock},
|
||||||
time::{Duration, Instant},
|
time::{Duration, Instant},
|
||||||
|
@ -39,6 +39,7 @@ pub struct Globals {
|
||||||
keypair: Arc<ruma::signatures::Ed25519KeyPair>,
|
keypair: Arc<ruma::signatures::Ed25519KeyPair>,
|
||||||
dns_resolver: TokioAsyncResolver,
|
dns_resolver: TokioAsyncResolver,
|
||||||
jwt_decoding_key: Option<jsonwebtoken::DecodingKey<'static>>,
|
jwt_decoding_key: Option<jsonwebtoken::DecodingKey<'static>>,
|
||||||
|
well_known_client: reqwest::Client,
|
||||||
basic_client: reqwest::Client,
|
basic_client: reqwest::Client,
|
||||||
pub(super) server_signingkeys: Arc<dyn Tree>,
|
pub(super) server_signingkeys: Arc<dyn Tree>,
|
||||||
pub bad_event_ratelimiter: Arc<RwLock<HashMap<Box<EventId>, RateLimitState>>>,
|
pub bad_event_ratelimiter: Arc<RwLock<HashMap<Box<EventId>, RateLimitState>>>,
|
||||||
|
@ -133,7 +134,16 @@ impl Globals {
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|secret| jsonwebtoken::DecodingKey::from_secret(secret.as_bytes()).into_static());
|
.map(|secret| jsonwebtoken::DecodingKey::from_secret(secret.as_bytes()).into_static());
|
||||||
|
|
||||||
let basic_client = reqwest_client_builder(&config, None)?.build()?;
|
let basic_client = reqwest_client_builder(&config)?.build()?;
|
||||||
|
let name_override = Arc::clone(&tls_name_override);
|
||||||
|
let well_known_client = reqwest_client_builder(&config)?
|
||||||
|
.resolve_fn(move |domain| {
|
||||||
|
let read_guard = name_override.read().unwrap();
|
||||||
|
let (override_name, port) = read_guard.get(&domain)?;
|
||||||
|
let first_name = override_name.get(0)?;
|
||||||
|
Some(SocketAddr::new(*first_name, *port))
|
||||||
|
})
|
||||||
|
.build()?;
|
||||||
|
|
||||||
let s = Self {
|
let s = Self {
|
||||||
globals,
|
globals,
|
||||||
|
@ -144,6 +154,7 @@ impl Globals {
|
||||||
})?,
|
})?,
|
||||||
actual_destination_cache: Arc::new(RwLock::new(WellKnownMap::new())),
|
actual_destination_cache: Arc::new(RwLock::new(WellKnownMap::new())),
|
||||||
tls_name_override,
|
tls_name_override,
|
||||||
|
well_known_client,
|
||||||
basic_client,
|
basic_client,
|
||||||
server_signingkeys,
|
server_signingkeys,
|
||||||
jwt_decoding_key,
|
jwt_decoding_key,
|
||||||
|
@ -173,9 +184,10 @@ impl Globals {
|
||||||
self.basic_client.clone()
|
self.basic_client.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a reqwest client builder which can be customized and used to send requests.
|
/// Returns a client used for resolving .well-knowns
|
||||||
pub fn reqwest_client_builder(&self) -> Result<reqwest::ClientBuilder> {
|
pub fn well_known_client(&self) -> reqwest::Client {
|
||||||
reqwest_client_builder(&self.config, Some(1))
|
// can't return &Client or else we'll hold a lock around the DB across an await
|
||||||
|
self.well_known_client.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument(skip(self))]
|
#[tracing::instrument(skip(self))]
|
||||||
|
@ -343,17 +355,11 @@ impl Globals {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reqwest_client_builder(
|
fn reqwest_client_builder(config: &Config) -> Result<reqwest::ClientBuilder> {
|
||||||
config: &Config,
|
|
||||||
max_idle: Option<usize>,
|
|
||||||
) -> Result<reqwest::ClientBuilder> {
|
|
||||||
let mut reqwest_client_builder = reqwest::Client::builder()
|
let mut reqwest_client_builder = reqwest::Client::builder()
|
||||||
.connect_timeout(Duration::from_secs(30))
|
.connect_timeout(Duration::from_secs(30))
|
||||||
.timeout(Duration::from_secs(60 * 3));
|
.timeout(Duration::from_secs(60 * 3));
|
||||||
|
|
||||||
if let Some(max_idle) = max_idle {
|
|
||||||
reqwest_client_builder = reqwest_client_builder.pool_max_idle_per_host(max_idle);
|
|
||||||
}
|
|
||||||
if let Some(proxy) = config.proxy.to_proxy()? {
|
if let Some(proxy) = config.proxy.to_proxy()? {
|
||||||
reqwest_client_builder = reqwest_client_builder.proxy(proxy);
|
reqwest_client_builder = reqwest_client_builder.proxy(proxy);
|
||||||
}
|
}
|
||||||
|
|
|
@ -237,25 +237,7 @@ where
|
||||||
|
|
||||||
let url = reqwest_request.url().clone();
|
let url = reqwest_request.url().clone();
|
||||||
|
|
||||||
let client = if let Some((override_name, port)) = globals
|
let response = globals.well_known_client().execute(reqwest_request).await;
|
||||||
.tls_name_override
|
|
||||||
.read()
|
|
||||||
.unwrap()
|
|
||||||
.get(&actual_destination.hostname())
|
|
||||||
{
|
|
||||||
globals
|
|
||||||
.reqwest_client_builder()?
|
|
||||||
.resolve(
|
|
||||||
&actual_destination.hostname(),
|
|
||||||
SocketAddr::new(override_name[0], *port),
|
|
||||||
)
|
|
||||||
.build()?
|
|
||||||
// port will be ignored
|
|
||||||
} else {
|
|
||||||
globals.reqwest_client()
|
|
||||||
};
|
|
||||||
|
|
||||||
let response = client.execute(reqwest_request).await;
|
|
||||||
|
|
||||||
match response {
|
match response {
|
||||||
Ok(mut response) => {
|
Ok(mut response) => {
|
||||||
|
|
Loading…
Reference in a new issue