2024-05-30 00:03:51 +02:00
|
|
|
//! This module is used in the TypeScript backend only.
|
|
|
|
// We may want to (re)implement these functions in the `federation` module
|
|
|
|
// in a Rusty way (e.g., traits of server type) if needed.
|
2024-04-22 01:31:28 +02:00
|
|
|
|
2024-05-05 14:22:57 +02:00
|
|
|
/// Checks if a server is blocked.
|
|
|
|
///
|
2024-05-30 00:03:51 +02:00
|
|
|
/// # Argument
|
2024-05-05 14:22:57 +02:00
|
|
|
/// `host` - punycoded instance host
|
2024-05-30 00:03:51 +02:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```no_run
|
|
|
|
/// # use backend_rs::misc::check_server_block::is_blocked_server;
|
|
|
|
/// # async fn f() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
/// assert_eq!(true, is_blocked_server("blocked.com").await?);
|
|
|
|
/// assert_eq!(false, is_blocked_server("not-blocked.com").await?);
|
|
|
|
/// assert_eq!(true, is_blocked_server("subdomain.of.blocked.com").await?);
|
|
|
|
/// assert_eq!(true, is_blocked_server("xn--l8jegik.blocked.com").await?);
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
#[crate::ts_export]
|
|
|
|
pub async fn is_blocked_server(host: &str) -> Result<bool, sea_orm::DbErr> {
|
|
|
|
Ok(crate::misc::meta::fetch_meta(true)
|
2024-04-22 01:31:28 +02:00
|
|
|
.await?
|
|
|
|
.blocked_hosts
|
|
|
|
.iter()
|
|
|
|
.any(|blocked_host| {
|
|
|
|
host == blocked_host || host.ends_with(format!(".{}", blocked_host).as_str())
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2024-05-05 14:22:57 +02:00
|
|
|
/// Checks if a server is silenced.
|
|
|
|
///
|
2024-05-30 00:03:51 +02:00
|
|
|
/// # Argument
|
2024-05-05 14:22:57 +02:00
|
|
|
/// `host` - punycoded instance host
|
2024-05-30 00:03:51 +02:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```no_run
|
|
|
|
/// # use backend_rs::misc::check_server_block::is_silenced_server;
|
|
|
|
/// # async fn f() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
/// assert_eq!(true, is_silenced_server("silenced.com").await?);
|
|
|
|
/// assert_eq!(false, is_silenced_server("not-silenced.com").await?);
|
|
|
|
/// assert_eq!(true, is_silenced_server("subdomain.of.silenced.com").await?);
|
|
|
|
/// assert_eq!(true, is_silenced_server("xn--l8jegik.silenced.com").await?);
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
#[crate::ts_export]
|
|
|
|
pub async fn is_silenced_server(host: &str) -> Result<bool, sea_orm::DbErr> {
|
|
|
|
Ok(crate::misc::meta::fetch_meta(true)
|
2024-04-22 01:31:28 +02:00
|
|
|
.await?
|
|
|
|
.silenced_hosts
|
|
|
|
.iter()
|
|
|
|
.any(|silenced_host| {
|
|
|
|
host == silenced_host || host.ends_with(format!(".{}", silenced_host).as_str())
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2024-05-05 14:22:57 +02:00
|
|
|
/// Checks if a server is allowlisted.
|
|
|
|
/// Returns `Ok(true)` if private mode is disabled.
|
|
|
|
///
|
2024-05-30 00:03:51 +02:00
|
|
|
/// # Argument
|
2024-05-05 14:22:57 +02:00
|
|
|
/// `host` - punycoded instance host
|
2024-05-30 00:03:51 +02:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```no_run
|
|
|
|
/// # use backend_rs::misc::check_server_block::is_allowed_server;
|
|
|
|
/// # async fn f() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
/// assert_eq!(true, is_allowed_server("allowed.com").await?);
|
|
|
|
/// assert_eq!(false, is_allowed_server("not-allowed.com").await?);
|
|
|
|
/// assert_eq!(false, is_allowed_server("subdomain.of.allowed.com").await?);
|
|
|
|
/// assert_eq!(false, is_allowed_server("xn--l8jegik.allowed.com").await?);
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
#[crate::ts_export]
|
|
|
|
pub async fn is_allowed_server(host: &str) -> Result<bool, sea_orm::DbErr> {
|
|
|
|
let meta = crate::misc::meta::fetch_meta(true).await?;
|
2024-04-22 01:31:28 +02:00
|
|
|
|
|
|
|
if !meta.private_mode.unwrap_or(false) {
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
if let Some(allowed_hosts) = meta.allowed_hosts {
|
|
|
|
return Ok(allowed_hosts.contains(&host.to_string()));
|
|
|
|
}
|
|
|
|
Ok(false)
|
|
|
|
}
|