save and notify only if peer wasn't on database

This commit is contained in:
serxoz 2024-04-18 12:40:25 +02:00
parent 82b871170e
commit 7437d458cb
3 changed files with 36 additions and 12 deletions

View File

@ -1,10 +1,11 @@
use rusqlite::{Connection, Result};
use std::time::SystemTime;
#[allow(dead_code)]
pub struct Suspicious {
ip_addr: String,
banned: bool,
date_added: String,
banned: u8,
create_time: u64,
}
pub fn init_db(path: &str) -> Result<Connection> {
@ -12,20 +13,21 @@ pub fn init_db(path: &str) -> Result<Connection> {
conn.execute(
"CREATE TABLE IF NOT EXISTS suspicious (
ip_addr VARCHAR(128) PRIMARY KEY,
banned BOOLEAN NOT NULL,
date_added DATE
banned INTEGER NOT NULL,
create_time INTEGER NOT NULL
)", [])?;
Ok(conn)
}
#[allow(dead_code)]
pub fn get_all_suspicious(conn: &Connection) -> Result<Vec<Suspicious>, rusqlite::Error> {
let mut stmt = conn.prepare("SELECT * FROM suspicious")?;
let rows = stmt.query_map([], |row| {
Ok(Suspicious {
ip_addr: row.get(0)?,
banned: row.get(1)?,
date_added: row.get(2)?,
create_time: row.get(2)?,
})
})?;
@ -38,7 +40,7 @@ pub fn get_suspicious_by_ip_addr(conn: &Connection, ip_addr: &str) -> Result<Sus
Ok(Suspicious {
ip_addr: row.get(0)?,
banned: row.get(1)?,
date_added: row.get(2)?,
create_time: row.get(2)?,
})
})?;
Ok(row)
@ -51,11 +53,18 @@ pub fn add_suspicious(conn: &Connection, mut ip_addr: &str) -> Result<(), rusqli
ip_addr = ip_addr.split(":").collect::<Vec<&str>>()[0];
}
let mut stmt = conn.prepare("INSERT OR IGNORE INTO suspicious (ip_addr, banned, date_added) VALUES (?, ?, ?)")?;
stmt.execute([ip_addr, "false", SystemTime::now()])?;
let epoch = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs()
.to_string();
let mut stmt = conn.prepare("INSERT OR IGNORE INTO suspicious (ip_addr, banned, create_time) VALUES (?, ?, ?)")?;
stmt.execute([ip_addr, "0", epoch.as_str()])?;
Ok(())
}
#[allow(dead_code)]
pub fn delete_suspicious(conn: &Connection, ip_addr: &str) -> Result<(), rusqlite::Error> {
let mut stmt = conn.prepare("DELETE FROM suspicious WHERE ip_addr = ?")?;
stmt.execute([ip_addr])?;

View File

@ -49,10 +49,24 @@ impl Server {
// If so then we try to send it back to the original source, waiting
// until it's writable and we're able to do so.
if let Some((size, peer)) = to_send {
log::info!("Suspicious peer: {}", peer);
match add_suspicious(&db_con, peer.to_string().as_str()) {
Ok(_) => {},
Err(_) => return Err(io::Error::new(io::ErrorKind::Other, "Error adding suspicious peer to database")),
// save peer
let str_peer_ip = peer.ip().to_string();
match get_suspicious_by_ip_addr(&db_con, str_peer_ip.as_str()) {
Ok(_) => {
// was added to database, do nothing
// log::info!("Peer already added to database");
},
Err(_e) => {
// was not in database
// log::info!("Error getting suspicious peer from database: {}", _e);
log::info!("Suspicious peer: {}", peer.ip());
match add_suspicious(&db_con, str_peer_ip.as_str()) {
Ok(_) => {
// TODO: launch action script
},
Err(_) => return Err(io::Error::new(io::ErrorKind::Other, "Error adding suspicious peer to database")),
}
}
}
// test type of message received

View File

@ -19,6 +19,7 @@ pub struct Forbidden {
pub content_length: Option<i32>, // 0
}
#[allow(dead_code)]
impl Forbidden {
pub fn serialize(&self) -> Vec<u8> {
let mut preout = format!(