use crate::{
models::{IpBlock, ModSecRule},
CpanelClient, CpanelError,
};
use serde::Deserialize;
#[derive(Debug, Clone, Deserialize)]
pub struct IpBlockList {
pub blocks: Vec<IpBlock>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct ModSecRuleList {
pub rules: Vec<ModSecRule>,
}
impl CpanelClient {
pub async fn list_ip_blocks(&self, user: &str) -> Result<Vec<IpBlock>, CpanelError> {
let result: IpBlockList = self
.uapi("IPBlocker", "list_ip_blocks", &[("user", user)])
.await?;
Ok(result.blocks)
}
pub async fn block_ip(
&self,
user: &str,
ip: &str,
reason: Option<&str>,
) -> Result<(), CpanelError> {
let mut params: Vec<(&str, &str)> = vec![("user", user), ("ip", ip)];
if let Some(r) = reason {
params.push(("reason", r));
}
let _: () = self.uapi("IPBlocker", "add_ip_block", ¶ms).await?;
Ok(())
}
pub async fn unblock_ip(&self, user: &str, ip: &str) -> Result<(), CpanelError> {
let _: () = self
.uapi("IPBlocker", "del_ip_block", &[("user", user), ("ip", ip)])
.await?;
Ok(())
}
pub async fn list_modsec_rules(&self, user: &str) -> Result<Vec<ModSecRule>, CpanelError> {
let result: ModSecRuleList = self
.uapi("ModSecurity", "list_rules", &[("user", user)])
.await?;
Ok(result.rules)
}
pub async fn disable_modsec_rule(&self, user: &str, rule_id: &str) -> Result<(), CpanelError> {
let _: () = self
.uapi(
"ModSecurity",
"disable_rule",
&[("user", user), ("rule_id", rule_id)],
)
.await?;
Ok(())
}
}