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:
parent
2b91422d18
commit
d7e800fcb4
6 changed files with 128 additions and 4 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue