leejunyoung5819 wayland-android-remote
공개Ubuntu(Wayland/X11)에서 Android 기기 화면 미러링 및 원격 제어 (Rust)
| 1 | //! 접속 주소 파싱과 최근 접속 IP 기록 (기획서 4.4). |
| 2 | |
| 3 | use std::fs; |
| 4 | use std::io; |
| 5 | use std::net::{IpAddr, SocketAddr}; |
| 6 | use std::path::PathBuf; |
| 7 | |
| 8 | use wayland_android_remote::config::config_dir; |
| 9 | |
| 10 | /// 데몬 기본 포트 (기획서 5.4). |
| 11 | pub const DEFAULT_PORT: u16 = 27200; |
| 12 | /// 최근 접속 기록 최대 개수. |
| 13 | const MAX_RECENT: usize = 8; |
| 14 | |
| 15 | /// 'IP' 또는 'IP:포트' 입력을 소켓 주소로 바꾼다. IPv6는 '[::1]:27200' 형식도 받는다. |
| 16 | pub fn parse_target(input: &str) -> Result<SocketAddr, &'static str> { |
| 17 | let s = input.trim(); |
| 18 | if s.is_empty() { |
| 19 | return Err("IP 주소를 입력하세요"); |
| 20 | } |
| 21 | if let Ok(addr) = s.parse::<SocketAddr>() { |
| 22 | if addr.port() == 0 { |
| 23 | return Err("포트 번호가 올바르지 않습니다"); |
| 24 | } |
| 25 | return Ok(addr); |
| 26 | } |
| 27 | if let Ok(ip) = s.parse::<IpAddr>() { |
| 28 | return Ok(SocketAddr::new(ip, DEFAULT_PORT)); |
| 29 | } |
| 30 | Err("IP 주소 형식이 올바르지 않습니다") |
| 31 | } |
| 32 | |
| 33 | /// 기본 포트면 IP만, 아니면 'IP:포트'로 표시한다. |
| 34 | pub fn display_target(addr: SocketAddr) -> String { |
| 35 | if addr.port() == DEFAULT_PORT { |
| 36 | addr.ip().to_string() |
| 37 | } else { |
| 38 | addr.to_string() |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /// 최근 접속에 성공한 주소 목록. 가장 최근 것이 맨 앞이다. |
| 43 | #[derive(Debug, Default)] |
| 44 | pub struct RecentTargets { |
| 45 | items: Vec<String>, |
| 46 | path: Option<PathBuf>, |
| 47 | } |
| 48 | |
| 49 | impl RecentTargets { |
| 50 | /// `~/.config/android-remote/recent_ips`에서 읽는다. 파일이 없으면 빈 목록이다. |
| 51 | pub fn load() -> Self { |
| 52 | let path = config_dir().map(|d| d.join("recent_ips")); |
| 53 | let items = path |
| 54 | .as_ref() |
| 55 | .and_then(|p| fs::read_to_string(p).ok()) |
| 56 | .map(|s| parse_lines(&s)) |
| 57 | .unwrap_or_default(); |
| 58 | Self { items, path } |
| 59 | } |
| 60 | |
| 61 | /// 파일에 저장하지 않는 빈 목록 (테스트용). |
| 62 | #[cfg(test)] |
| 63 | pub fn in_memory() -> Self { |
| 64 | Self::default() |
| 65 | } |
| 66 | |
| 67 | pub fn items(&self) -> &[String] { |
| 68 | &self.items |
| 69 | } |
| 70 | |
| 71 | /// 접속 성공한 주소를 맨 앞에 넣고 파일에 저장한다. |
| 72 | pub fn record(&mut self, target: &str) -> io::Result<()> { |
| 73 | push_front_dedup(&mut self.items, target); |
| 74 | let Some(path) = &self.path else { |
| 75 | return Ok(()); |
| 76 | }; |
| 77 | if let Some(dir) = path.parent() { |
| 78 | fs::create_dir_all(dir)?; |
| 79 | } |
| 80 | fs::write(path, self.items.join("\n") + "\n") |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | fn parse_lines(s: &str) -> Vec<String> { |
| 85 | let mut items = Vec::new(); |
| 86 | for line in s.lines().map(str::trim).filter(|l| !l.is_empty()) { |
| 87 | if parse_target(line).is_ok() && !items.iter().any(|i| i == line) { |
| 88 | items.push(line.to_owned()); |
| 89 | } |
| 90 | } |
| 91 | items.truncate(MAX_RECENT); |
| 92 | items |
| 93 | } |
| 94 | |
| 95 | fn push_front_dedup(items: &mut Vec<String>, target: &str) { |
| 96 | items.retain(|i| i != target); |
| 97 | items.insert(0, target.to_owned()); |
| 98 | items.truncate(MAX_RECENT); |
| 99 | } |
| 100 | |
| 101 | #[cfg(test)] |
| 102 | mod tests { |
| 103 | use super::*; |
| 104 | |
| 105 | #[test] |
| 106 | fn parses_ip_with_default_port() { |
| 107 | let addr = parse_target(" 192.168.0.12 ").unwrap(); |
| 108 | assert_eq!(addr, "192.168.0.12:27200".parse().unwrap()); |
| 109 | assert_eq!(display_target(addr), "192.168.0.12"); |
| 110 | } |
| 111 | |
| 112 | #[test] |
| 113 | fn parses_ip_with_port() { |
| 114 | let addr = parse_target("100.64.1.2:5000").unwrap(); |
| 115 | assert_eq!(addr.port(), 5000); |
| 116 | assert_eq!(display_target(addr), "100.64.1.2:5000"); |
| 117 | assert_eq!(parse_target("[fd00::1]:27200").unwrap().port(), 27200); |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn rejects_bad_input() { |
| 122 | assert!(parse_target("").is_err()); |
| 123 | assert!(parse_target("phone.local").is_err()); |
| 124 | assert!(parse_target("192.168.0.300").is_err()); |
| 125 | assert!(parse_target("10.0.0.1:0").is_err()); |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn recent_list_dedups_and_caps() { |
| 130 | let mut items = Vec::new(); |
| 131 | for i in 0..10 { |
| 132 | push_front_dedup(&mut items, &format!("10.0.0.{i}")); |
| 133 | } |
| 134 | push_front_dedup(&mut items, "10.0.0.5"); |
| 135 | assert_eq!(items.len(), MAX_RECENT); |
| 136 | assert_eq!(items[0], "10.0.0.5"); |
| 137 | assert_eq!(items.iter().filter(|i| *i == "10.0.0.5").count(), 1); |
| 138 | } |
| 139 | |
| 140 | #[test] |
| 141 | fn load_skips_invalid_lines() { |
| 142 | let items = parse_lines("10.0.0.1\n\nnot-an-ip\n10.0.0.1\n10.0.0.2:5000\n"); |
| 143 | assert_eq!(items, vec!["10.0.0.1", "10.0.0.2:5000"]); |
| 144 | } |
| 145 | } |