leejunyoung5819 wayland-android-remote
공개Ubuntu(Wayland/X11)에서 Android 기기 화면 미러링 및 원격 제어 (Rust)
| 1 | //! 고정된 기기 인증서 지문 목록 (기획서 5.5 "인증서 고정"). |
| 2 | //! |
| 3 | //! 파일 형식: 한 줄에 `IP 지문(16진수 64자)`. |
| 4 | |
| 5 | use std::collections::BTreeMap; |
| 6 | use std::fs; |
| 7 | use std::io; |
| 8 | use std::net::IpAddr; |
| 9 | use std::path::{Path, PathBuf}; |
| 10 | |
| 11 | use crate::protocol::{fingerprint_hex, parse_fingerprint_hex}; |
| 12 | |
| 13 | #[derive(Debug, Default)] |
| 14 | pub struct KnownDevices { |
| 15 | path: Option<PathBuf>, |
| 16 | devices: BTreeMap<IpAddr, [u8; 32]>, |
| 17 | } |
| 18 | |
| 19 | impl KnownDevices { |
| 20 | /// 기본 경로 `~/.config/android-remote/known_devices`에서 읽는다. |
| 21 | pub fn load_default() -> Self { |
| 22 | match crate::config::config_dir() { |
| 23 | Some(dir) => Self::load(&dir.join("known_devices")), |
| 24 | None => Self::default(), |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | /// 파일을 읽는다. 없거나 형식이 틀린 줄은 건너뛴다. |
| 29 | pub fn load(path: &Path) -> Self { |
| 30 | let devices = fs::read_to_string(path) |
| 31 | .map(|s| parse(&s)) |
| 32 | .unwrap_or_default(); |
| 33 | Self { |
| 34 | path: Some(path.to_owned()), |
| 35 | devices, |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /// 파일 없이 메모리에서만 쓴다 (테스트용). |
| 40 | pub fn in_memory() -> Self { |
| 41 | Self::default() |
| 42 | } |
| 43 | |
| 44 | pub fn get(&self, ip: IpAddr) -> Option<[u8; 32]> { |
| 45 | self.devices.get(&ip).copied() |
| 46 | } |
| 47 | |
| 48 | pub fn insert(&mut self, ip: IpAddr, fingerprint: [u8; 32]) -> io::Result<()> { |
| 49 | self.devices.insert(ip, fingerprint); |
| 50 | self.save() |
| 51 | } |
| 52 | |
| 53 | /// 사이드 메뉴의 "기기 기록 지우기"에서 쓴다. |
| 54 | pub fn remove(&mut self, ip: IpAddr) -> io::Result<()> { |
| 55 | if self.devices.remove(&ip).is_some() { |
| 56 | self.save()?; |
| 57 | } |
| 58 | Ok(()) |
| 59 | } |
| 60 | |
| 61 | fn save(&self) -> io::Result<()> { |
| 62 | let Some(path) = &self.path else { |
| 63 | return Ok(()); |
| 64 | }; |
| 65 | if let Some(dir) = path.parent() { |
| 66 | fs::create_dir_all(dir)?; |
| 67 | } |
| 68 | let body: String = self |
| 69 | .devices |
| 70 | .iter() |
| 71 | .map(|(ip, fp)| format!("{ip} {}\n", fingerprint_hex(fp))) |
| 72 | .collect(); |
| 73 | fs::write(path, body) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | fn parse(s: &str) -> BTreeMap<IpAddr, [u8; 32]> { |
| 78 | s.lines() |
| 79 | .filter_map(|line| { |
| 80 | let mut parts = line.split_whitespace(); |
| 81 | let ip = parts.next()?.parse().ok()?; |
| 82 | let fp = parse_fingerprint_hex(parts.next()?)?; |
| 83 | Some((ip, fp)) |
| 84 | }) |
| 85 | .collect() |
| 86 | } |
| 87 | |
| 88 | #[cfg(test)] |
| 89 | mod tests { |
| 90 | use super::*; |
| 91 | |
| 92 | #[test] |
| 93 | fn save_and_reload() { |
| 94 | let dir = std::env::temp_dir().join(format!("ar-known-{}", std::process::id())); |
| 95 | let path = dir.join("known_devices"); |
| 96 | let _ = fs::remove_dir_all(&dir); |
| 97 | |
| 98 | let ip: IpAddr = "192.168.0.7".parse().unwrap(); |
| 99 | let mut known = KnownDevices::load(&path); |
| 100 | known.insert(ip, [7; 32]).unwrap(); |
| 101 | assert_eq!(KnownDevices::load(&path).get(ip), Some([7; 32])); |
| 102 | |
| 103 | known.remove(ip).unwrap(); |
| 104 | assert_eq!(KnownDevices::load(&path).get(ip), None); |
| 105 | fs::remove_dir_all(&dir).unwrap(); |
| 106 | } |
| 107 | |
| 108 | #[test] |
| 109 | fn skips_malformed_lines() { |
| 110 | let map = parse("10.0.0.1 abc\nnot-ip 00\n10.0.0.2 ".to_owned().as_str()); |
| 111 | assert!(map.is_empty()); |
| 112 | } |
| 113 | } |