Stamp a traceable incident id into every kick and kill
A single choke-point in handle() gives each outgoing Kick / KillUser a short id, appends it to the reason the user sees ([#3F]), and records a searchable summary in a bounded per-node incident ring on the live network view. So a bot flood-kick, a fantasy kick, a votekick, and an operator KICK are all logged the same way, and the id in a kick reason ties back to the log entry. Exposed via NetView::search_incidents (id-exact or summary substring, newest first) for the LOGSEARCH command to come.
This commit is contained in:
parent
1f7591778f
commit
d2a8d0ef05
3 changed files with 142 additions and 2 deletions
|
|
@ -1,9 +1,12 @@
|
|||
use std::collections::{BTreeMap, HashMap, HashSet};
|
||||
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
// The read-only network view a module sees; re-exported so the engine keeps
|
||||
// naming it locally.
|
||||
pub use fedserv_api::{NetView, SeenView};
|
||||
pub use fedserv_api::{IncidentView, NetView, SeenView};
|
||||
|
||||
// The most recent moderation/action incidents kept for LOGSEARCH.
|
||||
const INCIDENT_CAP: usize = 10_000;
|
||||
|
||||
// Live network view, rebuilt from the uplink's burst each connect (ephemeral —
|
||||
// unlike the account store, which persists).
|
||||
|
|
@ -22,6 +25,33 @@ pub struct Network {
|
|||
stats: BTreeMap<String, u64>,
|
||||
// Live session count per connecting IP, for OperServ session limiting.
|
||||
sessions: HashMap<String, u32>,
|
||||
// Recent moderation/action incidents (bounded ring), for OperServ LOGSEARCH.
|
||||
incidents: VecDeque<Incident>,
|
||||
incident_seq: u64,
|
||||
}
|
||||
|
||||
// One recorded action: a short id (also stamped into the action's reason), the
|
||||
// unix time it happened, and a human summary.
|
||||
struct Incident {
|
||||
id: String,
|
||||
ts: u64,
|
||||
summary: String,
|
||||
}
|
||||
|
||||
// A short uppercase base-36 incident code from a monotonic counter.
|
||||
fn incident_code(seq: u64) -> String {
|
||||
const C: &[u8] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
if seq == 0 {
|
||||
return "0".to_string();
|
||||
}
|
||||
let mut n = seq;
|
||||
let mut out = Vec::new();
|
||||
while n > 0 {
|
||||
out.push(C[(n % 36) as usize]);
|
||||
n /= 36;
|
||||
}
|
||||
out.reverse();
|
||||
String::from_utf8(out).unwrap()
|
||||
}
|
||||
|
||||
pub struct User {
|
||||
|
|
@ -78,6 +108,31 @@ impl Network {
|
|||
v
|
||||
}
|
||||
|
||||
// Record an action in the incident log and return its short id (to stamp into
|
||||
// the action's reason). The ring is bounded, dropping the oldest.
|
||||
pub fn record_incident(&mut self, summary: String, now: u64) -> String {
|
||||
let id = incident_code(self.incident_seq);
|
||||
self.incident_seq += 1;
|
||||
self.incidents.push_back(Incident { id: id.clone(), ts: now, summary });
|
||||
while self.incidents.len() > INCIDENT_CAP {
|
||||
self.incidents.pop_front();
|
||||
}
|
||||
id
|
||||
}
|
||||
|
||||
// Incidents matching `pattern` (id-exact or summary-substring, case-
|
||||
// insensitive; empty = all), newest first, capped at `limit`.
|
||||
pub fn search_incidents(&self, pattern: &str, limit: usize) -> Vec<IncidentView> {
|
||||
let p = pattern.to_ascii_lowercase();
|
||||
self.incidents
|
||||
.iter()
|
||||
.rev()
|
||||
.filter(|i| p.is_empty() || i.id.eq_ignore_ascii_case(&p) || i.summary.to_ascii_lowercase().contains(&p))
|
||||
.take(limit)
|
||||
.map(|i| IncidentView { id: i.id.clone(), ts: i.ts, summary: i.summary.clone() })
|
||||
.collect()
|
||||
}
|
||||
|
||||
// Resolve a nick to its uid (case-insensitive). Checks real users first,
|
||||
// then our own service bots.
|
||||
pub fn uid_by_nick(&self, nick: &str) -> Option<&str> {
|
||||
|
|
@ -307,4 +362,7 @@ impl NetView for Network {
|
|||
fn sessions_over(&self, min: u32) -> Vec<(String, u32)> {
|
||||
Network::sessions_over(self, min)
|
||||
}
|
||||
fn search_incidents(&self, pattern: &str, limit: usize) -> Vec<IncidentView> {
|
||||
Network::search_incidents(self, pattern, limit)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue