mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2025-07-09 10:56:41 +02:00
fmt: ran cargo format
This commit is contained in:
parent
cadc36700f
commit
0add1c7c52
12 changed files with 189 additions and 140 deletions
|
@ -156,7 +156,10 @@ where
|
|||
|
||||
debug!("Checking acl allowance for {}", destination);
|
||||
|
||||
if !services().acl.is_federation_with_allowed_fedi_dest(&actual_destination) {
|
||||
if !services()
|
||||
.acl
|
||||
.is_federation_with_allowed_fedi_dest(&actual_destination)
|
||||
{
|
||||
debug!("blocked sending federation to {:?}", actual_destination);
|
||||
|
||||
return Err(Error::ACLBlock(destination.to_owned()));
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use std::collections::HashSet;
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashSet;
|
||||
use url::Host;
|
||||
#[derive(Deserialize, Debug, Default, Clone)]
|
||||
pub struct AccessControlListConfig {
|
||||
|
@ -7,5 +7,5 @@ pub struct AccessControlListConfig {
|
|||
pub(crate) allow_list: Option<HashSet<Host<String>>>,
|
||||
|
||||
#[serde(default)]
|
||||
pub(crate)block_list: HashSet<Host<String>>
|
||||
pub(crate) block_list: HashSet<Host<String>>,
|
||||
}
|
|
@ -2,7 +2,8 @@ use std::{
|
|||
collections::BTreeMap,
|
||||
fmt,
|
||||
net::{IpAddr, Ipv4Addr},
|
||||
path::PathBuf, sync::Arc,
|
||||
path::PathBuf,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use figment::Figment;
|
||||
|
@ -13,7 +14,7 @@ use tracing::{error, warn};
|
|||
pub(crate) mod acl;
|
||||
mod proxy;
|
||||
|
||||
use self::{proxy::ProxyConfig, acl::AccessControlListConfig};
|
||||
use self::{acl::AccessControlListConfig, proxy::ProxyConfig};
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
|
|
|
@ -3,7 +3,10 @@ use std::collections::HashSet;
|
|||
use tracing::warn;
|
||||
use url::Host;
|
||||
|
||||
use crate::{service::acl::{Data, AclDatabaseEntry, AclMode}, KeyValueDatabase};
|
||||
use crate::{
|
||||
service::acl::{AclDatabaseEntry, AclMode, Data},
|
||||
KeyValueDatabase,
|
||||
};
|
||||
|
||||
impl Data for KeyValueDatabase {
|
||||
fn check_acl(&self, host: &Host<String>) -> crate::Result<Option<AclMode>> {
|
||||
|
@ -13,7 +16,10 @@ impl Data for KeyValueDatabase {
|
|||
Some(0x1) => Ok(Some(AclMode::Allow)),
|
||||
Some(0x0) => Ok(Some(AclMode::Block)),
|
||||
Some(invalid) => {
|
||||
warn!("found invalid value for mode byte in value {}, probably db corruption", invalid);
|
||||
warn!(
|
||||
"found invalid value for mode byte in value {}, probably db corruption",
|
||||
invalid
|
||||
);
|
||||
Ok(None)
|
||||
}
|
||||
None => Ok(None),
|
||||
|
@ -24,10 +30,13 @@ impl Data for KeyValueDatabase {
|
|||
}
|
||||
|
||||
fn add_acl(&self, acl: AclDatabaseEntry) -> crate::Result<()> {
|
||||
self.acl_list.insert(acl.hostname.to_string().as_bytes(), match acl.mode {
|
||||
self.acl_list.insert(
|
||||
acl.hostname.to_string().as_bytes(),
|
||||
match acl.mode {
|
||||
AclMode::Block => &[0x0],
|
||||
AclMode::Allow => &[0x1],
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn remove_acl(&self, host: Host<String>) -> crate::Result<()> {
|
||||
|
@ -49,12 +58,18 @@ impl Data for KeyValueDatabase {
|
|||
Some(0x1) => AclMode::Allow,
|
||||
Some(0x0) => AclMode::Block,
|
||||
Some(invalid) => {
|
||||
warn!("found invalid value for mode byte in value {}, probably db corruption", invalid);
|
||||
warn!(
|
||||
"found invalid value for mode byte in value {}, probably db corruption",
|
||||
invalid
|
||||
);
|
||||
return;
|
||||
}
|
||||
None => return,
|
||||
};
|
||||
set.insert(AclDatabaseEntry { mode: mode, hostname: parsed_host });
|
||||
set.insert(AclDatabaseEntry {
|
||||
mode: mode,
|
||||
hostname: parsed_host,
|
||||
});
|
||||
});
|
||||
set
|
||||
}
|
||||
|
|
|
@ -173,7 +173,7 @@ pub struct KeyValueDatabase {
|
|||
pub(super) lasttimelinecount_cache: Mutex<HashMap<OwnedRoomId, PduCount>>,
|
||||
pub(super) presence_timer_sender: Arc<mpsc::UnboundedSender<(OwnedUserId, Duration)>>,
|
||||
|
||||
pub(super) acl_list: Arc<dyn KvTree>
|
||||
pub(super) acl_list: Arc<dyn KvTree>,
|
||||
}
|
||||
|
||||
impl KeyValueDatabase {
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
use std::collections::HashSet;
|
||||
|
||||
use clap::ValueEnum;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use url::Host;
|
||||
|
||||
|
||||
pub trait Data: Send + Sync {
|
||||
/// check if given host exists in Acls, if so return it
|
||||
fn check_acl(&self, host: &Host<String>) -> crate::Result<Option<AclMode>>;
|
||||
|
@ -21,7 +20,7 @@ pub trait Data: Send + Sync {
|
|||
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Hash, Eq, PartialEq, ValueEnum)]
|
||||
pub enum AclMode {
|
||||
Block,
|
||||
Allow
|
||||
Allow,
|
||||
}
|
||||
|
||||
impl AclMode {
|
||||
|
@ -36,5 +35,5 @@ impl AclMode {
|
|||
|
||||
pub struct AclDatabaseEntry {
|
||||
pub(crate) mode: AclMode,
|
||||
pub(crate) hostname: Host
|
||||
pub(crate) hostname: Host,
|
||||
}
|
|
@ -1,28 +1,37 @@
|
|||
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use ruma::ServerName;
|
||||
use tracing::{warn, debug, error};
|
||||
use tracing::{debug, error, warn};
|
||||
use url::Host;
|
||||
|
||||
use crate::{config::acl::AccessControlListConfig, api::server_server::FedDest};
|
||||
use crate::{api::server_server::FedDest, config::acl::AccessControlListConfig};
|
||||
|
||||
pub use self::data::*;
|
||||
mod data;
|
||||
pub struct Service {
|
||||
pub db: &'static dyn Data,
|
||||
pub acl_config: Arc<AccessControlListConfig>
|
||||
pub acl_config: Arc<AccessControlListConfig>,
|
||||
}
|
||||
|
||||
impl Service {
|
||||
pub fn list_acls(&self, filter: Option<AclMode>) -> Vec<AclDatabaseEntry> {
|
||||
let mut set = self.db.get_all_acls();
|
||||
self.acl_config.allow_list.clone().unwrap_or_default().iter().for_each(|it| {
|
||||
set.insert(AclDatabaseEntry { mode: AclMode::Allow, hostname: it.to_owned() });
|
||||
self.acl_config
|
||||
.allow_list
|
||||
.clone()
|
||||
.unwrap_or_default()
|
||||
.iter()
|
||||
.for_each(|it| {
|
||||
set.insert(AclDatabaseEntry {
|
||||
mode: AclMode::Allow,
|
||||
hostname: it.to_owned(),
|
||||
});
|
||||
});
|
||||
self.acl_config.block_list.clone().iter().for_each(|it| {
|
||||
set.insert(AclDatabaseEntry { mode: AclMode::Block, hostname: it.to_owned() });
|
||||
set.insert(AclDatabaseEntry {
|
||||
mode: AclMode::Block,
|
||||
hostname: it.to_owned(),
|
||||
});
|
||||
});
|
||||
match filter {
|
||||
Some(filter) => set.into_iter().filter(|it| it.mode == filter).collect(),
|
||||
|
@ -34,14 +43,20 @@ impl Service {
|
|||
}
|
||||
|
||||
pub fn add_acl(&self, host: Host, mode: AclMode) -> crate::Result<()> {
|
||||
self.db.add_acl(AclDatabaseEntry { mode: mode, hostname: host })
|
||||
self.db.add_acl(AclDatabaseEntry {
|
||||
mode: mode,
|
||||
hostname: host,
|
||||
})
|
||||
}
|
||||
/// same as federation_with_allowed however it can work with the fedi_dest type
|
||||
pub fn is_federation_with_allowed_fedi_dest(&self, fedi_dest: &FedDest) -> bool {
|
||||
let hostname = if let Ok(name) = Host::parse(&fedi_dest.hostname()) {
|
||||
name
|
||||
} else {
|
||||
warn!("cannot deserialise hostname for server with name {:?}",fedi_dest);
|
||||
warn!(
|
||||
"cannot deserialise hostname for server with name {:?}",
|
||||
fedi_dest
|
||||
);
|
||||
return false;
|
||||
};
|
||||
return self.is_federation_with_allowed(hostname);
|
||||
|
@ -87,7 +102,6 @@ impl Service {
|
|||
warn!("allowlist value found in database for {} but allow list is not enabled, denied request", server_host_name);
|
||||
false
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -40,7 +40,7 @@ use crate::{
|
|||
Error, PduEvent, Result,
|
||||
};
|
||||
|
||||
use super::{pdu::PduBuilder, acl::AclMode};
|
||||
use super::{acl::AclMode, pdu::PduBuilder};
|
||||
|
||||
const PAGE_SIZE: usize = 100;
|
||||
|
||||
|
@ -81,16 +81,9 @@ enum AdminCommand {
|
|||
#[cfg_attr(test, derive(Debug))]
|
||||
#[derive(Subcommand)]
|
||||
enum AclCommand {
|
||||
Add {
|
||||
mode: AclMode,
|
||||
hostname: String
|
||||
},
|
||||
Remove {
|
||||
hostname: String
|
||||
},
|
||||
List{
|
||||
filter: Option<AclMode>
|
||||
}
|
||||
Add { mode: AclMode, hostname: String },
|
||||
Remove { hostname: String },
|
||||
List { filter: Option<AclMode> },
|
||||
}
|
||||
|
||||
#[cfg_attr(test, derive(Debug))]
|
||||
|
@ -1279,42 +1272,60 @@ impl Service {
|
|||
AdminCommand::Acl(AclCommand::Add { mode, hostname }) => {
|
||||
let host = match Host::parse(&hostname) {
|
||||
Ok(host) => host,
|
||||
Err(error) => return Ok(RoomMessageEventContent::text_plain(format!("failed to parse hostname with error {}",error))),
|
||||
Err(error) => {
|
||||
return Ok(RoomMessageEventContent::text_plain(format!(
|
||||
"failed to parse hostname with error {}",
|
||||
error
|
||||
)))
|
||||
}
|
||||
};
|
||||
if let Err(error) = services().acl.add_acl(host.clone(), mode) {
|
||||
error!("encountered {} while trying to add acl with host {} and mode {:?}",error,host,mode);
|
||||
error!(
|
||||
"encountered {} while trying to add acl with host {} and mode {:?}",
|
||||
error, host, mode
|
||||
);
|
||||
RoomMessageEventContent::text_plain("error, couldn't add acl")
|
||||
} else {
|
||||
RoomMessageEventContent::text_plain("successfully added ACL")
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
AdminCommand::Acl(AclCommand::Remove { hostname }) => {
|
||||
let host = match Host::parse(&hostname) {
|
||||
Ok(host) => host,
|
||||
Err(error) => return Ok(RoomMessageEventContent::text_plain(format!("failed to parse hostname with error {}",error))),
|
||||
Err(error) => {
|
||||
return Ok(RoomMessageEventContent::text_plain(format!(
|
||||
"failed to parse hostname with error {}",
|
||||
error
|
||||
)))
|
||||
}
|
||||
};
|
||||
|
||||
if let Err(error) = services().acl.remove_acl(host.clone()) {
|
||||
error!("encountered {} while trying to remove acl with host {}",error,host);
|
||||
error!(
|
||||
"encountered {} while trying to remove acl with host {}",
|
||||
error, host
|
||||
);
|
||||
RoomMessageEventContent::text_plain("error, couldn't remove acl")
|
||||
} else {
|
||||
RoomMessageEventContent::text_plain("successfully removed ACL")
|
||||
}
|
||||
},
|
||||
}
|
||||
AdminCommand::Acl(AclCommand::List { filter }) => {
|
||||
let results = services().acl.list_acls(filter);
|
||||
let mut results_html = String::new();
|
||||
results.iter().for_each(|it| {
|
||||
results_html.push_str(&format!("* {} | {}\n", it.hostname, it.mode.to_emoji()));
|
||||
});
|
||||
RoomMessageEventContent::text_plain(format!("
|
||||
RoomMessageEventContent::text_plain(format!(
|
||||
"
|
||||
List of services: \n
|
||||
❎ = blocked\n
|
||||
✅ = allowed\n
|
||||
{}
|
||||
",results_html))
|
||||
},
|
||||
",
|
||||
results_html
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
Ok(reply_message_content)
|
||||
|
|
|
@ -6,8 +6,8 @@ use std::{
|
|||
use lru_cache::LruCache;
|
||||
|
||||
use crate::{Config, Result};
|
||||
pub mod acl;
|
||||
pub mod account_data;
|
||||
pub mod acl;
|
||||
pub mod admin;
|
||||
pub mod appservice;
|
||||
pub mod globals;
|
||||
|
@ -34,7 +34,7 @@ pub struct Services {
|
|||
pub key_backups: key_backups::Service,
|
||||
pub media: media::Service,
|
||||
pub sending: Arc<sending::Service>,
|
||||
pub acl: acl::Service
|
||||
pub acl: acl::Service,
|
||||
}
|
||||
|
||||
impl Services {
|
||||
|
@ -121,7 +121,10 @@ impl Services {
|
|||
sending: sending::Service::build(db, &config),
|
||||
|
||||
globals: globals::Service::load(db, config)?,
|
||||
acl: acl::Service { db: db, acl_config: acl_conf },
|
||||
acl: acl::Service {
|
||||
db: db,
|
||||
acl_config: acl_conf,
|
||||
},
|
||||
})
|
||||
}
|
||||
fn memory_usage(&self) -> String {
|
||||
|
|
|
@ -1645,7 +1645,10 @@ impl Service {
|
|||
|
||||
/// Returns Ok if the acl allows the server
|
||||
pub fn acl_check(&self, server_name: &ServerName, room_id: &RoomId) -> Result<()> {
|
||||
if !services().acl.is_federation_with_allowed_server_name(server_name) {
|
||||
if !services()
|
||||
.acl
|
||||
.is_federation_with_allowed_server_name(server_name)
|
||||
{
|
||||
info!(
|
||||
"Server {} was denied by server ACL in {}",
|
||||
server_name, room_id
|
||||
|
|
|
@ -85,7 +85,7 @@ pub enum Error {
|
|||
#[error("{0} in {1}")]
|
||||
InconsistentRoomState(&'static str, ruma::OwnedRoomId),
|
||||
#[error("blocked {0}")]
|
||||
ACLBlock(OwnedServerName)
|
||||
ACLBlock(OwnedServerName),
|
||||
}
|
||||
|
||||
impl Error {
|
||||
|
|
Loading…
Add table
Reference in a new issue