mirror of
https://github.com/pldubouilh/blockfast.git
synced 2026-08-28 21:07:00 -04:00
run iptables/ipset directly
This commit is contained in:
+10
-10
@@ -14,9 +14,9 @@ pub struct Jail {
|
||||
remand: Mutex<HashMap<IpAddr, (u8, u64)>>,
|
||||
}
|
||||
|
||||
fn exec(cmd: &str, err: &str) -> Result<(), Error> {
|
||||
fn exec(program: &str, cmd: &str, err: &str) -> Result<(), Error> {
|
||||
let sentence_sl: Vec<&str> = cmd.split_whitespace().collect();
|
||||
let out = Command::new("sudo").args(sentence_sl).output()?;
|
||||
let out = Command::new(program).args(sentence_sl).output()?;
|
||||
let sc = out.status.code();
|
||||
ensure!(sc == Some(0), "err exec {}, {:?}\n{}", cmd, out, err);
|
||||
Ok(())
|
||||
@@ -28,16 +28,16 @@ impl Jail {
|
||||
let n = format!("blockfast_jail_{}", jailtime);
|
||||
|
||||
// create
|
||||
let cmd = format!("ipset create -exist {} hash:ip timeout {}", n, jailtime);
|
||||
exec(&cmd, ERR_MSG)?;
|
||||
let cmd = format!("create -exist {} hash:ip timeout {}", n, jailtime);
|
||||
exec("ipset", &cmd, ERR_MSG)?;
|
||||
|
||||
// setup input
|
||||
let cmd = format!("iptables -I INPUT 1 -m set -j DROP --match-set {} src", n);
|
||||
exec(&cmd, ERR_MSG)?;
|
||||
let cmd = format!("-I INPUT 1 -m set -j DROP --match-set {} src", n);
|
||||
exec("iptables", &cmd, ERR_MSG)?;
|
||||
|
||||
// setup fwd
|
||||
let cmd = format!("iptables -I FORWARD 1 -m set -j DROP --match-set {} src", n);
|
||||
exec(&cmd, ERR_MSG)?;
|
||||
let cmd = format!("-I FORWARD 1 -m set -j DROP --match-set {} src", n);
|
||||
exec("iptables", &cmd, ERR_MSG)?;
|
||||
|
||||
log!("jail setup, allowance {}, time {}s", allowance, jailtime);
|
||||
Ok(Jail {
|
||||
@@ -75,8 +75,8 @@ impl Jail {
|
||||
};
|
||||
|
||||
if should_ban {
|
||||
let cmd = format!("ipset add -exist {} {}", self.name, ip);
|
||||
exec(&cmd, "")?;
|
||||
let cmd = format!("add -exist {} {}", self.name, ip);
|
||||
exec("ipset", &cmd, "")?;
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user