//!//! Root view: nav rail, header bar (interface picker, privilege badge, //! monitor/capture toggle), page content, status line, and the background //! task that polls the service event channel. /// Word-wrap toast text. gpuikit toasts render their message on one line, /// so a long message (the deauth-storm alert carries two MACs) stretches /// the 360 px toast box past the window edge. 36 chars ≈ the usable width /// at text_sm once icon, padding and the close button are subtracted. fn wrap_toast_text(msg: &str) -> String { const LIMIT: usize = 36; let mut out = String::with_capacity(msg.len() + 4); let mut col = 0usize; for word in msg.split(' ') { if col > 0 && col + 1 + word.len() > LIMIT { out.push('\n'); col = 0; } else if col > 0 { out.push(' '); col += 1; } out.push_str(word); col += word.len(); // Unbreakable token longer than one line (e.g. a file path in an // error): hard-split it so it can never stretch the box. while col > LIMIT { let start = out.len() - col; let cut = start + out[start..] .char_indices() .nth(LIMIT) .map(|(i, _)| i) .unwrap_or(col); out.insert(cut, '\n'); col -= LIMIT; } } out } use std::collections::BTreeMap; use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::{Duration, Instant}; use crossbeam_channel::{Receiver, Sender}; use gpui::{ App, AppContext, Context, Entity, FocusHandle, FontWeight, InteractiveElement, IntoElement, KeyBinding, ParentElement, Render, SharedString, StatefulInteractiveElement, Styled, Window, actions, div, prelude::FluentBuilder, px, }; use gpuikit::elements::badge::badge; use gpuikit::elements::button::button; use gpuikit::elements::select::{SelectState, select}; use gpuikit::elements::table::SortDescriptor; use gpuikit::elements::toast::ToastExt; use gpuikit::input::InputState; use gpuikit::layout::{h_stack, v_stack}; use gpuikit::theme::{ActiveTheme, Themeable}; use gpuikit::traits::disableable::Disableable; use crate::common::{AccessPoint, Cmd, Event, MacAddr}; use crate::service::ServiceClient; use crate::ui::{attacks, cracker, log as log_view, networks, state::AppState}; /// How often the UI drains the service event channel. const EVENT_POLL: Duration = Duration::from_millis(100); actions!( wifi_lab, [ /// ctrl-q: quit the app (the release hook stops UI-process jobs /// and shuts the service down). QuitApp, /// ctrl-1: switch to the Networks page. ShowNetworksPage, /// ctrl-2: switch to the Attacks page. ShowAttacksPage, /// ctrl-3: switch to the Cracker page. ShowCrackerPage, /// ctrl-4: switch to the Log page. ShowLogPage, ] ); /// Global (window-level) keybindings: quit + page switches. Registered /// once at app start (see [`crate::ui::run_app`]); the matching /// `.on_action` handlers live on the root view so the bindings fire /// regardless of which page or control is focused. No per-page bindings /// here — pages register their own (e.g. the cracker page's Enter). pub fn bind_app_keys(cx: &mut App) { cx.bind_keys([ KeyBinding::new("ctrl-q", QuitApp, None), KeyBinding::new("ctrl-1", ShowNetworksPage, None), KeyBinding::new("ctrl-2", ShowAttacksPage, None), KeyBinding::new("ctrl-3", ShowCrackerPage, None), KeyBinding::new("ctrl-4", ShowLogPage, None), ]); } /// Left nav destinations. #[derive(Clone, Copy, PartialEq, Eq)] pub enum Page { Networks, Attacks, Cracker, Log, } impl Page { const ALL: [Page; 4] = [Page::Networks, Page::Attacks, Page::Cracker, Page::Log]; fn label(self) -> &'static str { match self { Page::Networks => "Networks", Page::Attacks => "Attacks", Page::Cracker => "Cracker", Page::Log => "Log", } } /// Startup page override (`WIFI_LAB_PAGE=attacks`) — dev/visual-verification /// hook; unknown values fall through to Networks. fn from_name(name: &str) -> Option { Page::ALL .iter() .copied() .find(|p| p.label().eq_ignore_ascii_case(name)) } } pub struct AppRoot { pub state: AppState, /// Service event channel (mock thread or service reader thread). pub rx: Receiver, /// UI-triggered actions are injected into the same pipeline. pub tx: Sender, pub page: Page, /// UI-process privilege ([`crate::iface::is_root`]); the header badge /// combines this with `service` presence to show the SERVICE's /// privilege state — any running service is root (direct spawn or /// pkexec). pub is_root: bool, /// Privileged radio service client (owns the child process). `None` /// in mock mode or before the first "Start capture". Kept across /// stop/start cycles. NOT involved in cracking — crack jobs are /// UI-process threads tracked via [`AppRoot::crack_stop`]. pub service: Option, /// Mock data mode (`--mock` / `WIFI_LAB_MOCK=1`): never touches devices. pub is_mock: bool, /// Target BSSID picked on the Networks page (AP row or client row). pub selected: Option, /// Last-known AP row for [`AppRoot::selected`], snapshotted at click /// time and refreshed whenever the live record updates. The Attacks /// page builds its target card from this even when the live row is /// momentarily absent (host-capture mode never re-scans: a target /// picked before the lock may have no AP row at all). pub selected_ap: Option, /// Client station picked on the Networks page; deauth targets it /// instead of the broadcast address when set (AP stays `selected`). pub selected_client: Option, /// Message bitmask (see [`Event::HandshakeProgress`]) per in-flight /// 4-way capture, keyed by (AP, client). pub hs_progress: BTreeMap<(MacAddr, MacAddr), u8>, /// "Spinner shown" flags for attack buttons awaiting results. pub attacking_handshake: bool, pub attacking_pmkid: bool, /// Channels the scanner is parked on for attacks (restricted StartScan /// plan; several entries cover dual-band targets). `None` = full hop /// list. Blanket spectrum-wide hopping makes capture/PMKID/deauth /// mostly deaf and blind to one band. pub parked_channels: Option>, /// Stop flag of the running crack job (UI-process thread, see /// [`crate::attack`]); flipped by Stop and on window close. pub crack_stop: Option>, /// Stop flag of the running rainbow-table build job. pub rainbow_stop: Option>, /// Throttle for resume-session writes (see [`AppRoot::maybe_save_crack_session`]). pub last_session_save: Instant, /// Toasts queued by the event loop (no Window there) — shown on the /// next render pass. Capped (oldest dropped) so a burst of alerts /// can't bury the window. pub pending_toast: std::collections::VecDeque, /// NetworkManager seen active when capture started (fights channels). pub nm_active: bool, /// Used to invalidate stale cracker simulation ticks after Stop/re-Start. pub crack_epoch: u64, pub ap_sort: Option, pub client_sort: Option, pub focus_handle: FocusHandle, pub iface_select: Entity>, /// Cracker target picker; the VALUE is the stable index into /// `state.capture_descs()` (mirrors [`state::CrackerState::target`]), /// the LABEL is the display description. pub target_select: Entity>, pub wordlist_input: Entity, /// Cracker attack-mode picker ([`crate::attack::Attack`]: wordlist / /// bruteforce / rainbow). pub attack_select: Entity>, /// Selected attack mode value ("wordlist" | "bruteforce" | "rainbow"). pub attack_mode: String, /// Bruteforce preset chips ([`crate::attack::charset`]): unioned with /// the custom field into the effective charset. pub cs_lower: bool, pub cs_upper: bool, pub cs_digits: bool, pub cs_special: bool, /// Bruteforce mode inputs: charset and min/max word length. pub charset_input: Entity, pub len_min_input: Entity, pub len_max_input: Entity, /// Rainbow mode: PMK table path; the build job writes it, the run /// reads it. pub rainbow_input: Entity, pub deauth_input: Entity, /// Networks-page label editor; the labels themselves live in /// `state.labels` ([`crate::store`]). pub label_input: Entity, /// Log page body: read-only multiline input so log text is /// mouse-selectable / Ctrl-C-copyable (plain text widgets aren't). pub log_input: Entity, /// Rules-attack mode: hashcat-style rule file path. pub rules_input: Entity, /// Crack-time estimator candidate (how strong might the password be). pub strength_input: Entity, /// Hybrid mode: appended-suffix length bounds. pub hyb_min_input: Entity, pub hyb_max_input: Entity, /// Bruteforce charset ordering: most-common characters first. pub freq_order: bool, /// A throughput benchmark run is in flight. pub bench_running: bool, /// A capture-file import is in flight. pub import_busy: bool, /// Target count behind the "Deauth all clients (N)" button label /// (0 = hidden); computed during attack-page render prep. pub deauth_all_targets: usize, /// Saved cracking presets + recent wordlists (~/wifi_lab/profiles.json). pub profiles: crate::store::Profiles, } impl AppRoot { pub fn new( window: &mut Window, cx: &mut Context, tx: Sender, rx: Receiver, is_mock: bool, ifaces: Vec, selected_iface: String, ) -> Self { let view = cx.entity(); let iface_select = cx.new(|_cx| { SelectState::new( select( "iface-select", "Interface", ifaces .iter() .map(|i| (i.clone(), i.clone())) .collect::>(), ) .selected(selected_iface.clone()) .on_change({ let view = view.clone(); move |ifname, _window, cx| { view.update(cx, |app, cx| { app.state.ifname = ifname.clone(); app.state.push_log(format!("iface: selected {ifname}")); cx.notify(); }); } }), ) }); let wordlist_input = cx.new(InputState::new_singleline); wordlist_input.update(cx, |input, cx| { input.set_content("/usr/share/wordlists/rockyou.txt", cx); }); let deauth_input = cx.new(InputState::new_singleline); deauth_input.update(cx, |input, cx| { input.set_content("16", cx); }); let label_input = cx.new(InputState::new_singleline); let log_input = cx.new(|cx| InputState::new_multiline(cx).read_only(true)); let attack_select = cx.new(|_cx| { SelectState::new( select( "cracker-attack", "Attack", vec![ ("wordlist".to_string(), "wordlist".to_string()), ("rules".to_string(), "rules".to_string()), ("hybrid".to_string(), "hybrid".to_string()), ("bruteforce".to_string(), "bruteforce".to_string()), ("rainbow".to_string(), "rainbow".to_string()), ], ) .selected("wordlist".to_string()) .on_change({ let view = view.clone(); move |mode, _window, cx| { view.update(cx, |app, cx| { app.attack_mode = mode; cx.notify(); }); } }), ) }); let charset_input = cx.new(InputState::new_singleline); let len_min_input = cx.new(InputState::new_singleline); len_min_input.update(cx, |input, cx| input.set_content("8", cx)); let len_max_input = cx.new(InputState::new_singleline); len_max_input.update(cx, |input, cx| input.set_content("10", cx)); let rainbow_input = cx.new(InputState::new_singleline); let rules_input = cx.new(InputState::new_singleline); rules_input.update(cx, |input, cx| { input.set_content("/path/to/rules (e.g. best64.rule)", cx); }); let strength_input = cx.new(InputState::new_singleline); let hyb_min_input = cx.new(InputState::new_singleline); hyb_min_input.update(cx, |input, cx| input.set_content("1", cx)); let hyb_max_input = cx.new(InputState::new_singleline); hyb_max_input.update(cx, |input, cx| input.set_content("2", cx)); // Focus the root view at startup so the window-level keybindings // (ctrl-q, ctrl-1..4) dispatch through it before any control // grabs focus. Rendered with `.track_focus` on the root div. let root_focus = cx.focus_handle(); window.focus(&root_focus, cx); let mut this = AppRoot { state: AppState::new(&selected_iface), rx, tx, page: std::env::var("WIFI_LAB_PAGE") .ok() .and_then(|p| Page::from_name(&p)) .unwrap_or(Page::Networks), is_root: crate::iface::is_root(), service: None, is_mock, selected: None, selected_ap: None, selected_client: None, hs_progress: BTreeMap::new(), attacking_handshake: false, attacking_pmkid: false, parked_channels: None, crack_stop: None, rainbow_stop: None, last_session_save: Instant::now(), pending_toast: std::collections::VecDeque::new(), nm_active: false, crack_epoch: 0, ap_sort: None, client_sort: None, focus_handle: root_focus, iface_select, target_select: cx.new(|_cx| { let empty: Vec<(usize, String)> = Vec::new(); SelectState::new(select("cracker-target", "Crack target", empty)) }), wordlist_input, attack_select, attack_mode: "wordlist".to_string(), cs_lower: false, cs_upper: false, cs_digits: true, cs_special: false, charset_input, len_min_input, len_max_input, rainbow_input, deauth_input, label_input, log_input, rules_input, strength_input, hyb_min_input, hyb_max_input, freq_order: true, bench_running: false, import_busy: false, deauth_all_targets: 0, // Mock mode is hermetic: it must never read the user's real // on-disk state (labels, captures, profiles) — it exists for // development and screenshots, and showing real network names // there defeats the point. profiles: if is_mock { crate::store::Profiles::default() } else { match crate::store::load_profiles() { Ok(p) => p, Err(e) => { eprintln!("profiles: load failed, starting empty: {e:#}"); crate::store::Profiles::default() } } }, }; if !is_mock { match crate::store::load() { Ok(labels) => { let n = labels.len(); this.state.labels = labels; this.state .push_log(format!("labels: {n} loaded from ~/wifi_lab/labels.json")); } Err(e) => this .state .push_log(format!("labels: load failed, starting empty: {e:#}")), } } // Restore previous captures so the cracker can keep working across // restarts without re-running attacks. if !is_mock { match crate::store::load_captures() { Ok(caps) => { let n = caps.len(); for cap in caps { match cap { crate::store::StoredCapture::Handshake(h) => { // Route restored partials through the // partial path so they land in // `state.partials` with the [partial] // picker marker (append-only restore keeps // stored index order). if h.partial { this.state.apply(Event::HandshakePartial(Box::new(h))); } else { this.state.apply(Event::HandshakeComplete(Box::new(h))); } } crate::store::StoredCapture::Pmkid(p) => { this.state.apply(Event::PmkidCaptured(p)); } } } if n > 0 { this.state.push_log(format!( "captures: {n} restored from ~/wifi_lab/captures.jsonl" )); } } Err(e) => this .state .push_log(format!("captures: restore failed: {e:#}")), } } this.rebuild_target_select(cx); if !is_mock { this.restore_crack_session(); } this.state.push_log(if is_mock { "wifi_lab UI started (mock backend)".to_string() } else { format!( "wifi_lab UI started (REAL backend, iface {selected_iface}) — Start capture spawns the privileged service (polkit) and brings up monitor mode" ) }); cx.on_release(|this, _cx| { // Window closed → root view released → stop UI-process jobs // (crack + rainbow build) and shut the service down (it stops // workers, restores managed mode, exits). for flag in [this.crack_stop.take(), this.rainbow_stop.take()] .into_iter() .flatten() { flag.store(true, Ordering::Relaxed); } if let Some(svc) = this.service.take() { svc.shutdown(); } }) .detach(); if !this.is_root && !this.is_mock { cx.toast("not root — polkit will ask for authentication when you start capture") .show(window, cx); } // Poll the service event channel: wait on the background executor, // then reduce everything queued into state on the main thread. The // service reader thread never touches gpui types. cx.spawn(async move |this, cx| { loop { cx.background_executor().timer(EVENT_POLL).await; match this.update(cx, |app, cx| app.drain_events(cx)) { Ok(()) => {} // Entity gone (window closed) — stop polling. Err(_) => break, } } }) .detach(); window.focus(&this.focus_handle, cx); this } /// Header "Start/Stop capture" behaviour. /// /// Real mode: lazily spawn the privileged service (polkit-elevated /// when needed) which switches the interface to monitor mode and runs /// the scanner; Stop only ends the scan session — the service stays /// alive for the cracker page and later scans. Mock mode: pure UI /// flips (the mock generator runs independently). pub fn toggle_monitor(&mut self, cx: &mut Context) { if self.is_mock { self.state.iface_running = !self.state.iface_running; let on = self.state.iface_running; self.state.push_log(format!( "iface {}: monitor mode {} (mock)", self.state.ifname, if on { "on" } else { "off" } )); cx.notify(); return; } if self.state.iface_running { // STOP: end the scan session but KEEP the service alive — the // cracker needs it, and a later Start reuses it. if let Some(svc) = &self.service { self.state .push_log("ui: stopping capture, service restores managed mode…"); svc.stop_scan(); } // Stopping also kills every attack worker — un-spin buttons. self.state.iface_running = false; self.parked_channels = None; self.attacking_handshake = false; self.attacking_pmkid = false; cx.notify(); } else { // NetworkManager fight: pin-to-connected-channel makes ONLY the // associated network (e.g. testnet) visible during the hop. if crate::iface::networkmanager_active() { self.nm_active = true; self.state.push_log( "ui: WARNING NetworkManager is active — it pins the card to the connected \ network's channel; you will likely see ONLY that SSID. Use \"Stop NM\" in the header." .to_string(), ); self.queue_toast( "NetworkManager is active — only your connected network may be visible; stop it from the header", ); } let ifname = self.state.ifname.clone(); if let Some(svc) = &self.service { // Service already up (from an earlier session): just ask // for a fresh scan session. svc.cmd(&Cmd::StartScan { ifname: ifname.clone(), channels: None, spool: Some(crate::store::data_dir().join("spool.pcap")), fresh_spool: true, }); self.state.iface_running = true; self.parked_channels = None; self.state .push_log(format!("ui: capture started on {ifname}")); cx.notify(); return; } match ServiceClient::start(&ifname, self.tx.clone()) { Ok(svc) => { self.service = Some(svc); self.state.iface_running = true; self.state .push_log(format!("ui: capture started on {ifname}")); } Err(e) => { self.state .push_log(format!("ERROR: start capture on {ifname}: {e:#}")); self.queue_toast(format!("start capture failed: {e:#}")); } } cx.notify(); } } /// Stop NetworkManager via the privileged service so it stops pinning /// the radio to the connected network's channel. pub fn stop_nm(&mut self, cx: &mut Context) { match &self.service { Some(svc) => { svc.cmd(&Cmd::StopNetworkManager); self.state .push_log("ui: asking service to stop NetworkManager…"); } None => { self.state.push_log( "ui: NM stop needs the privileged service — Start capture first".to_string(), ); } } cx.notify(); } /// Queue a toast for the next render pass (the event loop has no /// Window). Capped at 3 — oldest dropped first. pub fn queue_toast(&mut self, msg: impl Into) { if self.pending_toast.len() == 3 { self.pending_toast.pop_front(); } self.pending_toast .push_back(wrap_toast_text(&msg.into()).into()); } /// Drain every queued event, track handshake progress, refresh widgets /// derived from the capture set. fn drain_events(&mut self, cx: &mut Context) { let mut changed = false; let mut captures_changed = false; while let Ok(event) = self.rx.try_recv() { changed = true; match &event { Event::HandshakeProgress { ap, client, have } => { self.hs_progress.insert((*ap, *client), *have); } Event::HandshakeComplete(cap) => { self.hs_progress.remove(&(cap.ap, cap.client)); self.attacking_handshake = false; // Mock captures are fabrications — never write them into // the real capture store. if !self.is_mock && let Err(e) = crate::store::append_capture( &crate::store::StoredCapture::Handshake(cap.as_ref().clone()), ) { self.state.push_log(format!("capture save FAILED: {e:#}")); } } Event::HandshakePartial(cap) => { self.hs_progress.remove(&(cap.ap, cap.client)); self.attacking_handshake = false; // Partial M2+M3 material is hashcat-exportable crack // material — persist it exactly like a complete // capture (the `partial` flag rides serde). if !self.is_mock && let Err(e) = crate::store::append_capture( &crate::store::StoredCapture::Handshake(cap.as_ref().clone()), ) { self.state.push_log(format!("capture save FAILED: {e:#}")); } } Event::PmkidCaptured(cap) => { self.attacking_pmkid = false; if !self.is_mock && let Err(e) = crate::store::append_capture( &crate::store::StoredCapture::Pmkid(cap.clone()), ) { self.state.push_log(format!("capture save FAILED: {e:#}")); } } // Service child died (EOF seen by the reader thread): // un-flip every running flag and drop the client so the // next Start spawns a fresh, alive service. Event::Error(line) if line == "service terminated" => { self.attacking_handshake = false; self.attacking_pmkid = false; self.state.iface_running = false; // Crack jobs are independent UI-process threads — // `state.cracker.running` stays as the worker left it. if let Some(svc) = self.service.take() { svc.shutdown(); // reaps the corpse, restores iface } } // Any worker error, or a terminal log from an attack // worker (prefixed by the service; scanner logs are // excluded), un-spins attack buttons so the UI is never // stuck "working…". Event::Error(_) => { self.attacking_handshake = false; self.attacking_pmkid = false; } // Terminal attack-worker log lines un-spin the buttons. // NOT a generic prefix match: "handshake: listening for // 4-way…" fires right at worker start and would instantly // kill the spinner while the worker runs. Event::Log(line) if line.starts_with("handshake: capture ended") || line.starts_with("pmkid: no PMKID") => { self.attacking_handshake = false; self.attacking_pmkid = false; } // Service-side NM result: `nm: stopped` / `nm: stop failed …`. Event::Log(line) if line.starts_with("nm:") => { if line == "nm: stopped" { self.nm_active = false; self.queue_toast( "NetworkManager stopped — all networks become visible as the hopper cycles", ); } } Event::CrackProgress { .. } => { self.maybe_save_crack_session(); } Event::CrackStopped { found } => { self.crack_stop = None; self.finalize_crack_session(*found); } // Backend failure (GPU lost, dispatch timeout, worker // panic): settle like a user-stop with room left — save // the resume position and arm the banner — but NEVER // clear the saved session; the run is resumable. The // AppState side (log line, running=false, activity // mirror) happens in `apply` below. Event::CrackError { .. } => { self.crack_stop = None; self.last_session_save = Instant::now(); self.save_crack_session(); self.state.cracker.active_attack = None; } Event::RainbowBuilt { .. } => { self.rainbow_stop = None; } Event::CrackFound { password } => { self.queue_toast(format!("password recovered: {password}")); // Best-effort desktop notification; headless/no-D-Bus // just drops the error. let _ = notify_rust::Notification::new() .summary("wifi_lab: password found") .body(&format!("password recovered: {password}")) .show(); } Event::CaptureImported(cap) if !self.is_mock => { let stored = match cap { crate::common::ImportedCapture::Handshake(h) => { crate::store::StoredCapture::Handshake(h.as_ref().clone()) } crate::common::ImportedCapture::Pmkid(p) => { crate::store::StoredCapture::Pmkid(p.clone()) } }; if let Err(e) = crate::store::append_capture(&stored) { self.state .push_log(format!("import: capture save FAILED: {e:#}")); } } Event::BenchmarkDone { .. } => { self.bench_running = false; } _ => {} } captures_changed |= self.state.apply(event); if let Some(m) = self.state.deauth_alert.take() { self.queue_toast(m); } if let Some(m) = self.state.rogue_alert.take() { self.queue_toast(m); } if let Some(m) = self.state.ident_alert.take() { self.queue_toast(m); } } // Stale-client backstop sweep, ONCE per drain tick (not per // event — the per-event form allocated a full `clients` snapshot // at 10 Hz+ probe volumes). 100 ms cadence is far tighter than // the 60 s expiry, so ClientLost-equivalent removal stays // observably identical. self.state.sweep_stale_clients(); // Keep the target snapshot in lockstep with the live AP row (a // click-time-only snapshot goes stale: channel jumps, beacon // counts, security updates). Also self-heals after service // restarts, when only the live row exists. if let Some(live) = self.selected.and_then(|b| self.state.aps.get(&b).cloned()) { self.selected_ap = Some(live); } // Mock-only convenience: pre-select the first heard AP so the // Attacks page shows its target card without a manual click — // invaluable for headless UI verification. Never in real mode. if self.is_mock && self.selected.is_none() && let Some((&b, _)) = self.state.aps.first_key_value() { self.selected = Some(b); self.selected_ap = self.state.aps.get(&b).cloned(); } if captures_changed { self.rebuild_target_select(cx); } if changed { cx.notify(); } } /// Persist the interrupted crack run (resume file), throttled to one /// write per 2 s of progress events. UI-thread only (called from the /// event drain). fn maybe_save_crack_session(&mut self) { if self.last_session_save.elapsed() < Duration::from_secs(2) { return; } self.last_session_save = Instant::now(); self.save_crack_session(); } /// Write the current run to ~/wifi_lab/crack-session.json. No-op when /// nothing resumable is in flight (mock runs never persist). fn save_crack_session(&mut self) { let (Some(attack), Some(idx)) = ( self.state.cracker.active_attack.clone(), self.state.cracker.target, ) else { return; }; let done = self.state.cracker.tried; if done == 0 { return; } // The target is already the capture index — no description-string // round trip (a rebuilt index space would never mis-key the run). let Some(capture) = self.state.stored_capture_at(idx) else { return; }; let total = self.state.cracker.total; let session = crate::store::CrackSession::new(capture, attack, done, total); if let Err(e) = crate::store::save_session(&session) { self.state .push_log(format!("cracker: session save failed: {e:#}")); return; } // The just-saved run is resumable right now (same session or the // next start): keep the banner in sync. let target_desc = self .state .capture_descs() .into_iter() .nth(idx) .unwrap_or_default(); self.state.cracker.resume = Some(crate::ui::state::ResumeInfo { target_idx: idx, target_desc, attack: session.attack, done, total, }); } /// Settle the session file when a run ends: found/exhausted → delete /// (nothing left to resume); stopped mid-way → final save + banner. fn finalize_crack_session(&mut self, found: bool) { let done = self.state.cracker.tried; let total = self.state.cracker.total; let exhausted = !found && total.is_some_and(|t| done >= t); if found || exhausted { if let Err(e) = crate::store::clear_session() { self.state .push_log(format!("cracker: session clear failed: {e:#}")); } self.state.cracker.resume = None; } else if self.state.cracker.active_attack.is_some() { // User-stopped (or worker-ended) with room left: persist the // position — this is the whole point of the feature. self.last_session_save = Instant::now(); self.save_crack_session(); } self.state.cracker.active_attack = None; } /// Match a saved session against the loaded captures on startup: /// a match arms the resume banner on the cracker page. fn restore_crack_session(&mut self) { let session = match crate::store::load_session() { Ok(Some(s)) => s, Ok(None) => return, Err(e) => { self.state .push_log(format!("cracker: saved session unreadable: {e:#}")); return; } }; let descs = self.state.capture_descs(); let hit = descs .iter() .enumerate() .find(|(i, _)| self.state.stored_capture_at(*i).as_ref() == Some(&session.capture)); match hit { Some((idx, d)) => { let done = session.done; self.state.cracker.resume = Some(crate::ui::state::ResumeInfo { target_idx: idx, target_desc: d.clone(), attack: session.attack, done, total: session.total, }); self.state.push_log(format!( "cracker: resumable saved run restored ({d}, {done} tried)" )); } None => self .state .push_log("cracker: saved session ignored — its capture is gone".to_string()), } } /// The cracker target dropdown mirrors `state.capture_descs()`; the /// option VALUES are the stable capture indices (the description /// strings are labels only, so selection never keys on regenerated /// display text). Indices are append-only (`capture_descs` is /// arrival-ordered), so a live selection survives new captures /// arriving mid-run. With no captures the select is disabled so its /// (empty) popup cannot open. pub fn rebuild_target_select(&mut self, cx: &mut Context) { let descs = self.state.capture_descs(); let n = descs.len(); // Keep the current choice while its index is still in range. let keep = self.state.cracker.target.take().filter(|&i| i < n); let view = cx.entity(); let mut builder = select( "cracker-target", "Crack target", descs.into_iter().enumerate().collect::>(), ) .placeholder(if n == 0 { "no captures yet — capture a handshake or PMKID first" } else { "Select a capture…" }) .disabled(n == 0) .on_change(move |idx, _window, cx| { view.update(cx, |app, cx| { app.state.cracker.target = Some(idx); cx.notify(); }); }); if let Some(cur) = keep { builder = builder.selected(cur); self.state.cracker.target = Some(cur); } self.target_select = cx.new(|_cx| SelectState::new(builder)); } /// Keyboard page switch (ctrl-1..4) — the same destination set the /// nav-rail clicks use. fn show_page(&mut self, page: Page, cx: &mut Context) { self.page = page; cx.notify(); } fn nav_item(&self, page: Page, view: &Entity, cx: &App) -> gpui::Stateful { let theme = cx.theme().clone(); let active = self.page == page; let view = view.clone(); div() .id(ElementKey::Nav(page as u8).id()) .px_3() .py_2() .rounded_md() .cursor_pointer() .when(active, |el| el.bg(theme.accent_bg())) .when(!active, |el| { let hover = theme.accent_bg().alpha(0.35); el.hover(move |style| style.bg(hover)) }) .text_sm() .child(page.label()) .on_click(move |_, _window, cx| { view.update(cx, |app, cx| { app.page = page; cx.notify(); }); }) } fn header(&self, view: &Entity, cx: &App) -> gpui::Div { let theme = cx.theme().clone(); let running = self.state.iface_running; // Status of the SERVICE process (the thing that actually touches // the radio): any running service is root — spawned directly when // the UI is root, via pkexec (root by definition) otherwise. let privilege = if self.is_mock { badge("not root (mock)").secondary() } else if self.service.is_some() { badge("service: root").secondary() } else if self.is_root { badge("ui: root · service not started").secondary() } else { badge("ui: user · service not started").secondary() }; let toggle = button( "monitor-toggle", if running { "Stop capture" } else if self.is_mock { "Start capture (mock)" } else { "Start capture (monitor)" }, ) // Stopping capture (or starting a new one) while a crack streams // would kill the crack job: radio access is exclusive. .disabled(!self.is_mock && self.state.cracker.running) .on_click({ let view = view.clone(); move |_, _window, cx| { view.update(cx, |app, cx| app.toggle_monitor(cx)); } }); let nm_badge = (self.nm_active && !self.is_mock) .then(|| badge("NM active — channels pinned").destructive()); let nm_btn = (self.nm_active && !self.is_mock).then(|| { button("stop-nm", "Stop NM").on_click({ let view = view.clone(); move |_, _window, cx| { view.update(cx, |app, cx| app.stop_nm(cx)); } }) }); h_stack() .w_full() .px_4() .py_2() .gap_3() .items_center() .border_b_1() .border_color(theme.border()) .bg(theme.surface()) .child( div() .text_lg() .font_weight(FontWeight::BOLD) .child("wifi_lab"), ) .child(privilege) .child(div().w(px(220.)).child(self.iface_select.clone())) .children(nm_badge) .children(nm_btn) .child(toggle) .child(div().flex_1()) .child( div() .text_xs() .text_color(theme.fg_muted()) .child(SharedString::from(format!( "channel hopper active · {}", self.state.ifname ))), ) } fn status_line(&self, cx: &App) -> gpui::Div { let theme = cx.theme().clone(); div() .w_full() .px_4() .py_1() .border_t_1() .border_color(theme.border()) .bg(theme.surface()) .text_xs() .text_color(theme.fg_muted()) .child(format!( "{} APs · {} clients · {} handshakes · {} PMKIDs · {} · capture {} · {}", self.state.aps.len(), self.state.clients.len(), self.state.handshakes.len(), self.state.pmkids.len(), self.state.ifname, if self.state.iface_running { "running" } else { "stopped" }, if self.is_mock { "MOCK DATA" } else { "LIVE" } )) } } /// Stable element ids that aren't string keys. enum ElementKey { Nav(u8), } impl ElementKey { fn id(self) -> SharedString { match self { ElementKey::Nav(n) => SharedString::from(format!("nav-{n}")), } } } impl Render for AppRoot { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { let theme = cx.theme().clone(); let view = cx.entity(); // Flush toasts queued by the event-drain loop (which has no Window). while let Some(msg) = self.pending_toast.pop_front() { cx.toast(msg).show(_window, cx); } let nav = v_stack() .w(px(170.)) .h_full() .gap_1() .p_2() .border_r_1() .border_color(theme.border()) .bg(theme.surface_secondary()) .children( Page::ALL .into_iter() .map(|page| self.nav_item(page, &view, cx)), ); let content = match self.page { Page::Networks => networks::render(self, &view, _window, cx), Page::Attacks => attacks::render(self, &view, _window, cx), Page::Cracker => cracker::render(self, &view, _window, cx), Page::Log => log_view::render(self, &view, _window, cx), }; // Window-level shortcuts (see [`bind_app_keys`]): handlers on the // root div catch the actions wherever focus sits inside the // window; `.track_focus` keeps the startup focus on the root. div() .flex() .size_full() .track_focus(&self.focus_handle) .on_action(cx.listener(|_this, _: &QuitApp, _window, cx| cx.quit())) .on_action(cx.listener(|this, _: &ShowNetworksPage, _window, cx| { this.show_page(Page::Networks, cx); })) .on_action(cx.listener(|this, _: &ShowAttacksPage, _window, cx| { this.show_page(Page::Attacks, cx); })) .on_action(cx.listener(|this, _: &ShowCrackerPage, _window, cx| { this.show_page(Page::Cracker, cx); })) .on_action(cx.listener(|this, _: &ShowLogPage, _window, cx| { this.show_page(Page::Log, cx); })) .bg(theme.bg()) .text_color(theme.fg()) .child(nav) .child( v_stack() .flex_1() .h_full() .min_w_0() .child(self.header(&view, cx)) .child(div().flex_1().min_h_0().child(content)) .child(self.status_line(cx)), ) // Toast overlay lives at the root so it floats over any page. .child(cx.toast_manager().clone()) } } #[cfg(test)] mod tests { use super::wrap_toast_text; #[test] fn toast_wrap_keeps_lines_within_box() { let msg = "DEAUTH STORM: aa:bb:cc:dd:ee:ff → ff:ff:ff:ff:ff:ff x64"; let wrapped = wrap_toast_text(msg); assert!( wrapped.lines().all(|l| l.chars().count() <= 36), "line over toast width:\n{wrapped}" ); // Same words, same order — wrapping only inserts breaks. assert_eq!( wrapped.split_whitespace().collect::>(), msg.split_whitespace().collect::>() ); } #[test] fn toast_wrap_splits_unbreakable_tokens() { let long = format!("error: couldn't open {}", "/very/long/path/".repeat(4)); for line in wrap_toast_text(&long).lines() { assert!(line.chars().count() <= 36, "overlong line: {line:?}"); } } #[test] fn toast_wrap_leaves_short_messages_alone() { let msg = "captures: 3 restored"; assert_eq!(wrap_toast_text(msg), msg); } }