save and notify only if peer wasn't on database
This commit is contained in:
parent
82b871170e
commit
7437d458cb
@ -1,10 +1,11 @@
|
|||||||
use rusqlite::{Connection, Result};
|
use rusqlite::{Connection, Result};
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub struct Suspicious {
|
pub struct Suspicious {
|
||||||
ip_addr: String,
|
ip_addr: String,
|
||||||
banned: bool,
|
banned: u8,
|
||||||
date_added: String,
|
create_time: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_db(path: &str) -> Result<Connection> {
|
pub fn init_db(path: &str) -> Result<Connection> {
|
||||||
@ -12,20 +13,21 @@ pub fn init_db(path: &str) -> Result<Connection> {
|
|||||||
conn.execute(
|
conn.execute(
|
||||||
"CREATE TABLE IF NOT EXISTS suspicious (
|
"CREATE TABLE IF NOT EXISTS suspicious (
|
||||||
ip_addr VARCHAR(128) PRIMARY KEY,
|
ip_addr VARCHAR(128) PRIMARY KEY,
|
||||||
banned BOOLEAN NOT NULL,
|
banned INTEGER NOT NULL,
|
||||||
date_added DATE
|
create_time INTEGER NOT NULL
|
||||||
)", [])?;
|
)", [])?;
|
||||||
|
|
||||||
Ok(conn)
|
Ok(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn get_all_suspicious(conn: &Connection) -> Result<Vec<Suspicious>, rusqlite::Error> {
|
pub fn get_all_suspicious(conn: &Connection) -> Result<Vec<Suspicious>, rusqlite::Error> {
|
||||||
let mut stmt = conn.prepare("SELECT * FROM suspicious")?;
|
let mut stmt = conn.prepare("SELECT * FROM suspicious")?;
|
||||||
let rows = stmt.query_map([], |row| {
|
let rows = stmt.query_map([], |row| {
|
||||||
Ok(Suspicious {
|
Ok(Suspicious {
|
||||||
ip_addr: row.get(0)?,
|
ip_addr: row.get(0)?,
|
||||||
banned: row.get(1)?,
|
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 {
|
Ok(Suspicious {
|
||||||
ip_addr: row.get(0)?,
|
ip_addr: row.get(0)?,
|
||||||
banned: row.get(1)?,
|
banned: row.get(1)?,
|
||||||
date_added: row.get(2)?,
|
create_time: row.get(2)?,
|
||||||
})
|
})
|
||||||
})?;
|
})?;
|
||||||
Ok(row)
|
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];
|
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 (?, ?, ?)")?;
|
let epoch = SystemTime::now()
|
||||||
stmt.execute([ip_addr, "false", 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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn delete_suspicious(conn: &Connection, ip_addr: &str) -> Result<(), rusqlite::Error> {
|
pub fn delete_suspicious(conn: &Connection, ip_addr: &str) -> Result<(), rusqlite::Error> {
|
||||||
let mut stmt = conn.prepare("DELETE FROM suspicious WHERE ip_addr = ?")?;
|
let mut stmt = conn.prepare("DELETE FROM suspicious WHERE ip_addr = ?")?;
|
||||||
stmt.execute([ip_addr])?;
|
stmt.execute([ip_addr])?;
|
||||||
|
22
src/main.rs
22
src/main.rs
@ -49,10 +49,24 @@ impl Server {
|
|||||||
// If so then we try to send it back to the original source, waiting
|
// 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.
|
// until it's writable and we're able to do so.
|
||||||
if let Some((size, peer)) = to_send {
|
if let Some((size, peer)) = to_send {
|
||||||
log::info!("Suspicious peer: {}", peer);
|
// save peer
|
||||||
match add_suspicious(&db_con, peer.to_string().as_str()) {
|
let str_peer_ip = peer.ip().to_string();
|
||||||
Ok(_) => {},
|
match get_suspicious_by_ip_addr(&db_con, str_peer_ip.as_str()) {
|
||||||
Err(_) => return Err(io::Error::new(io::ErrorKind::Other, "Error adding suspicious peer to database")),
|
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
|
// test type of message received
|
||||||
|
@ -19,6 +19,7 @@ pub struct Forbidden {
|
|||||||
pub content_length: Option<i32>, // 0
|
pub content_length: Option<i32>, // 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
impl Forbidden {
|
impl Forbidden {
|
||||||
pub fn serialize(&self) -> Vec<u8> {
|
pub fn serialize(&self) -> Vec<u8> {
|
||||||
let mut preout = format!(
|
let mut preout = format!(
|
||||||
|
Loading…
Reference in New Issue
Block a user