Respect IP ban expiry times

This commit is contained in:
timvisee
2021-11-17 18:42:26 +01:00
parent 74d772ab42
commit b168dcefde
3 changed files with 63 additions and 2 deletions

43
Cargo.lock generated
View File

@@ -225,6 +225,19 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
dependencies = [
"libc",
"num-integer",
"num-traits",
"time",
"winapi",
]
[[package]]
name = "clap"
version = "3.0.0-beta.5"
@@ -640,6 +653,7 @@ version = "0.2.0"
dependencies = [
"anyhow",
"bytes",
"chrono",
"clap",
"colored",
"derive_builder",
@@ -774,6 +788,25 @@ dependencies = [
"winapi",
]
[[package]]
name = "num-integer"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
dependencies = [
"autocfg 1.0.1",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
dependencies = [
"autocfg 1.0.1",
]
[[package]]
name = "num_cpus"
version = "1.13.0"
@@ -1242,6 +1275,16 @@ dependencies = [
"syn",
]
[[package]]
name = "time"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "tokio"
version = "1.14.0"

View File

@@ -47,6 +47,7 @@ serde = "1.0"
serde_json = "1.0"
shlex = "1.1"
thiserror = "1.0"
chrono = "0.4"
tokio = { version = "1", default-features = false, features = ["rt-multi-thread", "io-util", "net", "macros", "time", "process", "signal", "sync"] }
toml = "0.5"
version-compare = "0.1"

View File

@@ -4,11 +4,15 @@ use std::fs;
use std::net::IpAddr;
use std::path::Path;
use chrono::{DateTime, Utc};
use serde::Deserialize;
/// File name.
pub const FILE: &str = "banned-ips.json";
/// The forever expiry literal.
const EXPIRY_FOREVER: &str = "forever";
/// List of banned IPs.
#[derive(Debug, Default)]
pub struct BannedIps {
@@ -56,8 +60,21 @@ pub struct BannedIp {
impl BannedIp {
/// Check if this entry is currently banned.
pub fn is_banned(&self) -> bool {
// TODO: check expiry date here!
true
// If expiry is forever, the user is banned
if self.expires.trim().to_lowercase() == EXPIRY_FOREVER {
return true;
}
// Parse expiry time, check if it has passed
let expiry = match DateTime::parse_from_str(&self.expires, "%Y-%m-%d %H:%M:%S %z") {
Ok(expiry) => expiry,
Err(err) => {
error!(target: "lazymc", "Failed to parse ban expiry '{}', assuming still banned: {}", self.expires, err);
return true;
}
};
expiry > Utc::now()
}
}