2020-06-21 20:58:42 +01:00
|
|
|
use crate::{utils, Error, Result};
|
2020-09-15 20:46:10 +01:00
|
|
|
use log::error;
|
2020-07-26 14:41:28 +01:00
|
|
|
use ruma::ServerName;
|
2020-09-15 15:13:54 +01:00
|
|
|
use std::{convert::TryInto, sync::Arc};
|
2020-07-24 04:03:24 +01:00
|
|
|
|
2020-05-03 16:25:31 +01:00
|
|
|
pub const COUNTER: &str = "c";
|
|
|
|
|
2020-09-15 15:13:54 +01:00
|
|
|
#[derive(Clone)]
|
2020-05-03 16:25:31 +01:00
|
|
|
pub struct Globals {
|
|
|
|
pub(super) globals: sled::Tree,
|
2020-09-15 15:13:54 +01:00
|
|
|
keypair: Arc<ruma::signatures::Ed25519KeyPair>,
|
2020-05-03 16:25:31 +01:00
|
|
|
reqwest_client: reqwest::Client,
|
2020-07-17 21:00:39 +01:00
|
|
|
server_name: Box<ServerName>,
|
2020-07-24 04:03:24 +01:00
|
|
|
max_request_size: u32,
|
2020-06-06 18:02:31 +01:00
|
|
|
registration_disabled: bool,
|
2020-07-26 19:41:10 +01:00
|
|
|
encryption_disabled: bool,
|
2020-10-06 20:04:51 +01:00
|
|
|
federation_enabled: bool,
|
2020-05-03 16:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Globals {
|
2020-06-09 14:13:17 +01:00
|
|
|
pub fn load(globals: sled::Tree, config: &rocket::Config) -> Result<Self> {
|
2020-09-15 20:46:10 +01:00
|
|
|
let bytes = &*globals
|
|
|
|
.update_and_fetch("keypair", utils::generate_keypair)?
|
|
|
|
.expect("utils::generate_keypair always returns Some");
|
|
|
|
|
|
|
|
let mut parts = 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::new(&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...");
|
|
|
|
globals.remove("keypair")?;
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
};
|
2020-05-03 16:25:31 +01:00
|
|
|
|
2020-06-09 14:13:17 +01:00
|
|
|
Ok(Self {
|
2020-05-03 16:25:31 +01:00
|
|
|
globals,
|
2020-09-15 20:46:10 +01:00
|
|
|
keypair: Arc::new(keypair),
|
2020-05-03 16:25:31 +01:00
|
|
|
reqwest_client: reqwest::Client::new(),
|
2020-07-17 21:00:39 +01:00
|
|
|
server_name: config
|
|
|
|
.get_str("server_name")
|
|
|
|
.unwrap_or("localhost")
|
|
|
|
.to_string()
|
|
|
|
.try_into()
|
2020-11-14 22:13:06 +00:00
|
|
|
.map_err(|_| Error::bad_config("Invalid server_name."))?,
|
2020-07-24 04:03:24 +01:00
|
|
|
max_request_size: config
|
|
|
|
.get_int("max_request_size")
|
|
|
|
.unwrap_or(20 * 1024 * 1024) // Default to 20 MB
|
|
|
|
.try_into()
|
2020-11-14 22:13:06 +00:00
|
|
|
.map_err(|_| Error::bad_config("Invalid max_request_size."))?,
|
2020-06-06 18:02:31 +01:00
|
|
|
registration_disabled: config.get_bool("registration_disabled").unwrap_or(false),
|
2020-07-26 19:41:10 +01:00
|
|
|
encryption_disabled: config.get_bool("encryption_disabled").unwrap_or(false),
|
2020-10-06 20:04:51 +01:00
|
|
|
federation_enabled: config.get_bool("federation_enabled").unwrap_or(false),
|
2020-06-09 14:13:17 +01:00
|
|
|
})
|
2020-05-03 16:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns this server's keypair.
|
2020-06-05 17:19:26 +01:00
|
|
|
pub fn keypair(&self) -> &ruma::signatures::Ed25519KeyPair {
|
2020-05-03 16:25:31 +01:00
|
|
|
&self.keypair
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a reqwest client which can be used to send requests.
|
|
|
|
pub fn reqwest_client(&self) -> &reqwest::Client {
|
|
|
|
&self.reqwest_client
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn next_count(&self) -> Result<u64> {
|
|
|
|
Ok(utils::u64_from_bytes(
|
|
|
|
&self
|
|
|
|
.globals
|
|
|
|
.update_and_fetch(COUNTER, utils::increment)?
|
|
|
|
.expect("utils::increment will always put in a value"),
|
2020-06-09 14:13:17 +01:00
|
|
|
)
|
2020-06-11 09:03:08 +01:00
|
|
|
.map_err(|_| Error::bad_database("Count has invalid bytes."))?)
|
2020-05-03 16:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn current_count(&self) -> Result<u64> {
|
2020-06-09 14:13:17 +01:00
|
|
|
self.globals.get(COUNTER)?.map_or(Ok(0_u64), |bytes| {
|
|
|
|
Ok(utils::u64_from_bytes(&bytes)
|
2020-06-11 09:03:08 +01:00
|
|
|
.map_err(|_| Error::bad_database("Count has invalid bytes."))?)
|
2020-06-09 14:13:17 +01:00
|
|
|
})
|
2020-05-03 16:25:31 +01:00
|
|
|
}
|
2020-06-06 18:02:31 +01:00
|
|
|
|
2020-07-17 21:00:39 +01:00
|
|
|
pub fn server_name(&self) -> &ServerName {
|
2020-06-21 20:58:42 +01:00
|
|
|
self.server_name.as_ref()
|
2020-06-06 18:02:31 +01:00
|
|
|
}
|
|
|
|
|
2020-07-24 04:03:24 +01:00
|
|
|
pub fn max_request_size(&self) -> u32 {
|
|
|
|
self.max_request_size
|
|
|
|
}
|
|
|
|
|
2020-06-06 18:02:31 +01:00
|
|
|
pub fn registration_disabled(&self) -> bool {
|
|
|
|
self.registration_disabled
|
|
|
|
}
|
2020-07-26 19:41:10 +01:00
|
|
|
|
|
|
|
pub fn encryption_disabled(&self) -> bool {
|
|
|
|
self.encryption_disabled
|
|
|
|
}
|
2020-10-06 20:04:51 +01:00
|
|
|
|
|
|
|
pub fn federation_enabled(&self) -> bool {
|
|
|
|
self.federation_enabled
|
|
|
|
}
|
2020-05-03 16:25:31 +01:00
|
|
|
}
|