update
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::command;
|
||||
use crate::{command, config::Config, privileged};
|
||||
use anyhow::{bail, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{net::IpAddr, time::Duration};
|
||||
@@ -9,6 +9,9 @@ pub struct Status {
|
||||
pub running: bool,
|
||||
pub version: Option<String>,
|
||||
pub jails: Vec<String>,
|
||||
pub jail_statuses: Vec<JailStatus>,
|
||||
pub management_enabled: bool,
|
||||
pub manageable_jails: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Serialize, PartialEq)]
|
||||
@@ -28,7 +31,7 @@ pub struct BanRequest {
|
||||
pub ip: String,
|
||||
}
|
||||
|
||||
pub async fn status(timeout: Duration) -> Status {
|
||||
pub async fn status(config: &Config, timeout: Duration) -> Status {
|
||||
let version = match command::run("fail2ban-client", &["--version"], timeout).await {
|
||||
Ok(output) => Some(output.stdout.trim().to_owned()),
|
||||
Err(_) => {
|
||||
@@ -37,21 +40,47 @@ pub async fn status(timeout: Duration) -> Status {
|
||||
running: false,
|
||||
version: None,
|
||||
jails: vec![],
|
||||
jail_statuses: vec![],
|
||||
management_enabled: false,
|
||||
manageable_jails: config.fail2ban.allowed_jails.clone(),
|
||||
}
|
||||
}
|
||||
};
|
||||
match command::run("fail2ban-client", &["status"], timeout).await {
|
||||
Ok(output) => Status {
|
||||
installed: true,
|
||||
running: true,
|
||||
version,
|
||||
jails: parse_jail_list(&output.stdout),
|
||||
},
|
||||
Ok(output) => {
|
||||
let jails = parse_jail_list(&output.stdout);
|
||||
let mut jail_statuses = Vec::new();
|
||||
let mut tasks = tokio::task::JoinSet::new();
|
||||
for name in jails.iter().take(64) {
|
||||
let name = name.clone();
|
||||
tasks.spawn(async move { jail(&name, timeout).await });
|
||||
}
|
||||
while let Some(result) = tasks.join_next().await {
|
||||
if let Ok(Ok(status)) = result {
|
||||
jail_statuses.push(status);
|
||||
}
|
||||
}
|
||||
jail_statuses.sort_by(|left, right| left.jail.cmp(&right.jail));
|
||||
Status {
|
||||
installed: true,
|
||||
running: true,
|
||||
version,
|
||||
jails,
|
||||
jail_statuses,
|
||||
management_enabled: config.fail2ban.management_enabled
|
||||
&& privileged::socket_available(),
|
||||
manageable_jails: config.fail2ban.allowed_jails.clone(),
|
||||
}
|
||||
}
|
||||
Err(_) => Status {
|
||||
installed: true,
|
||||
running: false,
|
||||
version,
|
||||
jails: vec![],
|
||||
jail_statuses: vec![],
|
||||
management_enabled: config.fail2ban.management_enabled
|
||||
&& privileged::socket_available(),
|
||||
manageable_jails: config.fail2ban.allowed_jails.clone(),
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -62,21 +91,71 @@ pub async fn jail(name: &str, timeout: Duration) -> Result<JailStatus> {
|
||||
Ok(parse_jail_status(name, &output.stdout))
|
||||
}
|
||||
|
||||
pub async fn ban(request: &BanRequest, unban: bool, timeout: Duration) -> Result<()> {
|
||||
pub async fn ban(
|
||||
config: &Config,
|
||||
request: &BanRequest,
|
||||
unban: bool,
|
||||
timeout: Duration,
|
||||
) -> Result<()> {
|
||||
validate_jail(&request.jail)?;
|
||||
let ip: IpAddr = request
|
||||
.ip
|
||||
.parse()
|
||||
.map_err(|_| anyhow::anyhow!("invalid IP address"))?;
|
||||
let operation = if unban { "unbanip" } else { "banip" };
|
||||
command::run(
|
||||
"fail2ban-client",
|
||||
&[
|
||||
"set",
|
||||
request.jail.as_str(),
|
||||
operation,
|
||||
ip.to_string().as_str(),
|
||||
],
|
||||
if !config
|
||||
.fail2ban
|
||||
.allowed_jails
|
||||
.iter()
|
||||
.any(|value| value == &request.jail)
|
||||
{
|
||||
bail!("jail is not allow-listed")
|
||||
}
|
||||
privileged::call(
|
||||
&privileged::Request::Fail2BanAction {
|
||||
jail: request.jail.clone(),
|
||||
ip: ip.to_string(),
|
||||
operation: if unban { "unban".into() } else { "ban".into() },
|
||||
},
|
||||
timeout,
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct ConfigureRequest {
|
||||
pub jail: String,
|
||||
pub enabled: bool,
|
||||
pub bantime_seconds: u32,
|
||||
pub findtime_seconds: u32,
|
||||
pub maxretry: u16,
|
||||
}
|
||||
|
||||
pub async fn configure(
|
||||
config: &Config,
|
||||
request: ConfigureRequest,
|
||||
timeout: Duration,
|
||||
) -> Result<()> {
|
||||
validate_jail(&request.jail)?;
|
||||
if !config
|
||||
.fail2ban
|
||||
.allowed_jails
|
||||
.iter()
|
||||
.any(|value| value == &request.jail)
|
||||
{
|
||||
bail!("jail is not allow-listed")
|
||||
}
|
||||
privileged::call(
|
||||
&privileged::Request::Fail2BanConfigure {
|
||||
settings: privileged::Fail2BanSettings {
|
||||
jail: request.jail,
|
||||
enabled: request.enabled,
|
||||
bantime_seconds: request.bantime_seconds,
|
||||
findtime_seconds: request.findtime_seconds,
|
||||
maxretry: request.maxretry,
|
||||
},
|
||||
},
|
||||
timeout,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Reference in New Issue
Block a user