Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dd14270a6e | ||
|
|
e8140cda33 | ||
|
|
0ac7ba4c24 | ||
|
|
a15ea4c156 | ||
|
|
33923ad6a7 | ||
|
|
6da3f62747 | ||
|
|
e534199195 |
@@ -46,11 +46,7 @@ where
|
||||
*reqwest_request.timeout_mut() = Some(Duration::from_secs(30));
|
||||
|
||||
let url = reqwest_request.url().clone();
|
||||
let mut response = globals
|
||||
.reqwest_client()?
|
||||
.build()?
|
||||
.execute(reqwest_request)
|
||||
.await?;
|
||||
let mut response = globals.reqwest_client().execute(reqwest_request).await?;
|
||||
|
||||
// reqwest::Response -> http::Response conversion
|
||||
let status = response.status();
|
||||
|
||||
+11
-25
@@ -1,6 +1,5 @@
|
||||
use super::SESSION_ID_LENGTH;
|
||||
use crate::{database::DatabaseGuard, utils, ConduitResult, Database, Error, Result, Ruma};
|
||||
use rocket::futures::{prelude::*, stream::FuturesUnordered};
|
||||
use ruma::{
|
||||
api::{
|
||||
client::{
|
||||
@@ -19,7 +18,7 @@ use ruma::{
|
||||
DeviceId, DeviceKeyAlgorithm, UserId,
|
||||
};
|
||||
use serde_json::json;
|
||||
use std::collections::{BTreeMap, HashMap, HashSet};
|
||||
use std::collections::{BTreeMap, HashSet};
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, post};
|
||||
@@ -295,7 +294,7 @@ pub async fn get_keys_helper<F: Fn(&UserId) -> bool>(
|
||||
let mut user_signing_keys = BTreeMap::new();
|
||||
let mut device_keys = BTreeMap::new();
|
||||
|
||||
let mut get_over_federation = HashMap::new();
|
||||
let mut get_over_federation = BTreeMap::new();
|
||||
|
||||
for (user_id, device_ids) in device_keys_input {
|
||||
if user_id.server_name() != db.globals.server_name() {
|
||||
@@ -365,16 +364,13 @@ pub async fn get_keys_helper<F: Fn(&UserId) -> bool>(
|
||||
|
||||
let mut failures = BTreeMap::new();
|
||||
|
||||
let mut futures = get_over_federation
|
||||
.into_iter()
|
||||
.map(|(server, vec)| async move {
|
||||
for (server, vec) in get_over_federation {
|
||||
let mut device_keys_input_fed = BTreeMap::new();
|
||||
for (user_id, keys) in vec {
|
||||
device_keys_input_fed.insert(user_id.clone(), keys.clone());
|
||||
}
|
||||
(
|
||||
server,
|
||||
db.sending
|
||||
match db
|
||||
.sending
|
||||
.send_federation_request(
|
||||
&db.globals,
|
||||
server,
|
||||
@@ -382,13 +378,8 @@ pub async fn get_keys_helper<F: Fn(&UserId) -> bool>(
|
||||
device_keys: device_keys_input_fed,
|
||||
},
|
||||
)
|
||||
.await,
|
||||
)
|
||||
})
|
||||
.collect::<FuturesUnordered<_>>();
|
||||
|
||||
while let Some((server, response)) = futures.next().await {
|
||||
match response {
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
master_keys.extend(response.master_keys);
|
||||
self_signing_keys.extend(response.self_signing_keys);
|
||||
@@ -439,15 +430,13 @@ pub async fn claim_keys_helper(
|
||||
one_time_keys.insert(user_id.clone(), container);
|
||||
}
|
||||
|
||||
let mut failures = BTreeMap::new();
|
||||
|
||||
for (server, vec) in get_over_federation {
|
||||
let mut one_time_keys_input_fed = BTreeMap::new();
|
||||
for (user_id, keys) in vec {
|
||||
one_time_keys_input_fed.insert(user_id.clone(), keys.clone());
|
||||
}
|
||||
// Ignore failures
|
||||
if let Ok(keys) = db
|
||||
let keys = db
|
||||
.sending
|
||||
.send_federation_request(
|
||||
&db.globals,
|
||||
@@ -456,16 +445,13 @@ pub async fn claim_keys_helper(
|
||||
one_time_keys: one_time_keys_input_fed,
|
||||
},
|
||||
)
|
||||
.await
|
||||
{
|
||||
.await?;
|
||||
|
||||
one_time_keys.extend(keys.one_time_keys);
|
||||
} else {
|
||||
failures.insert(server.to_string(), json!({}));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(claim_keys::Response {
|
||||
failures,
|
||||
failures: BTreeMap::new(),
|
||||
one_time_keys,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -69,7 +69,6 @@ pub async fn join_room_by_id_route(
|
||||
&db,
|
||||
body.sender_user.as_ref(),
|
||||
&body.room_id,
|
||||
body.reason.clone(),
|
||||
&servers,
|
||||
body.third_party_signed.as_ref(),
|
||||
)
|
||||
@@ -121,7 +120,6 @@ pub async fn join_room_by_id_or_alias_route(
|
||||
&db,
|
||||
body.sender_user.as_ref(),
|
||||
&room_id,
|
||||
body.reason.clone(),
|
||||
&servers,
|
||||
body.third_party_signed.as_ref(),
|
||||
)
|
||||
@@ -146,9 +144,7 @@ pub async fn leave_room_route(
|
||||
) -> ConduitResult<leave_room::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
db.rooms
|
||||
.leave_room(sender_user, &body.room_id, body.reason.clone(), &db)
|
||||
.await?;
|
||||
db.rooms.leave_room(sender_user, &body.room_id, &db).await?;
|
||||
|
||||
db.flush()?;
|
||||
|
||||
@@ -167,15 +163,7 @@ pub async fn invite_user_route(
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
if let invite_user::IncomingInvitationRecipient::UserId { user_id } = &body.recipient {
|
||||
invite_helper(
|
||||
sender_user,
|
||||
user_id,
|
||||
&body.room_id,
|
||||
body.reason.clone(),
|
||||
&db,
|
||||
false,
|
||||
)
|
||||
.await?;
|
||||
invite_helper(sender_user, user_id, &body.room_id, &db, false).await?;
|
||||
db.flush()?;
|
||||
Ok(invite_user::Response {}.into())
|
||||
} else {
|
||||
@@ -213,7 +201,7 @@ pub async fn kick_user_route(
|
||||
.map_err(|_| Error::bad_database("Invalid member event in database."))?;
|
||||
|
||||
event.membership = ruma::events::room::member::MembershipState::Leave;
|
||||
event.reason = body.reason.clone();
|
||||
// TODO: reason
|
||||
|
||||
let mutex_state = Arc::clone(
|
||||
db.globals
|
||||
@@ -257,6 +245,8 @@ pub async fn ban_user_route(
|
||||
) -> ConduitResult<ban_user::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
// TODO: reason
|
||||
|
||||
let event = db
|
||||
.rooms
|
||||
.room_state_get(
|
||||
@@ -272,7 +262,7 @@ pub async fn ban_user_route(
|
||||
is_direct: None,
|
||||
third_party_invite: None,
|
||||
blurhash: db.users.blurhash(&body.user_id)?,
|
||||
reason: body.reason.clone(),
|
||||
reason: None,
|
||||
}),
|
||||
|event| {
|
||||
let mut event = serde_json::from_value::<Raw<member::MemberEventContent>>(
|
||||
@@ -282,7 +272,6 @@ pub async fn ban_user_route(
|
||||
.deserialize()
|
||||
.map_err(|_| Error::bad_database("Invalid member event in database."))?;
|
||||
event.membership = ruma::events::room::member::MembershipState::Ban;
|
||||
event.reason = body.reason.clone();
|
||||
Ok(event)
|
||||
},
|
||||
)?;
|
||||
@@ -348,7 +337,6 @@ pub async fn unban_user_route(
|
||||
.map_err(|_| Error::bad_database("Invalid member event in database."))?;
|
||||
|
||||
event.membership = ruma::events::room::member::MembershipState::Leave;
|
||||
event.reason = body.reason.clone();
|
||||
|
||||
let mutex_state = Arc::clone(
|
||||
db.globals
|
||||
@@ -494,7 +482,6 @@ async fn join_room_by_id_helper(
|
||||
db: &Database,
|
||||
sender_user: Option<&UserId>,
|
||||
room_id: &RoomId,
|
||||
reason: Option<String>,
|
||||
servers: &HashSet<Box<ServerName>>,
|
||||
_third_party_signed: Option<&IncomingThirdPartySigned>,
|
||||
) -> ConduitResult<join_room_by_id::Response> {
|
||||
@@ -577,7 +564,7 @@ async fn join_room_by_id_helper(
|
||||
is_direct: None,
|
||||
third_party_invite: None,
|
||||
blurhash: db.users.blurhash(&sender_user)?,
|
||||
reason,
|
||||
reason: None,
|
||||
})
|
||||
.expect("event is valid, we just created it"),
|
||||
);
|
||||
@@ -727,7 +714,7 @@ async fn join_room_by_id_helper(
|
||||
is_direct: None,
|
||||
third_party_invite: None,
|
||||
blurhash: db.users.blurhash(&sender_user)?,
|
||||
reason,
|
||||
reason: None,
|
||||
};
|
||||
|
||||
db.rooms.build_and_append_pdu(
|
||||
@@ -820,7 +807,6 @@ pub async fn invite_helper<'a>(
|
||||
sender_user: &UserId,
|
||||
user_id: &UserId,
|
||||
room_id: &RoomId,
|
||||
reason: Option<String>,
|
||||
db: &Database,
|
||||
is_direct: bool,
|
||||
) -> Result<()> {
|
||||
@@ -883,7 +869,7 @@ pub async fn invite_helper<'a>(
|
||||
membership: MembershipState::Invite,
|
||||
third_party_invite: None,
|
||||
blurhash: None,
|
||||
reason,
|
||||
reason: None,
|
||||
})
|
||||
.expect("member event is valid value");
|
||||
|
||||
@@ -1078,7 +1064,7 @@ pub async fn invite_helper<'a>(
|
||||
is_direct: Some(is_direct),
|
||||
third_party_invite: None,
|
||||
blurhash: db.users.blurhash(&user_id)?,
|
||||
reason,
|
||||
reason: None,
|
||||
})
|
||||
.expect("event is valid, we just created it"),
|
||||
unsigned: None,
|
||||
|
||||
+102
-27
@@ -1,3 +1,34 @@
|
||||
use std::hash::Hash;
|
||||
use std::{
|
||||
collections::{BTreeMap, HashMap, HashSet},
|
||||
convert::{TryFrom, TryInto},
|
||||
fs::{self, remove_dir_all},
|
||||
io::Write,
|
||||
mem::size_of,
|
||||
ops::Deref,
|
||||
path::Path,
|
||||
sync::{Arc, Mutex, RwLock},
|
||||
};
|
||||
|
||||
use directories::ProjectDirs;
|
||||
use lru_cache::LruCache;
|
||||
use rocket::{
|
||||
futures::{channel::mpsc, stream::FuturesUnordered, StreamExt},
|
||||
outcome::{try_outcome, IntoOutcome},
|
||||
request::{FromRequest, Request},
|
||||
Shutdown, State,
|
||||
};
|
||||
use ruma::{DeviceId, EventId, RoomId, ServerName, UserId};
|
||||
use serde::{de::IgnoredAny, Deserialize};
|
||||
use tokio::sync::{OwnedRwLockReadGuard, RwLock as TokioRwLock, Semaphore};
|
||||
use tracing::{debug, error, warn};
|
||||
|
||||
use abstraction::DatabaseEngine;
|
||||
|
||||
use crate::{utils, Error, Result};
|
||||
|
||||
use self::proxy::ProxyConfig;
|
||||
|
||||
pub mod abstraction;
|
||||
|
||||
pub mod account_data;
|
||||
@@ -14,33 +45,6 @@ pub mod transaction_ids;
|
||||
pub mod uiaa;
|
||||
pub mod users;
|
||||
|
||||
use crate::{utils, Error, Result};
|
||||
use abstraction::DatabaseEngine;
|
||||
use directories::ProjectDirs;
|
||||
use lru_cache::LruCache;
|
||||
use rocket::{
|
||||
futures::{channel::mpsc, stream::FuturesUnordered, StreamExt},
|
||||
outcome::{try_outcome, IntoOutcome},
|
||||
request::{FromRequest, Request},
|
||||
Shutdown, State,
|
||||
};
|
||||
use ruma::{DeviceId, EventId, RoomId, ServerName, UserId};
|
||||
use serde::{de::IgnoredAny, Deserialize};
|
||||
use std::{
|
||||
collections::{BTreeMap, HashMap, HashSet},
|
||||
convert::{TryFrom, TryInto},
|
||||
fs::{self, remove_dir_all},
|
||||
io::Write,
|
||||
mem::size_of,
|
||||
ops::Deref,
|
||||
path::Path,
|
||||
sync::{Arc, Mutex, RwLock},
|
||||
};
|
||||
use tokio::sync::{OwnedRwLockReadGuard, RwLock as TokioRwLock, Semaphore};
|
||||
use tracing::{debug, error, warn};
|
||||
|
||||
use self::proxy::ProxyConfig;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
server_name: Box<ServerName>,
|
||||
@@ -132,6 +136,17 @@ pub type Engine = abstraction::sqlite::Engine;
|
||||
#[cfg(feature = "heed")]
|
||||
pub type Engine = abstraction::heed::Engine;
|
||||
|
||||
// for each key: (memory_usage in bytes, items in cache, capacity)
|
||||
pub struct CacheUsageStatistics {
|
||||
pdu_cache: (usize, usize, usize),
|
||||
auth_chain_cache: (usize, usize, usize),
|
||||
shorteventid_cache: (usize, usize, usize),
|
||||
eventidshort_cache: (usize, usize, usize),
|
||||
statekeyshort_cache: (usize, usize, usize),
|
||||
shortstatekey_cache: (usize, usize, usize),
|
||||
stateinfo_cache: (usize, usize, usize),
|
||||
}
|
||||
|
||||
pub struct Database {
|
||||
_db: Arc<Engine>,
|
||||
pub globals: globals::Globals,
|
||||
@@ -917,6 +932,66 @@ impl Database {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Measures memory usage in bytes and how full the caches are in percent for all caches in the Database struct.
|
||||
pub fn get_cache_usage(&mut self) -> Result<CacheUsageStatistics> {
|
||||
fn memory_usage_of_locked_cache<K: Eq + Hash, V>(
|
||||
cache: &mut Mutex<LruCache<K, V>>,
|
||||
) -> usize {
|
||||
let raw_cache = cache.lock().unwrap();
|
||||
let mut cache_items_size_sum: usize = 0;
|
||||
for cache_item in raw_cache.iter() {
|
||||
cache_items_size_sum += std::mem::size_of_val(&cache_item);
|
||||
}
|
||||
cache_items_size_sum += std::mem::size_of_val(&cache);
|
||||
cache_items_size_sum
|
||||
}
|
||||
|
||||
fn items_in_locked_cache<K: Eq + Hash, V>(cache: &mut Mutex<LruCache<K, V>>) -> usize {
|
||||
cache.lock().unwrap().len()
|
||||
}
|
||||
fn capacity_of_locked_cache<K: Eq + Hash, V>(cache: &mut Mutex<LruCache<K, V>>) -> usize {
|
||||
cache.lock().unwrap().capacity()
|
||||
}
|
||||
|
||||
return Ok(CacheUsageStatistics {
|
||||
pdu_cache: (
|
||||
memory_usage_of_locked_cache(&mut self.rooms.pdu_cache),
|
||||
items_in_locked_cache(&mut self.rooms.pdu_cache),
|
||||
capacity_of_locked_cache(&mut self.rooms.pdu_cache),
|
||||
),
|
||||
auth_chain_cache: (
|
||||
memory_usage_of_locked_cache(&mut self.rooms.auth_chain_cache),
|
||||
items_in_locked_cache(&mut self.rooms.auth_chain_cache),
|
||||
capacity_of_locked_cache(&mut self.rooms.auth_chain_cache),
|
||||
),
|
||||
shorteventid_cache: (
|
||||
memory_usage_of_locked_cache(&mut self.rooms.shorteventid_cache),
|
||||
items_in_locked_cache(&mut self.rooms.shorteventid_cache),
|
||||
capacity_of_locked_cache(&mut self.rooms.shorteventid_cache),
|
||||
),
|
||||
eventidshort_cache: (
|
||||
memory_usage_of_locked_cache(&mut self.rooms.eventidshort_cache),
|
||||
items_in_locked_cache(&mut self.rooms.eventidshort_cache),
|
||||
capacity_of_locked_cache(&mut self.rooms.eventidshort_cache),
|
||||
),
|
||||
statekeyshort_cache: (
|
||||
memory_usage_of_locked_cache(&mut self.rooms.statekeyshort_cache),
|
||||
items_in_locked_cache(&mut self.rooms.statekeyshort_cache),
|
||||
capacity_of_locked_cache(&mut self.rooms.statekeyshort_cache),
|
||||
),
|
||||
shortstatekey_cache: (
|
||||
memory_usage_of_locked_cache(&mut self.rooms.shortstatekey_cache),
|
||||
items_in_locked_cache(&mut self.rooms.shortstatekey_cache),
|
||||
capacity_of_locked_cache(&mut self.rooms.shortstatekey_cache),
|
||||
),
|
||||
stateinfo_cache: (
|
||||
memory_usage_of_locked_cache(&mut self.rooms.stateinfo_cache),
|
||||
items_in_locked_cache(&mut self.rooms.stateinfo_cache),
|
||||
capacity_of_locked_cache(&mut self.rooms.stateinfo_cache),
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DatabaseGuard(OwnedRwLockReadGuard<Database>);
|
||||
|
||||
+38
-4
@@ -3,19 +3,21 @@ use std::{
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use crate::{pdu::PduBuilder, Database};
|
||||
use rocket::futures::{channel::mpsc, stream::StreamExt};
|
||||
use ruma::{
|
||||
events::{room::message, EventType},
|
||||
UserId,
|
||||
};
|
||||
use tokio::sync::{MutexGuard, RwLock, RwLockReadGuard};
|
||||
use tokio::sync::{MutexGuard, RwLock, RwLockWriteGuard};
|
||||
use tracing::warn;
|
||||
|
||||
use crate::{pdu::PduBuilder, Database};
|
||||
|
||||
pub enum AdminCommand {
|
||||
RegisterAppservice(serde_yaml::Value),
|
||||
ListAppservices,
|
||||
SendMessage(message::MessageEventContent),
|
||||
ShowCacheUsage,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -59,7 +61,7 @@ impl Admin {
|
||||
drop(guard);
|
||||
|
||||
let send_message = |message: message::MessageEventContent,
|
||||
guard: RwLockReadGuard<'_, Database>,
|
||||
guard: RwLockWriteGuard<'_, Database>,
|
||||
mutex_lock: &MutexGuard<'_, ()>| {
|
||||
guard
|
||||
.rooms
|
||||
@@ -83,7 +85,7 @@ impl Admin {
|
||||
loop {
|
||||
tokio::select! {
|
||||
Some(event) = receiver.next() => {
|
||||
let guard = db.read().await;
|
||||
let mut guard = db.write().await;
|
||||
let mutex_state = Arc::clone(
|
||||
guard.globals
|
||||
.roomid_mutex_state
|
||||
@@ -92,6 +94,7 @@ impl Admin {
|
||||
.entry(conduit_room.clone())
|
||||
.or_default(),
|
||||
);
|
||||
|
||||
let state_lock = mutex_state.lock().await;
|
||||
|
||||
match event {
|
||||
@@ -114,6 +117,37 @@ impl Admin {
|
||||
AdminCommand::SendMessage(message) => {
|
||||
send_message(message, guard, &state_lock);
|
||||
}
|
||||
AdminCommand::ShowCacheUsage => {
|
||||
|
||||
fn format_cache_statistics_triple(name: String, triple: (usize, usize, usize)) -> String {
|
||||
let (memory_usage, item_count, capacity) = triple;
|
||||
format!(
|
||||
"{0} is using {1} MB ({2} bytes) of RAM at {3:.2}% utilization.",
|
||||
name,
|
||||
memory_usage / 100_00,
|
||||
memory_usage ,
|
||||
((item_count as f32 / capacity as f32) * 100.0)
|
||||
)
|
||||
}
|
||||
|
||||
if let Ok(cache_usage_statistics) = guard.get_cache_usage() {
|
||||
|
||||
let mut statistics_lines = Vec::with_capacity(7);
|
||||
statistics_lines.push(format_cache_statistics_triple("pdu_cache".to_string(), cache_usage_statistics.pdu_cache));
|
||||
statistics_lines.push(format_cache_statistics_triple("auth_chain_cache".to_string(), cache_usage_statistics.auth_chain_cache));
|
||||
statistics_lines.push(format_cache_statistics_triple("shorteventid_cache".to_string(), cache_usage_statistics.shorteventid_cache));
|
||||
statistics_lines.push(format_cache_statistics_triple("eventidshort_cache".to_string(), cache_usage_statistics.eventidshort_cache));
|
||||
statistics_lines.push(format_cache_statistics_triple("statekeyshort_cache".to_string(), cache_usage_statistics.statekeyshort_cache));
|
||||
statistics_lines.push(format_cache_statistics_triple("shortstatekey_cache".to_string(), cache_usage_statistics.shortstatekey_cache));
|
||||
statistics_lines.push(format_cache_statistics_triple("stateinfo_cache".to_string(), cache_usage_statistics.stateinfo_cache));
|
||||
|
||||
send_message(message::MessageEventContent::text_plain(statistics_lines.join("\n")), guard, &state_lock);
|
||||
} else {
|
||||
let result_text = "Could not calculate database cache size";
|
||||
send_message(message::MessageEventContent::text_plain(result_text), guard, &state_lock);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
drop(state_lock);
|
||||
|
||||
+61
-16
@@ -1,4 +1,4 @@
|
||||
use crate::{database::Config, server_server::FedDest, utils, ConduitResult, Error, Result};
|
||||
use crate::{database::Config, utils, ConduitResult, Error, Result};
|
||||
use ruma::{
|
||||
api::{
|
||||
client::r0::sync::sync_events,
|
||||
@@ -6,25 +6,25 @@ use ruma::{
|
||||
},
|
||||
DeviceId, EventId, MilliSecondsSinceUnixEpoch, RoomId, ServerName, ServerSigningKeyId, UserId,
|
||||
};
|
||||
use rustls::{ServerCertVerifier, WebPKIVerifier};
|
||||
use std::{
|
||||
collections::{BTreeMap, HashMap},
|
||||
fs,
|
||||
future::Future,
|
||||
net::IpAddr,
|
||||
path::PathBuf,
|
||||
sync::{Arc, Mutex, RwLock},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use tokio::sync::{broadcast, watch::Receiver, Mutex as TokioMutex, Semaphore};
|
||||
use tracing::error;
|
||||
use tracing::{error, info};
|
||||
use trust_dns_resolver::TokioAsyncResolver;
|
||||
|
||||
use super::abstraction::Tree;
|
||||
|
||||
pub const COUNTER: &[u8] = b"c";
|
||||
|
||||
type WellKnownMap = HashMap<Box<ServerName>, (FedDest, String)>;
|
||||
type TlsNameMap = HashMap<String, Vec<IpAddr>>;
|
||||
type WellKnownMap = HashMap<Box<ServerName>, (String, String)>;
|
||||
type TlsNameMap = HashMap<String, webpki::DNSName>;
|
||||
type RateLimitState = (Instant, u32); // Time if last failed try, number of failed tries
|
||||
type SyncHandle = (
|
||||
Option<String>, // since
|
||||
@@ -37,6 +37,7 @@ pub struct Globals {
|
||||
pub(super) globals: Arc<dyn Tree>,
|
||||
config: Config,
|
||||
keypair: Arc<ruma::signatures::Ed25519KeyPair>,
|
||||
reqwest_client: reqwest::Client,
|
||||
dns_resolver: TokioAsyncResolver,
|
||||
jwt_decoding_key: Option<jsonwebtoken::DecodingKey<'static>>,
|
||||
pub(super) server_signingkeys: Arc<dyn Tree>,
|
||||
@@ -50,6 +51,40 @@ pub struct Globals {
|
||||
pub rotate: RotationHandler,
|
||||
}
|
||||
|
||||
struct MatrixServerVerifier {
|
||||
inner: WebPKIVerifier,
|
||||
tls_name_override: Arc<RwLock<TlsNameMap>>,
|
||||
}
|
||||
|
||||
impl ServerCertVerifier for MatrixServerVerifier {
|
||||
#[tracing::instrument(skip(self, roots, presented_certs, dns_name, ocsp_response))]
|
||||
fn verify_server_cert(
|
||||
&self,
|
||||
roots: &rustls::RootCertStore,
|
||||
presented_certs: &[rustls::Certificate],
|
||||
dns_name: webpki::DNSNameRef<'_>,
|
||||
ocsp_response: &[u8],
|
||||
) -> std::result::Result<rustls::ServerCertVerified, rustls::TLSError> {
|
||||
if let Some(override_name) = self.tls_name_override.read().unwrap().get(dns_name.into()) {
|
||||
let result = self.inner.verify_server_cert(
|
||||
roots,
|
||||
presented_certs,
|
||||
override_name.as_ref(),
|
||||
ocsp_response,
|
||||
);
|
||||
if result.is_ok() {
|
||||
return result;
|
||||
}
|
||||
info!(
|
||||
"Server {:?} is non-compliant, retrying TLS verification with original name",
|
||||
dns_name
|
||||
);
|
||||
}
|
||||
self.inner
|
||||
.verify_server_cert(roots, presented_certs, dns_name, ocsp_response)
|
||||
}
|
||||
}
|
||||
|
||||
/// Handles "rotation" of long-polling requests. "Rotation" in this context is similar to "rotation" of log files and the like.
|
||||
///
|
||||
/// This is utilized to have sync workers return early and release read locks on the database.
|
||||
@@ -127,6 +162,24 @@ impl Globals {
|
||||
};
|
||||
|
||||
let tls_name_override = Arc::new(RwLock::new(TlsNameMap::new()));
|
||||
let verifier = Arc::new(MatrixServerVerifier {
|
||||
inner: WebPKIVerifier::new(),
|
||||
tls_name_override: tls_name_override.clone(),
|
||||
});
|
||||
let mut tlsconfig = rustls::ClientConfig::new();
|
||||
tlsconfig.dangerous().set_certificate_verifier(verifier);
|
||||
tlsconfig.root_store =
|
||||
rustls_native_certs::load_native_certs().expect("Error loading system certificates");
|
||||
|
||||
let mut reqwest_client_builder = reqwest::Client::builder()
|
||||
.connect_timeout(Duration::from_secs(30))
|
||||
.timeout(Duration::from_secs(60 * 3))
|
||||
.pool_max_idle_per_host(1)
|
||||
.use_preconfigured_tls(tlsconfig);
|
||||
if let Some(proxy) = config.proxy.to_proxy()? {
|
||||
reqwest_client_builder = reqwest_client_builder.proxy(proxy);
|
||||
}
|
||||
let reqwest_client = reqwest_client_builder.build().unwrap();
|
||||
|
||||
let jwt_decoding_key = config
|
||||
.jwt_secret
|
||||
@@ -137,6 +190,7 @@ impl Globals {
|
||||
globals,
|
||||
config,
|
||||
keypair: Arc::new(keypair),
|
||||
reqwest_client,
|
||||
dns_resolver: TokioAsyncResolver::tokio_from_system_conf().map_err(|_| {
|
||||
Error::bad_config("Failed to set up trust dns resolver with system config.")
|
||||
})?,
|
||||
@@ -165,17 +219,8 @@ impl Globals {
|
||||
}
|
||||
|
||||
/// Returns a reqwest client which can be used to send requests.
|
||||
pub fn reqwest_client(&self) -> Result<reqwest::ClientBuilder> {
|
||||
let mut reqwest_client_builder = reqwest::Client::builder()
|
||||
.connect_timeout(Duration::from_secs(30))
|
||||
.timeout(Duration::from_secs(60 * 3))
|
||||
.pool_max_idle_per_host(1);
|
||||
//.use_preconfigured_tls(tlsconfig);
|
||||
if let Some(proxy) = self.config.proxy.to_proxy()? {
|
||||
reqwest_client_builder = reqwest_client_builder.proxy(proxy);
|
||||
}
|
||||
|
||||
Ok(reqwest_client_builder)
|
||||
pub fn reqwest_client(&self) -> &reqwest::Client {
|
||||
&self.reqwest_client
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
|
||||
@@ -113,11 +113,7 @@ where
|
||||
//*reqwest_request.timeout_mut() = Some(Duration::from_secs(5));
|
||||
|
||||
let url = reqwest_request.url().clone();
|
||||
let response = globals
|
||||
.reqwest_client()?
|
||||
.build()?
|
||||
.execute(reqwest_request)
|
||||
.await;
|
||||
let response = globals.reqwest_client().execute(reqwest_request).await;
|
||||
|
||||
match response {
|
||||
Ok(mut response) => {
|
||||
|
||||
+17
-14
@@ -1,10 +1,12 @@
|
||||
mod edus;
|
||||
use std::{
|
||||
collections::{BTreeMap, HashMap, HashSet},
|
||||
convert::{TryFrom, TryInto},
|
||||
mem::size_of,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
pub use edus::RoomEdus;
|
||||
use member::MembershipState;
|
||||
|
||||
use crate::{pdu::PduBuilder, utils, Database, Error, PduEvent, Result};
|
||||
use lru_cache::LruCache;
|
||||
use member::MembershipState;
|
||||
use regex::Regex;
|
||||
use ring::digest;
|
||||
use rocket::http::RawStr;
|
||||
@@ -22,17 +24,17 @@ use ruma::{
|
||||
state_res::{self, RoomVersion, StateMap},
|
||||
uint, EventId, RoomAliasId, RoomId, RoomVersionId, ServerName, UserId,
|
||||
};
|
||||
use std::{
|
||||
collections::{BTreeMap, HashMap, HashSet},
|
||||
convert::{TryFrom, TryInto},
|
||||
mem::size_of,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
use tokio::sync::MutexGuard;
|
||||
use tracing::{error, warn};
|
||||
|
||||
pub use edus::RoomEdus;
|
||||
|
||||
use crate::{pdu::PduBuilder, utils, Database, Error, PduEvent, Result};
|
||||
|
||||
use super::{abstraction::Tree, admin::AdminCommand, pusher};
|
||||
|
||||
mod edus;
|
||||
|
||||
/// The unique identifier of each state group.
|
||||
///
|
||||
/// This is created when a state group is added to the database by
|
||||
@@ -1566,10 +1568,13 @@ impl Rooms {
|
||||
));
|
||||
}
|
||||
}
|
||||
"show_cache_usage" => {
|
||||
db.admin.send(AdminCommand::ShowCacheUsage);
|
||||
}
|
||||
_ => {
|
||||
db.admin.send(AdminCommand::SendMessage(
|
||||
message::MessageEventContent::text_plain(format!(
|
||||
"Unrecognized command: {}",
|
||||
"Unrecognized command: {}\nAvailable commands:\n- register_appservice\n- list_appservices\n- get_pdu\n- show_cache_usage",
|
||||
command
|
||||
)),
|
||||
));
|
||||
@@ -2482,7 +2487,6 @@ impl Rooms {
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
room_id: &RoomId,
|
||||
reason: Option<String>,
|
||||
db: &Database,
|
||||
) -> Result<()> {
|
||||
// Ask a remote server if we don't have this room
|
||||
@@ -2531,7 +2535,6 @@ impl Rooms {
|
||||
.map_err(|_| Error::bad_database("Invalid member event in database."))?;
|
||||
|
||||
event.membership = member::MembershipState::Leave;
|
||||
event.reason = reason;
|
||||
|
||||
self.build_and_append_pdu(
|
||||
PduBuilder {
|
||||
|
||||
+41
-86
@@ -83,7 +83,7 @@ use rocket::{get, post, put};
|
||||
/// FedDest::Named("198.51.100.5".to_owned(), "".to_owned());
|
||||
/// ```
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum FedDest {
|
||||
enum FedDest {
|
||||
Literal(SocketAddr),
|
||||
Named(String, String),
|
||||
}
|
||||
@@ -109,13 +109,6 @@ impl FedDest {
|
||||
Self::Named(host, _) => host.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
fn port(&self) -> Option<u16> {
|
||||
match &self {
|
||||
Self::Literal(addr) => Some(addr.port()),
|
||||
Self::Named(_, port) => port[1..].parse().ok(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(globals, request))]
|
||||
@@ -131,34 +124,41 @@ where
|
||||
return Err(Error::bad_config("Federation is disabled."));
|
||||
}
|
||||
|
||||
let mut write_destination_to_cache = false;
|
||||
|
||||
let cached_result = globals
|
||||
let maybe_result = globals
|
||||
.actual_destination_cache
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(destination)
|
||||
.cloned();
|
||||
|
||||
let (actual_destination, host) = if let Some(result) = cached_result {
|
||||
let (actual_destination, host) = if let Some(result) = maybe_result {
|
||||
result
|
||||
} else {
|
||||
write_destination_to_cache = true;
|
||||
|
||||
let result = find_actual_destination(globals, &destination).await;
|
||||
|
||||
(result.0, result.1.clone().into_uri_string())
|
||||
let (actual_destination, host) = result.clone();
|
||||
let result_string = (result.0.into_https_string(), result.1.into_uri_string());
|
||||
globals
|
||||
.actual_destination_cache
|
||||
.write()
|
||||
.unwrap()
|
||||
.insert(Box::<ServerName>::from(destination), result_string.clone());
|
||||
let dest_hostname = actual_destination.hostname();
|
||||
let host_hostname = host.hostname();
|
||||
if dest_hostname != host_hostname {
|
||||
globals.tls_name_override.write().unwrap().insert(
|
||||
dest_hostname,
|
||||
webpki::DNSNameRef::try_from_ascii_str(&host_hostname)
|
||||
.unwrap()
|
||||
.to_owned(),
|
||||
);
|
||||
}
|
||||
result_string
|
||||
};
|
||||
|
||||
let actual_destination_str = actual_destination.clone().into_https_string();
|
||||
|
||||
let mut http_request = request
|
||||
.try_into_http_request::<Vec<u8>>(&actual_destination_str, SendAccessToken::IfRequired(""))
|
||||
.try_into_http_request::<Vec<u8>>(&actual_destination, SendAccessToken::IfRequired(""))
|
||||
.map_err(|e| {
|
||||
warn!(
|
||||
"Failed to find destination {}: {}",
|
||||
actual_destination_str, e
|
||||
);
|
||||
warn!("Failed to find destination {}: {}", actual_destination, e);
|
||||
Error::BadServerResponse("Invalid destination")
|
||||
})?;
|
||||
|
||||
@@ -232,22 +232,7 @@ where
|
||||
.expect("all http requests are valid reqwest requests");
|
||||
|
||||
let url = reqwest_request.url().clone();
|
||||
|
||||
let mut client = globals.reqwest_client()?;
|
||||
if let Some(override_name) = globals
|
||||
.tls_name_override
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&actual_destination.hostname())
|
||||
{
|
||||
client = client.resolve(
|
||||
&actual_destination.hostname(),
|
||||
SocketAddr::new(override_name[0], 0),
|
||||
);
|
||||
// port will be ignored
|
||||
}
|
||||
|
||||
let response = client.build()?.execute(reqwest_request).await;
|
||||
let response = globals.reqwest_client().execute(reqwest_request).await;
|
||||
|
||||
match response {
|
||||
Ok(mut response) => {
|
||||
@@ -286,13 +271,6 @@ where
|
||||
|
||||
if status == 200 {
|
||||
let response = T::IncomingResponse::try_from_http_response(http_response);
|
||||
if response.is_ok() && write_destination_to_cache {
|
||||
globals.actual_destination_cache.write().unwrap().insert(
|
||||
Box::<ServerName>::from(destination),
|
||||
(actual_destination, host),
|
||||
);
|
||||
}
|
||||
|
||||
response.map_err(|e| {
|
||||
warn!(
|
||||
"Invalid 200 response from {} on: {} {}",
|
||||
@@ -365,46 +343,16 @@ async fn find_actual_destination(
|
||||
match get_ip_with_port(&delegated_hostname) {
|
||||
Some(host_and_port) => host_and_port, // 3.1: IP literal in .well-known file
|
||||
None => {
|
||||
if let Some(pos) = delegated_hostname.find(':') {
|
||||
if let Some(pos) = destination_str.find(':') {
|
||||
// 3.2: Hostname with port in .well-known file
|
||||
let (host, port) = delegated_hostname.split_at(pos);
|
||||
let (host, port) = destination_str.split_at(pos);
|
||||
FedDest::Named(host.to_string(), port.to_string())
|
||||
} else {
|
||||
if let Some(hostname_override) =
|
||||
query_srv_record(globals, &delegated_hostname).await
|
||||
{
|
||||
match query_srv_record(globals, &delegated_hostname).await {
|
||||
// 3.3: SRV lookup successful
|
||||
let host = match delegated_hostname.find(':') {
|
||||
None => delegated_hostname.clone(),
|
||||
Some(pos) => {
|
||||
delegated_hostname.split_at(pos).0.to_owned()
|
||||
}
|
||||
};
|
||||
|
||||
if let Ok(override_ip) = globals
|
||||
.dns_resolver()
|
||||
.lookup_ip(hostname_override.hostname())
|
||||
.await
|
||||
{
|
||||
globals
|
||||
.tls_name_override
|
||||
.write()
|
||||
.unwrap()
|
||||
.insert(host.clone(), override_ip.iter().collect());
|
||||
} else {
|
||||
warn!("Using SRV record, but could not resolve to IP");
|
||||
}
|
||||
|
||||
let force_port = hostname_override.port();
|
||||
|
||||
if let Some(port) = force_port {
|
||||
FedDest::Named(host, format!(":{}", port.to_string()))
|
||||
} else {
|
||||
add_port_to_hostname(&delegated_hostname)
|
||||
}
|
||||
} else {
|
||||
Some(hostname) => hostname,
|
||||
// 3.4: No SRV records, just use the hostname from .well-known
|
||||
add_port_to_hostname(&delegated_hostname)
|
||||
None => add_port_to_hostname(&delegated_hostname),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -475,9 +423,6 @@ pub async fn request_well_known(
|
||||
let body: serde_json::Value = serde_json::from_str(
|
||||
&globals
|
||||
.reqwest_client()
|
||||
.ok()?
|
||||
.build()
|
||||
.ok()?
|
||||
.get(&format!(
|
||||
"https://{}/.well-known/matrix/server",
|
||||
destination
|
||||
@@ -2035,9 +1980,15 @@ fn get_auth_chain(
|
||||
let mut buckets = vec![BTreeSet::new(); NUM_BUCKETS];
|
||||
|
||||
for id in starting_events {
|
||||
let short = db.rooms.get_or_create_shorteventid(&id, &db.globals)?;
|
||||
if let Some(pdu) = db.rooms.get_pdu(&id)? {
|
||||
for auth_event in &pdu.auth_events {
|
||||
let short = db
|
||||
.rooms
|
||||
.get_or_create_shorteventid(&auth_event, &db.globals)?;
|
||||
let bucket_id = (short % NUM_BUCKETS as u64) as usize;
|
||||
buckets[bucket_id].insert((short, id.clone()));
|
||||
buckets[bucket_id].insert((short, auth_event.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut full_auth_chain = HashSet::new();
|
||||
@@ -2049,6 +2000,10 @@ fn get_auth_chain(
|
||||
continue;
|
||||
}
|
||||
|
||||
// The code below will only get the auth chains, not the events in the chunk. So let's add
|
||||
// them first
|
||||
full_auth_chain.extend(chunk.iter().map(|(id, _)| id));
|
||||
|
||||
let chunk_key = chunk
|
||||
.iter()
|
||||
.map(|(short, _)| short)
|
||||
|
||||
Reference in New Issue
Block a user