add: ban ipv6 addresses by prefix

assumes all ipv6 addresses have 64-bit prefix (8 bytes at the start + 2 bytes for colons)
This commit is contained in:
trisua 2025-05-21 23:32:45 -04:00
parent 2b91422d18
commit d7e800fcb4
6 changed files with 128 additions and 4 deletions

View file

@ -1,5 +1,6 @@
use super::*;
use crate::cache::Cache;
use crate::model::addr::RemoteAddr;
use crate::model::moderation::AuditLogEntry;
use crate::model::{Error, Result, auth::IpBan, auth::User, permissions::FinePermission};
use crate::{auto_method, execute, get, query_row, query_rows, params};
@ -26,6 +27,30 @@ impl DataManager {
auto_method!(get_ipban_by_ip(&str)@get_ipban_from_row -> "SELECT * FROM ipbans WHERE ip = $1" --name="ip ban" --returns=IpBan --cache-key-tmpl="atto.ipban:{}");
/// Get an IP ban as a [`RemoteAddr`].
///
/// # Arguments
/// * `prefix`
pub async fn get_ipban_by_addr(&self, addr: RemoteAddr) -> Result<IpBan> {
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_row!(
&conn,
"SELECT * FROM ipbans WHERE ip LIKE $1",
&[&format!("{}%", addr.prefix(None))],
|x| { Ok(Self::get_ipban_from_row(x)) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("ip ban".to_string()));
}
Ok(res.unwrap())
}
/// Get all IP bans (paginated).
///
/// # Arguments