Display ban seconds in log

This commit is contained in:
Mike Dilger 2024-02-20 22:08:17 +13:00
parent be494421b6
commit 3ef2c4ae71
3 changed files with 30 additions and 15 deletions

View File

@ -51,11 +51,17 @@ lazy_static! {
}
impl Globals {
pub async fn ban(ipaddr: std::net::IpAddr, bankind: Ban) {
pub fn ban(ipaddr: std::net::IpAddr, bankind: Ban) -> u64 {
let mut ban_seconds: u64 = 0;
GLOBALS
.ip_data
.entry(ipaddr)
.and_modify(|ipdata| ipdata.ban(bankind))
.or_insert(IpData::new(bankind));
.and_modify(|ipdata| ban_seconds = ipdata.ban(bankind))
.or_insert({
let (ipdata, seconds) = IpData::new(bankind);
ban_seconds = seconds;
ipdata
});
ban_seconds
}
}

View File

@ -17,7 +17,7 @@ pub struct IpData {
}
impl IpData {
pub fn new(ban: Ban) -> IpData {
pub fn new(ban: Ban) -> (IpData, u64) {
let mut ipdata = IpData {
ban_until: Time::now(),
number_of_error_exits: 0,
@ -25,12 +25,12 @@ impl IpData {
number_of_timeouts: 0,
};
ipdata.ban(ban);
let seconds = ipdata.ban(ban);
ipdata
(ipdata, seconds)
}
pub fn ban(&mut self, ban: Ban) {
pub fn ban(&mut self, ban: Ban) -> u64 {
// Update numbers
match ban {
Ban::ErrorExit => self.number_of_error_exits += 1,
@ -41,17 +41,20 @@ impl IpData {
// Compute ban_until
let mut until = Time::now();
until.0 += self.ban_seconds(ban);
let seconds = self.ban_seconds(ban);
until.0 += seconds;
self.ban_until = Time(self.ban_until.0.max(until.0));
seconds
}
fn ban_seconds(&self, thisban: Ban) -> u64 {
match thisban {
Ban::General => 3,
Ban::ErrorExit => 3 + self.number_of_error_exits * 10,
Ban::TooManyErrors => 3 + self.number_of_too_many_error_bans * 15,
Ban::Timeout => 3 + self.number_of_timeouts * 5,
Ban::General => 2,
Ban::ErrorExit => 2 + self.number_of_error_exits * 10,
Ban::TooManyErrors => 2 + self.number_of_too_many_error_bans * 15,
Ban::Timeout => 2 + self.number_of_timeouts * 5,
}
}
}

View File

@ -307,10 +307,16 @@ async fn handle_http_request(
// Decrement count of active websockets
let old_num_websockets = GLOBALS.num_clients.fetch_sub(1, Ordering::SeqCst);
log::info!("{}: TOTAL={}, {}", peer, old_num_websockets - 1, msg);
// Ban for the appropriate duration
Globals::ban(peer.ip(), bankind).await;
let ban_seconds = Globals::ban(peer.ip(), bankind);
log::info!(
"{}: TOTAL={}, {}, ban={}s",
peer,
old_num_websockets - 1,
msg,
ban_seconds
);
}
Err(e) => {
log::error!("{}: {}", peer, e);