conduit/src/database/globals.rs

320 lines
11 KiB
Rust
Raw Normal View History

2021-08-26 21:11:13 +00:00
use crate::{database::Config, server_server::FedDest, utils, ConduitResult, Error, Result};
use ruma::{
2021-06-30 07:52:01 +00:00
api::{
client::r0::sync::sync_events,
federation::discovery::{ServerSigningKeys, VerifyKey},
},
DeviceId, EventId, MilliSecondsSinceUnixEpoch, RoomId, ServerName, ServerSigningKeyId, UserId,
};
2021-06-06 12:28:32 +00:00
use std::{
collections::{BTreeMap, HashMap},
2021-06-08 13:20:06 +00:00
fs,
2021-07-14 07:07:08 +00:00
future::Future,
2021-08-26 21:11:13 +00:00
net::IpAddr,
2021-06-06 12:28:32 +00:00
path::PathBuf,
2021-08-03 09:10:58 +00:00
sync::{Arc, Mutex, RwLock},
2021-06-06 12:28:32 +00:00
time::{Duration, Instant},
};
use tokio::sync::{broadcast, watch::Receiver, Mutex as TokioMutex, Semaphore};
2021-08-26 21:11:13 +00:00
use tracing::error;
use trust_dns_resolver::TokioAsyncResolver;
2021-06-08 16:10:00 +00:00
use super::abstraction::Tree;
pub const COUNTER: &[u8] = b"c";
2021-08-26 21:11:13 +00:00
type WellKnownMap = HashMap<Box<ServerName>, (FedDest, String)>;
type TlsNameMap = HashMap<String, (Vec<IpAddr>, u16)>;
type RateLimitState = (Instant, u32); // Time if last failed try, number of failed tries
type SyncHandle = (
Option<String>, // since
Receiver<Option<ConduitResult<sync_events::Response>>>, // rx
);
pub struct Globals {
pub actual_destination_cache: Arc<RwLock<WellKnownMap>>, // actual_destination, host
pub tls_name_override: Arc<RwLock<TlsNameMap>>,
2021-06-08 16:10:00 +00:00
pub(super) globals: Arc<dyn Tree>,
2020-12-06 10:05:51 +00:00
config: Config,
2020-09-15 14:13:54 +00:00
keypair: Arc<ruma::signatures::Ed25519KeyPair>,
2020-12-06 10:05:51 +00:00
dns_resolver: TokioAsyncResolver,
2021-02-07 16:38:45 +00:00
jwt_decoding_key: Option<jsonwebtoken::DecodingKey<'static>>,
2021-06-08 16:10:00 +00:00
pub(super) server_signingkeys: Arc<dyn Tree>,
2021-07-18 18:43:39 +00:00
pub bad_event_ratelimiter: Arc<RwLock<HashMap<EventId, RateLimitState>>>,
pub bad_signature_ratelimiter: Arc<RwLock<HashMap<Vec<String>, RateLimitState>>>,
pub servername_ratelimiter: Arc<RwLock<HashMap<Box<ServerName>, Arc<Semaphore>>>>,
pub sync_receivers: RwLock<HashMap<(UserId, Box<DeviceId>), SyncHandle>>,
2021-08-03 09:10:58 +00:00
pub roomid_mutex_insert: RwLock<HashMap<RoomId, Arc<Mutex<()>>>>,
pub roomid_mutex_state: RwLock<HashMap<RoomId, Arc<TokioMutex<()>>>>,
pub roomid_mutex_federation: RwLock<HashMap<RoomId, Arc<TokioMutex<()>>>>, // this lock will be held longer
2021-07-14 07:07:08 +00:00
pub rotate: RotationHandler,
}
2021-07-14 07:07:08 +00:00
/// 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.
pub struct RotationHandler(broadcast::Sender<()>, broadcast::Receiver<()>);
impl RotationHandler {
pub fn new() -> Self {
let (s, r) = broadcast::channel::<()>(1);
Self(s, r)
}
pub fn watch(&self) -> impl Future<Output = ()> {
let mut r = self.0.subscribe();
async move {
let _ = r.recv().await;
}
}
pub fn fire(&self) {
let _ = self.0.send(());
}
}
impl Default for RotationHandler {
fn default() -> Self {
Self::new()
}
}
impl Globals {
pub fn load(
2021-06-08 16:10:00 +00:00
globals: Arc<dyn Tree>,
server_signingkeys: Arc<dyn Tree>,
config: Config,
) -> Result<Self> {
2021-06-08 16:10:00 +00:00
let keypair_bytes = globals.get(b"keypair")?.map_or_else(
|| {
let keypair = utils::generate_keypair();
globals.insert(b"keypair", &keypair)?;
Ok::<_, Error>(keypair)
},
|s| Ok(s.to_vec()),
)?;
2021-06-08 16:10:00 +00:00
let mut parts = keypair_bytes.splitn(2, |&b| b == 0xff);
let keypair = utils::string_from_bytes(
// 1. version
parts
.next()
.expect("splitn always returns at least one element"),
)
.map_err(|_| Error::bad_database("Invalid version bytes in keypair."))
.and_then(|version| {
// 2. key
parts
.next()
.ok_or_else(|| Error::bad_database("Invalid keypair format in database."))
.map(|key| (version, key))
})
.and_then(|(version, key)| {
ruma::signatures::Ed25519KeyPair::from_der(&key, version)
.map_err(|_| Error::bad_database("Private or public keys are invalid."))
});
let keypair = match keypair {
Ok(k) => k,
Err(e) => {
error!("Keypair invalid. Deleting...");
2021-06-08 16:10:00 +00:00
globals.remove(b"keypair")?;
return Err(e);
}
};
let tls_name_override = Arc::new(RwLock::new(TlsNameMap::new()));
2020-12-19 15:00:11 +00:00
2021-02-07 16:38:45 +00:00
let jwt_decoding_key = config
.jwt_secret
.as_ref()
.map(|secret| jsonwebtoken::DecodingKey::from_secret(secret.as_bytes()).into_static());
2021-06-08 13:20:06 +00:00
let s = Self {
globals,
2020-12-06 10:05:51 +00:00
config,
keypair: Arc::new(keypair),
dns_resolver: TokioAsyncResolver::tokio_from_system_conf().map_err(|_| {
Error::bad_config("Failed to set up trust dns resolver with system config.")
})?,
actual_destination_cache: Arc::new(RwLock::new(WellKnownMap::new())),
tls_name_override,
server_signingkeys,
jwt_decoding_key,
2021-07-18 18:43:39 +00:00
bad_event_ratelimiter: Arc::new(RwLock::new(HashMap::new())),
bad_signature_ratelimiter: Arc::new(RwLock::new(HashMap::new())),
servername_ratelimiter: Arc::new(RwLock::new(HashMap::new())),
2021-08-03 09:10:58 +00:00
roomid_mutex_state: RwLock::new(HashMap::new()),
roomid_mutex_insert: RwLock::new(HashMap::new()),
2021-07-18 18:43:39 +00:00
roomid_mutex_federation: RwLock::new(HashMap::new()),
sync_receivers: RwLock::new(HashMap::new()),
2021-07-14 07:07:08 +00:00
rotate: RotationHandler::new(),
2021-06-08 13:20:06 +00:00
};
fs::create_dir_all(s.get_media_folder())?;
Ok(s)
}
/// Returns this server's keypair.
2020-06-05 16:19:26 +00:00
pub fn keypair(&self) -> &ruma::signatures::Ed25519KeyPair {
&self.keypair
}
/// Returns a reqwest client which can be used to send requests.
2021-08-26 21:11:13 +00:00
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);
if let Some(proxy) = self.config.proxy.to_proxy()? {
reqwest_client_builder = reqwest_client_builder.proxy(proxy);
}
Ok(reqwest_client_builder)
}
#[tracing::instrument(skip(self))]
pub fn next_count(&self) -> Result<u64> {
2021-06-17 18:34:14 +00:00
utils::u64_from_bytes(&self.globals.increment(COUNTER)?)
.map_err(|_| Error::bad_database("Count has invalid bytes."))
}
#[tracing::instrument(skip(self))]
pub fn current_count(&self) -> Result<u64> {
2020-06-09 13:13:17 +00:00
self.globals.get(COUNTER)?.map_or(Ok(0_u64), |bytes| {
2021-06-17 18:34:14 +00:00
utils::u64_from_bytes(&bytes)
.map_err(|_| Error::bad_database("Count has invalid bytes."))
2020-06-09 13:13:17 +00:00
})
}
pub fn server_name(&self) -> &ServerName {
self.config.server_name.as_ref()
}
pub fn max_request_size(&self) -> u32 {
self.config.max_request_size
}
2021-01-01 12:47:53 +00:00
pub fn allow_registration(&self) -> bool {
self.config.allow_registration
}
2021-01-01 12:47:53 +00:00
pub fn allow_encryption(&self) -> bool {
self.config.allow_encryption
}
2021-01-01 12:47:53 +00:00
pub fn allow_federation(&self) -> bool {
self.config.allow_federation
}
2020-12-06 10:05:51 +00:00
pub fn trusted_servers(&self) -> &[Box<ServerName>] {
&self.config.trusted_servers
}
2020-12-06 10:05:51 +00:00
pub fn dns_resolver(&self) -> &TokioAsyncResolver {
&self.dns_resolver
}
2021-02-07 16:38:45 +00:00
pub fn jwt_decoding_key(&self) -> Option<&jsonwebtoken::DecodingKey<'_>> {
self.jwt_decoding_key.as_ref()
}
/// TODO: the key valid until timestamp is only honored in room version > 4
/// Remove the outdated keys and insert the new ones.
///
/// This doesn't actually check that the keys provided are newer than the old set.
pub fn add_signing_key(
&self,
origin: &ServerName,
new_keys: ServerSigningKeys,
) -> Result<BTreeMap<ServerSigningKeyId, VerifyKey>> {
2021-06-08 16:10:00 +00:00
// Not atomic, but this is not critical
let signingkeys = self.server_signingkeys.get(origin.as_bytes())?;
let mut keys = signingkeys
.and_then(|keys| serde_json::from_slice(&keys).ok())
.unwrap_or_else(|| {
// Just insert "now", it doesn't matter
ServerSigningKeys::new(origin.to_owned(), MilliSecondsSinceUnixEpoch::now())
});
let ServerSigningKeys {
verify_keys,
old_verify_keys,
..
} = new_keys;
keys.verify_keys.extend(verify_keys.into_iter());
keys.old_verify_keys.extend(old_verify_keys.into_iter());
self.server_signingkeys.insert(
origin.as_bytes(),
&serde_json::to_vec(&keys).expect("serversigningkeys can be serialized"),
)?;
let mut tree = keys.verify_keys;
tree.extend(
keys.old_verify_keys
.into_iter()
.map(|old| (old.0, VerifyKey::new(old.1.key))),
);
Ok(tree)
}
/// This returns an empty `Ok(BTreeMap<..>)` when there are no keys found for the server.
pub fn signing_keys_for(
&self,
origin: &ServerName,
) -> Result<BTreeMap<ServerSigningKeyId, VerifyKey>> {
let signingkeys = self
.server_signingkeys
.get(origin.as_bytes())?
.and_then(|bytes| serde_json::from_slice::<ServerSigningKeys>(&bytes).ok())
.map(|keys| {
let mut tree = keys.verify_keys;
tree.extend(
keys.old_verify_keys
.into_iter()
.map(|old| (old.0, VerifyKey::new(old.1.key))),
);
tree
})
.unwrap_or_else(BTreeMap::new);
Ok(signingkeys)
}
pub fn database_version(&self) -> Result<u64> {
2021-06-08 16:10:00 +00:00
self.globals.get(b"version")?.map_or(Ok(0), |version| {
utils::u64_from_bytes(&version)
.map_err(|_| Error::bad_database("Database version id is invalid."))
})
}
pub fn bump_database_version(&self, new_version: u64) -> Result<()> {
2021-06-08 16:10:00 +00:00
self.globals
.insert(b"version", &new_version.to_be_bytes())?;
Ok(())
}
2021-06-04 03:36:12 +00:00
pub fn get_media_folder(&self) -> PathBuf {
let mut r = PathBuf::new();
r.push(self.config.database_path.clone());
r.push("media");
r
}
2021-06-06 12:28:32 +00:00
pub fn get_media_file(&self, key: &[u8]) -> PathBuf {
2021-06-04 03:36:12 +00:00
let mut r = PathBuf::new();
r.push(self.config.database_path.clone());
r.push("media");
r.push(base64::encode_config(key, base64::URL_SAFE_NO_PAD));
r
}
}