feat(cli,api): human-readable ban durations, absolute expires_at, strict target/port validation
This commit is contained in:
parent
e2adf8601c
commit
da85225070
15 changed files with 570 additions and 36 deletions
|
|
@ -13,6 +13,7 @@ struct AddResponse {
|
|||
struct BlacklistItem {
|
||||
target: String,
|
||||
reason: String,
|
||||
expires_at: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
|
@ -46,14 +47,51 @@ async fn authenticated_client() -> anyhow::Result<(reqwest::Client, String, Stri
|
|||
Ok((client, manager_url, token))
|
||||
}
|
||||
|
||||
pub async fn add(target: String, reason: Option<String>) -> anyhow::Result<()> {
|
||||
/// clap value parser: a single address, strictly validated.
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns the validation message for anything but a bare IPv4/IPv6 address.
|
||||
pub fn parse_target_arg(text: &str) -> Result<String, String> {
|
||||
let parsed = crate::manager::input::parse_target(text)?;
|
||||
if parsed.prefix.is_some() {
|
||||
return Err("CIDR targets are not supported; ban a single IP address".into());
|
||||
}
|
||||
Ok(parsed.ip.to_string())
|
||||
}
|
||||
|
||||
/// clap value parser for `--for`: a human duration that must be non-zero.
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns the parser message (with the valid units) or a zero-duration message.
|
||||
pub fn parse_duration_arg(text: &str) -> Result<u64, String> {
|
||||
match crate::cli::duration::parse_duration_secs(text)? {
|
||||
0 => Err("duration must be greater than zero".into()),
|
||||
secs => Ok(secs),
|
||||
}
|
||||
}
|
||||
|
||||
fn remaining(expires_at: Option<&str>) -> String {
|
||||
let Some(at) = expires_at.and_then(crate::timeutil::parse_rfc3339_unix) else {
|
||||
return "permanent".to_string();
|
||||
};
|
||||
let left = at.saturating_sub(crate::timeutil::unix_now());
|
||||
match u64::try_from(left) {
|
||||
Ok(secs) if secs > 0 => format!("{} left", crate::cli::duration::format_duration_secs(secs)),
|
||||
_ => "expired".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn add(target: String, reason: Option<String>, duration_secs: Option<u64>) -> anyhow::Result<()> {
|
||||
let (client, manager_url, token) = authenticated_client().await?;
|
||||
|
||||
let body = serde_json::json!({
|
||||
let mut body = serde_json::json!({
|
||||
"target": target,
|
||||
"type": "ip",
|
||||
"reason": reason.unwrap_or_else(|| "manual".to_string()),
|
||||
});
|
||||
if let Some(secs) = duration_secs {
|
||||
body["duration_secs"] = secs.into();
|
||||
}
|
||||
|
||||
let resp = client
|
||||
.post(format!("{manager_url}/api/v1/blacklist"))
|
||||
|
|
@ -95,7 +133,12 @@ pub async fn list() -> anyhow::Result<()> {
|
|||
println!("Blacklist ({} entries)", list.total);
|
||||
println!("----------------------");
|
||||
for item in &list.items {
|
||||
println!(" {} ({})", item.target, item.reason);
|
||||
println!(
|
||||
" {} ({}) [{}]",
|
||||
item.target,
|
||||
item.reason,
|
||||
remaining(item.expires_at.as_deref())
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue