moving code from command server into cli

This commit is contained in:
midefos committed 2024-05-28 16:03:32 +02:00
1 parent 8a6021aa83
commit d53ca8a33a
8 files changed
+323 -146

No files matched your search

+40 -20
View File
@@ -1,37 +1,57 @@
pub mod cli;
pub mod iptables_save;
pub mod iptables_wrapper;
pub mod login_attempt;
pub mod tpc_command_server;
use crate::tpc_command_server::start_tcp_command_server;
use cli::Arguments;
use iptables_wrapper::{allow_ip_port, ban_port, remove_ip_port, unban_port};
use linemux::MuxedLines;
use login_attempt::LoginAttempt;
use std::path::PathBuf;
use std::thread;
use std::{collections::HashMap, thread::sleep, time::Duration};
use structopt::StructOpt;
#[tokio::main]
async fn main() -> std::io::Result<()> {
async fn main() {
let opts = Arguments::from_args();
match opts {
Arguments::BanServer {
ssh_auth_log,
iptables_save,
} => {
let _ = start_ban_server(ssh_auth_log, iptables_save).await;
}
Arguments::BanPort { port } => ban_port(port),
Arguments::UnbanPort { port } => unban_port(port),
Arguments::AllowIpPort { ip, port } => allow_ip_port(&ip, port),
Arguments::RemoveIpPort { ip, port } => remove_ip_port(&ip, port),
}
}
async fn start_ban_server(
ssh_auth_log: PathBuf,
iptables_save: Option<PathBuf>,
) -> std::io::Result<()> {
let iptables = iptables::new(false).unwrap();
let mut lines = MuxedLines::new()?;
lines.add_file("/host_ssh/auth.log").await?;
lines.add_file(&ssh_auth_log).await?;
let mut login_attempts: HashMap<String, usize> = HashMap::new();
let seconds_iptables = Duration::from_secs(60);
println!(
"starting iptables-save, run every {} seconds",
seconds_iptables.as_secs()
);
if let Some(iptables_save) = iptables_save {
let seconds_iptables = Duration::from_secs(60);
println!(
"starting iptables-save, run every {} seconds",
seconds_iptables.as_secs()
);
thread::spawn(move || loop {
sleep(seconds_iptables);
iptables_save::save_iptables(&iptables_save);
println!("saved iptables rules");
});
}
thread::spawn(move || loop {
sleep(seconds_iptables);
iptables_save::save_iptables();
println!("saved iptables rules");
});
thread::spawn(|| {
start_tcp_command_server();
});
println!("listening to changes over /host_ssh/auth.log");
println!("listening to changes over {}", ssh_auth_log.display());
while let Ok(Some(line)) = lines.next_line().await {
if let Some(login_attempt) = LoginAttempt::capture(line.line()) {
println!(