CH-J Toolsserverhantering över SSH
Menyn
Publicerad källa

CH-J Tools

Bläddra i kataloger och filer för en specifik programutgåva.

Ladda ner källa ZIP
CH-J / Server Manager 1704 filer
0.1.2-beta Tillagd: · Byggd:
lock.rs Tillagd: Datum inte noterat 2,2 KB · 60 linjer
Kod Ladda ner fil
1//! Vault lock controller (port of `security/vaultLockController.js`): inactivity
2//! auto-lock, optional lock on focus loss, and the ordered lock sequence (key wiped
3//! synchronously first, then module windows, diagnostics and SSH sessions).
5use crate::core::app::Core;
6use crate::core::vault::VaultStatus;
7use parking_lot::Mutex;
8use serde_json::{json, Value};
9use std::time::{Duration, Instant};
11pub struct LockController {
12 last_activity: Mutex<Instant>,
13 locking: tokio::sync::Mutex<()>,
16impl Default for LockController {
17 fn default() -> Self {
18 Self { last_activity: Mutex::new(Instant::now()), locking: tokio::sync::Mutex::new(()) }
19 }
22impl LockController {
23 pub fn activity(&self) {
24 *self.last_activity.lock() = Instant::now();
25 }
27 pub fn idle_for(&self) -> Duration {
28 self.last_activity.lock().elapsed()
29 }
31 pub async fn check(&self, core: &Core) {
32 let minutes = core.config.get()["security"]["autoLockMinutes"].as_u64().unwrap_or(0);
33 if minutes > 0 && core.vault.is_unlocked() && self.idle_for() >= Duration::from_secs(minutes * 60) {
34 self.lock(core, "inactivity").await;
35 }
36 }
38 pub async fn lost_focus(&self, core: &Core) {
39 if core.config.get()["security"]["lockOnBlur"].as_bool() == Some(true) && core.vault.is_unlocked() {
40 self.lock(core, "focus-loss").await;
41 }
42 }
44 pub async fn lock(&self, core: &Core, reason: &str) -> VaultStatus {
45 let _guard = self.locking.lock().await;
46 core.services.biometrics.cancel();
47 if reason != "manual" {
48 core.services.biometrics.arm_auto_unlock();
49 }
50 // Clear the key synchronously, before waiting for potentially slow SSH cleanup.
51 let status = core.vault.lock();
52 core.services.modules.close_all(core);
53 core.services.diagnostics.stop_all();
54 core.services.sessions.disconnect_all(&format!("vault-{reason}")).await;
55 let mut payload: Value = serde_json::to_value(&status).unwrap_or_else(|_| json!({}));
56 payload["reason"] = Value::String(reason.to_string());
57 core.emit_main("vault:locked", payload);
58 status
59 }
SHA-256f1c8d4e9ccf09b9a9a88ebf2807a7a5d5a425dc18eea3f016c7bff072974a913

CH-J Proprietary Software License 1.14 · Källan finns under CH-J Proprietary Software License 1.14. Dess tillgänglighet ändrar inte licensvillkoren eller ger ytterligare rättigheter.

Arkiv SHA-256ac8fe790f4ba60fc098f8c985fc2656cfcc6cdc21fb34b30e308a2b54e62258f
Filer