//! Application state: the UI-side home for everything arriving over the //! service event channel (mock thread or privileged-service reader). use std::collections::{BTreeMap, HashMap, HashSet, VecDeque}; use crate::common::{ AccessPoint, ChannelStat, ClientInfo, Event, HandshakeCapture, IdentConfidence, MacAddr, PmkidCapture, ProbeInfo, Security, }; use crate::crypto::CrackTarget; /// Ring capacity of the on-screen log. pub const LOG_CAP: usize = 500; /// Live status of the (currently simulated) handshake cracker. #[derive(Default)] pub struct CrackerState { /// Selected capture, as an index into [`AppState::capture_descs`] / /// the aligned target vectors. Index (not description string) keyed so /// a selection never depends on regenerated display strings; the /// vectors are append-only, so new captures arriving mid-run cannot /// shift an existing selection. pub target: Option, pub wordlist: Option, pub running: bool, /// Guesses per second. pub hps: f64, /// Candidates tried so far. pub tried: u64, /// Recovered password, once found. pub found: Option, /// Rainbow-table build job in flight (RainbowProgress feeds the /// counters, RainbowBuilt clears the flag). pub rainbow_building: bool, /// PMKs written by the running build job. pub rainbow_done: u64, /// Build job throughput. pub rainbow_hps: f64, /// Worst-case candidate total of the running crack (wordlist lines / /// brute-force space / table entries), when countable. pub total: Option, /// Worst-case size of the table being built (wordlist lines). pub rainbow_total: Option, /// The currently running attack, tracked so the UI can persist a /// resume session ([`crate::store::CrackSession`]) without the /// worker's cooperation. Cleared when the run settles. pub active_attack: Option, /// A saved, interrupted run matched against a loaded capture — /// shown as a resume banner on the cracker page. pub resume: Option, } /// Saved-run banner payload (mirror of [`crate::store::CrackSession`] /// resolved to the capture's current description). #[derive(Clone)] pub struct ResumeInfo { /// Capture index (the [`AppState::capture_descs`] / target-select /// index) of the capture this run belongs to — the Resume action sets /// `CrackerState.target` from this without any string re-matching. pub target_idx: usize, /// Dropdown description of the capture this run belongs to (display /// text for the banner; selection logic keys on `target_idx`). pub target_desc: String, /// Attack definition at the saved candidate offset. pub attack: crate::attack::Attack, /// Candidates already tried when the run was saved. pub done: u64, /// Worst-case total of the saved run, when countable. pub total: Option, } /// Everything the window renders. Updated exclusively by /// [`AppState::apply`] so a real service can reuse the reduction logic. pub struct AppState { /// Whether the interface is (pretending to be) capturing in monitor mode. pub iface_running: bool, pub ifname: String, pub aps: BTreeMap, pub clients: BTreeMap, /// User-assigned names by MAC (APs and clients share one namespace; /// a MAC is unique). Persisted to `~/wifi_lab/labels.json` /// ([`crate::store`]); UI-side only. pub labels: BTreeMap, pub handshakes: Vec, pub pmkids: Vec, /// Partial M2+M3 captures (from [`Event::HandshakePartial`]): crackable /// with the identical PTK/MIC math, distinguished for provenance and /// export (`partial == true`, hashcat message-pair field `02`). /// Deduped on (AP, client) + MIC. pub partials: Vec, /// [`capture_descs`]-aligned crack targets, in ARRIVAL ORDER /// (handshakes, PMKIDs and partials interleaved as they arrived): /// `capture_targets[i]` is the [`CrackTarget`] described by /// `capture_descs()[i]`, so the cracker starts a run without /// re-parsing an hc22000 line. Append-only by construction — new /// captures never shift existing indices. capture_targets: Vec, pub log: VecDeque, pub cracker: CrackerState, /// Observed probe requests, newest last. pub probes: VecDeque, /// Recent RSSI samples per MAC (APs and clients keyed by their own /// MAC) — the Networks page renders these as sparklines. pub rssi_hist: HashMap>, /// Normalized (trim, lowercase) SSIDs observed on more than one BSSID /// with differing security — possible rogue APs. pub rogue_ssids: HashSet, /// One-shot rogue-AP alert message; taken by the app drain into a toast. pub rogue_alert: Option, /// One-shot deauth-storm alert message; taken by the app drain into a /// toast. pub deauth_alert: Option, /// Last benchmark throughput (H/s); `None` = no GPU / not run yet. pub bench_gpu: Option, pub bench_cpu: Option, /// MAC-randomization pairing ([`Event::ClientIdent`]): device-group id + /// confidence per correlated MAC. Outlives the client row (groups keep /// their members while any one of them is live). pub idents: HashMap, /// Reverse view per group: set of currently-known member MACs (live /// rows + remembered) — the Networks page shows "N MACs" per device. pub ident_groups: HashMap>, /// One-shot pairing alert ("device rotated MAC, paired into group #N"); /// taken by the app drain into a toast. pub ident_alert: Option, /// SSID (normalized) → BSSID → security seen, backing `rogue_ssids`. seen_ssids: BTreeMap>, /// ChannelStats time-series, one entry per snapshot, oldest first /// (~3 min at the scanner's 2 s cadence) — the Networks page renders /// it as a scrolling per-channel activity timeline. pub chan_timeline: VecDeque>, /// Crack H/s samples, one per [`Event::CrackProgress`] of the current /// run (cleared at run start) — the Cracker page renders it as a /// throughput curve. pub hps_hist: VecDeque, } /// Cap on [`AppState::probes`] (oldest dropped first). const PROBE_CAP: usize = 2000; /// RSSI samples kept per MAC in [`AppState::rssi_hist`]. const RSSI_CAP: usize = 60; /// ChannelStats snapshots kept in [`AppState::chan_timeline`]. const CHAN_TL_CAP: usize = 90; /// H/s samples kept in [`AppState::hps_hist`]. const HPS_CAP: usize = 240; impl AppState { pub fn new(ifname: &str) -> Self { AppState { iface_running: false, ifname: ifname.to_string(), aps: BTreeMap::new(), clients: BTreeMap::new(), labels: BTreeMap::new(), handshakes: Vec::new(), pmkids: Vec::new(), partials: Vec::new(), capture_targets: Vec::new(), log: VecDeque::with_capacity(LOG_CAP), cracker: CrackerState::default(), probes: VecDeque::with_capacity(64), rssi_hist: HashMap::new(), rogue_ssids: HashSet::new(), rogue_alert: None, deauth_alert: None, bench_gpu: None, bench_cpu: None, idents: HashMap::new(), ident_groups: HashMap::new(), ident_alert: None, seen_ssids: BTreeMap::new(), chan_timeline: VecDeque::with_capacity(CHAN_TL_CAP + 1), hps_hist: VecDeque::with_capacity(HPS_CAP + 1), } } /// Append one RSSI sample for `mac` (APs and clients keyed by their /// own MAC), capping the per-MAC history. fn note_rssi(&mut self, mac: MacAddr, rssi_dbm: i8) { let hist = self.rssi_hist.entry(mac).or_default(); if hist.len() == RSSI_CAP { hist.pop_front(); } hist.push_back(rssi_dbm); } /// Normalize an SSID for rogue comparison (trim + lowercase). fn norm_ssid(ssid: &str) -> String { ssid.trim().to_lowercase() } /// Crack target described by `capture_descs()[i]` (cloned — the cracker /// worker takes ownership). pub fn crack_target_at(&self, i: usize) -> Option { self.capture_targets.get(i).cloned() } /// The same capture as a [`crate::store::StoredCapture`] (resume /// session identity), `capture_descs()`-aligned like /// [`AppState::crack_target_at`]. Sourced from the arrival-ordered /// `capture_targets` so the index space never shifts under appends. pub fn stored_capture_at(&self, i: usize) -> Option { match self.capture_targets.get(i)? { CrackTarget::Handshake(h) => Some(crate::store::StoredCapture::Handshake(h.clone())), CrackTarget::Pmkid(p) => Some(crate::store::StoredCapture::Pmkid(p.clone())), } } pub fn push_log(&mut self, line: impl Into) { if self.log.len() == LOG_CAP { self.log.pop_front(); } self.log.push_back(line.into()); } /// Reduce one service event into state. Returns `true` when the set of /// crackable captures changed (callers rebuild dependent widgets). /// /// Does NOT sweep stale clients: at 10 Hz+ probe volumes a per-event /// sweep allocates a full `clients` snapshot every call. The sweep /// runs once per 100 ms drain tick instead — see /// [`AppState::sweep_stale_clients`], invoked by the app drain loop /// after the event batch is applied. pub fn apply(&mut self, event: Event) -> bool { self.reduce(event) } /// Backstop mirror of the scanner's 60 s client-expiry: rows learned /// before a scanner restart (the restart wipes the scanner's internal /// map, so no ClientLost can ever come for them) would linger as "up" /// forever without this. Clock-offset between the service and UI /// epochs (both `now_ms`, different process-local zero points) is a /// few seconds at most — negligible against a 60 s threshold. pub fn sweep_stale_clients(&mut self) { const UI_CLIENT_EXPIRE_MS: u64 = 60_000; let now = crate::common::now_ms(); let swept: Vec = self .clients .iter() .filter(|(_, c)| now.saturating_sub(c.last_seen_ms) > UI_CLIENT_EXPIRE_MS) .map(|(m, _)| *m) .collect(); for m in swept { self.clients.remove(&m); self.forget_ident(&m); } } /// Drop a MAC's pairing record (client row gone). fn forget_ident(&mut self, mac: &MacAddr) { if let Some((gid, _)) = self.idents.remove(mac) && let Some(set) = self.ident_groups.get_mut(&gid) { set.remove(mac); if set.is_empty() { self.ident_groups.remove(&gid); } } } fn reduce(&mut self, event: Event) -> bool { match event { Event::ApUpdated(ap) => { self.note_rssi(ap.bssid, ap.rssi_dbm); // Rogue detection: the same non-empty SSID advertised by a // *different* BSSID with *different* security settings is // the classic evil-twin smell (same security on multi-AP // deployments is normal). let norm = Self::norm_ssid(&ap.ssid); if !norm.is_empty() { let entry = self.seen_ssids.entry(norm.clone()).or_default(); if !self.rogue_ssids.contains(&norm) { let conflict = entry .iter() .find(|(b, sec)| **b != ap.bssid && **sec != ap.security); if let Some((known, sec1)) = conflict { self.rogue_ssids.insert(norm.clone()); self.rogue_alert = Some(format!( "possible rogue AP: \"{}\" on {} ({}) vs {} ({})", norm, known, sec1.label(), ap.bssid, ap.security.label() )); } } entry.insert(ap.bssid, ap.security); } self.aps.insert(ap.bssid, ap); false } Event::ApLost(bssid) => { self.aps.remove(&bssid); false } Event::ClientUpdated(mut client) => { self.note_rssi(client.mac, client.rssi_dbm); // Overlay the known pairing (the wire fields may lag or be // absent for clients learned before this process knew the // group). if let Some(&(gid, conf)) = self.idents.get(&client.mac) { client.ident_group = Some(gid); client.ident_conf = Some(conf); } self.clients.insert(client.mac, client); false } Event::ClientLost(mac) => { self.clients.remove(&mac); self.forget_ident(&mac); false } Event::ClientIdent { mac, group, confidence, } => { let rotated = self .ident_groups .get(&group) .is_some_and(|ms| ms.iter().any(|m| *m != mac)); self.idents.insert(mac, (group, confidence)); self.ident_groups.entry(group).or_default().insert(mac); // Message names the whole rotation set — the actionable fact // is "these N MACs are one radio". let n = self.ident_groups.get(&group).map_or(1, |ms| ms.len()); let msg = format!( "device paired: {mac} → group #{group} ({n} MAC{}) [{}]", if n == 1 { "" } else { "s" }, confidence.label() ); self.push_log(msg.clone()); if rotated { self.ident_alert = Some(msg); } // Live row gets the badge immediately. if let Some(c) = self.clients.get_mut(&mac) { c.ident_group = Some(group); c.ident_conf = Some(confidence); } false } Event::HandshakeProgress { ap, client, have } => { self.push_log(format!( "4-way {ap} → {client}: {}/4 messages", have.count_ones() )); false } Event::HandshakeComplete(cap) => { self.push_log(format!( "handshake captured: {} ({} ⇄ {})", cap.essid, cap.ap, cap.client )); self.capture_targets .push(CrackTarget::Handshake(cap.as_ref().clone())); self.handshakes.push(*cap); true } Event::HandshakePartial(cap) => { // Same material re-delivered (e.g. tracker drain after the // capture session already reported it) must not duplicate // the picker entry. if self .partials .iter() .any(|p| p.ap == cap.ap && p.client == cap.client && p.mic() == cap.mic()) { return false; } self.push_log(format!( "partial handshake (M2+M3) for {}: hashcat-exportable", cap.essid )); self.capture_targets .push(CrackTarget::Handshake(cap.as_ref().clone())); self.partials.push(*cap); true } Event::PmkidCaptured(cap) => { self.push_log(format!("PMKID captured: {} ({})", cap.essid, cap.ap)); self.capture_targets.push(CrackTarget::Pmkid(cap.clone())); self.pmkids.push(cap); true } Event::DeauthSent { ap, target, count } => { self.push_log(format!("deauth: {count} frames → {target} (AP {ap})")); false } Event::CrackProgress { done, hps, total } => { if self.cracker.running { self.cracker.tried = done; self.cracker.hps = hps; self.cracker.total = total; if self.hps_hist.len() == HPS_CAP { self.hps_hist.pop_front(); } self.hps_hist.push_back(hps); } false } Event::CrackFound { password } => { self.push_log(format!("PASSWORD FOUND: {password}")); self.cracker.found = Some(password); false } Event::CrackStopped { found } => { self.push_log(format!( "cracker: run ended ({})", if found { "password recovered" } else { "stopped/exhausted" } )); self.cracker.running = false; true // force dependent-widget refresh so the cracker page updates promptly } Event::CrackError { reason } => { // Backend failure (GPU device lost, dispatch timeout, // worker panic) — NOT exhaustion, NOT user stop. Log the // reason (the cracker page's recent-activity block is a // filtered view over this log, so the line mirrors there // automatically) and reset the cracker to idle like // CrackStopped, but do NOT touch the saved resume session: // the failure is recoverable and the run may be resumed. self.push_log(format!("crack FAILED (backend): {reason}")); self.cracker.running = false; true // force dependent-widget refresh so the cracker page updates promptly } Event::RainbowProgress { done, hps, total } => { if self.cracker.rainbow_building { self.cracker.rainbow_done = done; self.cracker.rainbow_hps = hps; self.cracker.rainbow_total = total; } false } Event::RainbowBuilt { path, entries } => { self.push_log(format!("rainbow: table finished: {path} ({entries} PMKs)")); self.cracker.rainbow_building = false; false } Event::Log(line) => { self.push_log(line); false } Event::Error(line) => { self.push_log(format!("ERROR: {line}")); false } Event::ChannelStats(stats) => { if self.chan_timeline.len() == CHAN_TL_CAP { self.chan_timeline.pop_front(); } self.chan_timeline.push_back(stats); false } Event::ProbeObserved(probe) => { self.note_rssi(probe.client, probe.rssi_dbm); if self.probes.len() == PROBE_CAP { self.probes.pop_front(); } self.probes.push_back(probe); false } Event::BenchmarkDone { gpu_hps, cpu_hps } => { self.bench_gpu = gpu_hps; self.bench_cpu = Some(cpu_hps); self.push_log(format!( "benchmark: gpu {} H/s, cpu {} H/s", gpu_hps .map(|h| h.to_string()) .unwrap_or_else(|| "none".to_string()), cpu_hps )); false } Event::DeauthAlert { source, target, count, } => { let msg = format!("DEAUTH STORM: {source} → {target} x{count}"); self.push_log(msg.clone()); self.deauth_alert = Some(msg); false } Event::CaptureImported(cap) => { match cap { crate::common::ImportedCapture::Handshake(h) => { self.push_log(format!( "import{} handshake: {} ({} ⇄ {})", if h.partial { "ed [partial]" } else { "ed" }, h.essid, h.ap, h.client )); self.capture_targets .push(CrackTarget::Handshake(h.as_ref().clone())); // Imported partials join the partial provenance // list (dedup, [partial] marker, hc22000-export // path) just like service-emitted partials. if h.partial { if !self .partials .iter() .any(|p| p.ap == h.ap && p.client == h.client && p.mic() == h.mic()) { self.partials.push(*h); } } else { self.handshakes.push(*h); } } crate::common::ImportedCapture::Pmkid(p) => { self.push_log(format!("imported PMKID: {} ({})", p.essid, p.ap)); self.capture_targets.push(CrackTarget::Pmkid(p.clone())); self.pmkids.push(p); } } true } } } /// Stable, unique descriptions of every crackable capture — the /// cracker's target dropdown is fed with these (index prefix keeps /// duplicates apart). Arrival-ordered ([`self.capture_targets`]), so /// appending new captures can never renumber an existing entry — the /// selection structs key on the index, not the string. Partial M2+M3 /// captures carry a `[partial]` marker so their provenance is visible /// at a glance. pub fn capture_descs(&self) -> Vec { self.capture_targets .iter() .enumerate() .map(|(i, t)| match t { CrackTarget::Handshake(hs) => format!( "#{:02} HS {}{} {} ⇄ {} kv{}", i + 1, if hs.partial { "[partial] " } else { "" }, hs.essid, hs.ap, hs.client, hs.key_ver ), CrackTarget::Pmkid(pm) => format!("#{:02} PMKID {} {}", i + 1, pm.essid, pm.ap), }) .collect() } } #[cfg(test)] mod tests { use super::*; fn stat(ch: u8, frames: u64) -> ChannelStat { ChannelStat { channel: ch, frames, beacons: 0, } } fn mac(byte: u8) -> MacAddr { MacAddr([byte; 6]) } fn partial_cap(seed: u8, essid: &str) -> HandshakeCapture { HandshakeCapture::new_partial( mac(0xaa), mac(0xbb), essid.to_string(), [seed; 32], [seed.wrapping_add(1); 32], [seed; 16], vec![1, 3, 0, 95], 2, ) } #[test] fn handshake_partial_appends_marked_and_dedups() { let mut s = AppState::new("wlan0"); let cap = partial_cap(7, "partialnet"); assert!(s.apply(Event::HandshakePartial(Box::new(cap.clone())))); // Same (AP, client) + MIC re-delivered: no duplicate picker entry. assert!(!s.apply(Event::HandshakePartial(Box::new(cap)))); assert_eq!(s.partials.len(), 1); assert!(s.partials[0].partial); let descs = s.capture_descs(); assert_eq!(descs.len(), 1); assert!(descs[0].contains("[partial]"), "desc: {}", descs[0]); // Cracker-side index space includes it (same run path as HS/PMKID). assert!(matches!( s.stored_capture_at(0), Some(crate::store::StoredCapture::Handshake(_)) )); assert_eq!( s.log.back().map(String::as_str), Some("partial handshake (M2+M3) for partialnet: hashcat-exportable") ); } #[test] fn crack_error_resets_running_and_logs_reason() { let mut s = AppState::new("wlan0"); s.cracker.running = true; assert!(s.apply(Event::CrackError { reason: "GPU device lost".to_string() })); assert!(!s.cracker.running); assert_eq!( s.log.back().map(String::as_str), Some("crack FAILED (backend): GPU device lost") ); } #[test] fn capture_index_space_is_append_stable() { let mut s = AppState::new("wlan0"); let h1 = HandshakeCapture::new( mac(1), mac(2), "alpha".to_string(), [0; 32], [1; 32], [2; 16], vec![0], 2, ); s.apply(Event::HandshakeComplete(Box::new(h1))); s.apply(Event::PmkidCaptured(PmkidCapture { ap: mac(3), client: mac(4), essid: "beta".to_string(), pmkid: [5; 16], })); let before = s.capture_descs(); assert_eq!(before.len(), 2); assert!(before[1].contains("PMKID")); // A later handshake must NOT renumber the PMKID entry: with an // index-keyed target selection, a shift would silently crack the // wrong capture. let h2 = HandshakeCapture::new( mac(5), mac(6), "gamma".to_string(), [1; 32], [2; 32], [3; 16], vec![0], 2, ); s.apply(Event::HandshakeComplete(Box::new(h2))); let after = s.capture_descs(); assert_eq!(after.len(), 3); assert_eq!(&after[..before.len()], &before[..]); assert!(matches!( s.stored_capture_at(1), Some(crate::store::StoredCapture::Pmkid(_)) )); } #[test] fn channel_timeline_keeps_snapshots_and_caps() { let mut s = AppState::new("wlan0"); for i in 0..(CHAN_TL_CAP + 5) { s.apply(Event::ChannelStats(vec![stat(1, i as u64 + 1)])); } assert_eq!(s.chan_timeline.len(), CHAN_TL_CAP); // Oldest dropped: the first surviving snapshot is frames=6. assert_eq!(s.chan_timeline.front().unwrap()[0].frames, 6); // Newest snapshot is the tail of the timeline. assert_eq!( s.chan_timeline.back().unwrap()[0].frames, (CHAN_TL_CAP + 5) as u64 ); } #[test] fn hps_history_advances_only_while_running() { let mut s = AppState::new("wlan0"); s.apply(Event::CrackProgress { done: 10, hps: 100.0, total: None, }); assert!(s.hps_hist.is_empty(), "stopped run must not feed the curve"); s.cracker.running = true; s.apply(Event::CrackProgress { done: 20, hps: 200.0, total: Some(50), }); s.apply(Event::CrackProgress { done: 30, hps: 300.0, total: Some(50), }); assert_eq!( s.hps_hist.iter().copied().collect::>(), vec![200.0, 300.0] ); assert_eq!(s.cracker.tried, 30); assert_eq!(s.cracker.total, Some(50)); } }