From 5774d9ea952c691649a715ffe1e3201cd334d3d2 Mon Sep 17 00:00:00 2001 From: Threepwood-7 Date: Fri, 13 Nov 2026 10:03:28 +0100 Subject: [PATCH 01/74] fix(android): enable atomic completed-file moves (RUST-FEAT-056) --- crates/emulebb-ed2k/Cargo.toml | 2 +- .../src/ed2k_transfer/fs_durability.rs | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/crates/emulebb-ed2k/Cargo.toml b/crates/emulebb-ed2k/Cargo.toml index 9d41bd84d..bfe1269b4 100644 --- a/crates/emulebb-ed2k/Cargo.toml +++ b/crates/emulebb-ed2k/Cargo.toml @@ -72,7 +72,7 @@ windows-sys = { workspace = true, features = ["Win32_Foundation", "Win32_Globali [target.'cfg(unix)'.dependencies] libc.workspace = true -[target.'cfg(target_os = "linux")'.dependencies] +[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies] rustix.workspace = true [dev-dependencies] diff --git a/crates/emulebb-ed2k/src/ed2k_transfer/fs_durability.rs b/crates/emulebb-ed2k/src/ed2k_transfer/fs_durability.rs index 1576837a7..7d4d83c2e 100644 --- a/crates/emulebb-ed2k/src/ed2k_transfer/fs_durability.rs +++ b/crates/emulebb-ed2k/src/ed2k_transfer/fs_durability.rs @@ -104,15 +104,16 @@ fn sync_file_for_publish_blocking(file: &std::fs::File) -> io::Result<()> { file.sync_all() } -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", target_os = "android"))] fn move_path_no_replace_blocking( source: &Path, destination: &Path, ) -> io::Result { use rustix::fs::{CWD, RenameFlags, renameat_with}; - // WHY: Rust's bundled musl can lack the renameat2 libc wrapper. Rustix - // supplies the safe Linux operation while preserving atomic no-clobber publication. + // WHY: Android is a separate Rust target but uses Linux's atomic no-replace + // rename too. Rustix also handles musl builds lacking the libc wrapper; + // never substitute a check-then-rename that could overwrite another file. let error = match renameat_with(CWD, source, CWD, destination, RenameFlags::NOREPLACE) { Ok(()) => return Ok(NoReplaceMoveOutcome::Moved), Err(error) => io::Error::from(error), @@ -200,7 +201,12 @@ fn move_path_no_replace_blocking( } } -#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))] +#[cfg(not(any( + target_os = "linux", + target_os = "android", + target_os = "macos", + windows +)))] fn move_path_no_replace_blocking( _source: &Path, _destination: &Path, From f5b18975d3e5086f6c7872bbe777d3ccdb98eb71 Mon Sep 17 00:00:00 2001 From: Threepwood-7 Date: Fri, 13 Nov 2026 10:11:31 +0100 Subject: [PATCH 02/74] feat(diagnostics): show daemon memory and CPU usage (RUST-FEAT-056) --- Cargo.lock | 2 + crates/emulebb-rest/Cargo.toml | 6 + crates/emulebb-rest/src/lib.rs | 2 + crates/emulebb-rest/src/process_resources.rs | 311 +++++++++++++++++++ crates/emulebb-rest/src/responses.rs | 8 +- crates/emulebb-rest/src/routes.rs | 1 + crates/emulebb-rest/src/tests/app.rs | 36 +++ webui/src/api.ts | 8 + webui/src/views.tsx | 16 + webui/tests/unit/diagnostics-view.test.tsx | 35 +++ 10 files changed, 423 insertions(+), 2 deletions(-) create mode 100644 crates/emulebb-rest/src/process_resources.rs diff --git a/Cargo.lock b/Cargo.lock index 514ebbf2b..7e6e27724 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -830,6 +830,7 @@ dependencies = [ "emulebb-index", "futures-util", "http-body-util", + "libc", "percent-encoding", "rusqlite", "serde", @@ -838,6 +839,7 @@ dependencies = [ "tokio", "tower", "tower-http 0.7.1", + "windows-sys 0.61.2", ] [[package]] diff --git a/crates/emulebb-rest/Cargo.toml b/crates/emulebb-rest/Cargo.toml index 27e182839..17748208c 100644 --- a/crates/emulebb-rest/Cargo.toml +++ b/crates/emulebb-rest/Cargo.toml @@ -29,6 +29,12 @@ tokio = { workspace = true, features = ["sync", "time"] } tower.workspace = true tower-http.workspace = true +[target.'cfg(unix)'.dependencies] +libc.workspace = true + +[target.'cfg(windows)'.dependencies] +windows-sys = { workspace = true, features = ["Win32_Foundation", "Win32_System_ProcessStatus", "Win32_System_Threading"] } + [dev-dependencies] rusqlite.workspace = true tokio = { workspace = true, features = ["macros", "rt"] } diff --git a/crates/emulebb-rest/src/lib.rs b/crates/emulebb-rest/src/lib.rs index 5bb71af3c..d76066ce9 100644 --- a/crates/emulebb-rest/src/lib.rs +++ b/crates/emulebb-rest/src/lib.rs @@ -13,6 +13,7 @@ mod diagnostic_bundle; mod dto; mod envelope; mod handlers; +mod process_resources; mod request_body_policy; mod responses; mod route_body_metadata; @@ -47,6 +48,7 @@ pub struct RestState { core: Arc, api_key: Arc, shutdown: Option>, + process_resources: Arc, } #[cfg(all(test, feature = "packet-diagnostics"))] #[path = "tests/protocol_fixtures.rs"] diff --git a/crates/emulebb-rest/src/process_resources.rs b/crates/emulebb-rest/src/process_resources.rs new file mode 100644 index 000000000..3a38954b9 --- /dev/null +++ b/crates/emulebb-rest/src/process_resources.rs @@ -0,0 +1,311 @@ +//! Bounded, on-demand metrics for this daemon, including native Android builds. + +use std::time::{Duration, Instant}; + +use serde::Serialize; +use tokio::sync::Mutex; + +const MIN_SAMPLE_INTERVAL: Duration = Duration::from_secs(1); + +#[derive(Debug, Default)] +pub(crate) struct ProcessResourceMonitor { + sampler: Mutex, +} + +#[derive(Clone, Copy, Debug, Default, Serialize)] +#[serde(rename_all = "camelCase")] +pub(crate) struct ProcessResources { + sampled_at_ms: Option, + resident_memory_bytes: Option, + /// Total process CPU use: 100% means one occupied core, not the whole host. + cpu_usage_percent: Option, + cpu_sample_interval_ms: Option, +} + +impl ProcessResourceMonitor { + /// Share a sample across REST consumers for at least one second. OS reads + /// stay off the async executor; missing counters remain null, never zero. + pub(crate) async fn snapshot(&self) -> ProcessResources { + let mut sampler = self.sampler.lock().await; + if sampler + .previous + .as_ref() + .is_some_and(|sample| sample.at.elapsed() < MIN_SAMPLE_INTERVAL) + { + return sampler.current; + } + match tokio::task::spawn_blocking(RawSample::read).await { + Ok(sample) => sampler.record(sample), + Err(_) => { + *sampler = Sampler::default(); + ProcessResources::default() + } + } + } +} + +#[derive(Debug, Default)] +struct Sampler { + previous: Option, + current: ProcessResources, +} + +#[derive(Debug)] +struct RawSample { + at: Instant, + sampled_at_ms: i64, + cpu_time: Option, + resident_memory_bytes: Option, +} + +impl RawSample { + fn read() -> Self { + let cpu_time = cpu_time(); + let at = Instant::now(); + Self { + at, + sampled_at_ms: chrono::Utc::now().timestamp_millis(), + cpu_time, + resident_memory_bytes: resident_memory_bytes(), + } + } +} + +impl Sampler { + fn record(&mut self, sample: RawSample) -> ProcessResources { + let cpu_sample = self.previous.as_ref().and_then(|previous| { + let elapsed = sample.at.checked_duration_since(previous.at)?; + let cpu = sample.cpu_time?.checked_sub(previous.cpu_time?)?; + (elapsed >= MIN_SAMPLE_INTERVAL).then(|| { + ( + cpu.as_secs_f64() / elapsed.as_secs_f64() * 100.0, + elapsed.as_millis().min(u64::MAX as u128) as u64, + ) + }) + }); + self.current = ProcessResources { + sampled_at_ms: Some(sample.sampled_at_ms), + resident_memory_bytes: sample.resident_memory_bytes, + cpu_usage_percent: cpu_sample.map(|(percent, _)| percent), + cpu_sample_interval_ms: cpu_sample.map(|(_, interval)| interval), + }; + self.previous = Some(sample); + self.current + } +} + +#[cfg(unix)] +fn cpu_time() -> Option { + let mut usage = std::mem::MaybeUninit::::uninit(); + // SAFETY: getrusage initializes the writable output on success; RUSAGE_SELF + // includes every thread in this process and excludes child processes. + if unsafe { libc::getrusage(libc::RUSAGE_SELF, usage.as_mut_ptr()) } != 0 { + return None; + } + // SAFETY: the successful call initialized `usage`. + let usage = unsafe { usage.assume_init() }; + let duration = |time: libc::timeval| { + Some(Duration::new( + time.tv_sec.try_into().ok()?, + u32::try_from(time.tv_usec).ok()?.checked_mul(1_000)?, + )) + }; + duration(usage.ru_utime)?.checked_add(duration(usage.ru_stime)?) +} + +#[cfg(any(target_os = "linux", target_os = "android"))] +fn resident_memory_bytes() -> Option { + use std::io::Read; + + // WHY: Android restricts global /proc counters. The current process's statm + // remains readable without JVM/root access; its RSS is in runtime pages, + // which must not be assumed to be 4 KiB on Android's 16 KiB builds. + let mut statm = String::new(); + std::fs::File::open("/proc/self/statm") + .ok()? + .take(256) + .read_to_string(&mut statm) + .ok()?; + // SAFETY: sysconf has no pointer arguments; nonpositive results are rejected. + let page_size = u64::try_from(unsafe { libc::sysconf(libc::_SC_PAGESIZE) }).ok()?; + resident_bytes_from_statm(&statm, page_size) +} + +#[cfg(any(target_os = "linux", target_os = "android", test))] +fn resident_bytes_from_statm(statm: &str, page_size: u64) -> Option { + if page_size == 0 { + return None; + } + statm + .split_whitespace() + .nth(1)? + .parse::() + .ok()? + .checked_mul(page_size) +} + +#[cfg(target_os = "macos")] +fn resident_memory_bytes() -> Option { + let mut info = std::mem::MaybeUninit::::uninit(); + let mut count = libc::MACH_TASK_BASIC_INFO_COUNT; + // SAFETY: the flavor, output size and count describe mach_task_basic_info. + // The current task port is borrowed; the output is read only on success. + let result = unsafe { + libc::task_info( + libc::mach_task_self(), + libc::MACH_TASK_BASIC_INFO, + info.as_mut_ptr().cast(), + &mut count, + ) + }; + if result != libc::KERN_SUCCESS || count != libc::MACH_TASK_BASIC_INFO_COUNT { + return None; + } + // SAFETY: the successful call initialized the complete requested structure. + Some(unsafe { info.assume_init() }.resident_size) +} + +#[cfg(windows)] +fn cpu_time() -> Option { + use windows_sys::Win32::{ + Foundation::FILETIME, + System::Threading::{GetCurrentProcess, GetProcessTimes}, + }; + + let mut created = FILETIME::default(); + let mut exited = FILETIME::default(); + let mut kernel = FILETIME::default(); + let mut user = FILETIME::default(); + // SAFETY: GetCurrentProcess returns a borrowed pseudo-handle and all four + // output pointers address writable FILETIME values for this call. + if unsafe { + GetProcessTimes( + GetCurrentProcess(), + &mut created, + &mut exited, + &mut kernel, + &mut user, + ) + } == 0 + { + return None; + } + let ticks = + |time: FILETIME| (u64::from(time.dwHighDateTime) << 32) | u64::from(time.dwLowDateTime); + let total = ticks(kernel).checked_add(ticks(user))?; + Some(Duration::new( + total / 10_000_000, + ((total % 10_000_000) * 100) as u32, + )) +} + +#[cfg(windows)] +fn resident_memory_bytes() -> Option { + use windows_sys::Win32::System::{ + ProcessStatus::{K32GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS}, + Threading::GetCurrentProcess, + }; + + let size = std::mem::size_of::() as u32; + let mut counters = PROCESS_MEMORY_COUNTERS { + cb: size, + ..Default::default() + }; + // SAFETY: the current-process pseudo-handle is borrowed, and counters is a + // writable structure whose size matches the buffer length supplied. + if unsafe { K32GetProcessMemoryInfo(GetCurrentProcess(), &mut counters, size) } == 0 { + return None; + } + Some(counters.WorkingSetSize as u64) +} + +#[cfg(not(any(unix, windows)))] +fn cpu_time() -> Option { + None +} + +#[cfg(not(any( + windows, + target_os = "linux", + target_os = "android", + target_os = "macos" +)))] +fn resident_memory_bytes() -> Option { + None +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn cpu_uses_interval_deltas_and_can_exceed_one_core() { + let start = Instant::now(); + let mut sampler = Sampler::default(); + let sample = |seconds, cpu: Option| RawSample { + at: start + Duration::from_secs(seconds), + sampled_at_ms: 1_000 + seconds as i64 * 1_000, + cpu_time: cpu.map(Duration::from_millis), + resident_memory_bytes: Some(42), + }; + let first = sampler.record(sample(0, Some(500))); + assert_eq!(first.resident_memory_bytes, Some(42)); + assert_eq!(first.cpu_usage_percent, None); + assert_eq!(first.cpu_sample_interval_ms, None); + let busy = sampler.record(sample(2, Some(3_000))); + assert_eq!(busy.cpu_usage_percent, Some(125.0)); + assert_eq!(busy.cpu_sample_interval_ms, Some(2_000)); + assert_eq!( + sampler.record(sample(4, Some(3_000))).cpu_usage_percent, + Some(0.0) + ); + assert_eq!(sampler.record(sample(6, None)).cpu_usage_percent, None); + assert_eq!( + sampler.record(sample(8, Some(4_000))).cpu_usage_percent, + None + ); + assert_eq!(sampler.record(sample(10, Some(1))).cpu_usage_percent, None); + } + + #[test] + fn resident_memory_uses_runtime_page_size_and_rejects_bad_counters() { + assert_eq!( + resident_bytes_from_statm("200 100 50 0 0 10 0\n", 16_384), + Some(1_638_400) + ); + assert_eq!(resident_bytes_from_statm("200 100", 4_096), Some(409_600)); + for invalid in [ + "", + "200", + "200 invalid", + "200 -1", + "200 18446744073709551615", + ] { + assert_eq!(resident_bytes_from_statm(invalid, 16_384), None); + } + assert_eq!(resident_bytes_from_statm("200 100", 0), None); + } + + #[tokio::test] + async fn native_metrics_are_live_and_repeated_requests_reuse_the_sample() { + let monitor = ProcessResourceMonitor::default(); + let first = monitor.snapshot().await; + assert!(first.sampled_at_ms.is_some()); + assert!(first.resident_memory_bytes.is_some_and(|bytes| bytes > 0)); + assert!( + monitor + .sampler + .lock() + .await + .previous + .as_ref() + .unwrap() + .cpu_time + .is_some() + ); + let second = monitor.snapshot().await; + assert_eq!(first.sampled_at_ms, second.sampled_at_ms); + assert_eq!(first.resident_memory_bytes, second.resident_memory_bytes); + assert_eq!(second.cpu_usage_percent, None); + } +} diff --git a/crates/emulebb-rest/src/responses.rs b/crates/emulebb-rest/src/responses.rs index 4dfc7f588..ea686bb65 100644 --- a/crates/emulebb-rest/src/responses.rs +++ b/crates/emulebb-rest/src/responses.rs @@ -121,7 +121,8 @@ pub(crate) async fn status_response(state: &RestState) -> Value { shared_hashing_count, shared_directory_reload_progress.clone(), &upload_policy, - ); + ) + .await; json!({ "lifecycle": lifecycle_response(&status.lifecycle), "stats": stats_response(&status, &upload_policy, &throughput, shared_hashing_count), @@ -161,9 +162,10 @@ pub(crate) async fn runtime_diagnostics_response(state: &RestState) -> Value { shared_directories.reload_progress, &upload_policy, ) + .await } -fn runtime_diagnostics_response_with( +async fn runtime_diagnostics_response_with( state: &RestState, shared_file_count: usize, download_file_count: usize, @@ -171,8 +173,10 @@ fn runtime_diagnostics_response_with( shared_directory_reload_progress: impl serde::Serialize, upload_policy: &UploadPolicyMetrics, ) -> Value { + let process_resources = state.process_resources.snapshot().await; json!({ "processId": std::process::id(), + "processResources": process_resources, "knownFileCount": shared_file_count, "sharedFileCount": shared_file_count, "sharedHashingCount": shared_hashing_count, diff --git a/crates/emulebb-rest/src/routes.rs b/crates/emulebb-rest/src/routes.rs index ebd7fba29..351737b3f 100644 --- a/crates/emulebb-rest/src/routes.rs +++ b/crates/emulebb-rest/src/routes.rs @@ -46,6 +46,7 @@ pub fn router_with_shutdown( core, api_key: Arc::new(config.api_key), shutdown, + process_resources: Arc::default(), }; let health_router = Router::new() .route("/healthz", get(health)) diff --git a/crates/emulebb-rest/src/tests/app.rs b/crates/emulebb-rest/src/tests/app.rs index fca044268..f8911012d 100644 --- a/crates/emulebb-rest/src/tests/app.rs +++ b/crates/emulebb-rest/src/tests/app.rs @@ -783,6 +783,42 @@ async fn diagnostics_returns_runtime_diagnostics_directly() { assert_eq!(value["data"]["geolocation"], Value::Null); } +#[tokio::test] +async fn process_resources_are_exposed_on_every_runtime_diagnostics_surface() { + let router = test_router(); + for (path, pointer) in [ + ("/api/v1/diagnostics", "/data/processResources"), + ( + "/api/v1/status", + "/data/runtimeDiagnostics/processResources", + ), + ( + "/api/v1/snapshot", + "/data/status/runtimeDiagnostics/processResources", + ), + ] { + let response = router + .clone() + .oneshot( + Request::builder() + .uri(path) + .header("X-API-Key", "secret") + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::OK); + let body = to_bytes(response.into_body(), usize::MAX).await.unwrap(); + let value: Value = serde_json::from_slice(&body).unwrap(); + let resources = value.pointer(pointer).unwrap(); + assert!(resources["sampledAtMs"].as_i64().unwrap() > 0); + assert!(resources["residentMemoryBytes"].as_u64().unwrap() > 0); + assert!(resources.get("cpuUsagePercent").is_some()); + assert!(resources.get("cpuSampleIntervalMs").is_some()); + } +} + #[tokio::test] async fn diagnostics_reports_transfer_event_subscribers() { let core = diff --git a/webui/src/api.ts b/webui/src/api.ts index 4f6a0e0aa..60170005d 100644 --- a/webui/src/api.ts +++ b/webui/src/api.ts @@ -114,6 +114,7 @@ export type ServerStatus = { export type RuntimeDiagnostics = { processId?: number; + processResources?: ProcessResources; knownFileCount?: number; sharedFileCount?: number; sharedHashingCount?: number; @@ -128,6 +129,13 @@ export type RuntimeDiagnostics = { [key: string]: unknown; }; +export type ProcessResources = { + sampledAtMs: number | null; + residentMemoryBytes: number | null; + cpuUsagePercent: number | null; + cpuSampleIntervalMs: number | null; +}; + export type TransferEventRuntimeDiagnostics = { enabled?: boolean; stream?: string; diff --git a/webui/src/views.tsx b/webui/src/views.tsx index a913196e7..0763ddbde 100644 --- a/webui/src/views.tsx +++ b/webui/src/views.tsx @@ -4100,6 +4100,7 @@ export function DiagnosticsView(props: { const [shutdownConfirm, setShutdownConfirm] = useState(""); const [bundleFileName, setBundleFileName] = useState(""); const diagnostics = props.runtimeDiagnostics ?? {}; + const resources = diagnostics.processResources; const eventStream = props.eventStreamStatus; const transferEvents = diagnostics.transferEvents ?? {}; const reload = diagnostics.sharedDirectoryReloadProgress ?? {}; @@ -4165,6 +4166,21 @@ export function DiagnosticsView(props: { +
+

Daemon resources

+
+ + +
+

Memory is the daemon's resident RAM. CPU: 100% means one fully occupied core; multiple cores can exceed 100%.

+

+ {resources?.sampledAtMs == null + ? "Resource metrics are unavailable." + : `Sampled ${new Date(resources.sampledAtMs).toLocaleTimeString()}. ${resources.cpuSampleIntervalMs == null + ? "CPU usage needs two samples at least one second apart; unavailable counters show n/a." + : `CPU average over ${(resources.cpuSampleIntervalMs / 1000).toFixed(1)} s.`}`} +

+
diff --git a/webui/tests/unit/diagnostics-view.test.tsx b/webui/tests/unit/diagnostics-view.test.tsx index c06a0f3b9..d07dc5a21 100644 --- a/webui/tests/unit/diagnostics-view.test.tsx +++ b/webui/tests/unit/diagnostics-view.test.tsx @@ -11,6 +11,39 @@ afterEach(() => { }); describe("DiagnosticsView", () => { + it.each([ + { memory: 67_108_864, cpu: 125.5, interval: 2_000, memoryText: "64 MiB", cpuText: "125.5%" }, + { memory: 0, cpu: 0, interval: 1_000, memoryText: "0 B", cpuText: "0.0%" }, + { memory: null, cpu: null, interval: null, memoryText: "n/a", cpuText: "n/a" } + ])("shows process memory and CPU without inventing unavailable values ($cpuText)", ({ memory, cpu, interval, memoryText, cpuText }) => { + render( + { await operation(); }} + /> + ); + expect(screen.getByText("Resident memory").parentElement).toHaveTextContent(memoryText); + expect(screen.getByText("CPU usage").parentElement).toHaveTextContent(cpuText); + expect(screen.getByText(/100% means one fully occupied core/)).toBeInTheDocument(); + if (interval === null) { + expect(screen.getByText(/CPU usage needs two samples/)).toBeInTheDocument(); + } else { + expect(screen.getByText(new RegExp(`CPU average over ${(interval / 1_000).toFixed(1)} s`))).toBeInTheDocument(); + } + }); + it("downloads caller-owned inline diagnostic bundles", async () => { const bundle = { schema: "emulebb.diagnostic-bundle.v5", @@ -48,6 +81,8 @@ describe("DiagnosticsView", () => { /> ); + expect(screen.getByText("CPU usage").parentElement).toHaveTextContent("n/a"); + expect(screen.getByText("Resident memory").parentElement).toHaveTextContent("n/a"); fireEvent.click(screen.getByRole("button", { name: "Capture bundle" })); await waitFor(() => expect(post).toHaveBeenCalledOnce()); From 999b38c166b8e8065f24f25d6a9b53867ad5f0a1 Mon Sep 17 00:00:00 2001 From: Threepwood-7 Date: Fri, 13 Nov 2026 11:20:24 +0100 Subject: [PATCH 03/74] feat(android): host daemon through a narrow JNI lifecycle (RUST-FEAT-056) --- Cargo.lock | 12 ++ Cargo.toml | 2 + crates/emulebb-android/Cargo.toml | 22 ++ crates/emulebb-android/src/lib.rs | 225 ++++++++++++++++++++ crates/emulebb-daemon/Cargo.toml | 1 + crates/emulebb-daemon/src/host_bootstrap.rs | 122 +++++++++++ crates/emulebb-daemon/src/lib.rs | 10 +- 7 files changed, 393 insertions(+), 1 deletion(-) create mode 100644 crates/emulebb-android/Cargo.toml create mode 100644 crates/emulebb-android/src/lib.rs create mode 100644 crates/emulebb-daemon/src/host_bootstrap.rs diff --git a/Cargo.lock b/Cargo.lock index 7e6e27724..8a66d12fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -569,6 +569,18 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba92c8f4591bf29f89716b45e5f5f56577adbb81426e5620c6e994da7c88bb7a" +[[package]] +name = "emulebb-android" +version = "0.1.0-beta.2" +dependencies = [ + "anyhow", + "emulebb-daemon", + "jni", + "serde", + "serde_json", + "tokio", +] + [[package]] name = "emulebb-core" version = "0.1.0-beta.2" diff --git a/Cargo.toml b/Cargo.toml index 1a20c52b7..fa459a498 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = [ + "crates/emulebb-android", "crates/emulebb-core", "crates/emulebb-daemon", "crates/emulebb-diagnostics", @@ -57,6 +58,7 @@ if-addrs = "0.15" ipnet = "2" ipconfig = "0.3" libc = "0.2" +jni = "0.22.4" md4 = "0.11" md5 = "0.8" notify = "8" diff --git a/crates/emulebb-android/Cargo.toml b/crates/emulebb-android/Cargo.toml new file mode 100644 index 000000000..1ed905c76 --- /dev/null +++ b/crates/emulebb-android/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "emulebb-android" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +publish.workspace = true +description = "Android application host for the eMuleBB daemon" + +[lib] +crate-type = ["cdylib", "rlib"] + +[lints] +workspace = true + +[dependencies] +anyhow.workspace = true +emulebb-daemon = { path = "../emulebb-daemon" } +jni.workspace = true +serde = { workspace = true, features = ["derive"] } +serde_json.workspace = true +tokio = { workspace = true, features = ["rt-multi-thread", "sync", "time"] } diff --git a/crates/emulebb-android/src/lib.rs b/crates/emulebb-android/src/lib.rs new file mode 100644 index 000000000..ac27b9847 --- /dev/null +++ b/crates/emulebb-android/src/lib.rs @@ -0,0 +1,225 @@ +//! Narrow application-host boundary; the Android shell owns no client settings. + +use std::{ + path::PathBuf, + sync::{Mutex, OnceLock}, + thread::JoinHandle, +}; + +use anyhow::{Context, Result, ensure}; +use emulebb_daemon::{DaemonProfile, RestBootstrapSettings, ShutdownRequest, host_bootstrap}; +use jni::{ + Env, native_method, + objects::{JObject, JString}, +}; +use serde::{Deserialize, Serialize}; +use serde_json::{Value, json}; + +#[derive(Deserialize)] +#[serde(tag = "op", rename_all = "camelCase", deny_unknown_fields)] +enum Request { + ReadSettings { + profile_dir: PathBuf, + }, + WriteSettings { + profile_dir: PathBuf, + bind_addr: String, + api_key: String, + web_root_dir: PathBuf, + }, + Start { + profile_dir: PathBuf, + transfer_root: PathBuf, + incoming_dir: PathBuf, + }, + Stop {}, + Status {}, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)] +enum Phase { + #[default] + Stopped, + Starting, + Running, + Stopping, + Failed, +} + +#[derive(Default)] +struct Host { + phase: Phase, + error: Option, + address: Option, + shutdown: Option, + worker: Option>, +} + +fn host() -> &'static Mutex { + static HOST: OnceLock> = OnceLock::new(); + HOST.get_or_init(Mutex::default) +} + +fn handle(request: Request) -> Result { + match request { + Request::ReadSettings { profile_dir } => { + ensure!( + profile_dir.is_absolute(), + "profile directory must be absolute" + ); + Ok(json!(host_bootstrap::read(&profile_dir)?)) + } + Request::WriteSettings { + profile_dir, + bind_addr, + api_key, + web_root_dir, + } => { + ensure!( + profile_dir.is_absolute() && web_root_dir.is_absolute(), + "host directories must be absolute" + ); + let rest = RestBootstrapSettings { + bind_addr: Some(bind_addr.parse().context("invalid REST bind address")?), + api_key, + web_root_dir: Some(web_root_dir), + }; + host_bootstrap::write(&profile_dir, rest)?; + Ok(Value::Null) + } + Request::Start { + profile_dir, + transfer_root, + incoming_dir, + } => { + ensure!( + profile_dir.is_absolute() + && transfer_root.is_absolute() + && incoming_dir.is_absolute(), + "host directories must be absolute" + ); + let mut state = host() + .lock() + .map_err(|_| anyhow::anyhow!("host state unavailable"))?; + if matches!( + state.phase, + Phase::Starting | Phase::Running | Phase::Stopping + ) { + return Ok(Value::Null); + } + if let Some(worker) = &state.worker { + ensure!(worker.is_finished(), "previous runtime is still draining"); + } + if let Some(worker) = state.worker.take() { + let _ = worker.join(); + } + let shutdown = ShutdownRequest::default(); + state.phase = Phase::Starting; + state.error = None; + state.address = None; + state.shutdown = Some(shutdown.clone()); + let worker = std::thread::Builder::new().name("emulebb-android".into()).spawn(move || { + let outcome = std::panic::catch_unwind(|| -> Result<()> { + if shutdown.subscribe().is_requested() { return Ok(()); } + let mut profile = DaemonProfile::load(Some(profile_dir))?; + profile.host_transfer_root = Some(transfer_root); + if profile.incoming_dir.is_none() { profile.incoming_dir = Some(incoming_dir); } + let runtime = tokio::runtime::Builder::new_multi_thread().enable_all().build()?; + let outcome = runtime.block_on(async { + let (ready, mut readiness) = tokio::sync::oneshot::channel(); + let run = emulebb_daemon::run_hosted(profile, shutdown.subscribe(), ready); + tokio::pin!(run); + tokio::select! { + result = &mut run => result, + result = &mut readiness => { + if let Ok(address) = result { + let mut state = host().lock().map_err(|_| anyhow::anyhow!("host state unavailable"))?; + state.address = Some(address.to_string()); + if state.phase == Phase::Starting { state.phase = Phase::Running; } + } + run.await + } + } + }); + // WHY: Tokio blocking work can outlive the daemon future. Drain + // the application runtime before publishing Stopped/releasing ownership. + drop(runtime); + outcome + }); + if let Ok(mut state) = host().lock() { + match outcome { + Ok(Ok(())) => { state.phase = Phase::Stopped; state.error = None; } + Ok(Err(error)) => { state.phase = Phase::Failed; state.error = Some(format!("{error:#}")); } + Err(_) => { state.phase = Phase::Failed; state.error = Some("native runtime failed".into()); } + } + state.shutdown = None; + state.address = None; + } + }); + match worker { + Ok(worker) => state.worker = Some(worker), + Err(error) => { + state.phase = Phase::Failed; + state.shutdown = None; + return Err(error.into()); + } + } + Ok(Value::Null) + } + Request::Stop {} => { + let mut state = host() + .lock() + .map_err(|_| anyhow::anyhow!("host state unavailable"))?; + if let Some(shutdown) = &state.shutdown { + shutdown.request(); + state.phase = Phase::Stopping; + } + Ok(Value::Null) + } + Request::Status {} => { + let state = host() + .lock() + .map_err(|_| anyhow::anyhow!("host state unavailable"))?; + Ok(json!({"phase":state.phase,"error":state.error,"address":state.address})) + } + } +} + +const _: jni::NativeMethod = native_method! { + java_type = "org.emulebb.android.NativeBridge", + extern fn request(payload: JString) -> JString, +}; + +fn request<'local>( + env: &mut Env<'local>, + _this: JObject<'local>, + payload: JString<'local>, +) -> Result, jni::errors::Error> { + let input = payload.to_string(); + // The JNI macro catches panics and prevents unwinding into the JVM. + let response = match serde_json::from_str::(&input) + .map_err(|_| anyhow::anyhow!("invalid host request")) + .and_then(handle) + { + Ok(data) => json!({"ok":true,"data":data}), + Err(error) => json!({"ok":false,"error":format!("{error:#}")}), + }; + JString::from_str(env, response.to_string()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn private_host_protocol_rejects_unknown_fields_and_retired_commands() { + assert!(serde_json::from_str::(r#"{"op":"status","api_key":"hidden"}"#).is_err()); + assert!(serde_json::from_str::(r#"{"op":"migrate"}"#).is_err()); + assert!( + handle(Request::ReadSettings { + profile_dir: "relative".into() + }) + .is_err() + ); + } +} diff --git a/crates/emulebb-daemon/Cargo.toml b/crates/emulebb-daemon/Cargo.toml index e0f14f28e..0017bff00 100644 --- a/crates/emulebb-daemon/Cargo.toml +++ b/crates/emulebb-daemon/Cargo.toml @@ -56,6 +56,7 @@ serde.workspace = true serde_json.workspace = true tokio = { workspace = true, features = ["macros", "net", "rt-multi-thread", "signal", "sync", "time"] } toml.workspace = true +tempfile.workspace = true tracing.workspace = true tracing-appender.workspace = true tracing-subscriber.workspace = true diff --git a/crates/emulebb-daemon/src/host_bootstrap.rs b/crates/emulebb-daemon/src/host_bootstrap.rs new file mode 100644 index 000000000..f94598393 --- /dev/null +++ b/crates/emulebb-daemon/src/host_bootstrap.rs @@ -0,0 +1,122 @@ +//! Canonical bootstrap access for application hosts, serialized by the profile lease. + +use std::{io::Write, path::Path}; + +use anyhow::{Context, Result}; + +use super::{ + DaemonBootstrapSettings, DaemonProfile, PROFILE_SETTINGS_FILE, ProfileLease, + RestBootstrapSettings, +}; + +/// Read existing settings without opening or changing the metadata database. +/// Atomic bootstrap publication permits read-only setup/status UI while the +/// daemon owns the profile. Only writes require an exclusive profile lease. +pub fn read(profile_dir: &Path) -> Result> { + let path = profile_dir.join(PROFILE_SETTINGS_FILE); + if !path.exists() { + return Ok(None); + } + super::validate_profile_settings_permissions(&path)?; + let text = std::fs::read_to_string(path)?; + let bootstrap: DaemonBootstrapSettings = + toml::from_str(&text).map_err(|error: toml::de::Error| { + anyhow::anyhow!("invalid bootstrap: {}", error.message()) + })?; + validate(&bootstrap.rest)?; + Ok(Some(bootstrap.rest)) +} + +/// Validate the same bootstrap contract used by daemon startup. +pub fn validate(rest: &RestBootstrapSettings) -> Result<()> { + let profile = DaemonProfile { + rest: rest.clone(), + ..Default::default() + }; + profile.validate_rest_api_key()?; + profile.rest_bind_addr()?; + Ok(()) +} + +/// Atomically replace stopped-host bootstrap settings while retaining exclusive ownership. +pub fn write(profile_dir: &Path, rest: RestBootstrapSettings) -> Result<()> { + validate(&rest)?; + let lease = ProfileLease::acquire(profile_dir, true)?; + let text = toml::to_string(&DaemonBootstrapSettings { rest })?; + let mut temporary = tempfile::NamedTempFile::new_in(lease.directory())?; + temporary.write_all(text.as_bytes())?; + temporary.as_file().sync_all()?; + temporary + .persist(lease.directory().join(PROFILE_SETTINGS_FILE)) + .map_err(|error| error.error) + .context("failed to publish bootstrap settings")?; + #[cfg(unix)] + std::fs::File::open(lease.directory())?.sync_all()?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn host_settings_keep_one_owner_and_do_not_initialize_metadata() { + let root = tempfile::tempdir().unwrap(); + let rest = RestBootstrapSettings { + bind_addr: Some("127.0.0.1:20017".parse().unwrap()), + api_key: "0123456789abcdef0123456789abcdef".into(), + web_root_dir: None, + }; + assert!(read(root.path()).unwrap().is_none()); + write(root.path(), rest.clone()).unwrap(); + assert_eq!(read(root.path()).unwrap().unwrap().api_key, rest.api_key); + assert!( + !root + .path() + .join(super::super::PROFILE_METADATA_FILE) + .exists() + ); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + assert_eq!( + std::fs::metadata(root.path().join(PROFILE_SETTINGS_FILE)) + .unwrap() + .permissions() + .mode() + & 0o777, + 0o600 + ); + } + let _lease = ProfileLease::acquire(root.path(), false).unwrap(); + assert!(write(root.path(), rest).is_err()); + assert!(read(root.path()).unwrap().is_some()); + } + + #[test] + fn invalid_settings_do_not_replace_the_existing_bootstrap() { + let root = tempfile::tempdir().unwrap(); + let mut rest = RestBootstrapSettings { + bind_addr: Some("127.0.0.1:20017".parse().unwrap()), + api_key: "0123456789abcdef0123456789abcdef".into(), + web_root_dir: None, + }; + write(root.path(), rest.clone()).unwrap(); + let original = std::fs::read(root.path().join(PROFILE_SETTINGS_FILE)).unwrap(); + rest.api_key = "short".into(); + assert!(write(root.path(), rest).is_err()); + assert_eq!( + std::fs::read(root.path().join(PROFILE_SETTINGS_FILE)).unwrap(), + original + ); + for text in [ + "[rest]\nbindAddr = '127.0.0.1:20017'", + "[rest]\nbind_addr = 'invalid'", + "[rest]\napi_key = 'short'", + "[unknown]\nvalue=1", + ] { + std::fs::write(root.path().join(PROFILE_SETTINGS_FILE), text).unwrap(); + assert!(read(root.path()).is_err()); + } + } +} diff --git a/crates/emulebb-daemon/src/lib.rs b/crates/emulebb-daemon/src/lib.rs index 06c7b4d5d..f924cb5fd 100644 --- a/crates/emulebb-daemon/src/lib.rs +++ b/crates/emulebb-daemon/src/lib.rs @@ -32,6 +32,7 @@ use tracing::{info, warn}; mod bind_config; mod discovery_bootstrap; +pub mod host_bootstrap; pub mod logging; mod profile_import; mod profile_ownership; @@ -69,6 +70,9 @@ const UNSAFE_REST_API_KEYS: &[&str] = &[ #[serde(default, rename_all = "camelCase")] pub struct DaemonProfile { pub profile_dir: PathBuf, + /// Application-owned payload storage, separate from private metadata. + #[serde(skip)] + pub host_transfer_root: Option, /// Global finished-file delivery directory (eMule Incoming folder). When a /// completed transfer has no category path, its payload is materialized here /// by its canonical name. Defaults to `/incoming` when unset. @@ -109,6 +113,7 @@ impl Default for DaemonProfile { let daemon = DaemonSettings::default(); Self { profile_dir: PathBuf::from("profile"), + host_transfer_root: None, incoming_dir: daemon.incoming_dir, p2p_bind_ip: daemon.p2p_bind_ip, p2p_bind_interface: daemon.p2p_bind_interface, @@ -183,6 +188,7 @@ impl DaemonProfile { let settings = load_settings(&metadata)?; let profile = Self { profile_dir, + host_transfer_root: None, incoming_dir: settings.daemon.incoming_dir, p2p_bind_ip: settings.daemon.p2p_bind_ip, p2p_bind_interface: settings.daemon.p2p_bind_interface, @@ -231,7 +237,9 @@ impl DaemonProfile { } pub fn transfer_root(&self) -> PathBuf { - self.profile_io_dir().join("transfers") + self.host_transfer_root + .clone() + .unwrap_or_else(|| self.profile_io_dir().join("transfers")) } /// Resolve the finished-file delivery directory: the configured From 61d1d8501e6a35984a309a3b6569a70a93aa2140 Mon Sep 17 00:00:00 2001 From: Threepwood-7 Date: Fri, 13 Nov 2026 11:35:32 +0100 Subject: [PATCH 04/74] feat(android): add lean service setup and browser launcher (RUST-FEAT-056) --- Cargo.lock | 1 + android/app/build.gradle.kts | 38 +++ android/app/src/main/AndroidManifest.xml | 35 ++ .../java/org/emulebb/android/BootReceiver.kt | 21 ++ .../java/org/emulebb/android/DaemonService.kt | 305 ++++++++++++++++++ .../java/org/emulebb/android/MainActivity.kt | 274 ++++++++++++++++ .../java/org/emulebb/android/NativeBridge.kt | 22 ++ .../java/org/emulebb/android/ShellFiles.kt | 79 +++++ android/app/src/main/res/values/ids.xml | 11 + android/build.gradle.kts | 9 + android/gradle.properties | 4 + android/settings.gradle.kts | 7 + crates/emulebb-android/Cargo.toml | 3 + crates/emulebb-android/src/lib.rs | 91 ++++++ crates/emulebb-daemon/src/host_bootstrap.rs | 8 + 15 files changed, 908 insertions(+) create mode 100644 android/app/build.gradle.kts create mode 100644 android/app/src/main/AndroidManifest.xml create mode 100644 android/app/src/main/java/org/emulebb/android/BootReceiver.kt create mode 100644 android/app/src/main/java/org/emulebb/android/DaemonService.kt create mode 100644 android/app/src/main/java/org/emulebb/android/MainActivity.kt create mode 100644 android/app/src/main/java/org/emulebb/android/NativeBridge.kt create mode 100644 android/app/src/main/java/org/emulebb/android/ShellFiles.kt create mode 100644 android/app/src/main/res/values/ids.xml create mode 100644 android/build.gradle.kts create mode 100644 android/gradle.properties create mode 100644 android/settings.gradle.kts diff --git a/Cargo.lock b/Cargo.lock index 8a66d12fd..4e40c4785 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -578,6 +578,7 @@ dependencies = [ "jni", "serde", "serde_json", + "tempfile", "tokio", ] diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts new file mode 100644 index 000000000..72a454b83 --- /dev/null +++ b/android/app/build.gradle.kts @@ -0,0 +1,38 @@ +plugins { id("com.android.application") } + +android { + namespace = "org.emulebb.android" + compileSdk = 37 + buildToolsVersion = "37.0.0" + defaultConfig { + applicationId = "org.emulebb.android" + minSdk = 34 + targetSdk = 37 + versionCode = providers.gradleProperty("emulebbVersionCode").get().toInt() + versionName = providers.gradleProperty("emulebbVersionName").get() + ndk { abiFilters.addAll(listOf("arm64-v8a", "x86_64")) } + } + buildFeatures { buildConfig = true } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 + } + sourceSets.getByName("main") { + jniLibs.srcDir(providers.gradleProperty("emulebbNativeDir").get()) + assets.srcDir(layout.buildDirectory.dir("generated/assets")) + } + packaging { + jniLibs { + useLegacyPackaging = false + // Exact source-bound native bytes are audited by the producer and APK harness. + keepDebugSymbols.add("**/*.so") + } + } +} + +val stageAssets by tasks.registering(Sync::class) { + into(layout.buildDirectory.dir("generated/assets")) + from(providers.gradleProperty("emulebbWebuiDir")) { into("webui") } + from(providers.gradleProperty("emulebbNoticesDir")) { into("licenses") } +} +tasks.named("preBuild") { dependsOn(stageAssets) } diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml new file mode 100644 index 000000000..266acec0d --- /dev/null +++ b/android/app/src/main/AndroidManifest.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/android/app/src/main/java/org/emulebb/android/BootReceiver.kt b/android/app/src/main/java/org/emulebb/android/BootReceiver.kt new file mode 100644 index 000000000..62305c646 --- /dev/null +++ b/android/app/src/main/java/org/emulebb/android/BootReceiver.kt @@ -0,0 +1,21 @@ +package org.emulebb.android + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent + +/** Runs after unlock; force-stop suppresses delivery according to Android rules. */ +class BootReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + val prefs = context.getSharedPreferences(DaemonService.PREFERENCES, Context.MODE_PRIVATE) + val enabled = prefs.getBoolean(DaemonService.DESIRED, false) + val allowed = when (intent.action) { + Intent.ACTION_BOOT_COMPLETED -> prefs.getBoolean(DaemonService.BOOT, false) + Intent.ACTION_MY_PACKAGE_REPLACED -> true + else -> false + } + if (enabled && allowed) { + context.startForegroundService(Intent(context, DaemonService::class.java).setAction(DaemonService.RESUME)) + } + } +} diff --git a/android/app/src/main/java/org/emulebb/android/DaemonService.kt b/android/app/src/main/java/org/emulebb/android/DaemonService.kt new file mode 100644 index 000000000..419033fc9 --- /dev/null +++ b/android/app/src/main/java/org/emulebb/android/DaemonService.kt @@ -0,0 +1,305 @@ +package org.emulebb.android + +import android.Manifest +import android.app.Notification +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent +import android.app.Service +import android.content.Context +import android.content.Intent +import android.content.pm.ServiceInfo +import android.content.pm.PackageManager +import android.net.ConnectivityManager +import android.net.Network +import android.os.Bundle +import android.os.Build +import android.os.Environment +import android.os.Handler +import android.os.IBinder +import android.os.Looper +import android.os.Message +import android.os.Messenger +import android.os.PowerManager +import android.os.SystemClock +import org.json.JSONObject +import java.util.concurrent.Executors +import java.util.concurrent.atomic.AtomicBoolean + +/** Owns service intent and the native runtime; no client settings are duplicated here. */ +class DaemonService : Service() { + companion object { + const val START = "org.emulebb.android.START" + const val STOP = "org.emulebb.android.STOP" + const val RESUME = "org.emulebb.android.RESUME" + const val STATUS = 1 + const val SET_BOOT_RESUME = 2 + const val PREFERENCES = "service-lifecycle" + const val DESIRED = "desired-running" + const val BOOT = "resume-on-boot" + const val LOCAL_NETWORK = Manifest.permission.ACCESS_LOCAL_NETWORK + private const val CHANNEL = "daemon" + private const val NOTICE = 1 + } + + private fun hasLocalNetworkPermission() = Build.VERSION.SDK_INT < 37 || + checkSelfPermission(LOCAL_NETWORK) == PackageManager.PERMISSION_GRANTED + + private val handler = Handler(Looper.getMainLooper()) + private val commands = Executors.newSingleThreadExecutor() + private val desired = AtomicBoolean(false) + private val prefs by lazy { getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE) } + private var phase = "Stopped" + private var error: String? = null + private var address: String? = null + private var starting = false + private var wakeLock: PowerManager.WakeLock? = null + private var network: Network? = null + private var restartForNetwork = false + private var stopFailures = 0 + private val connectivity by lazy { getSystemService(ConnectivityManager::class.java) } + + private val networkCallback = object : ConnectivityManager.NetworkCallback() { + override fun onAvailable(available: Network) { + handler.post { + val changed = network != null && network != available + network = available + if (changed && desired.get() && phase == "Running") { + restartForNetwork = true + phase = "Stopping" + requestNativeStop() + } + } + } + } + + private val messenger = Messenger(object : Handler(Looper.getMainLooper()) { + override fun handleMessage(message: Message) { + // The service is non-exported; Binder calls remain within this application's UID. + if (message.what == SET_BOOT_RESUME) { + if (!prefs.edit().putBoolean(BOOT, message.arg1 != 0).commit()) { + error = "Cannot save reboot preference" + } + } + if (message.what == STATUS || message.what == SET_BOOT_RESUME) { + val response = Message.obtain(null, STATUS).apply { + data = Bundle().apply { + putString("phase", phase) + putString("error", error) + putString("address", address) + putBoolean("resumeOnBoot", prefs.getBoolean(BOOT, false)) + } + } + try { message.replyTo?.send(response) } catch (_: android.os.RemoteException) { } + } + } + }) + + override fun onCreate() { + super.onCreate() + error = prefs.getString("last-error", null) + if (error != null) phase = "Failed" + getSystemService(NotificationManager::class.java).createNotificationChannel( + NotificationChannel(CHANNEL, "eMuleBB service", NotificationManager.IMPORTANCE_LOW)) + network = connectivity.activeNetwork + connectivity.registerDefaultNetworkCallback(networkCallback) + } + + override fun onBind(intent: Intent): IBinder = messenger.binder + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + if (intent?.action == STOP) { + stopExplicitly() + return START_NOT_STICKY + } + if (intent?.action != START && !prefs.getBoolean(DESIRED, false)) { + stopSelf() + return START_NOT_STICKY + } + if (intent?.action == START) { + prefs.edit().putInt("restarts", 0).putLong("restart-window", SystemClock.elapsedRealtime()).commit() + } else if (!admitRecovery()) { + fail("Repeated service termination; open the app and press Start to retry") + return START_NOT_STICKY + } + if (!prefs.edit().putBoolean(DESIRED, true).remove("last-error").commit()) { + fail("Cannot persist service state") + return START_NOT_STICKY + } + desired.set(true) + error = null + stopFailures = 0 + startForeground(NOTICE, notification("Starting"), ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE) + if (!Environment.isExternalStorageManager()) { + fail("All-files access is required. Open the app to grant it.") + return START_NOT_STICKY + } + if (!hasLocalNetworkPermission()) { + fail("Local-network permission is required. Open the app to grant it.") + return START_NOT_STICKY + } + if (wakeLock == null) { + wakeLock = getSystemService(PowerManager::class.java).newWakeLock( + PowerManager.PARTIAL_WAKE_LOCK, "$packageName:continuous-service").apply { + setReferenceCounted(false) + acquire() + } + } + if (!starting && phase != "Running" && phase != "Stopping") { + phase = "Starting" + val delay = if (intent?.action == START) 0L else (1L shl prefs.getInt("restarts", 1)) * 1_000 + handler.postDelayed({ if (desired.get() && !starting) startNative() }, delay) + } + return START_STICKY + } + + private fun admitRecovery(): Boolean { + val now = SystemClock.elapsedRealtime() + val window = prefs.getLong("restart-window", now) + val fresh = now < window || now - window > 600_000 + val attempts = if (fresh) 0 else prefs.getInt("restarts", 0) + return attempts < 3 && prefs.edit().putInt("restarts", attempts + 1) + .putLong("restart-window", if (fresh) now else window).commit() + } + + private fun startNative() { + starting = true + phase = "Starting" + commands.execute { + try { + ShellFiles.prepareStart(this) + // Commands are serialized: Stop queued during preparation runs after + // any Start already entering JNI, and cancellation before it skips Start. + if (desired.get()) ShellFiles.start(this) + handler.post { starting = false; handler.removeCallbacks(poll); handler.post(poll) } + } catch (failure: Exception) { + handler.post { starting = false; fail(failure.message ?: "Service startup failed") } + } + } + } + + private val poll = object : Runnable { + override fun run() { + try { + if (!Environment.isExternalStorageManager() && desired.get()) { + restartForNetwork = false + error = "All-files access was revoked; stopping the daemon" + setDesired(false) + requestNativeStop() + } + if (!hasLocalNetworkPermission() && desired.get()) { + restartForNetwork = false + error = "Local-network permission was revoked; stopping the daemon" + setDesired(false) + requestNativeStop() + } + val status = NativeBridge.call("status") as JSONObject + phase = status.getString("phase") + address = status.optString("address").takeUnless { it.isEmpty() || it == "null" } + if (phase == "Failed") { + error = status.optString("error").takeUnless { it.isBlank() || it == "null" } + ?: error ?: "Native runtime failed" + finishStopped() + return + } + if (!desired.get() && (phase == "Running" || phase == "Starting")) { + phase = "Stopping" + if (stopFailures in 1..2) requestNativeStop() + if (stopFailures >= 3) error = "Native Stop failed. Use Android app settings to force-stop eMuleBB." + } + if (phase == "Stopped") { + if (desired.get() && restartForNetwork) { + restartForNetwork = false + startNative() + return + } + // A graceful REST shutdown is an explicit stop, too. Process + // death never reaches this path, retaining restart intent. + finishStopped() + return + } + getSystemService(NotificationManager::class.java).notify(NOTICE, notification(phase)) + handler.postDelayed(this, 1_000) + } catch (failure: Exception) { + error = failure.message ?: "Cannot inspect native runtime" + setDesired(false) + restartForNetwork = false + phase = "Stopping" + if (!NativeBridge.isAvailable()) finishStopped() + else { + // Unknown native state is not Stopped. Retain the foreground + // owner until JNI reports drained; never start a second core. + if (stopFailures < 3) requestNativeStop() + else error = "Native shutdown could not be confirmed. Use Android app settings to force-stop eMuleBB." + handler.postDelayed(this, 1_000) + } + } + } + } + + private fun setDesired(enabled: Boolean) { + desired.set(enabled) + if (!prefs.edit().putBoolean(DESIRED, enabled).commit()) error = "Cannot persist service state" + } + + private fun stopExplicitly() { + setDesired(false) + restartForNetwork = false + phase = "Stopping" + requestNativeStop() + } + + private fun fail(message: String) { + error = message + setDesired(false) + restartForNetwork = false + phase = "Stopping" + requestNativeStop() + } + + private fun requestNativeStop() { + commands.execute { + try { NativeBridge.call("stop") } + catch (failure: Exception) { + handler.post { stopFailures++; error = failure.message ?: "Native Stop failed" } + } + handler.post { handler.removeCallbacks(poll); handler.post(poll) } + } + } + + private fun finishStopped() { + setDesired(false) + phase = if (error == null) "Stopped" else "Failed" + if (error != null) prefs.edit().putString("last-error", error).commit() + releaseWakeLock() + stopForeground(STOP_FOREGROUND_REMOVE) + stopSelf() + } + + private fun releaseWakeLock() { + wakeLock?.let { if (it.isHeld) it.release() } + wakeLock = null + } + + private fun notification(state: String): Notification { + val open = PendingIntent.getActivity(this, 0, Intent(this, MainActivity::class.java), PendingIntent.FLAG_IMMUTABLE) + val stop = PendingIntent.getService(this, 1, Intent(this, DaemonService::class.java).setAction(STOP), PendingIntent.FLAG_IMMUTABLE) + return Notification.Builder(this, CHANNEL) + .setSmallIcon(android.R.drawable.stat_sys_upload_done) + .setContentTitle("eMuleBB: $state") + .setContentText("Continuous eD2K/Kad peer service") + .setContentIntent(open).setOngoing(true) + .addAction(Notification.Action.Builder(null, "Stop", stop).build()).build() + } + + override fun onDestroy() { + handler.removeCallbacksAndMessages(null) + connectivity.unregisterNetworkCallback(networkCallback) + // Deliberately retain desired-running on OS teardown. Explicit Stop and + // graceful REST shutdown clear it before entering this path. + commands.execute { try { NativeBridge.call("stop") } catch (_: Exception) { } } + commands.shutdown() + releaseWakeLock() + super.onDestroy() + } +} diff --git a/android/app/src/main/java/org/emulebb/android/MainActivity.kt b/android/app/src/main/java/org/emulebb/android/MainActivity.kt new file mode 100644 index 000000000..470e1e1c1 --- /dev/null +++ b/android/app/src/main/java/org/emulebb/android/MainActivity.kt @@ -0,0 +1,274 @@ +package org.emulebb.android + +import android.Manifest +import android.app.Activity +import android.content.ClipData +import android.content.ClipDescription +import android.content.ClipboardManager +import android.content.ComponentName +import android.content.Context +import android.content.Intent +import android.content.ServiceConnection +import android.content.pm.PackageManager +import android.net.Uri +import android.os.Bundle +import android.os.Build +import android.os.Environment +import android.os.Handler +import android.os.IBinder +import android.os.Looper +import android.os.Message +import android.os.Messenger +import android.os.PersistableBundle +import android.os.PowerManager +import android.provider.Settings +import android.text.InputType +import android.view.View +import android.view.WindowManager +import android.widget.Button +import android.widget.CheckBox +import android.widget.EditText +import android.widget.LinearLayout +import android.widget.ScrollView +import android.widget.TextView +import android.widget.Toast +import java.util.concurrent.Executors + +/** Bootstrap and service control only. Client operations belong to the browser WebUI. */ +class MainActivity : Activity() { + private val handler = Handler(Looper.getMainLooper()) + private val setup = Executors.newSingleThreadExecutor() + private var remote: Messenger? = null + private var bound = false + private var resumed = false + private var busy = true + private var phase = "Stopped" + private var address: String? = null + private var updatingBoot = false + private lateinit var status: TextView + private lateinit var error: TextView + private lateinit var permissions: TextView + private lateinit var bind: EditText + private lateinit var key: EditText + private lateinit var start: Button + private lateinit var stop: Button + private lateinit var save: Button + private lateinit var regenerate: Button + private lateinit var open: Button + private lateinit var boot: CheckBox + + private val replies = Messenger(object : Handler(Looper.getMainLooper()) { + override fun handleMessage(message: Message) { + if (message.what != DaemonService.STATUS) return + phase = message.data.getString("phase", "Stopped") + address = message.data.getString("address") + status.text = phase + message.data.getString("error")?.let { error.text = it } + updatingBoot = true + boot.isChecked = message.data.getBoolean("resumeOnBoot") + updatingBoot = false + updateControls() + } + }) + + private val connection = object : ServiceConnection { + override fun onServiceConnected(name: ComponentName, service: IBinder) { + remote = Messenger(service) + queryStatus() + } + override fun onServiceDisconnected(name: ComponentName) { + remote = null + phase = "Starting" + status.text = "Service connection interrupted" + updateControls() + } + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + // The setup screen deliberately shows credentials; keep them out of + // screenshots, recent-task previews, autofill and saved instance state. + window.addFlags(WindowManager.LayoutParams.FLAG_SECURE) + val panel = LinearLayout(this).apply { + orientation = LinearLayout.VERTICAL + val padding = (16 * resources.displayMetrics.density).toInt() + setPadding(padding, padding, padding, padding) + } + fun label(text: String, id: Int = View.NO_ID) = TextView(this).apply { + this.text = text; this.id = id; panel.addView(this) + } + fun edit(label: String, id: Int) = EditText(this).apply { + this.id = id + hint = label + contentDescription = label + inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS + isSingleLine = true + isSaveEnabled = false + importantForAutofill = View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS + panel.addView(this) + } + fun button(text: String, id: Int = View.NO_ID, action: () -> Unit) = Button(this).apply { + this.text = text; this.id = id; setOnClickListener { action() }; panel.addView(this) + } + label("eMuleBB ${BuildConfig.VERSION_NAME}") + status = label("Stopped", R.id.service_status) + error = label("") + permissions = label("") + button("Grant all-files access") { + launchSettings(Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, Uri.parse("package:$packageName"))) + } + button("Allow notifications") { requestPermissions(arrayOf(Manifest.permission.POST_NOTIFICATIONS), 1) } + button("Allow local network") { + if (Build.VERSION.SDK_INT >= 37) requestPermissions(arrayOf(DaemonService.LOCAL_NETWORK), 2) + }.isEnabled = Build.VERSION.SDK_INT >= 37 + button("Allow continuous background operation") { + launchSettings(Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse("package:$packageName"))) + } + label("REST bind address") + bind = edit("REST bind address", R.id.bind_addr).apply { setText("127.0.0.1:20017") } + label("API key") + key = edit("API key", R.id.api_key) + button("Copy API key", R.id.copy_api_key) { + val clip = ClipData.newPlainText("eMuleBB API key", key.text.toString()) + clip.description.extras = PersistableBundle().apply { putBoolean(ClipDescription.EXTRA_IS_SENSITIVE, true) } + getSystemService(ClipboardManager::class.java).setPrimaryClip(clip) + Toast.makeText(this, "API key copied", Toast.LENGTH_SHORT).show() + } + regenerate = button("Regenerate API key") { key.setText(ShellFiles.newKey()) } + save = button("Save settings") { saveSettings(false) } + start = button("Start", R.id.start_service) { saveSettings(true) } + stop = button("Stop", R.id.stop_service) { + phase = "Stopping" + status.text = phase + updateControls() + startService(Intent(this, DaemonService::class.java).setAction(DaemonService.STOP)) + } + open = button("Open WebUI", R.id.open_webui) { + address?.let { endpoint -> + try { startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(ShellFiles.browserUrl(endpoint)))) } + catch (_: android.content.ActivityNotFoundException) { error.text = "Install a browser to open the WebUI" } + } + } + boot = CheckBox(this).apply { + id = R.id.resume_on_boot + text = "Resume after reboot" + setOnCheckedChangeListener { _, checked -> + if (!updatingBoot) send(DaemonService.SET_BOOT_RESUME, if (checked) 1 else 0) + } + panel.addView(this) + } + label("Client settings, sharing, transfers and diagnostics are available in the WebUI. Copy the API key into its sign-in screen.") + label("Continuous operation uses battery and network traffic. All-files access is required; notifications and battery exemption are recommended.") + setContentView(ScrollView(this).apply { addView(panel) }) + updateControls() + setup.execute { + try { + var settings = ShellFiles.readSettings(this) + if (settings == null) { + ShellFiles.saveSettings(this, "127.0.0.1:20017", ShellFiles.newKey(), ShellFiles.prepareWebui(this)) + settings = ShellFiles.readSettings(this) + } + val loaded = requireNotNull(settings) + handler.post { + if (!isDestroyed) { + bind.setText(loaded.getString("bind_addr")) + key.setText(loaded.getString("api_key")) + busy = false + updateControls() + } + } + } catch (failure: Exception) { + handler.post { + if (!isDestroyed) { + busy = false + error.text = failure.message ?: "Cannot load bootstrap settings" + updateControls() + } + } + } + } + } + + private fun launchSettings(intent: Intent) { + try { startActivity(intent) } + catch (_: android.content.ActivityNotFoundException) { error.text = "This device does not expose the requested permission settings" } + } + + private fun saveSettings(andStart: Boolean) { + if (busy || phase !in listOf("Stopped", "Failed")) return + if (andStart && !Environment.isExternalStorageManager()) { + error.text = "Grant all-files access before starting" + return + } + if (andStart && Build.VERSION.SDK_INT >= 37 && checkSelfPermission(DaemonService.LOCAL_NETWORK) != PackageManager.PERMISSION_GRANTED) { + error.text = "Allow local-network access before starting" + return + } + val bindValue = bind.text.toString().trim() + val keyValue = key.text.toString() + busy = true + error.text = "" + updateControls() + setup.execute { + try { + ShellFiles.saveSettings(this, bindValue, keyValue, ShellFiles.prepareWebui(this)) + handler.post { + if (!isDestroyed) { + busy = false + if (andStart && resumed) { + try { + startForegroundService(Intent(this, DaemonService::class.java).setAction(DaemonService.START)) + phase = "Starting" + status.text = phase + } catch (failure: Exception) { error.text = failure.message ?: "Cannot start service" } + } else if (andStart) error.text = "Return to the app and press Start" + updateControls() + } + } + } catch (failure: Exception) { + handler.post { if (!isDestroyed) { busy = false; error.text = failure.message; updateControls() } } + } + } + } + + private fun updateControls() { + val stopped = phase == "Stopped" || phase == "Failed" + bind.isEnabled = !busy && stopped + key.isEnabled = !busy && stopped + regenerate.isEnabled = !busy && stopped + save.isEnabled = !busy && stopped + start.isEnabled = !busy && stopped + stop.isEnabled = phase == "Starting" || phase == "Running" + open.isEnabled = phase == "Running" && address != null + boot.isEnabled = remote != null + } + + private fun send(what: Int, arg: Int = 0) { + try { remote?.send(Message.obtain(null, what, arg, 0).apply { replyTo = replies }) } + catch (_: android.os.RemoteException) { remote = null } + } + + private fun queryStatus() = send(DaemonService.STATUS) + private val poll = object : Runnable { + override fun run() { + queryStatus() + val notifications = checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED + val exemption = getSystemService(PowerManager::class.java).isIgnoringBatteryOptimizations(packageName) + val local = Build.VERSION.SDK_INT < 37 || checkSelfPermission(DaemonService.LOCAL_NETWORK) == PackageManager.PERMISSION_GRANTED + permissions.text = "Files: ${if (Environment.isExternalStorageManager()) "allowed" else "required"}; local network: ${if (local) "allowed" else "required"}; notifications: ${if (notifications) "allowed" else "disabled"}; battery exemption: ${if (exemption) "allowed" else "disabled"}" + handler.postDelayed(this, 1_000) + } + } + + override fun onStart() { + super.onStart() + bound = bindService(Intent(this, DaemonService::class.java), connection, Context.BIND_AUTO_CREATE) + } + override fun onResume() { super.onResume(); resumed = true; handler.post(poll) } + override fun onPause() { resumed = false; handler.removeCallbacks(poll); super.onPause() } + override fun onStop() { + if (bound) { unbindService(connection); bound = false; remote = null } + super.onStop() + } + override fun onDestroy() { handler.removeCallbacksAndMessages(null); setup.shutdown(); super.onDestroy() } +} diff --git a/android/app/src/main/java/org/emulebb/android/NativeBridge.kt b/android/app/src/main/java/org/emulebb/android/NativeBridge.kt new file mode 100644 index 000000000..5ccf85a30 --- /dev/null +++ b/android/app/src/main/java/org/emulebb/android/NativeBridge.kt @@ -0,0 +1,22 @@ +package org.emulebb.android + +import org.json.JSONObject + +/** Private, in-process JNI protocol. Never expose it through an Android component. */ +internal object NativeBridge { + private val loadFailure = try { + System.loadLibrary("emulebb_android") + null + } catch (_: LinkageError) { "Cannot load the packaged native daemon" } + private external fun request(payload: String): String + fun isAvailable(): Boolean = loadFailure == null + + fun call(op: String, vararg fields: Pair): Any? { + check(loadFailure == null) { requireNotNull(loadFailure) } + val input = JSONObject().put("op", op) + fields.forEach { input.put(it.first, it.second) } + val output = JSONObject(request(input.toString())) + check(output.getBoolean("ok")) { output.optString("error", "Native operation failed") } + return output.opt("data").takeUnless { it === JSONObject.NULL } + } +} diff --git a/android/app/src/main/java/org/emulebb/android/ShellFiles.kt b/android/app/src/main/java/org/emulebb/android/ShellFiles.kt new file mode 100644 index 000000000..3bc4a1d54 --- /dev/null +++ b/android/app/src/main/java/org/emulebb/android/ShellFiles.kt @@ -0,0 +1,79 @@ +package org.emulebb.android + +import android.content.Context +import android.os.Environment +import org.json.JSONObject +import java.io.File +import java.io.RandomAccessFile +import java.security.SecureRandom + +internal object ShellFiles { + fun profile(context: Context) = File(context.filesDir, "profile") + + fun newKey(): String = ByteArray(32).also { SecureRandom().nextBytes(it) } + .joinToString("") { "%02x".format(it.toInt() and 255) } + + /** Assets are immutable per APK version. Cross-process locking prevents partial reads. */ + fun prepareWebui(context: Context): File { + val root = File(context.filesDir, "webui-${BuildConfig.VERSION_CODE}") + RandomAccessFile(File(context.filesDir, "webui.lock"), "rw").use { lockFile -> + lockFile.channel.lock().use { + val ready = File(root, ".ready") + if (!ready.isFile) { + check(root.mkdirs() || root.isDirectory) { "Cannot create WebUI directory" } + copyAssets(context, "webui", root) + check(File(root, "index.html").isFile) { "Packaged WebUI is incomplete" } + ready.outputStream().use { it.fd.sync() } + } + } + } + return root + } + + private fun copyAssets(context: Context, source: String, target: File) { + val children = context.assets.list(source) ?: error("Missing packaged WebUI") + if (children.isEmpty()) { + context.assets.open(source).use { input -> + target.outputStream().use { output -> input.copyTo(output); output.fd.sync() } + } + } else { + check(target.mkdirs() || target.isDirectory) { "Cannot create WebUI assets" } + children.forEach { copyAssets(context, "$source/$it", File(target, it)) } + } + } + + fun readSettings(context: Context): JSONObject? = NativeBridge.call( + "readSettings", "profile_dir" to profile(context).absolutePath + ) as JSONObject? + + fun saveSettings(context: Context, bind: String, key: String, webui: File) { + NativeBridge.call("writeSettings", "profile_dir" to profile(context).absolutePath, + "bind_addr" to bind, "api_key" to key, "web_root_dir" to webui.absolutePath) + NativeBridge.call("pruneAssets", "profile_dir" to profile(context).absolutePath, + "current_web_root" to webui.absolutePath) + } + + fun prepareStart(context: Context) { + val settings = readSettings(context) ?: error("Open eMuleBB and complete setup before starting") + val webui = prepareWebui(context) + if (settings.optString("web_root_dir") != webui.absolutePath) { + saveSettings(context, settings.getString("bind_addr"), settings.getString("api_key"), webui) + } + } + + fun start(context: Context) { + val external = context.getExternalFilesDir(null) ?: error("Payload storage is unavailable") + val incoming = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "eMuleBB") + NativeBridge.call("start", "profile_dir" to profile(context).absolutePath, + "transfer_root" to File(external, "transfers").absolutePath, "incoming_dir" to incoming.absolutePath) + } + + fun browserUrl(address: String): String { + val authority = when { + address.startsWith("0.0.0.0:") -> "127.0.0.1:" + address.substringAfterLast(':') + address.startsWith("[::]:") -> "[::1]:" + address.substringAfterLast(':') + else -> address + } + return "http://$authority/" + } +} diff --git a/android/app/src/main/res/values/ids.xml b/android/app/src/main/res/values/ids.xml new file mode 100644 index 000000000..56d41e18e --- /dev/null +++ b/android/app/src/main/res/values/ids.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/android/build.gradle.kts b/android/build.gradle.kts new file mode 100644 index 000000000..09be6ef9a --- /dev/null +++ b/android/build.gradle.kts @@ -0,0 +1,9 @@ +plugins { id("com.android.application") version "9.2.0" apply false } + +val externalBuildRoot = providers.gradleProperty("androidBuildRoot").get() +require(file(externalBuildRoot).isAbsolute) { "androidBuildRoot must be an absolute output directory" } +require(!file(externalBuildRoot).canonicalFile.toPath().startsWith(projectDir.canonicalFile.toPath())) { + "Android build output must be outside the source project" +} +layout.buildDirectory.set(file(externalBuildRoot)) +subprojects { layout.buildDirectory.set(rootProject.layout.buildDirectory.dir(name)) } diff --git a/android/gradle.properties b/android/gradle.properties new file mode 100644 index 000000000..d24a3f6e1 --- /dev/null +++ b/android/gradle.properties @@ -0,0 +1,4 @@ +org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 +org.gradle.daemon=false +android.useAndroidX=false +android.nonTransitiveRClass=true diff --git a/android/settings.gradle.kts b/android/settings.gradle.kts new file mode 100644 index 000000000..1d6db57bb --- /dev/null +++ b/android/settings.gradle.kts @@ -0,0 +1,7 @@ +pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } } +dependencyResolutionManagement { + repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) + repositories { google(); mavenCentral() } +} +rootProject.name = "emulebb-android" +include(":app") diff --git a/crates/emulebb-android/Cargo.toml b/crates/emulebb-android/Cargo.toml index 1ed905c76..a3a23744f 100644 --- a/crates/emulebb-android/Cargo.toml +++ b/crates/emulebb-android/Cargo.toml @@ -20,3 +20,6 @@ jni.workspace = true serde = { workspace = true, features = ["derive"] } serde_json.workspace = true tokio = { workspace = true, features = ["rt-multi-thread", "sync", "time"] } + +[dev-dependencies] +tempfile.workspace = true diff --git a/crates/emulebb-android/src/lib.rs b/crates/emulebb-android/src/lib.rs index ac27b9847..b871a5a60 100644 --- a/crates/emulebb-android/src/lib.rs +++ b/crates/emulebb-android/src/lib.rs @@ -27,6 +27,10 @@ enum Request { api_key: String, web_root_dir: PathBuf, }, + PruneAssets { + profile_dir: PathBuf, + current_web_root: PathBuf, + }, Start { profile_dir: PathBuf, transfer_root: PathBuf, @@ -62,6 +66,15 @@ fn host() -> &'static Mutex { fn handle(request: Request) -> Result { match request { + Request::PruneAssets { + profile_dir, + current_web_root, + } => { + host_bootstrap::while_stopped(&profile_dir, || { + prune_assets(&profile_dir, ¤t_web_root) + })?; + Ok(Value::Null) + } Request::ReadSettings { profile_dir } => { ensure!( profile_dir.is_absolute(), @@ -185,6 +198,58 @@ fn handle(request: Request) -> Result { } } +fn prune_assets(profile_dir: &std::path::Path, current_web_root: &std::path::Path) -> Result<()> { + let parent = profile_dir + .parent() + .context("profile has no parent")? + .canonicalize()?; + let current = current_web_root.canonicalize()?; + ensure!( + current.parent() == Some(parent.as_path()), + "WebUI assets must belong to the application directory" + ); + ensure!( + current.join(".ready").is_file(), + "current WebUI assets are not ready" + ); + let mut versions = Vec::new(); + for entry in std::fs::read_dir(&parent)? { + let entry = entry?; + if !entry.file_type()?.is_dir() { + continue; + } + let name = entry.file_name(); + let Some(version) = name + .to_str() + .and_then(|name| name.strip_prefix("webui-")) + .and_then(|value| value.parse::().ok()) + else { + continue; + }; + versions.push((version, entry.path())); + } + versions.sort_by(|left, right| right.0.cmp(&left.0)); + let mut retained_previous = false; + for (_, path) in versions { + if path == current { + continue; + } + if !retained_previous { + retained_previous = true; + continue; + } + // WHY: source assets are disposable, but metadata/payloads are not. + // Limit cleanup to real, directly owned version directories while the + // profile lease excludes every running daemon, including other processes. + ensure!( + path.canonicalize()?.parent() == Some(parent.as_path()), + "asset cleanup escaped application directory" + ); + std::fs::remove_dir_all(path)?; + } + Ok(()) +} + const _: jni::NativeMethod = native_method! { java_type = "org.emulebb.android.NativeBridge", extern fn request(payload: JString) -> JString, @@ -211,6 +276,32 @@ fn request<'local>( mod tests { use super::*; + #[test] + fn asset_cleanup_retains_current_previous_and_unrelated_data() { + let root = tempfile::tempdir().unwrap(); + let profile = root.path().join("profile"); + for name in [ + "profile", + "webui-1", + "webui-2", + "webui-3", + "webui-custom", + "payloads", + ] { + std::fs::create_dir(root.path().join(name)).unwrap(); + } + std::fs::write(root.path().join("webui-3/.ready"), b"").unwrap(); + handle(Request::PruneAssets { + profile_dir: profile, + current_web_root: root.path().join("webui-3"), + }) + .unwrap(); + assert!(!root.path().join("webui-1").exists()); + for name in ["profile", "webui-2", "webui-3", "webui-custom", "payloads"] { + assert!(root.path().join(name).is_dir()); + } + } + #[test] fn private_host_protocol_rejects_unknown_fields_and_retired_commands() { assert!(serde_json::from_str::(r#"{"op":"status","api_key":"hidden"}"#).is_err()); diff --git a/crates/emulebb-daemon/src/host_bootstrap.rs b/crates/emulebb-daemon/src/host_bootstrap.rs index f94598393..4c4f8bfff 100644 --- a/crates/emulebb-daemon/src/host_bootstrap.rs +++ b/crates/emulebb-daemon/src/host_bootstrap.rs @@ -9,6 +9,13 @@ use super::{ RestBootstrapSettings, }; +/// Execute application-owned maintenance only while no daemon owns this profile. +/// The operation must finish before returning; ownership never escapes this call. +pub fn while_stopped(profile_dir: &Path, operation: impl FnOnce() -> Result) -> Result { + let _lease = ProfileLease::acquire(profile_dir, true)?; + operation() +} + /// Read existing settings without opening or changing the metadata database. /// Atomic bootstrap publication permits read-only setup/status UI while the /// daemon owns the profile. Only writes require an exclusive profile lease. @@ -110,6 +117,7 @@ mod tests { original ); for text in [ + "[rest]\napi_key = '0123456789abcdef0123456789abcdef'", "[rest]\nbindAddr = '127.0.0.1:20017'", "[rest]\nbind_addr = 'invalid'", "[rest]\napi_key = 'short'", From c2c2fb781c4d54c00b6ae1fd70bc55d65d16e782 Mon Sep 17 00:00:00 2001 From: Threepwood-7 Date: Fri, 13 Nov 2026 11:37:18 +0100 Subject: [PATCH 05/74] fix(android): honor system task-manager Stop across recovery (RUST-FEAT-056) --- .../java/org/emulebb/android/BootReceiver.kt | 2 +- .../java/org/emulebb/android/DaemonService.kt | 8 +++-- .../org/emulebb/android/ServiceRecovery.kt | 29 +++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 android/app/src/main/java/org/emulebb/android/ServiceRecovery.kt diff --git a/android/app/src/main/java/org/emulebb/android/BootReceiver.kt b/android/app/src/main/java/org/emulebb/android/BootReceiver.kt index 62305c646..df7da826f 100644 --- a/android/app/src/main/java/org/emulebb/android/BootReceiver.kt +++ b/android/app/src/main/java/org/emulebb/android/BootReceiver.kt @@ -8,7 +8,7 @@ import android.content.Intent class BootReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val prefs = context.getSharedPreferences(DaemonService.PREFERENCES, Context.MODE_PRIVATE) - val enabled = prefs.getBoolean(DaemonService.DESIRED, false) + val enabled = ServiceRecovery.mayResume(context) val allowed = when (intent.action) { Intent.ACTION_BOOT_COMPLETED -> prefs.getBoolean(DaemonService.BOOT, false) Intent.ACTION_MY_PACKAGE_REPLACED -> true diff --git a/android/app/src/main/java/org/emulebb/android/DaemonService.kt b/android/app/src/main/java/org/emulebb/android/DaemonService.kt index 419033fc9..b7bfb13fc 100644 --- a/android/app/src/main/java/org/emulebb/android/DaemonService.kt +++ b/android/app/src/main/java/org/emulebb/android/DaemonService.kt @@ -112,12 +112,16 @@ class DaemonService : Service() { stopExplicitly() return START_NOT_STICKY } - if (intent?.action != START && !prefs.getBoolean(DESIRED, false)) { + if (intent?.action != START && !ServiceRecovery.mayResume(this)) { stopSelf() return START_NOT_STICKY } if (intent?.action == START) { - prefs.edit().putInt("restarts", 0).putLong("restart-window", SystemClock.elapsedRealtime()).commit() + if (!prefs.edit().putInt("restarts", 0).putLong("restart-window", SystemClock.elapsedRealtime()) + .putLong(ServiceRecovery.LAST_EXPLICIT_START, System.currentTimeMillis()).commit()) { + fail("Cannot persist explicit service start") + return START_NOT_STICKY + } } else if (!admitRecovery()) { fail("Repeated service termination; open the app and press Start to retry") return START_NOT_STICKY diff --git a/android/app/src/main/java/org/emulebb/android/ServiceRecovery.kt b/android/app/src/main/java/org/emulebb/android/ServiceRecovery.kt new file mode 100644 index 000000000..d84e3c64e --- /dev/null +++ b/android/app/src/main/java/org/emulebb/android/ServiceRecovery.kt @@ -0,0 +1,29 @@ +package org.emulebb.android + +import android.app.ActivityManager +import android.app.ApplicationExitInfo +import android.content.Context + +/** Android's Active apps Stop kills the process without any service callback. */ +internal object ServiceRecovery { + const val LAST_EXPLICIT_START = "last-explicit-start" + + fun mayResume(context: Context): Boolean { + val prefs = context.getSharedPreferences(DaemonService.PREFERENCES, Context.MODE_PRIVATE) + if (!prefs.getBoolean(DaemonService.DESIRED, false)) return false + val started = prefs.getLong(LAST_EXPLICIT_START, 0) + val exits = context.getSystemService(ActivityManager::class.java) + .getHistoricalProcessExitReasons(context.packageName, 0, 0) + val stoppedByUser = exits.any { + it.processName == "${context.packageName}:daemon" && + it.reason == ApplicationExitInfo.REASON_USER_REQUESTED && it.timestamp >= started + } + if (stoppedByUser) { + // Only daemon-process exits count: dismissing the setup activity's + // task must not cancel a user-enabled foreground peer service. + prefs.edit().putBoolean(DaemonService.DESIRED, false).commit() + return false + } + return true + } +} From 2a28df8e9d1ca0d50f22faa206e61b000a294ab1 Mon Sep 17 00:00:00 2001 From: Threepwood-7 Date: Fri, 13 Nov 2026 11:38:33 +0100 Subject: [PATCH 06/74] fix(android): surface platform background restrictions (RUST-FEAT-056) --- .../app/src/main/java/org/emulebb/android/BootReceiver.kt | 6 +++++- .../app/src/main/java/org/emulebb/android/DaemonService.kt | 1 + .../app/src/main/java/org/emulebb/android/MainActivity.kt | 7 ++++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/android/app/src/main/java/org/emulebb/android/BootReceiver.kt b/android/app/src/main/java/org/emulebb/android/BootReceiver.kt index df7da826f..8dd682943 100644 --- a/android/app/src/main/java/org/emulebb/android/BootReceiver.kt +++ b/android/app/src/main/java/org/emulebb/android/BootReceiver.kt @@ -15,7 +15,11 @@ class BootReceiver : BroadcastReceiver() { else -> false } if (enabled && allowed) { - context.startForegroundService(Intent(context, DaemonService::class.java).setAction(DaemonService.RESUME)) + try { + context.startForegroundService(Intent(context, DaemonService::class.java).setAction(DaemonService.RESUME)) + } catch (_: Exception) { + prefs.edit().putString("last-error", "Android blocked automatic service startup. Open the app and check its battery settings.").commit() + } } } } diff --git a/android/app/src/main/java/org/emulebb/android/DaemonService.kt b/android/app/src/main/java/org/emulebb/android/DaemonService.kt index b7bfb13fc..8891ac464 100644 --- a/android/app/src/main/java/org/emulebb/android/DaemonService.kt +++ b/android/app/src/main/java/org/emulebb/android/DaemonService.kt @@ -97,6 +97,7 @@ class DaemonService : Service() { override fun onCreate() { super.onCreate() + ServiceRecovery.mayResume(this) error = prefs.getString("last-error", null) if (error != null) phase = "Failed" getSystemService(NotificationManager::class.java).createNotificationChannel( diff --git a/android/app/src/main/java/org/emulebb/android/MainActivity.kt b/android/app/src/main/java/org/emulebb/android/MainActivity.kt index 470e1e1c1..094866998 100644 --- a/android/app/src/main/java/org/emulebb/android/MainActivity.kt +++ b/android/app/src/main/java/org/emulebb/android/MainActivity.kt @@ -2,6 +2,7 @@ package org.emulebb.android import android.Manifest import android.app.Activity +import android.app.ActivityManager import android.content.ClipData import android.content.ClipDescription import android.content.ClipboardManager @@ -124,6 +125,9 @@ class MainActivity : Activity() { button("Allow continuous background operation") { launchSettings(Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse("package:$packageName"))) } + button("App battery settings") { + launchSettings(Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:$packageName"))) + } label("REST bind address") bind = edit("REST bind address", R.id.bind_addr).apply { setText("127.0.0.1:20017") } label("API key") @@ -255,7 +259,8 @@ class MainActivity : Activity() { val notifications = checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED val exemption = getSystemService(PowerManager::class.java).isIgnoringBatteryOptimizations(packageName) val local = Build.VERSION.SDK_INT < 37 || checkSelfPermission(DaemonService.LOCAL_NETWORK) == PackageManager.PERMISSION_GRANTED - permissions.text = "Files: ${if (Environment.isExternalStorageManager()) "allowed" else "required"}; local network: ${if (local) "allowed" else "required"}; notifications: ${if (notifications) "allowed" else "disabled"}; battery exemption: ${if (exemption) "allowed" else "disabled"}" + val restricted = getSystemService(ActivityManager::class.java).isBackgroundRestricted + permissions.text = "Files: ${if (Environment.isExternalStorageManager()) "allowed" else "required"}; local network: ${if (local) "allowed" else "required"}; notifications: ${if (notifications) "allowed" else "disabled"}; battery exemption: ${if (exemption) "allowed" else "disabled"}; background restriction: ${if (restricted) "enabled" else "disabled"}" handler.postDelayed(this, 1_000) } } From 2bdbf8e18487565d9ff948be9dfb1f89c406e327 Mon Sep 17 00:00:00 2001 From: Threepwood-7 Date: Fri, 13 Nov 2026 11:42:25 +0100 Subject: [PATCH 07/74] build(android): align Gradle with the pinned Android SDK and NDK (RUST-FEAT-056) --- android/app/build.gradle.kts | 3 ++- android/build.gradle.kts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 72a454b83..1ff1ef5f2 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -2,8 +2,9 @@ plugins { id("com.android.application") } android { namespace = "org.emulebb.android" - compileSdk = 37 + compileSdk { version = release(37) { minorApiLevel = 2 } } buildToolsVersion = "37.0.0" + ndkVersion = "30.0.16248370" defaultConfig { applicationId = "org.emulebb.android" minSdk = 34 diff --git a/android/build.gradle.kts b/android/build.gradle.kts index 09be6ef9a..c5daa4108 100644 --- a/android/build.gradle.kts +++ b/android/build.gradle.kts @@ -1,4 +1,4 @@ -plugins { id("com.android.application") version "9.2.0" apply false } +plugins { id("com.android.application") version "9.5.1" apply false } val externalBuildRoot = providers.gradleProperty("androidBuildRoot").get() require(file(externalBuildRoot).isAbsolute) { "androidBuildRoot must be an absolute output directory" } From d43ad8d5ee5c1b38a2091b335126712d05c23f6d Mon Sep 17 00:00:00 2001 From: Threepwood-7 Date: Fri, 13 Nov 2026 11:45:00 +0100 Subject: [PATCH 08/74] fix(android): report foreground promotion failures before startup (RUST-FEAT-056) --- .../main/java/org/emulebb/android/DaemonService.kt | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/android/app/src/main/java/org/emulebb/android/DaemonService.kt b/android/app/src/main/java/org/emulebb/android/DaemonService.kt index 8891ac464..d27e0b95a 100644 --- a/android/app/src/main/java/org/emulebb/android/DaemonService.kt +++ b/android/app/src/main/java/org/emulebb/android/DaemonService.kt @@ -134,7 +134,19 @@ class DaemonService : Service() { desired.set(true) error = null stopFailures = 0 - startForeground(NOTICE, notification("Starting"), ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE) + try { + startForeground(NOTICE, notification("Starting"), ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE) + } catch (failure: Exception) { + // Promotion happens before native startup. A platform/AppOp denial + // must be visible rather than becoming an automatic crash loop. + error = "Android refused the foreground service: ${failure.javaClass.simpleName}" + setDesired(false) + phase = "Failed" + prefs.edit().putString("last-error", error).commit() + releaseWakeLock() + stopSelf() + return START_NOT_STICKY + } if (!Environment.isExternalStorageManager()) { fail("All-files access is required. Open the app to grant it.") return START_NOT_STICKY From fb629d2b21d9ccaae759bf147a431414f878067e Mon Sep 17 00:00:00 2001 From: Threepwood-7 Date: Fri, 13 Nov 2026 11:51:50 +0100 Subject: [PATCH 09/74] fix(android): retain canonical logging across service restarts (RUST-FEAT-056) --- crates/emulebb-android/src/lib.rs | 27 ++++++++ crates/emulebb-daemon/src/logging.rs | 66 ++++++++++++++++--- .../emulebb-daemon/tests/process_logging.rs | 28 ++++++++ 3 files changed, 111 insertions(+), 10 deletions(-) create mode 100644 crates/emulebb-daemon/tests/process_logging.rs diff --git a/crates/emulebb-android/src/lib.rs b/crates/emulebb-android/src/lib.rs index b871a5a60..59abdf1e0 100644 --- a/crates/emulebb-android/src/lib.rs +++ b/crates/emulebb-android/src/lib.rs @@ -64,6 +64,32 @@ fn host() -> &'static Mutex { HOST.get_or_init(Mutex::default) } +struct ProcessLogging { + profile: PathBuf, + _guard: emulebb_daemon::logging::LoggingGuard, +} + +fn ensure_process_logging(profile: &std::path::Path) -> Result<()> { + static LOGGING: OnceLock>> = OnceLock::new(); + let mut logging = LOGGING + .get_or_init(Mutex::default) + .lock() + .map_err(|_| anyhow::anyhow!("process logging state unavailable"))?; + let profile = profile.canonicalize()?; + if let Some(existing) = logging.as_ref() { + ensure!( + existing.profile == profile, + "one Android service process owns one logging profile" + ); + } else { + *logging = Some(ProcessLogging { + _guard: emulebb_daemon::logging::init_process(&profile)?, + profile, + }); + } + Ok(()) +} + fn handle(request: Request) -> Result { match request { Request::PruneAssets { @@ -135,6 +161,7 @@ fn handle(request: Request) -> Result { let outcome = std::panic::catch_unwind(|| -> Result<()> { if shutdown.subscribe().is_requested() { return Ok(()); } let mut profile = DaemonProfile::load(Some(profile_dir))?; + ensure_process_logging(profile.profile_io_dir())?; profile.host_transfer_root = Some(transfer_root); if profile.incoming_dir.is_none() { profile.incoming_dir = Some(incoming_dir); } let runtime = tokio::runtime::Builder::new_multi_thread().enable_all().build()?; diff --git a/crates/emulebb-daemon/src/logging.rs b/crates/emulebb-daemon/src/logging.rs index f28fbd7ea..86c10844f 100644 --- a/crates/emulebb-daemon/src/logging.rs +++ b/crates/emulebb-daemon/src/logging.rs @@ -110,12 +110,17 @@ pub struct LoggingGuard { error_counter: Option, monitor_shutdown: Option>, monitor_task: Option>, + process_monitor: Option<(std::sync::mpsc::Sender<()>, std::thread::JoinHandle<()>)>, shutdown_reported: bool, } impl LoggingGuard { /// Stops drop monitoring, reports any loss, and flushes retained logs. pub async fn shutdown(mut self) { + if let Some((shutdown, task)) = self.process_monitor.take() { + let _ = shutdown.send(()); + let _ = tokio::task::spawn_blocking(move || task.join()).await; + } if let Some(shutdown) = self.monitor_shutdown.take() { let _ = shutdown.send(true); } @@ -138,6 +143,8 @@ impl LoggingGuard { impl Drop for LoggingGuard { fn drop(&mut self) { + // Dropping the sender wakes the optional process monitor immediately. + self.process_monitor.take(); if let Some(task) = self.monitor_task.take() { task.abort(); } @@ -157,6 +164,16 @@ impl Drop for LoggingGuard { /// The profile must already be validated before this function is called. File /// setup failure is non-fatal: console and REST logging remain available. pub fn init(profile_dir: &Path) -> Result { + init_inner(profile_dir, false) +} + +/// Install the canonical subscriber for a host process spanning several Tokio +/// runtimes. Retain its guard for that process's lifetime, not one daemon run. +pub fn init_process(profile_dir: &Path) -> Result { + init_inner(profile_dir, true) +} + +fn init_inner(profile_dir: &Path, process_owned: bool) -> Result { let (filter_spec, filter_warning) = read_filter_spec(); let mut file_warning = None; let file_output = match create_file_output(profile_dir) { @@ -213,7 +230,7 @@ pub fn init(profile_dir: &Path) -> Result { ); } - let (monitor_shutdown, monitor_task) = match error_counter.as_ref() { + let (monitor_shutdown, monitor_task) = match error_counter.as_ref().filter(|_| !process_owned) { Some(counter) => { let (shutdown_tx, shutdown_rx) = watch::channel(false); let task = tokio::spawn(monitor_dropped_lines(counter.clone(), shutdown_rx)); @@ -221,12 +238,37 @@ pub fn init(profile_dir: &Path) -> Result { } None => (None, None), }; + let process_monitor = error_counter + .as_ref() + .filter(|_| process_owned) + .and_then(|counter| { + let (sender, receiver) = std::sync::mpsc::channel(); + let counter = counter.clone(); + match std::thread::Builder::new() + .name("emulebb-log-health".into()) + .spawn(move || { + let mut last_dropped = 0; + while matches!( + receiver.recv_timeout(DROP_MONITOR_INTERVAL), + Err(std::sync::mpsc::RecvTimeoutError::Timeout) + ) { + report_dropped_lines(&counter, &mut last_dropped); + } + }) { + Ok(task) => Some((sender, task)), + Err(error) => { + tracing::warn!(%error, "persistent log drop monitoring is unavailable"); + None + } + } + }); Ok(LoggingGuard { worker_guard, error_counter, monitor_shutdown, monitor_task, + process_monitor, shutdown_reported: false, }) } @@ -294,20 +336,24 @@ async fn monitor_dropped_lines(counter: ErrorCounter, mut shutdown: watch::Recei } } _ = interval.tick() => { - let dropped = counter.dropped_lines(); - if dropped > last_dropped { - tracing::warn!( - dropped_file_log_lines = dropped, - dropped_since_last_report = dropped - last_dropped, - "persistent log writer is dropping events" - ); - last_dropped = dropped; - } + report_dropped_lines(&counter, &mut last_dropped); } } } } +fn report_dropped_lines(counter: &ErrorCounter, last_dropped: &mut usize) { + let dropped = counter.dropped_lines(); + if dropped > *last_dropped { + tracing::warn!( + dropped_file_log_lines = dropped, + dropped_since_last_report = dropped - *last_dropped, + "persistent log writer is dropping events" + ); + *last_dropped = dropped; + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/emulebb-daemon/tests/process_logging.rs b/crates/emulebb-daemon/tests/process_logging.rs new file mode 100644 index 000000000..5a0227418 --- /dev/null +++ b/crates/emulebb-daemon/tests/process_logging.rs @@ -0,0 +1,28 @@ +//! Process-owned logging survives replacement of an application-owned Tokio runtime. + +#[test] +fn process_logging_keeps_the_canonical_writer_across_runtime_restarts() { + let root = tempfile::tempdir().unwrap(); + let first = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + let guard = + first.block_on(async { emulebb_daemon::logging::init_process(root.path()).unwrap() }); + drop(first); + tracing::info!("first application runtime released"); + let second = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + second.block_on(async { + tracing::info!("second application runtime active"); + guard.shutdown().await; + }); + let mut retained = String::new(); + for entry in std::fs::read_dir(root.path().join("logs")).unwrap() { + retained.push_str(&std::fs::read_to_string(entry.unwrap().path()).unwrap()); + } + assert!(retained.contains("first application runtime released")); + assert!(retained.contains("second application runtime active")); +} From 09f2a35c3d13ef088aa284facb8e33e13bee34ae Mon Sep 17 00:00:00 2001 From: Threepwood-7 Date: Fri, 13 Nov 2026 11:52:38 +0100 Subject: [PATCH 10/74] fix(android): handle SDK nullable platform values explicitly (RUST-FEAT-056) --- .../src/main/java/org/emulebb/android/DaemonService.kt | 8 ++++---- .../app/src/main/java/org/emulebb/android/MainActivity.kt | 8 ++++---- .../src/main/java/org/emulebb/android/ServiceRecovery.kt | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/android/app/src/main/java/org/emulebb/android/DaemonService.kt b/android/app/src/main/java/org/emulebb/android/DaemonService.kt index d27e0b95a..c6bd53c5e 100644 --- a/android/app/src/main/java/org/emulebb/android/DaemonService.kt +++ b/android/app/src/main/java/org/emulebb/android/DaemonService.kt @@ -57,7 +57,7 @@ class DaemonService : Service() { private var network: Network? = null private var restartForNetwork = false private var stopFailures = 0 - private val connectivity by lazy { getSystemService(ConnectivityManager::class.java) } + private val connectivity by lazy { requireNotNull(getSystemService(ConnectivityManager::class.java)) } private val networkCallback = object : ConnectivityManager.NetworkCallback() { override fun onAvailable(available: Network) { @@ -100,7 +100,7 @@ class DaemonService : Service() { ServiceRecovery.mayResume(this) error = prefs.getString("last-error", null) if (error != null) phase = "Failed" - getSystemService(NotificationManager::class.java).createNotificationChannel( + requireNotNull(getSystemService(NotificationManager::class.java)).createNotificationChannel( NotificationChannel(CHANNEL, "eMuleBB service", NotificationManager.IMPORTANCE_LOW)) network = connectivity.activeNetwork connectivity.registerDefaultNetworkCallback(networkCallback) @@ -156,7 +156,7 @@ class DaemonService : Service() { return START_NOT_STICKY } if (wakeLock == null) { - wakeLock = getSystemService(PowerManager::class.java).newWakeLock( + wakeLock = requireNotNull(getSystemService(PowerManager::class.java)).newWakeLock( PowerManager.PARTIAL_WAKE_LOCK, "$packageName:continuous-service").apply { setReferenceCounted(false) acquire() @@ -235,7 +235,7 @@ class DaemonService : Service() { finishStopped() return } - getSystemService(NotificationManager::class.java).notify(NOTICE, notification(phase)) + requireNotNull(getSystemService(NotificationManager::class.java)).notify(NOTICE, notification(phase)) handler.postDelayed(this, 1_000) } catch (failure: Exception) { error = failure.message ?: "Cannot inspect native runtime" diff --git a/android/app/src/main/java/org/emulebb/android/MainActivity.kt b/android/app/src/main/java/org/emulebb/android/MainActivity.kt index 094866998..34335214c 100644 --- a/android/app/src/main/java/org/emulebb/android/MainActivity.kt +++ b/android/app/src/main/java/org/emulebb/android/MainActivity.kt @@ -61,7 +61,7 @@ class MainActivity : Activity() { private val replies = Messenger(object : Handler(Looper.getMainLooper()) { override fun handleMessage(message: Message) { if (message.what != DaemonService.STATUS) return - phase = message.data.getString("phase", "Stopped") + phase = message.data.getString("phase") ?: "Stopped" address = message.data.getString("address") status.text = phase message.data.getString("error")?.let { error.text = it } @@ -135,7 +135,7 @@ class MainActivity : Activity() { button("Copy API key", R.id.copy_api_key) { val clip = ClipData.newPlainText("eMuleBB API key", key.text.toString()) clip.description.extras = PersistableBundle().apply { putBoolean(ClipDescription.EXTRA_IS_SENSITIVE, true) } - getSystemService(ClipboardManager::class.java).setPrimaryClip(clip) + requireNotNull(getSystemService(ClipboardManager::class.java)).setPrimaryClip(clip) Toast.makeText(this, "API key copied", Toast.LENGTH_SHORT).show() } regenerate = button("Regenerate API key") { key.setText(ShellFiles.newKey()) } @@ -257,9 +257,9 @@ class MainActivity : Activity() { override fun run() { queryStatus() val notifications = checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED - val exemption = getSystemService(PowerManager::class.java).isIgnoringBatteryOptimizations(packageName) + val exemption = requireNotNull(getSystemService(PowerManager::class.java)).isIgnoringBatteryOptimizations(packageName) val local = Build.VERSION.SDK_INT < 37 || checkSelfPermission(DaemonService.LOCAL_NETWORK) == PackageManager.PERMISSION_GRANTED - val restricted = getSystemService(ActivityManager::class.java).isBackgroundRestricted + val restricted = requireNotNull(getSystemService(ActivityManager::class.java)).isBackgroundRestricted permissions.text = "Files: ${if (Environment.isExternalStorageManager()) "allowed" else "required"}; local network: ${if (local) "allowed" else "required"}; notifications: ${if (notifications) "allowed" else "disabled"}; battery exemption: ${if (exemption) "allowed" else "disabled"}; background restriction: ${if (restricted) "enabled" else "disabled"}" handler.postDelayed(this, 1_000) } diff --git a/android/app/src/main/java/org/emulebb/android/ServiceRecovery.kt b/android/app/src/main/java/org/emulebb/android/ServiceRecovery.kt index d84e3c64e..b8159d70b 100644 --- a/android/app/src/main/java/org/emulebb/android/ServiceRecovery.kt +++ b/android/app/src/main/java/org/emulebb/android/ServiceRecovery.kt @@ -12,7 +12,7 @@ internal object ServiceRecovery { val prefs = context.getSharedPreferences(DaemonService.PREFERENCES, Context.MODE_PRIVATE) if (!prefs.getBoolean(DaemonService.DESIRED, false)) return false val started = prefs.getLong(LAST_EXPLICIT_START, 0) - val exits = context.getSystemService(ActivityManager::class.java) + val exits = requireNotNull(context.getSystemService(ActivityManager::class.java)) .getHistoricalProcessExitReasons(context.packageName, 0, 0) val stoppedByUser = exits.any { it.processName == "${context.packageName}:daemon" && From 57dd18d88b28791be9704e7a49eba18f6121110e Mon Sep 17 00:00:00 2001 From: Threepwood-7 Date: Fri, 13 Nov 2026 12:20:28 +0100 Subject: [PATCH 11/74] fix(android): wire generated assets through the variant API (RUST-FEAT-056) --- android/app/build.gradle.kts | 17 ++++++++++++----- android/gradle.properties | 1 - 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 1ff1ef5f2..b4a608de5 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -1,5 +1,10 @@ plugins { id("com.android.application") } +abstract class StageAssets : Sync() { + @get:OutputDirectory + abstract val outputDirectory: DirectoryProperty +} + android { namespace = "org.emulebb.android" compileSdk { version = release(37) { minorApiLevel = 2 } } @@ -19,8 +24,7 @@ android { targetCompatibility = JavaVersion.VERSION_17 } sourceSets.getByName("main") { - jniLibs.srcDir(providers.gradleProperty("emulebbNativeDir").get()) - assets.srcDir(layout.buildDirectory.dir("generated/assets")) + jniLibs.directories.add(providers.gradleProperty("emulebbNativeDir").get()) } packaging { jniLibs { @@ -31,9 +35,12 @@ android { } } -val stageAssets by tasks.registering(Sync::class) { - into(layout.buildDirectory.dir("generated/assets")) +val stageAssets = tasks.register("stageAssets") { + outputDirectory.set(layout.buildDirectory.dir("generated/assets")) + into(outputDirectory) from(providers.gradleProperty("emulebbWebuiDir")) { into("webui") } from(providers.gradleProperty("emulebbNoticesDir")) { into("licenses") } } -tasks.named("preBuild") { dependsOn(stageAssets) } +androidComponents.onVariants { variant -> + variant.sources.assets?.addGeneratedSourceDirectory(stageAssets, StageAssets::outputDirectory) +} diff --git a/android/gradle.properties b/android/gradle.properties index d24a3f6e1..aec357bc5 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -1,4 +1,3 @@ org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 org.gradle.daemon=false -android.useAndroidX=false android.nonTransitiveRClass=true From 0cd2262f80333379a340acac1955c6373c970c13 Mon Sep 17 00:00:00 2001 From: Threepwood-7 Date: Fri, 13 Nov 2026 12:31:36 +0100 Subject: [PATCH 12/74] feat(android): hand off eD2K file links for explicit WebUI confirmation (RUST-FEAT-056) --- android/app/src/main/AndroidManifest.xml | 8 ++- .../java/org/emulebb/android/DaemonService.kt | 7 +- .../java/org/emulebb/android/MainActivity.kt | 71 +++++++++++++++++-- android/app/src/main/res/values/ids.xml | 2 + crates/emulebb-android/src/lib.rs | 29 ++++++++ crates/emulebb-core/src/lib.rs | 16 +++++ crates/emulebb-daemon/src/lib.rs | 1 + webui/src/app.tsx | 2 +- webui/src/ed2kHandoff.ts | 19 +++++ webui/src/views.tsx | 30 +++++++- webui/tests/e2e/app-smoke.spec.ts | 25 +++++++ webui/tests/unit/transfers-view.test.tsx | 37 +++++++++- 12 files changed, 238 insertions(+), 9 deletions(-) create mode 100644 webui/src/ed2kHandoff.ts diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 266acec0d..ddbfff2aa 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -13,11 +13,17 @@ - + + + + + + + - try { startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(ShellFiles.browserUrl(endpoint)))) } + try { + val uri = Uri.parse(ShellFiles.browserUrl(endpoint)).buildUpon().apply { + pendingLink?.let { link -> + path("/transfers") + encodedFragment("ed2k=" + Uri.encode(link)) + } + }.build() + startActivity(Intent(Intent.ACTION_VIEW, uri)) + pendingLink = null + updatePendingLink() + } catch (_: android.content.ActivityNotFoundException) { error.text = "Install a browser to open the WebUI" } } } @@ -164,6 +186,11 @@ class MainActivity : Activity() { label("Client settings, sharing, transfers and diagnostics are available in the WebUI. Copy the API key into its sign-in screen.") label("Continuous operation uses battery and network traffic. All-files access is required; notifications and battery exemption are recommended.") setContentView(ScrollView(this).apply { addView(panel) }) + if (savedInstanceState != null) { + pendingLink = savedInstanceState.getString("pending-link") + pendingLinkError = savedInstanceState.getString("pending-link-error") ?: "" + updatePendingLink() + } else acceptLink(intent) updateControls() setup.execute { try { @@ -193,6 +220,42 @@ class MainActivity : Activity() { } } + override fun onNewIntent(intent: Intent) { + super.onNewIntent(intent) + setIntent(intent) + acceptLink(intent) + } + + override fun onSaveInstanceState(outState: Bundle) { + outState.putString("pending-link", pendingLink) + outState.putString("pending-link-error", pendingLinkError) + super.onSaveInstanceState(outState) + } + + private fun acceptLink(intent: Intent?) { + if (intent?.action == Intent.ACTION_VIEW) { + pendingLink = null + pendingLinkError = "" + try { + val raw = requireNotNull(intent.dataString) + require(raw.length <= 2048) + pendingLink = NativeBridge.call("validateLink", "link" to raw) as String + } catch (_: Exception) { pendingLinkError = "Unsupported or malformed eD2K file link" } + } + updatePendingLink() + } + + private fun updatePendingLink() { + pending.show(pendingLink?.let { "Pending link: $it\nStart the service, then Open WebUI to review and add this file." } ?: "") + linkError.show(pendingLinkError) + discardLink.visibility = if (pendingLink != null || pendingLinkError.isNotEmpty()) View.VISIBLE else View.GONE + } + + private fun TextView.show(value: String) { + // Avoid recurring accessibility changes while the service state is unchanged. + if (text.toString() != value) text = value + } + private fun launchSettings(intent: Intent) { try { startActivity(intent) } catch (_: android.content.ActivityNotFoundException) { error.text = "This device does not expose the requested permission settings" } @@ -260,7 +323,7 @@ class MainActivity : Activity() { val exemption = requireNotNull(getSystemService(PowerManager::class.java)).isIgnoringBatteryOptimizations(packageName) val local = Build.VERSION.SDK_INT < 37 || checkSelfPermission(DaemonService.LOCAL_NETWORK) == PackageManager.PERMISSION_GRANTED val restricted = requireNotNull(getSystemService(ActivityManager::class.java)).isBackgroundRestricted - permissions.text = "Files: ${if (Environment.isExternalStorageManager()) "allowed" else "required"}; local network: ${if (local) "allowed" else "required"}; notifications: ${if (notifications) "allowed" else "disabled"}; battery exemption: ${if (exemption) "allowed" else "disabled"}; background restriction: ${if (restricted) "enabled" else "disabled"}" + permissions.show("Files: ${if (Environment.isExternalStorageManager()) "allowed" else "required"}; local network: ${if (local) "allowed" else "required"}; notifications: ${if (notifications) "allowed" else "disabled"}; battery exemption: ${if (exemption) "allowed" else "disabled"}; background restriction: ${if (restricted) "enabled" else "disabled"}") handler.postDelayed(this, 1_000) } } diff --git a/android/app/src/main/res/values/ids.xml b/android/app/src/main/res/values/ids.xml index 56d41e18e..4ff1a0ded 100644 --- a/android/app/src/main/res/values/ids.xml +++ b/android/app/src/main/res/values/ids.xml @@ -8,4 +8,6 @@ + + diff --git a/crates/emulebb-android/src/lib.rs b/crates/emulebb-android/src/lib.rs index 59abdf1e0..9d92596c2 100644 --- a/crates/emulebb-android/src/lib.rs +++ b/crates/emulebb-android/src/lib.rs @@ -18,6 +18,9 @@ use serde_json::{Value, json}; #[derive(Deserialize)] #[serde(tag = "op", rename_all = "camelCase", deny_unknown_fields)] enum Request { + ValidateLink { + link: String, + }, ReadSettings { profile_dir: PathBuf, }, @@ -92,6 +95,10 @@ fn ensure_process_logging(profile: &std::path::Path) -> Result<()> { fn handle(request: Request) -> Result { match request { + Request::ValidateLink { link } => { + emulebb_daemon::validate_ed2k_file_link(&link)?; + Ok(Value::String(link)) + } Request::PruneAssets { profile_dir, current_web_root, @@ -340,4 +347,26 @@ mod tests { .is_err() ); } + + #[test] + fn external_link_validation_has_no_profile_or_runtime_side_effects() { + let link = "ed2k://|file|Sample%20%26%20Guide.bin|42|00112233445566778899aabbccddeeff|/"; + assert_eq!( + handle(Request::ValidateLink { link: link.into() }).unwrap(), + json!(link) + ); + for invalid in [ + "ed2k://|server|192.0.2.1|4661|/".to_string(), + "ed2k://|file|Sample.bin|42|invalid|/".to_string(), + "ed2k://|file|Sample.bin|invalid|00112233445566778899aabbccddeeff|/".to_string(), + "ed2k://|file|Sample%0Abin|42|00112233445566778899aabbccddeeff|/".to_string(), + format!( + "ed2k://|file|{}|42|00112233445566778899aabbccddeeff|/", + "A".repeat(2048) + ), + ] { + assert!(handle(Request::ValidateLink { link: invalid }).is_err()); + } + assert_eq!(handle(Request::Status {}).unwrap()["phase"], "Stopped"); + } } diff --git a/crates/emulebb-core/src/lib.rs b/crates/emulebb-core/src/lib.rs index 0686cd0ab..1fc003c0f 100644 --- a/crates/emulebb-core/src/lib.rs +++ b/crates/emulebb-core/src/lib.rs @@ -7409,6 +7409,22 @@ struct ParsedEd2kLink { sources: Vec, } +/// Validate an external file-link handoff without opening a profile or queuing work. +/// Transfer creation remains an explicit, separately authenticated operation. +pub fn validate_ed2k_file_link(link: &str) -> Result<()> { + ensure!( + link.len() <= 2048 && !link.chars().any(|c| c.is_whitespace() || c.is_control()), + "ED2K file link must be bounded and contain no whitespace or controls" + ); + let parsed = parse_ed2k_link(link)?; + let _: Ed2kHash = parsed.file_hash.parse().context("invalid ED2K file hash")?; + ensure!( + !parsed.name.is_empty() && !parsed.name.chars().any(char::is_control), + "invalid ED2K file name" + ); + Ok(()) +} + fn parse_ed2k_link(link: &str) -> Result { let parts = link .strip_prefix("ed2k://|file|") diff --git a/crates/emulebb-daemon/src/lib.rs b/crates/emulebb-daemon/src/lib.rs index f924cb5fd..d51b137f9 100644 --- a/crates/emulebb-daemon/src/lib.rs +++ b/crates/emulebb-daemon/src/lib.rs @@ -49,6 +49,7 @@ pub const PROFILE_SETTINGS_FILE: &str = "emulebb-rust-settings.toml"; pub const PROFILE_METADATA_FILE: &str = "emulebb-rust-metadata.db"; pub use emulebb_core::{ BUILD_CHANNEL, BUILD_SOURCE_REVISION, BUILD_VERSION, build_version_description, + validate_ed2k_file_link, }; const REGULAR_DIAGNOSTIC_SUMMARY_INTERVAL: Duration = Duration::from_secs(10); diff --git a/webui/src/app.tsx b/webui/src/app.tsx index 6ae243acb..8b2bd2e76 100644 --- a/webui/src/app.tsx +++ b/webui/src/app.tsx @@ -125,7 +125,7 @@ function currentRoute(): AppRoute { if (route) { const canonicalPath = pathForRoute(route); if (window.location.pathname !== canonicalPath) { - window.history.replaceState(null, "", canonicalPath); + window.history.replaceState(null, "", canonicalPath + window.location.search + window.location.hash); } return route; } diff --git a/webui/src/ed2kHandoff.ts b/webui/src/ed2kHandoff.ts new file mode 100644 index 000000000..d1e71bf48 --- /dev/null +++ b/webui/src/ed2kHandoff.ts @@ -0,0 +1,19 @@ +export type Ed2kHandoff = { link: string; error?: string }; + +/** Read a browser handoff as form input only; the daemon validates on submission. */ +export function readEd2kHandoff(fragment: string): Ed2kHandoff | null { + if (!fragment) return null; + const invalid = { link: "", error: "Unsupported or malformed eD2K file link" }; + if (fragment.length > 8192) return invalid; + const fields = new URLSearchParams(fragment.slice(1)); + if (!fields.has("ed2k")) return null; + const link = fields.get("ed2k") ?? ""; + if (fields.size !== 1 || link.length > 2048 || !/^ed2k:\/\/\|file\|[^\s\u0000-\u001f\u007f-\u009f]+\|\/$/.test(link)) { + return invalid; + } + return { link }; +} + +export function clearEd2kHandoff(): void { + window.history.replaceState(null, "", window.location.pathname + window.location.search); +} diff --git a/webui/src/views.tsx b/webui/src/views.tsx index 0763ddbde..34c2052fe 100644 --- a/webui/src/views.tsx +++ b/webui/src/views.tsx @@ -1,5 +1,6 @@ import type { ComponentChildren } from "preact"; import { useEffect, useMemo, useState } from "preact/hooks"; +import { clearEd2kHandoff, readEd2kHandoff } from "./ed2kHandoff"; import { Ban, Clipboard, @@ -244,7 +245,8 @@ export function TransfersView(props: { run: RunFunction; }) { const [stateFilter, setStateFilter] = useState(""); - const [ed2kLinks, setEd2kLinks] = useState(""); + const [handoff, setHandoff] = useState(() => readEd2kHandoff(window.location.hash)); + const [ed2kLinks, setEd2kLinks] = useState(handoff?.link ?? ""); const [pausedCreate, setPausedCreate] = useState(false); const [selectedHash, setSelectedHash] = useState(""); const [details, setDetails] = useState(null); @@ -259,6 +261,21 @@ export function TransfersView(props: { const selected = props.transfers.find((transfer) => transfer.hash === selectedHash); const selectedId = selected?.hash ?? ""; + useEffect(() => { + const receive = () => { + const next = readEd2kHandoff(window.location.hash); + setHandoff(next); + if (next) setEd2kLinks(next.link); + }; + window.addEventListener("hashchange", receive); + return () => window.removeEventListener("hashchange", receive); + }, []); + + const consumeHandoff = () => { + if (handoff) clearEd2kHandoff(); + setHandoff(null); + }; + useEffect(() => { if (!selectedId) { setDetails(null); @@ -298,6 +315,9 @@ export function TransfersView(props: { if (error) { throw new Error(error); } + // Explicit submission consumes the external handoff even if the operation + // needs a retry. The editable form remains available on failure. + consumeHandoff(); await createDownloadWithPolicy((knownFilePolicy) => props.client.post("transfers", { links, paused: pausedCreate, knownFilePolicy }) ); @@ -355,6 +375,9 @@ export function TransfersView(props: { + {handoff &&

+ {handoff.error ?? "Review the linked file and press Add links to queue it."} +

}
{ @@ -363,6 +386,7 @@ export function TransfersView(props: { }} >