// Identity this device advertises to LAN peers: a friendly name, a stable // per-install id, and a form factor. use tauri::{AppHandle, Manager}; // A friendly name to show a peer. Desktop hostnames are the machine name a // person recognises; on Android the hostname is a useless "localhost", so fall // back to the build model there. pub(crate) fn device_name() -> String { #[cfg(target_os = "android")] { if let Some(name) = android_device_name() { return name; } } hostname::get() .ok() .and_then(|h| h.into_string().ok()) .map(|h| h.trim_end_matches(".local").to_string()) .filter(|h| !h.is_empty() && h != "localhost") .unwrap_or_else(|| "Synapse device".into()) } #[cfg(target_os = "android")] fn android_device_name() -> Option { // ro.product.model is the marketing model (e.g. "Pixel 8"), the closest to a // device name Android exposes without a JNI round-trip. std::process::Command::new("getprop") .arg("ro.product.model") .output() .ok() .and_then(|o| String::from_utf8(o.stdout).ok()) .map(|s| s.trim().to_string()) .filter(|s| !s.is_empty()) } // A short id stable for this install (persisted in the app data dir) and unique // across installs. Used in the mDNS instance name so a restart keeps the same // service identity instead of appearing as a new peer. pub(crate) fn stable_instance_id(app: &AppHandle) -> String { let dir = app.path().app_data_dir().ok(); let file = dir.as_ref().map(|d| d.join("device-id")); if let Some(path) = &file { if let Ok(existing) = std::fs::read_to_string(path) { let trimmed = existing.trim(); if !trimmed.is_empty() { return trimmed.to_string(); } } } // Generated once from a high-res timestamp + pid; only needs to be unique. let seed = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .map(|d| d.as_nanos()) .unwrap_or(0) ^ u128::from(std::process::id()); let id = format!("{seed:x}"); if let (Some(dir), Some(path)) = (&dir, &file) { let _ = std::fs::create_dir_all(dir); let _ = std::fs::write(path, &id); } id } // Form factor for the peer-list icon. Phones and tablets both report as mobile // here; the webview can't cheaply tell an iPad from an iPhone, so tablet stays a // value the protocol carries rather than one this side sets. pub(crate) fn device_kind() -> String { if cfg!(any(target_os = "android", target_os = "ios")) { "mobile".into() } else { "desktop".into() } }