2020-07-29 21:27:49 +02:00
|
|
|
pub mod account_data;
|
2020-11-09 12:21:04 +01:00
|
|
|
pub mod admin;
|
2020-12-08 10:33:44 +01:00
|
|
|
pub mod appservice;
|
2020-07-29 21:27:49 +02:00
|
|
|
pub mod globals;
|
|
|
|
pub mod key_backups;
|
|
|
|
pub mod media;
|
2021-01-27 03:54:35 +01:00
|
|
|
pub mod pusher;
|
2020-07-29 21:27:49 +02:00
|
|
|
pub mod rooms;
|
2020-09-15 16:13:54 +02:00
|
|
|
pub mod sending;
|
2020-08-25 13:24:38 +02:00
|
|
|
pub mod transaction_ids;
|
2020-07-29 21:27:49 +02:00
|
|
|
pub mod uiaa;
|
|
|
|
pub mod users;
|
2020-05-03 17:25:31 +02:00
|
|
|
|
2020-06-09 15:13:17 +02:00
|
|
|
use crate::{Error, Result};
|
2020-03-30 13:46:18 +02:00
|
|
|
use directories::ProjectDirs;
|
2020-07-27 17:36:54 +02:00
|
|
|
use futures::StreamExt;
|
2020-10-21 21:43:59 +02:00
|
|
|
use log::info;
|
2020-12-05 21:03:43 +01:00
|
|
|
use rocket::futures::{self, channel::mpsc};
|
|
|
|
use ruma::{DeviceId, ServerName, UserId};
|
|
|
|
use serde::Deserialize;
|
2021-01-27 03:54:35 +01:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
fs::remove_dir_all,
|
|
|
|
sync::{Arc, RwLock},
|
|
|
|
};
|
2020-12-19 16:00:11 +01:00
|
|
|
use tokio::sync::Semaphore;
|
2020-12-05 21:03:43 +01:00
|
|
|
|
2021-01-15 03:32:22 +01:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
2020-12-05 21:03:43 +01:00
|
|
|
pub struct Config {
|
|
|
|
server_name: Box<ServerName>,
|
2020-12-31 21:07:05 +01:00
|
|
|
database_path: String,
|
2020-12-05 21:03:43 +01:00
|
|
|
#[serde(default = "default_cache_capacity")]
|
2021-02-06 15:27:43 +01:00
|
|
|
cache_capacity: u32,
|
2020-12-05 21:03:43 +01:00
|
|
|
#[serde(default = "default_max_request_size")]
|
|
|
|
max_request_size: u32,
|
2020-12-19 16:00:11 +01:00
|
|
|
#[serde(default = "default_max_concurrent_requests")]
|
|
|
|
max_concurrent_requests: u16,
|
2021-02-07 13:20:00 +01:00
|
|
|
#[serde(default = "true_fn")]
|
2021-01-01 13:47:53 +01:00
|
|
|
allow_registration: bool,
|
|
|
|
#[serde(default = "true_fn")]
|
|
|
|
allow_encryption: bool,
|
|
|
|
#[serde(default = "false_fn")]
|
|
|
|
allow_federation: bool,
|
2021-02-28 12:41:03 +01:00
|
|
|
#[serde(default = "false_fn")]
|
|
|
|
pub allow_jaeger: bool,
|
2021-02-07 17:38:45 +01:00
|
|
|
jwt_secret: Option<String>,
|
2021-03-01 15:17:53 +01:00
|
|
|
#[serde(default = "Vec::new")]
|
|
|
|
trusted_servers: Vec<Box<ServerName>>,
|
2021-01-01 13:47:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn false_fn() -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn true_fn() -> bool {
|
|
|
|
true
|
2020-12-05 21:03:43 +01:00
|
|
|
}
|
|
|
|
|
2021-02-06 15:27:43 +01:00
|
|
|
fn default_cache_capacity() -> u32 {
|
2020-12-05 21:03:43 +01:00
|
|
|
1024 * 1024 * 1024
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_max_request_size() -> u32 {
|
|
|
|
20 * 1024 * 1024 // Default to 20 MB
|
|
|
|
}
|
2020-05-06 15:36:44 +02:00
|
|
|
|
2020-12-19 16:00:11 +01:00
|
|
|
fn default_max_concurrent_requests() -> u16 {
|
|
|
|
4
|
|
|
|
}
|
|
|
|
|
2020-11-09 12:21:04 +01:00
|
|
|
#[derive(Clone)]
|
2020-03-30 13:46:18 +02:00
|
|
|
pub struct Database {
|
2020-05-03 17:25:31 +02:00
|
|
|
pub globals: globals::Globals,
|
|
|
|
pub users: users::Users,
|
2020-06-06 18:44:50 +02:00
|
|
|
pub uiaa: uiaa::Uiaa,
|
2020-05-03 17:25:31 +02:00
|
|
|
pub rooms: rooms::Rooms,
|
|
|
|
pub account_data: account_data::AccountData,
|
2020-05-18 17:53:34 +02:00
|
|
|
pub media: media::Media,
|
2020-06-16 12:11:38 +02:00
|
|
|
pub key_backups: key_backups::KeyBackups,
|
2020-08-25 13:24:38 +02:00
|
|
|
pub transaction_ids: transaction_ids::TransactionIds,
|
2020-09-15 16:13:54 +02:00
|
|
|
pub sending: sending::Sending,
|
2020-11-09 12:21:04 +01:00
|
|
|
pub admin: admin::Admin,
|
2020-12-08 10:33:44 +01:00
|
|
|
pub appservice: appservice::Appservice,
|
2021-01-27 03:54:35 +01:00
|
|
|
pub pusher: pusher::PushData,
|
2020-05-03 17:25:31 +02:00
|
|
|
pub _db: sled::Db,
|
2020-03-30 13:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Database {
|
2020-04-10 13:36:57 +02:00
|
|
|
/// Tries to remove the old database but ignores all errors.
|
2020-06-09 15:13:17 +02:00
|
|
|
pub fn try_remove(server_name: &str) -> Result<()> {
|
2020-04-11 20:03:22 +02:00
|
|
|
let mut path = ProjectDirs::from("xyz", "koesters", "conduit")
|
2020-11-15 12:17:21 +01:00
|
|
|
.ok_or_else(|| Error::bad_config("The OS didn't return a valid home directory path."))?
|
2020-04-10 13:36:57 +02:00
|
|
|
.data_dir()
|
|
|
|
.to_path_buf();
|
2020-05-06 15:36:44 +02:00
|
|
|
path.push(server_name);
|
2020-04-10 13:36:57 +02:00
|
|
|
let _ = remove_dir_all(path);
|
2020-06-09 15:13:17 +02:00
|
|
|
|
|
|
|
Ok(())
|
2020-04-10 13:36:57 +02:00
|
|
|
}
|
|
|
|
|
2020-03-30 13:46:18 +02:00
|
|
|
/// Load an existing database or create a new one.
|
2020-12-06 11:05:51 +01:00
|
|
|
pub async fn load_or_create(config: Config) -> Result<Self> {
|
2020-10-21 21:43:59 +02:00
|
|
|
let db = sled::Config::default()
|
2020-12-31 21:07:05 +01:00
|
|
|
.path(&config.database_path)
|
2021-02-06 15:27:43 +01:00
|
|
|
.cache_capacity(config.cache_capacity as u64)
|
2020-10-21 21:43:59 +02:00
|
|
|
.open()?;
|
|
|
|
|
2020-12-31 21:07:05 +01:00
|
|
|
info!("Opened sled database at {}", config.database_path);
|
2020-03-30 13:46:18 +02:00
|
|
|
|
2020-11-09 12:21:04 +01:00
|
|
|
let (admin_sender, admin_receiver) = mpsc::unbounded();
|
|
|
|
|
|
|
|
let db = Self {
|
2021-01-15 03:32:22 +01:00
|
|
|
globals: globals::Globals::load(
|
|
|
|
db.open_tree("global")?,
|
|
|
|
db.open_tree("servertimeout_signingkey")?,
|
|
|
|
config,
|
2021-01-29 17:20:33 +01:00
|
|
|
)?,
|
2020-05-03 17:25:31 +02:00
|
|
|
users: users::Users {
|
2020-06-09 15:13:17 +02:00
|
|
|
userid_password: db.open_tree("userid_password")?,
|
|
|
|
userid_displayname: db.open_tree("userid_displayname")?,
|
|
|
|
userid_avatarurl: db.open_tree("userid_avatarurl")?,
|
|
|
|
userdeviceid_token: db.open_tree("userdeviceid_token")?,
|
|
|
|
userdeviceid_metadata: db.open_tree("userdeviceid_metadata")?,
|
|
|
|
token_userdeviceid: db.open_tree("token_userdeviceid")?,
|
|
|
|
onetimekeyid_onetimekeys: db.open_tree("onetimekeyid_onetimekeys")?,
|
2020-07-29 20:44:06 +02:00
|
|
|
userid_lastonetimekeyupdate: db.open_tree("userid_lastonetimekeyupdate")?,
|
2020-06-16 12:11:38 +02:00
|
|
|
keychangeid_userid: db.open_tree("devicekeychangeid_userid")?,
|
|
|
|
keyid_key: db.open_tree("keyid_key")?,
|
|
|
|
userid_masterkeyid: db.open_tree("userid_masterkeyid")?,
|
|
|
|
userid_selfsigningkeyid: db.open_tree("userid_selfsigningkeyid")?,
|
|
|
|
userid_usersigningkeyid: db.open_tree("userid_usersigningkeyid")?,
|
2020-06-09 15:13:17 +02:00
|
|
|
todeviceid_events: db.open_tree("todeviceid_events")?,
|
2020-05-03 17:25:31 +02:00
|
|
|
},
|
2020-06-06 18:44:50 +02:00
|
|
|
uiaa: uiaa::Uiaa {
|
2020-06-09 15:13:17 +02:00
|
|
|
userdeviceid_uiaainfo: db.open_tree("userdeviceid_uiaainfo")?,
|
2020-06-06 18:44:50 +02:00
|
|
|
},
|
2020-05-03 17:25:31 +02:00
|
|
|
rooms: rooms::Rooms {
|
|
|
|
edus: rooms::RoomEdus {
|
2020-08-23 17:29:39 +02:00
|
|
|
readreceiptid_readreceipt: db.open_tree("readreceiptid_readreceipt")?,
|
|
|
|
roomuserid_privateread: db.open_tree("roomuserid_privateread")?, // "Private" read receipt
|
2020-08-25 13:24:38 +02:00
|
|
|
roomuserid_lastprivatereadupdate: db
|
|
|
|
.open_tree("roomid_lastprivatereadupdate")?,
|
2020-08-23 17:29:39 +02:00
|
|
|
typingid_userid: db.open_tree("typingid_userid")?,
|
|
|
|
roomid_lasttypingupdate: db.open_tree("roomid_lasttypingupdate")?,
|
2020-07-28 15:58:50 +02:00
|
|
|
presenceid_presence: db.open_tree("presenceid_presence")?,
|
|
|
|
userid_lastpresenceupdate: db.open_tree("userid_lastpresenceupdate")?,
|
2020-05-03 17:25:31 +02:00
|
|
|
},
|
2020-06-09 15:13:17 +02:00
|
|
|
pduid_pdu: db.open_tree("pduid_pdu")?,
|
|
|
|
eventid_pduid: db.open_tree("eventid_pduid")?,
|
|
|
|
roomid_pduleaves: db.open_tree("roomid_pduleaves")?,
|
2020-05-03 17:25:31 +02:00
|
|
|
|
2020-06-09 15:13:17 +02:00
|
|
|
alias_roomid: db.open_tree("alias_roomid")?,
|
|
|
|
aliasid_alias: db.open_tree("alias_roomid")?,
|
|
|
|
publicroomids: db.open_tree("publicroomids")?,
|
2020-05-25 23:24:13 +02:00
|
|
|
|
2020-08-18 12:15:27 +02:00
|
|
|
tokenids: db.open_tree("tokenids")?,
|
|
|
|
|
2020-09-14 20:23:19 +02:00
|
|
|
roomserverids: db.open_tree("roomserverids")?,
|
2020-06-09 15:13:17 +02:00
|
|
|
userroomid_joined: db.open_tree("userroomid_joined")?,
|
|
|
|
roomuserid_joined: db.open_tree("roomuserid_joined")?,
|
2020-08-06 13:21:53 +02:00
|
|
|
roomuseroncejoinedids: db.open_tree("roomuseroncejoinedids")?,
|
2020-06-09 15:13:17 +02:00
|
|
|
userroomid_invited: db.open_tree("userroomid_invited")?,
|
|
|
|
roomuserid_invited: db.open_tree("roomuserid_invited")?,
|
|
|
|
userroomid_left: db.open_tree("userroomid_left")?,
|
2020-08-06 14:29:59 +02:00
|
|
|
|
2021-03-17 22:30:25 +01:00
|
|
|
statekey_shortstatekey: db.open_tree("statekey_shortstatekey")?,
|
|
|
|
stateid_shorteventid: db.open_tree("stateid_shorteventid")?,
|
|
|
|
eventid_shorteventid: db.open_tree("eventid_shorteventid")?,
|
|
|
|
shorteventid_eventid: db.open_tree("shorteventid_eventid")?,
|
|
|
|
shorteventid_shortstatehash: db.open_tree("eventid_shortstatehash")?,
|
|
|
|
roomid_shortstatehash: db.open_tree("roomid_shortstatehash")?,
|
|
|
|
statehash_shortstatehash: db.open_tree("statehash_shortstatehash")?,
|
|
|
|
|
2021-03-13 16:30:12 +01:00
|
|
|
eventid_outlierpdu: db.open_tree("roomeventid_outlierpdu")?,
|
2021-02-04 02:00:01 +01:00
|
|
|
prevevent_parent: db.open_tree("prevevent_parent")?,
|
2020-05-03 17:25:31 +02:00
|
|
|
},
|
|
|
|
account_data: account_data::AccountData {
|
2020-06-09 15:13:17 +02:00
|
|
|
roomuserdataid_accountdata: db.open_tree("roomuserdataid_accountdata")?,
|
2020-05-03 17:25:31 +02:00
|
|
|
},
|
2020-05-18 17:53:34 +02:00
|
|
|
media: media::Media {
|
2020-06-09 15:13:17 +02:00
|
|
|
mediaid_file: db.open_tree("mediaid_file")?,
|
2020-05-18 17:53:34 +02:00
|
|
|
},
|
2020-06-16 12:11:38 +02:00
|
|
|
key_backups: key_backups::KeyBackups {
|
|
|
|
backupid_algorithm: db.open_tree("backupid_algorithm")?,
|
|
|
|
backupid_etag: db.open_tree("backupid_etag")?,
|
|
|
|
backupkeyid_backup: db.open_tree("backupkeyid_backupmetadata")?,
|
|
|
|
},
|
2020-08-25 13:24:38 +02:00
|
|
|
transaction_ids: transaction_ids::TransactionIds {
|
|
|
|
userdevicetxnid_response: db.open_tree("userdevicetxnid_response")?,
|
|
|
|
},
|
2020-09-15 16:13:54 +02:00
|
|
|
sending: sending::Sending {
|
2020-10-21 16:08:54 +02:00
|
|
|
servernamepduids: db.open_tree("servernamepduids")?,
|
|
|
|
servercurrentpdus: db.open_tree("servercurrentpdus")?,
|
2020-12-19 16:00:11 +01:00
|
|
|
maximum_requests: Arc::new(Semaphore::new(10)),
|
2020-09-15 16:13:54 +02:00
|
|
|
},
|
2020-11-09 12:21:04 +01:00
|
|
|
admin: admin::Admin {
|
|
|
|
sender: admin_sender,
|
|
|
|
},
|
2020-12-08 10:33:44 +01:00
|
|
|
appservice: appservice::Appservice {
|
|
|
|
cached_registrations: Arc::new(RwLock::new(HashMap::new())),
|
|
|
|
id_appserviceregistrations: db.open_tree("id_appserviceregistrations")?,
|
|
|
|
},
|
2021-01-27 03:54:35 +01:00
|
|
|
pusher: pusher::PushData::new(&db)?,
|
2020-05-03 17:25:31 +02:00
|
|
|
_db: db,
|
2020-11-09 12:21:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
db.admin.start_handler(db.clone(), admin_receiver);
|
|
|
|
|
|
|
|
Ok(db)
|
2020-03-30 13:46:18 +02:00
|
|
|
}
|
2020-07-27 17:36:54 +02:00
|
|
|
|
2020-07-28 15:00:23 +02:00
|
|
|
pub async fn watch(&self, user_id: &UserId, device_id: &DeviceId) {
|
2020-07-30 14:05:08 +02:00
|
|
|
let userid_bytes = user_id.to_string().as_bytes().to_vec();
|
|
|
|
let mut userid_prefix = userid_bytes.clone();
|
2020-07-27 17:36:54 +02:00
|
|
|
userid_prefix.push(0xff);
|
2020-07-30 14:05:08 +02:00
|
|
|
|
2020-07-27 17:36:54 +02:00
|
|
|
let mut userdeviceid_prefix = userid_prefix.clone();
|
|
|
|
userdeviceid_prefix.extend_from_slice(device_id.as_bytes());
|
|
|
|
userdeviceid_prefix.push(0xff);
|
|
|
|
|
|
|
|
let mut futures = futures::stream::FuturesUnordered::new();
|
|
|
|
|
|
|
|
// Return when *any* user changed his key
|
|
|
|
// TODO: only send for user they share a room with
|
|
|
|
futures.push(
|
|
|
|
self.users
|
|
|
|
.todeviceid_events
|
|
|
|
.watch_prefix(&userdeviceid_prefix),
|
|
|
|
);
|
|
|
|
|
|
|
|
futures.push(self.rooms.userroomid_joined.watch_prefix(&userid_prefix));
|
|
|
|
futures.push(self.rooms.userroomid_invited.watch_prefix(&userid_prefix));
|
|
|
|
futures.push(self.rooms.userroomid_left.watch_prefix(&userid_prefix));
|
|
|
|
|
|
|
|
// Events for rooms we are in
|
|
|
|
for room_id in self.rooms.rooms_joined(user_id).filter_map(|r| r.ok()) {
|
2020-08-22 22:02:32 +02:00
|
|
|
let roomid_bytes = room_id.to_string().as_bytes().to_vec();
|
|
|
|
let mut roomid_prefix = roomid_bytes.clone();
|
2020-07-27 17:36:54 +02:00
|
|
|
roomid_prefix.push(0xff);
|
|
|
|
|
|
|
|
// PDUs
|
|
|
|
futures.push(self.rooms.pduid_pdu.watch_prefix(&roomid_prefix));
|
|
|
|
|
|
|
|
// EDUs
|
|
|
|
futures.push(
|
|
|
|
self.rooms
|
|
|
|
.edus
|
2020-08-23 17:29:39 +02:00
|
|
|
.roomid_lasttypingupdate
|
2020-08-22 22:02:32 +02:00
|
|
|
.watch_prefix(&roomid_bytes),
|
2020-07-27 17:36:54 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
futures.push(
|
|
|
|
self.rooms
|
|
|
|
.edus
|
2020-08-23 17:29:39 +02:00
|
|
|
.readreceiptid_readreceipt
|
2020-07-27 17:36:54 +02:00
|
|
|
.watch_prefix(&roomid_prefix),
|
|
|
|
);
|
|
|
|
|
2020-07-30 14:05:08 +02:00
|
|
|
// Key changes
|
|
|
|
futures.push(self.users.keychangeid_userid.watch_prefix(&roomid_prefix));
|
|
|
|
|
2020-07-27 17:36:54 +02:00
|
|
|
// Room account data
|
|
|
|
let mut roomuser_prefix = roomid_prefix.clone();
|
|
|
|
roomuser_prefix.extend_from_slice(&userid_prefix);
|
|
|
|
|
|
|
|
futures.push(
|
|
|
|
self.account_data
|
|
|
|
.roomuserdataid_accountdata
|
|
|
|
.watch_prefix(&roomuser_prefix),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut globaluserdata_prefix = vec![0xff];
|
|
|
|
globaluserdata_prefix.extend_from_slice(&userid_prefix);
|
|
|
|
|
|
|
|
futures.push(
|
|
|
|
self.account_data
|
|
|
|
.roomuserdataid_accountdata
|
|
|
|
.watch_prefix(&globaluserdata_prefix),
|
|
|
|
);
|
|
|
|
|
2020-07-30 14:05:08 +02:00
|
|
|
// More key changes (used when user is not joined to any rooms)
|
|
|
|
futures.push(self.users.keychangeid_userid.watch_prefix(&userid_prefix));
|
|
|
|
|
|
|
|
// One time keys
|
|
|
|
futures.push(
|
|
|
|
self.users
|
|
|
|
.userid_lastonetimekeyupdate
|
|
|
|
.watch_prefix(&userid_bytes),
|
|
|
|
);
|
|
|
|
|
2020-07-27 17:36:54 +02:00
|
|
|
// Wait until one of them finds something
|
|
|
|
futures.next().await;
|
|
|
|
}
|
2020-10-21 21:28:02 +02:00
|
|
|
|
|
|
|
pub async fn flush(&self) -> Result<()> {
|
|
|
|
self._db.flush_async().await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-03-30 13:46:18 +02:00
|
|
|
}
|