2024-03-05 19:19:06 -05:00
use std ::{
collections ::{ BTreeMap , HashMap } ,
fs ,
2024-03-24 19:21:05 -07:00
future ::Future ,
2024-03-05 19:19:06 -05:00
path ::PathBuf ,
sync ::{
atomic ::{ self , AtomicBool } ,
2024-03-24 19:21:05 -07:00
Arc ,
2024-03-05 19:19:06 -05:00
} ,
2024-04-26 18:55:45 -07:00
time ::{ Instant , SystemTime } ,
2022-10-09 17:25:06 +02:00
} ;
2022-08-07 19:42:22 +02:00
2024-03-05 19:19:06 -05:00
use argon2 ::Argon2 ;
use base64 ::{ engine ::general_purpose , Engine as _ } ;
2024-04-22 23:48:57 -04:00
pub ( crate ) use data ::Data ;
2024-03-19 19:12:49 -04:00
use hickory_resolver ::TokioAsyncResolver ;
2024-04-21 22:32:45 -07:00
use ipaddress ::IPAddress ;
2024-03-05 19:19:06 -05:00
use regex ::RegexSet ;
2021-01-14 21:32:22 -05:00
use ruma ::{
2021-06-30 09:52:01 +02:00
api ::{
2024-05-16 21:02:05 -07:00
client ::discovery ::discover_support ::ContactRole ,
2021-06-30 09:52:01 +02:00
federation ::discovery ::{ ServerSigningKeys , VerifyKey } ,
} ,
2024-03-05 19:19:06 -05:00
serde ::Base64 ,
2024-05-16 21:02:05 -07:00
DeviceId , OwnedEventId , OwnedRoomId , OwnedServerName , OwnedServerSigningKeyId , OwnedUserId , RoomVersionId ,
ServerName , UserId ,
2021-06-06 16:58:32 +04:30
} ;
2024-05-16 21:02:05 -07:00
use tokio ::sync ::{ broadcast , Mutex , RwLock } ;
2024-04-21 22:32:45 -07:00
use tracing ::{ error , info , trace } ;
2024-04-11 20:17:30 -04:00
use url ::Url ;
2020-07-23 23:03:24 -04:00
2024-04-25 09:07:59 -07:00
use crate ::{ services , Config , LogLevelReloadHandles , Result } ;
2024-03-05 19:19:06 -05:00
2024-04-22 23:48:57 -04:00
mod client ;
2024-03-05 19:19:06 -05:00
mod data ;
2024-04-22 23:48:57 -04:00
mod resolver ;
2023-08-01 14:48:50 -10:00
2021-05-20 23:46:52 +02:00
type RateLimitState = ( Instant , u32 ) ; // Time if last failed try, number of failed tries
2021-07-14 12:31:38 +02:00
2024-04-22 23:48:57 -04:00
pub ( crate ) struct Service < ' a > {
pub ( crate ) db : & 'static dyn Data ,
2024-03-05 19:48:54 -05:00
2024-04-25 09:07:59 -07:00
pub ( crate ) tracing_reload_handle : LogLevelReloadHandles ,
2024-04-22 23:48:57 -04:00
pub ( crate ) config : Config ,
pub ( crate ) cidr_range_denylist : Vec < IPAddress > ,
2020-09-15 16:13:54 +02:00
keypair : Arc < ruma ::signatures ::Ed25519KeyPair > ,
2022-10-09 15:34:36 +02:00
jwt_decoding_key : Option < jsonwebtoken ::DecodingKey > ,
2024-04-22 23:48:57 -04:00
pub ( crate ) resolver : Arc < resolver ::Resolver > ,
pub ( crate ) client : client ::Client ,
pub ( crate ) stable_room_versions : Vec < RoomVersionId > ,
pub ( crate ) unstable_room_versions : Vec < RoomVersionId > ,
pub ( crate ) bad_event_ratelimiter : Arc < RwLock < HashMap < OwnedEventId , RateLimitState > > > ,
pub ( crate ) bad_signature_ratelimiter : Arc < RwLock < HashMap < Vec < String > , RateLimitState > > > ,
pub ( crate ) bad_query_ratelimiter : Arc < RwLock < HashMap < OwnedServerName , RateLimitState > > > ,
pub ( crate ) roomid_mutex_insert : RwLock < HashMap < OwnedRoomId , Arc < Mutex < ( ) > > > > ,
pub ( crate ) roomid_mutex_state : RwLock < HashMap < OwnedRoomId , Arc < Mutex < ( ) > > > > ,
pub ( crate ) roomid_mutex_federation : RwLock < HashMap < OwnedRoomId , Arc < Mutex < ( ) > > > > , // this lock will be held longer
pub ( crate ) roomid_federationhandletime : RwLock < HashMap < OwnedRoomId , ( OwnedEventId , Instant ) > > ,
pub ( crate ) stateres_mutex : Arc < Mutex < ( ) > > ,
2024-01-14 22:39:08 -05:00
pub ( crate ) rotate : RotationHandler ,
2024-04-26 18:55:45 -07:00
pub ( crate ) started : SystemTime ,
2024-04-22 23:48:57 -04:00
pub ( crate ) shutdown : AtomicBool ,
pub ( crate ) argon : Argon2 < ' a > ,
2020-05-03 17:25:31 +02:00
}
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.
2024-01-14 22:39:08 -05:00
pub ( crate ) struct RotationHandler ( broadcast ::Sender < ( ) > , ( ) ) ;
2021-07-14 07:07:08 +00:00
impl RotationHandler {
2024-04-22 23:48:57 -04:00
fn new ( ) -> Self {
2024-01-14 22:39:08 -05:00
let ( s , _r ) = broadcast ::channel ( 1 ) ;
Self ( s , ( ) )
2021-07-14 07:07:08 +00:00
}
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn watch ( & self ) -> impl Future < Output = ( ) > {
2021-07-14 07:07:08 +00:00
let mut r = self . 0. subscribe ( ) ;
2024-03-05 19:48:54 -05:00
2021-07-14 07:07:08 +00:00
async move {
2024-03-23 14:38:15 -04:00
_ = r . recv ( ) . await ;
2021-07-14 07:07:08 +00:00
}
2024-03-05 19:48:54 -05:00
}
2024-04-22 23:48:57 -04:00
fn fire ( & self ) { _ = self . 0. send ( ( ) ) ; }
2021-07-14 07:07:08 +00:00
}
2021-07-14 12:31:38 +02:00
impl Default for RotationHandler {
fn default ( ) -> Self { Self ::new ( ) }
}
2023-12-25 16:28:56 +01:00
impl Service < '_ > {
2024-04-22 23:48:57 -04:00
pub ( crate ) fn load (
2024-04-25 09:07:59 -07:00
db : & 'static dyn Data , config : & Config , tracing_reload_handle : LogLevelReloadHandles ,
2024-04-07 00:13:47 -04:00
) -> Result < Self > {
2022-09-07 13:25:51 +02:00
let keypair = db . load_keypair ( ) ;
2024-03-05 19:48:54 -05:00
2020-09-15 21:46:10 +02:00
let keypair = match keypair {
Ok ( k ) = > k ,
Err ( e ) = > {
error! ( " Keypair invalid. Deleting... " ) ;
2022-10-08 13:02:52 +02:00
db . remove_keypair ( ) ? ;
2020-09-15 21:46:10 +02:00
return Err ( e ) ;
} ,
} ;
2024-03-05 19:48:54 -05:00
2024-03-25 17:05:11 -04:00
let jwt_decoding_key = config
. jwt_secret
. as_ref ( )
. map ( | secret | jsonwebtoken ::DecodingKey ::from_secret ( secret . as_bytes ( ) ) ) ;
2024-03-05 19:48:54 -05:00
2024-03-24 19:21:05 -07:00
let resolver = Arc ::new ( resolver ::Resolver ::new ( config ) ) ;
2021-11-01 01:58:26 +00:00
// Supported and stable room versions
2022-02-18 13:41:37 +01:00
let stable_room_versions = vec! [
RoomVersionId ::V6 ,
RoomVersionId ::V7 ,
RoomVersionId ::V8 ,
RoomVersionId ::V9 ,
2022-10-10 15:00:44 +02:00
RoomVersionId ::V10 ,
2023-09-02 12:18:46 -04:00
RoomVersionId ::V11 ,
] ;
2024-04-11 17:44:00 -04:00
// Experimental, partially supported room versions
let unstable_room_versions = vec! [ RoomVersionId ::V2 , RoomVersionId ::V3 , RoomVersionId ::V4 , RoomVersionId ::V5 ] ;
2023-12-25 16:28:56 +01:00
// 19456 Kib blocks, iterations = 2, parallelism = 1 for more info https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#argon2id
let argon = Argon2 ::new (
argon2 ::Algorithm ::Argon2id ,
argon2 ::Version ::default ( ) ,
argon2 ::Params ::new ( 19456 , 2 , 1 , None ) . expect ( " valid parameters " ) ,
) ;
2024-04-07 00:13:47 -04:00
2024-04-21 22:32:45 -07:00
let mut cidr_range_denylist = Vec ::new ( ) ;
for cidr in config . ip_range_denylist . clone ( ) {
let cidr = IPAddress ::parse ( cidr ) . expect ( " valid cidr range " ) ;
trace! ( " Denied CIDR range: {:?} " , cidr ) ;
cidr_range_denylist . push ( cidr ) ;
}
2021-11-05 20:47:11 +00:00
let mut s = Self {
2024-04-07 00:13:47 -04:00
tracing_reload_handle ,
2022-09-07 13:25:51 +02:00
db ,
2024-03-16 15:54:58 -07:00
config : config . clone ( ) ,
2024-04-21 22:32:45 -07:00
cidr_range_denylist ,
2020-09-15 21:46:10 +02:00
keypair : Arc ::new ( keypair ) ,
2024-03-24 19:21:05 -07:00
resolver : resolver . clone ( ) ,
client : client ::Client ::new ( config , & resolver ) ,
2022-02-07 12:55:21 +01:00
jwt_decoding_key ,
stable_room_versions ,
2021-01-29 11:20:33 -05:00
unstable_room_versions ,
bad_event_ratelimiter : Arc ::new ( RwLock ::new ( HashMap ::new ( ) ) ) ,
2021-04-15 22:07:27 -03:00
bad_signature_ratelimiter : Arc ::new ( RwLock ::new ( HashMap ::new ( ) ) ) ,
bad_query_ratelimiter : Arc ::new ( RwLock ::new ( HashMap ::new ( ) ) ) ,
2024-02-10 11:22:25 -05:00
roomid_mutex_state : RwLock ::new ( HashMap ::new ( ) ) ,
2021-08-03 11:10:58 +02:00
roomid_mutex_insert : RwLock ::new ( HashMap ::new ( ) ) ,
2022-01-28 12:42:47 -06:00
roomid_mutex_federation : RwLock ::new ( HashMap ::new ( ) ) ,
2021-07-18 20:43:39 +02:00
roomid_federationhandletime : RwLock ::new ( HashMap ::new ( ) ) ,
stateres_mutex : Arc ::new ( Mutex ::new ( ( ) ) ) ,
2023-09-13 20:54:53 +02:00
rotate : RotationHandler ::new ( ) ,
2024-04-26 18:55:45 -07:00
started : SystemTime ::now ( ) ,
2021-07-18 20:43:39 +02:00
shutdown : AtomicBool ::new ( false ) ,
argon ,
2024-03-05 19:48:54 -05:00
} ;
2021-07-18 20:43:39 +02:00
fs ::create_dir_all ( s . get_media_folder ( ) ) ? ;
2024-03-05 19:48:54 -05:00
2024-03-25 17:05:11 -04:00
if ! s
. supported_room_versions ( )
. contains ( & s . config . default_room_version )
{
2021-07-18 20:43:39 +02:00
error! ( config = ? s . config . default_room_version , fallback = ? crate ::config ::default_default_room_version ( ) , " Room version in config isn't supported, falling back to default version " ) ;
2022-05-30 12:58:43 +02:00
s . config . default_room_version = crate ::config ::default_default_room_version ( ) ;
2024-03-05 19:48:54 -05:00
} ;
Ok ( s )
}
2021-07-18 20:43:39 +02:00
/// Returns this server's keypair.
2024-04-22 23:48:57 -04:00
pub ( crate ) fn keypair ( & self ) -> & ruma ::signatures ::Ed25519KeyPair { & self . keypair }
2024-03-05 19:48:54 -05:00
2024-02-10 11:22:25 -05:00
#[ tracing::instrument(skip(self)) ]
2024-04-22 23:48:57 -04:00
pub ( crate ) fn next_count ( & self ) -> Result < u64 > { self . db . next_count ( ) }
2024-03-05 19:48:54 -05:00
2024-02-10 11:22:25 -05:00
#[ tracing::instrument(skip(self)) ]
2024-04-22 23:48:57 -04:00
pub ( crate ) fn current_count ( & self ) -> Result < u64 > { self . db . current_count ( ) }
2024-03-05 19:48:54 -05:00
2024-02-10 11:22:25 -05:00
#[ tracing::instrument(skip(self)) ]
2024-04-22 23:48:57 -04:00
pub ( crate ) fn last_check_for_updates_id ( & self ) -> Result < u64 > { self . db . last_check_for_updates_id ( ) }
2024-03-05 19:48:54 -05:00
2024-02-10 11:22:25 -05:00
#[ tracing::instrument(skip(self)) ]
2024-04-22 23:48:57 -04:00
pub ( crate ) fn update_check_for_updates_id ( & self , id : u64 ) -> Result < ( ) > { self . db . update_check_for_updates_id ( id ) }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) async fn watch ( & self , user_id : & UserId , device_id : & DeviceId ) -> Result < ( ) > {
2022-01-28 12:42:47 -06:00
self . db . watch ( user_id , device_id ) . await
2024-03-05 19:48:54 -05:00
}
2024-04-22 23:48:57 -04:00
pub ( crate ) fn cleanup ( & self ) -> Result < ( ) > { self . db . cleanup ( ) }
2024-03-05 19:48:54 -05:00
2024-04-22 23:54:56 -04:00
/// TODO: use this?
#[ allow(dead_code) ]
2024-04-22 23:48:57 -04:00
pub ( crate ) fn flush ( & self ) -> Result < ( ) > { self . db . flush ( ) }
2024-03-06 18:14:30 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn server_name ( & self ) -> & ServerName { self . config . server_name . as_ref ( ) }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn max_request_size ( & self ) -> u32 { self . config . max_request_size }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn max_fetch_prev_events ( & self ) -> u16 { self . config . max_fetch_prev_events }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn allow_registration ( & self ) -> bool { self . config . allow_registration }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn allow_guest_registration ( & self ) -> bool { self . config . allow_guest_registration }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn allow_guests_auto_join_rooms ( & self ) -> bool { self . config . allow_guests_auto_join_rooms }
2024-04-13 20:33:24 -04:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn log_guest_registrations ( & self ) -> bool { self . config . log_guest_registrations }
2024-04-13 20:19:10 -04:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn allow_encryption ( & self ) -> bool { self . config . allow_encryption }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn allow_federation ( & self ) -> bool { self . config . allow_federation }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn allow_public_room_directory_over_federation ( & self ) -> bool {
2022-01-28 12:42:47 -06:00
self . config . allow_public_room_directory_over_federation
2020-05-03 17:25:31 +02:00
}
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn allow_device_name_federation ( & self ) -> bool { self . config . allow_device_name_federation }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn allow_room_creation ( & self ) -> bool { self . config . allow_room_creation }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn allow_unstable_room_versions ( & self ) -> bool { self . config . allow_unstable_room_versions }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn default_room_version ( & self ) -> RoomVersionId { self . config . default_room_version . clone ( ) }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn new_user_displayname_suffix ( & self ) -> & String { & self . config . new_user_displayname_suffix }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn allow_check_for_updates ( & self ) -> bool { self . config . allow_check_for_updates }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn trusted_servers ( & self ) -> & [ OwnedServerName ] { & self . config . trusted_servers }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn query_trusted_key_servers_first ( & self ) -> bool { self . config . query_trusted_key_servers_first }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn dns_resolver ( & self ) -> & TokioAsyncResolver { & self . resolver . resolver }
2024-03-24 19:21:05 -07:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn actual_destinations ( & self ) -> & Arc < RwLock < resolver ::WellKnownMap > > { & self . resolver . destinations }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn jwt_decoding_key ( & self ) -> Option < & jsonwebtoken ::DecodingKey > { self . jwt_decoding_key . as_ref ( ) }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn turn_password ( & self ) -> & String { & self . config . turn_password }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn turn_ttl ( & self ) -> u64 { self . config . turn_ttl }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn turn_uris ( & self ) -> & [ String ] { & self . config . turn_uris }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn turn_username ( & self ) -> & String { & self . config . turn_username }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn turn_secret ( & self ) -> & String { & self . config . turn_secret }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn allow_profile_lookup_federation_requests ( & self ) -> bool {
2024-04-07 22:38:33 -04:00
self . config . allow_profile_lookup_federation_requests
}
2024-04-22 23:48:57 -04:00
pub ( crate ) fn notification_push_path ( & self ) -> & String { & self . config . notification_push_path }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn emergency_password ( & self ) -> & Option < String > { & self . config . emergency_password }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn url_preview_domain_contains_allowlist ( & self ) -> & Vec < String > {
2021-01-01 13:47:53 +01:00
& self . config . url_preview_domain_contains_allowlist
2020-06-06 19:02:31 +02:00
}
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn url_preview_domain_explicit_allowlist ( & self ) -> & Vec < String > {
2023-12-21 20:44:58 -05:00
& self . config . url_preview_domain_explicit_allowlist
2024-03-05 19:48:54 -05:00
}
2024-04-22 23:48:57 -04:00
pub ( crate ) fn url_preview_domain_explicit_denylist ( & self ) -> & Vec < String > {
2024-04-14 21:12:48 -04:00
& self . config . url_preview_domain_explicit_denylist
}
2024-04-22 23:48:57 -04:00
pub ( crate ) fn url_preview_url_contains_allowlist ( & self ) -> & Vec < String > {
& self . config . url_preview_url_contains_allowlist
}
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn url_preview_max_spider_size ( & self ) -> usize { self . config . url_preview_max_spider_size }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn url_preview_check_root_domain ( & self ) -> bool { self . config . url_preview_check_root_domain }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn forbidden_alias_names ( & self ) -> & RegexSet { & self . config . forbidden_alias_names }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn forbidden_usernames ( & self ) -> & RegexSet { & self . config . forbidden_usernames }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn allow_local_presence ( & self ) -> bool { self . config . allow_local_presence }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn allow_incoming_presence ( & self ) -> bool { self . config . allow_incoming_presence }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn allow_outgoing_presence ( & self ) -> bool { self . config . allow_outgoing_presence }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn allow_incoming_read_receipts ( & self ) -> bool { self . config . allow_incoming_read_receipts }
2024-03-17 12:16:04 -04:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn allow_outgoing_read_receipts ( & self ) -> bool { self . config . allow_outgoing_read_receipts }
2024-04-10 15:22:50 -07:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn prevent_media_downloads_from ( & self ) -> & [ OwnedServerName ] {
& self . config . prevent_media_downloads_from
}
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn forbidden_remote_room_directory_server_names ( & self ) -> & [ OwnedServerName ] {
2024-04-15 22:02:08 -04:00
& self . config . forbidden_remote_room_directory_server_names
}
2024-04-22 23:48:57 -04:00
pub ( crate ) fn well_known_support_page ( & self ) -> & Option < Url > { & self . config . well_known . support_page }
2024-04-06 18:42:00 -04:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn well_known_support_role ( & self ) -> & Option < ContactRole > { & self . config . well_known . support_role }
2024-04-06 18:42:00 -04:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn well_known_support_email ( & self ) -> & Option < String > { & self . config . well_known . support_email }
2024-04-06 18:42:00 -04:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn well_known_support_mxid ( & self ) -> & Option < OwnedUserId > { & self . config . well_known . support_mxid }
2024-04-06 18:42:00 -04:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn block_non_admin_invites ( & self ) -> bool { self . config . block_non_admin_invites }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn supported_room_versions ( & self ) -> Vec < RoomVersionId > {
2021-11-01 01:58:26 +00:00
let mut room_versions : Vec < RoomVersionId > = vec! [ ] ;
2021-11-05 20:47:11 +00:00
room_versions . extend ( self . stable_room_versions . clone ( ) ) ;
if self . allow_unstable_room_versions ( ) {
room_versions . extend ( self . unstable_room_versions . clone ( ) ) ;
2024-03-05 19:48:54 -05:00
} ;
2021-11-01 01:58:26 +00:00
room_versions
}
2024-03-05 19:48:54 -05:00
2024-02-20 23:08:53 -05:00
/// TODO: the key valid until timestamp (`valid_until_ts`) is only honored
/// in room version > 4
2024-03-05 19:48:54 -05:00
///
2021-01-14 21:32:22 -05:00
/// Remove the outdated keys and insert the new ones.
2024-03-05 19:48:54 -05:00
///
2021-01-14 21:32:22 -05:00
/// This doesn't actually check that the keys provided are newer than the
/// old set.
2024-04-22 23:48:57 -04:00
pub ( crate ) fn add_signing_key (
2024-02-20 23:08:53 -05:00
& self , origin : & ServerName , new_keys : ServerSigningKeys ,
2023-07-29 20:01:38 +02:00
) -> Result < BTreeMap < OwnedServerSigningKeyId , VerifyKey > > {
self . db . add_signing_key ( origin , new_keys )
2024-03-05 19:48:54 -05:00
}
2023-07-29 20:01:38 +02:00
/// This returns an empty `Ok(BTreeMap<..>)` when there are no keys found
2022-10-09 17:25:06 +02:00
/// for the server.
2024-04-22 23:48:57 -04:00
pub ( crate ) fn signing_keys_for ( & self , origin : & ServerName ) -> Result < BTreeMap < OwnedServerSigningKeyId , VerifyKey > > {
2024-03-05 19:19:06 -05:00
let mut keys = self . db . signing_keys_for ( origin ) ? ;
2020-12-06 11:05:51 +01:00
if origin = = self . server_name ( ) {
keys . insert (
format! ( " ed25519: {} " , services ( ) . globals . keypair ( ) . version ( ) )
. try_into ( )
. expect ( " found invalid server signing keys in DB " ) ,
VerifyKey {
key : Base64 ::new ( self . keypair . public_key ( ) . to_vec ( ) ) ,
2024-03-05 19:48:54 -05:00
} ,
) ;
2020-12-06 11:05:51 +01:00
}
2021-01-14 21:32:22 -05:00
2022-10-09 15:34:36 +02:00
Ok ( keys )
2021-02-07 17:38:45 +01:00
}
2021-01-14 21:32:22 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn database_version ( & self ) -> Result < u64 > { self . db . database_version ( ) }
2021-10-01 15:53:16 +02:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn bump_database_version ( & self , new_version : u64 ) -> Result < ( ) > {
self . db . bump_database_version ( new_version )
}
2021-10-01 15:53:16 +02:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn get_media_folder ( & self ) -> PathBuf {
2023-10-28 23:43:41 -04:00
let mut r = PathBuf ::new ( ) ;
r . push ( self . config . database_path . clone ( ) ) ;
2022-04-07 12:11:55 +00:00
r . push ( " media " ) ;
2024-03-05 19:48:54 -05:00
r
2022-04-07 12:11:55 +00:00
}
2024-03-05 19:48:54 -05:00
2024-02-10 13:29:12 -05:00
/// new SHA256 file name media function, requires "sha256_media" feature
2024-02-11 11:42:55 -05:00
/// flag enabled and database migrated uses SHA256 hash of the base64 key as
2024-02-08 19:11:48 -05:00
/// the file name
2024-03-09 15:53:22 -05:00
#[ cfg(feature = " sha256_media " ) ]
2024-04-22 23:48:57 -04:00
pub ( crate ) fn get_media_file_new ( & self , key : & [ u8 ] ) -> PathBuf {
2024-02-08 19:11:48 -05:00
let mut r = PathBuf ::new ( ) ;
2023-09-08 14:36:39 +02:00
r . push ( self . config . database_path . clone ( ) ) ;
r . push ( " media " ) ;
// Using the hash of the base64 key as the filename
// This is to prevent the total length of the path from exceeding the maximum
2024-01-21 13:28:51 -05:00
// length in most filesystems
2024-03-09 15:53:22 -05:00
r . push ( general_purpose ::URL_SAFE_NO_PAD . encode ( < sha2 ::Sha256 as sha2 ::Digest > ::digest ( key ) ) ) ;
2024-03-05 19:48:54 -05:00
r
}
2023-07-10 16:41:00 +02:00
/// old base64 file name media function
2023-12-02 21:30:06 -05:00
/// This is the old version of `get_media_file` that uses the full base64
/// key as the filename.
2024-04-22 23:48:57 -04:00
pub ( crate ) fn get_media_file ( & self , key : & [ u8 ] ) -> PathBuf {
2021-11-01 01:58:26 +00:00
let mut r = PathBuf ::new ( ) ;
2021-11-05 20:47:11 +00:00
r . push ( self . config . database_path . clone ( ) ) ;
2024-02-07 11:25:26 -05:00
r . push ( " media " ) ;
2021-01-14 21:32:22 -05:00
r . push ( general_purpose ::URL_SAFE_NO_PAD . encode ( key ) ) ;
r
2024-03-05 19:48:54 -05:00
}
2024-04-22 23:48:57 -04:00
pub ( crate ) fn well_known_client ( & self ) -> & Option < Url > { & self . config . well_known . client }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn well_known_server ( & self ) -> & Option < OwnedServerName > { & self . config . well_known . server }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn unix_socket_path ( & self ) -> & Option < PathBuf > { & self . config . unix_socket_path }
2024-03-05 19:48:54 -05:00
2024-04-22 23:48:57 -04:00
pub ( crate ) fn valid_cidr_range ( & self , ip : & IPAddress ) -> bool {
2024-04-21 22:32:45 -07:00
for cidr in & self . cidr_range_denylist {
if cidr . includes ( ip ) {
return false ;
}
}
true
}
2024-04-22 23:48:57 -04:00
pub ( crate ) fn shutdown ( & self ) {
2023-11-25 02:11:41 -05:00
self . shutdown . store ( true , atomic ::Ordering ::Relaxed ) ;
// On shutdown
2024-03-05 19:48:54 -05:00
2023-11-25 02:11:41 -05:00
if self . unix_socket_path ( ) . is_some ( ) {
2021-06-06 16:58:32 +04:30
match & self . unix_socket_path ( ) {
2021-06-04 08:06:12 +04:30
Some ( path ) = > {
2024-03-08 09:25:47 -05:00
fs ::remove_file ( path ) . unwrap ( ) ;
2021-06-04 08:06:12 +04:30
} ,
2024-01-07 21:24:55 -05:00
None = > error! (
" Unable to remove socket file at {:?} during shutdown. " ,
2023-07-29 21:57:41 +00:00
& self . unix_socket_path ( )
2023-03-18 08:58:20 +01:00
) ,
} ;
2023-07-29 21:57:41 +00:00
} ;
2024-03-05 19:48:54 -05:00
2023-03-18 08:58:20 +01:00
info! ( target : " shutdown-sync " , " Received shutdown notification, notifying sync helpers... " ) ;
services ( ) . globals . rotate . fire ( ) ;
}
2020-05-03 17:25:31 +02:00
}