Files
continuwuity/src/service/globals/mod.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

214 lines
6.2 KiB
Rust
Raw Normal View History

mod data;
use std::{
collections::HashMap,
2024-07-04 03:26:19 +00:00
fmt::Write,
sync::{Arc, RwLock},
2024-05-09 15:59:08 -07:00
time::Instant,
2022-10-09 17:25:06 +02:00
};
use conduwuit::{Result, Server, error, utils::bytes::pretty};
2024-05-26 21:29:19 +00:00
use data::Data;
use regex::RegexSet;
2024-12-28 23:31:24 +00:00
use ruma::{OwnedEventId, OwnedRoomAliasId, OwnedServerName, OwnedUserId, ServerName, UserId};
2020-07-23 23:03:24 -04:00
2024-07-22 07:43:51 +00:00
use crate::service;
2023-08-01 14:48:50 -10:00
2024-05-09 15:59:08 -07:00
pub struct Service {
2024-05-27 03:17:20 +00:00
pub db: Data,
server: Arc<Server>,
2024-03-05 19:48:54 -05:00
2024-05-09 15:59:08 -07:00
pub bad_event_ratelimiter: Arc<RwLock<HashMap<OwnedEventId, RateLimitState>>>,
pub server_user: OwnedUserId,
pub admin_alias: OwnedRoomAliasId,
pub turn_secret: String,
pub registration_token: Option<String>,
2021-07-14 12:31:38 +02:00
}
2024-07-09 20:04:43 +00:00
type RateLimitState = (Instant, u32); // Time if last failed try, number of failed tries
2024-07-04 03:26:19 +00:00
impl crate::Service for Service {
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
2024-07-18 06:37:47 +00:00
let db = Data::new(&args);
2024-07-04 03:26:19 +00:00
let config = &args.server.config;
2024-03-05 19:48:54 -05:00
let turn_secret =
config
.turn_secret_file
.as_ref()
.map_or(config.turn_secret.clone(), |path| {
std::fs::read_to_string(path).unwrap_or_else(|e| {
error!("Failed to read the TURN secret file: {e}");
config.turn_secret.clone()
})
});
let registration_token = config.registration_token_file.as_ref().map_or(
config.registration_token.clone(),
|path| {
let Ok(token) = std::fs::read_to_string(path).inspect_err(|e| {
error!("Failed to read the registration token file: {e}");
}) else {
return config.registration_token.clone();
};
Some(token)
},
);
Ok(Arc::new(Self {
2022-09-07 13:25:51 +02:00
db,
server: args.server.clone(),
bad_event_ratelimiter: Arc::new(RwLock::new(HashMap::new())),
2025-01-25 23:41:39 +00:00
admin_alias: OwnedRoomAliasId::try_from(format!("#admins:{}", &args.server.name))
.expect("#admins:server_name is valid alias name"),
server_user: UserId::parse_with_server_name(
String::from("conduit"),
2025-01-25 23:41:39 +00:00
&args.server.name,
)
.expect("@conduit:server_name is valid"),
turn_secret,
registration_token,
}))
2024-07-04 03:26:19 +00:00
}
fn memory_usage(&self, out: &mut dyn Write) -> Result {
let (ber_count, ber_bytes) = self.bad_event_ratelimiter.read()?.iter().fold(
(0_usize, 0_usize),
|(mut count, mut bytes), (event_id, _)| {
bytes = bytes.saturating_add(event_id.capacity());
bytes = bytes.saturating_add(size_of::<RateLimitState>());
count = count.saturating_add(1);
(count, bytes)
},
);
writeln!(out, "bad_event_ratelimiter: {ber_count} ({})", pretty(ber_bytes))?;
2024-07-04 03:26:19 +00:00
Ok(())
}
fn clear_cache(&self) {
self.bad_event_ratelimiter
.write()
.expect("locked for writing")
.clear();
2024-03-05 19:48:54 -05:00
}
2024-07-16 22:00:54 +00:00
fn name(&self) -> &str { service::make_name(std::module_path!()) }
2024-07-04 03:26:19 +00:00
}
impl Service {
2024-07-02 07:56:45 +00:00
#[inline]
2024-05-09 15:59:08 -07:00
pub fn next_count(&self) -> Result<u64> { self.db.next_count() }
2024-03-05 19:48:54 -05:00
2024-07-02 07:56:45 +00:00
#[inline]
pub fn current_count(&self) -> Result<u64> { Ok(self.db.current_count()) }
2024-03-05 19:48:54 -05:00
2024-07-16 08:05:25 +00:00
#[inline]
2025-01-25 23:41:39 +00:00
pub fn server_name(&self) -> &ServerName { self.server.name.as_ref() }
2024-03-05 19:48:54 -05:00
pub fn allow_registration(&self) -> bool { self.server.config.allow_registration }
2024-03-05 19:48:54 -05:00
pub fn allow_guest_registration(&self) -> bool { self.server.config.allow_guest_registration }
2024-03-05 19:48:54 -05:00
pub fn allow_guests_auto_join_rooms(&self) -> bool {
self.server.config.allow_guests_auto_join_rooms
}
pub fn log_guest_registrations(&self) -> bool { self.server.config.log_guest_registrations }
pub fn allow_encryption(&self) -> bool { self.server.config.allow_encryption }
2024-03-05 19:48:54 -05:00
pub fn allow_federation(&self) -> bool { self.server.config.allow_federation }
2024-03-05 19:48:54 -05:00
2024-05-09 15:59:08 -07:00
pub fn allow_public_room_directory_over_federation(&self) -> bool {
self.server
.config
.allow_public_room_directory_over_federation
}
2024-03-05 19:48:54 -05:00
pub fn allow_device_name_federation(&self) -> bool {
self.server.config.allow_device_name_federation
}
2024-03-05 19:48:54 -05:00
pub fn allow_room_creation(&self) -> bool { self.server.config.allow_room_creation }
2024-03-05 19:48:54 -05:00
pub fn new_user_displayname_suffix(&self) -> &String {
&self.server.config.new_user_displayname_suffix
}
2024-03-05 19:48:54 -05:00
pub fn allow_check_for_updates(&self) -> bool { self.server.config.allow_check_for_updates }
2024-03-05 19:48:54 -05:00
pub fn trusted_servers(&self) -> &[OwnedServerName] { &self.server.config.trusted_servers }
2024-03-05 19:48:54 -05:00
pub fn turn_password(&self) -> &String { &self.server.config.turn_password }
2024-03-05 19:48:54 -05:00
pub fn turn_ttl(&self) -> u64 { self.server.config.turn_ttl }
2024-03-05 19:48:54 -05:00
pub fn turn_uris(&self) -> &[String] { &self.server.config.turn_uris }
2024-03-05 19:48:54 -05:00
pub fn turn_username(&self) -> &String { &self.server.config.turn_username }
2024-03-05 19:48:54 -05:00
pub fn notification_push_path(&self) -> &String { &self.server.config.notification_push_path }
2024-03-05 19:48:54 -05:00
2024-05-09 15:59:08 -07:00
pub fn url_preview_domain_contains_allowlist(&self) -> &Vec<String> {
&self.server.config.url_preview_domain_contains_allowlist
}
2024-03-05 19:48:54 -05:00
2024-05-09 15:59:08 -07:00
pub fn url_preview_domain_explicit_allowlist(&self) -> &Vec<String> {
&self.server.config.url_preview_domain_explicit_allowlist
2024-03-05 19:48:54 -05:00
}
2024-05-09 15:59:08 -07:00
pub fn url_preview_domain_explicit_denylist(&self) -> &Vec<String> {
&self.server.config.url_preview_domain_explicit_denylist
}
pub fn url_preview_url_contains_allowlist(&self) -> &Vec<String> {
&self.server.config.url_preview_url_contains_allowlist
}
2024-03-05 19:48:54 -05:00
pub fn url_preview_max_spider_size(&self) -> usize {
self.server.config.url_preview_max_spider_size
}
2024-03-05 19:48:54 -05:00
pub fn url_preview_check_root_domain(&self) -> bool {
self.server.config.url_preview_check_root_domain
}
2024-03-05 19:48:54 -05:00
pub fn forbidden_alias_names(&self) -> &RegexSet { &self.server.config.forbidden_alias_names }
2024-03-05 19:48:54 -05:00
pub fn forbidden_usernames(&self) -> &RegexSet { &self.server.config.forbidden_usernames }
2024-03-05 19:48:54 -05:00
pub fn allow_local_presence(&self) -> bool { self.server.config.allow_local_presence }
2024-03-05 19:48:54 -05:00
pub fn allow_incoming_presence(&self) -> bool { self.server.config.allow_incoming_presence }
2024-03-05 19:48:54 -05:00
pub fn allow_outgoing_presence(&self) -> bool { self.server.config.allow_outgoing_presence }
2024-03-05 19:48:54 -05:00
pub fn allow_incoming_read_receipts(&self) -> bool {
self.server.config.allow_incoming_read_receipts
}
pub fn allow_outgoing_read_receipts(&self) -> bool {
self.server.config.allow_outgoing_read_receipts
}
pub fn block_non_admin_invites(&self) -> bool { self.server.config.block_non_admin_invites }
2024-03-05 19:48:54 -05:00
2024-07-22 07:43:51 +00:00
/// checks if `user_id` is local to us via server_name comparison
#[inline]
pub fn user_is_local(&self, user_id: &UserId) -> bool {
self.server_is_ours(user_id.server_name())
}
2024-03-05 19:48:54 -05:00
2024-07-22 07:43:51 +00:00
#[inline]
pub fn server_is_ours(&self, server_name: &ServerName) -> bool {
2025-01-25 23:41:39 +00:00
server_name == self.server_name()
}
2024-10-01 04:20:31 +00:00
#[inline]
pub fn is_read_only(&self) -> bool { self.db.db.is_read_only() }
2024-07-22 07:43:51 +00:00
}