diff --git a/src/database.rs b/src/database.rs index 9b40205..f10eb94 100644 --- a/src/database.rs +++ b/src/database.rs @@ -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 { @@ -12,20 +13,21 @@ pub fn init_db(path: &str) -> Result { 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, 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 Result<(), rusqli ip_addr = ip_addr.split(":").collect::>()[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])?; diff --git a/src/main.rs b/src/main.rs index d3cf9e5..c88b937 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 diff --git a/src/sip/forbidden.rs b/src/sip/forbidden.rs index d3c229a..a50b1ec 100644 --- a/src/sip/forbidden.rs +++ b/src/sip/forbidden.rs @@ -19,6 +19,7 @@ pub struct Forbidden { pub content_length: Option, // 0 } +#[allow(dead_code)] impl Forbidden { pub fn serialize(&self) -> Vec { let mut preout = format!(