This commit is contained in:
netadmin committed 2026-10-25 23:16:47 +03:00
1 parent 552c009120
commit 8a95abf18b
8 files changed
+559 -126

No files matched your search

+20
View File
@@ -35,6 +35,7 @@ pub const DISCOVERY_PORT: u16 = 5354;
pub const MIN_PING_SEC: u64 = 10;
pub const MAX_PING_SEC: u64 = 30;
pub const DEFAULT_PING_SEC: u64 = 15;
pub const MAX_PING_FAILS: u8 = 5;
// ───────────────────────── Ошибки ─────────────────────────
pub struct AppError(pub StatusCode, pub String);
@@ -94,6 +95,8 @@ pub struct PeerDevice {
pub last_seen: u64,
/// Когда мы последний раз опрашивали (пинговали) это устройство (unix timestamp, сек).
pub last_ping: u64,
/// Количество подряд неудачных попыток пинга (при достижении 5 автоопрос прекращается).
pub fail_count: u8,
}
impl PeerDevice {
@@ -110,6 +113,7 @@ impl PeerDevice {
online: false,
last_seen: 0,
last_ping: 0,
fail_count: 0,
}
}
}
@@ -161,6 +165,11 @@ pub struct UiTrustRequest {
pub trust: bool,
}
#[derive(Deserialize)]
pub struct UiPingPeerRequest {
pub peer_id: String,
}
#[derive(Deserialize)]
pub struct UiManualPeerRequest {
pub ip: String,
@@ -373,6 +382,7 @@ impl AppState {
p.name = name.clone();
p.port = port;
p.online = true;
p.fail_count = 0;
p.last_seen = now;
p.last_ping = now;
p.ips.retain(|x| x != &ip_s);
@@ -385,6 +395,7 @@ impl AppState {
Entry::Vacant(v) => {
let mut p = PeerDevice::new(fp, name.clone(), vec![ip_s.clone()], port, &self.my_fingerprint, false);
p.online = true;
p.fail_count = 0;
p.last_seen = now;
p.last_ping = now;
v.insert(p);
@@ -419,6 +430,7 @@ impl AppState {
if let Some(p) = self.peers.write().get_mut(fp) {
let changed = p.ips.first() != Some(&ip_s);
p.online = true;
p.fail_count = 0;
p.last_seen = now;
if changed {
p.ips.retain(|x| x != &ip_s);
@@ -434,6 +446,14 @@ impl AppState {
}
}
/// Сбрасывает счётчик неудачных пингов у всех известных устройств (вызывается при ручном опросе или сканировании).
pub fn reset_all_ping_failures(&self) {
let mut peers = self.peers.write();
for p in peers.values_mut() {
p.fail_count = 0;
}
}
/// Пир уже недавно виден по этому адресу и порту — повторный probe по beacon не нужен.
pub fn is_fresh(&self, fp: &str, ip: IpAddr, port: u16) -> bool {
let ip_s = ip.to_string();