This commit is contained in:
netadmin committed 2026-10-23 12:36:37 +03:00
1 parent 69e2329695
commit 8b954f757b
11 files changed
+563 -528

No files matched your search

+93 -10
View File
@@ -7,16 +7,25 @@ use std::{
pub const MAX_PAYLOAD_SIZE: usize = 200 * 1024 * 1024;
pub const MAX_RAM_TEXTS: usize = 50;
pub const MAX_LOGS: usize = 100;
pub const MEDIA_DIR: &str = "received_media";
pub const DISCOVERY_PORT: u16 = 5354;
#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SavedIdentity {
pub cert_hex: String,
pub key_hex: String,
pub fingerprint: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TextItem {
pub sender: String,
pub text: String,
pub timestamp: u64,
}
#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MediaItem {
pub sender: String,
pub filename: String,
@@ -26,14 +35,25 @@ pub struct MediaItem {
pub timestamp: u64,
}
#[derive(Clone, Serialize, Deserialize)]
pub struct PeerNode {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PeerDevice {
pub id: String,
pub name: String,
pub ip: String,
pub ips: Vec<String>,
pub port: u16,
pub fingerprint: String,
pub pin_code: String,
pub verified: bool,
pub online: bool,
pub last_seen: u64,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct BeaconPacket {
pub name: String,
pub ip: String,
pub port: u16,
pub fingerprint: String,
}
pub struct AppState {
@@ -41,29 +61,91 @@ pub struct AppState {
pub my_ip: String,
pub p2p_port: u16,
pub my_fingerprint: String,
pub ping_interval_sec: u64,
pub ram_texts: RwLock<VecDeque<TextItem>>,
pub disk_media: RwLock<VecDeque<MediaItem>>,
pub peers: RwLock<HashMap<String, PeerNode>>,
pub trusted_fingerprints: Arc<RwLock<HashMap<String, String>>>,
/// Ключ таблицы — постоянный fingerprint (ID) устройства
pub peers: Arc<RwLock<HashMap<String, PeerDevice>>>,
pub http_client: reqwest::Client,
pub probe_client: reqwest::Client,
pub transfers: RwLock<Vec<String>>,
pub logs: RwLock<VecDeque<String>>,
}
impl AppState {
pub fn add_log(&self, msg: impl Into<String>) {
let text = msg.into();
println!("[LOG] {}", text);
let mut logs = self.logs.write().unwrap();
if logs.len() >= MAX_LOGS {
logs.pop_back();
}
logs.push_front(format!("[{}] {}", now_sec(), text));
}
/// Добавляет или обновляет найденное устройство по его постоянному fingerprint
pub fn upsert_peer(&self, name: String, ip: String, port: u16, fingerprint: String) {
if fingerprint == self.my_fingerprint || fingerprint.len() < 8 {
return;
}
if ip.is_empty() || ip == "0.0.0.0" || ip == "127.0.0.1" {
return;
}
let pin_code = fingerprint[..8].to_uppercase();
let now = now_sec();
let mut peers = self.peers.write().unwrap();
if let Some(existing) = peers.get_mut(&fingerprint) {
existing.name = name;
existing.port = port;
existing.online = true;
existing.last_seen = now;
if let Some(pos) = existing.ips.iter().position(|x| x == &ip) {
existing.ips.remove(pos);
}
existing.ips.insert(0, ip);
} else {
drop(peers);
self.add_log(format!(
"Обнаружено устройство '{}' ({}:{}) PIN: [{}]",
name, ip, port, pin_code
));
let mut peers = self.peers.write().unwrap();
peers.insert(
fingerprint.clone(),
PeerDevice {
id: fingerprint.clone(),
name,
ips: vec![ip],
port,
fingerprint,
pin_code,
verified: false,
online: true,
last_seen: now,
},
);
}
}
}
#[derive(Serialize, Deserialize)]
pub struct IncomingTextPayload {
pub sender: String,
pub sender_fp: String,
pub text: String,
}
#[derive(Deserialize)]
pub struct UiSendTextRequest {
pub target_ip: String,
pub peer_id: String,
pub text: String,
}
#[derive(Deserialize)]
pub struct UiTrustRequest {
pub target_ip: String,
pub peer_id: String,
pub trust: bool,
}
@@ -86,7 +168,8 @@ pub fn url_decode(input: &str) -> String {
while let Some(b) = chars.next() {
if b == b'%' {
if let (Some(c1), Some(c2)) = (chars.next(), chars.next()) {
if let Ok(decoded) = u8::from_str_radix(&format!("{}{}", c1 as char, c2 as char), 16)
if let Ok(decoded) =
u8::from_str_radix(&format!("{}{}", c1 as char, c2 as char), 16)
{
bytes.push(decoded);
continue;