From 6916dd54fb3fe078ce80c7c99d2e601e92a5e1cf Mon Sep 17 00:00:00 2001 From: tongtongstudio Date: Thu, 12 Nov 2026 21:17:44 +0800 Subject: [PATCH] feat: add headless agent management to CLI and both desktop clients --- AGENTS.md | 6 +- Cargo.lock | 42 +- Cargo.toml | 4 +- README.md | 13 +- agent/Cargo.toml | 9 + agent/src/gateway.rs | 126 ++++ agent/src/main.rs | 170 ++--- agent/src/managed.rs | 374 +++++++++++ agent/src/supervisor.rs | 120 ++++ crates/ads-management/Cargo.toml | 21 + crates/ads-management/src/ipc.rs | 295 +++++++++ crates/ads-management/src/lib.rs | 367 +++++++++++ crates/ads-management/src/service.rs | 170 +++++ ctl/Cargo.toml | 12 + ctl/src/main.rs | 342 ++++++++++ deploy/windows/README.md | 16 + deploy/windows/migrate-managed.ps1 | 67 ++ deploy/windows/run-agent.ps1 | 4 +- desktop/Ads.Desktop.Smoke/Program.cs | 41 +- desktop/Ads.Desktop/AgentManagement.cs | 54 ++ desktop/Ads.Desktop/AgentManagementView.axaml | 33 + .../Ads.Desktop/AgentManagementView.axaml.cs | 19 + .../Ads.Desktop/AgentManagementViewModel.cs | 203 ++++++ desktop/Ads.Desktop/App.axaml.cs | 23 +- desktop/Ads.Desktop/MainViewModel.cs | 6 +- desktop/Ads.Desktop/MainWindow.axaml | 3 +- desktop/Ads.Desktop/ProcessTools.cs | 2 +- desktop/README.md | 4 + docs/agent-management.md | 118 ++++ flutter_manager/README.md | 8 +- flutter_manager/lib/data/agent_models.dart | 75 +++ .../lib/data/agent_models.freezed.dart | 594 ++++++++++++++++++ .../lib/data/agent_repository.dart | 54 ++ flutter_manager/lib/data/agent_service.dart | 181 ++++++ .../lib/data/manager_repository.dart | 28 +- flutter_manager/lib/ui/agent_model.dart | 263 ++++++++ flutter_manager/lib/ui/agent_view.dart | 322 ++++++++++ flutter_manager/lib/ui/manager_model.dart | 24 + flutter_manager/lib/ui/manager_view.dart | 4 + flutter_manager/test/agent_live_smoke.dart | 79 +++ flutter_manager/test/agent_test.dart | 144 +++++ scripts/build-desktop.ps1 | 2 +- scripts/build-flutter.ps1 | 20 + scripts/build.ps1 | 2 +- scripts/build.sh | 2 +- scripts/start-agent-tray.ps1 | 5 + server/main.go | 17 +- server/management.go | 284 +++++++++ server/management_test.go | 101 +++ tests/management_e2e.py | 112 ++++ 50 files changed, 4837 insertions(+), 148 deletions(-) create mode 100644 agent/src/gateway.rs create mode 100644 agent/src/managed.rs create mode 100644 agent/src/supervisor.rs create mode 100644 crates/ads-management/Cargo.toml create mode 100644 crates/ads-management/src/ipc.rs create mode 100644 crates/ads-management/src/lib.rs create mode 100644 crates/ads-management/src/service.rs create mode 100644 ctl/Cargo.toml create mode 100644 ctl/src/main.rs create mode 100644 deploy/windows/migrate-managed.ps1 create mode 100644 desktop/Ads.Desktop/AgentManagement.cs create mode 100644 desktop/Ads.Desktop/AgentManagementView.axaml create mode 100644 desktop/Ads.Desktop/AgentManagementView.axaml.cs create mode 100644 desktop/Ads.Desktop/AgentManagementViewModel.cs create mode 100644 docs/agent-management.md create mode 100644 flutter_manager/lib/data/agent_models.dart create mode 100644 flutter_manager/lib/data/agent_models.freezed.dart create mode 100644 flutter_manager/lib/data/agent_repository.dart create mode 100644 flutter_manager/lib/data/agent_service.dart create mode 100644 flutter_manager/lib/ui/agent_model.dart create mode 100644 flutter_manager/lib/ui/agent_view.dart create mode 100644 flutter_manager/test/agent_live_smoke.dart create mode 100644 flutter_manager/test/agent_test.dart create mode 100644 scripts/build-flutter.ps1 create mode 100644 scripts/start-agent-tray.ps1 create mode 100644 server/management.go create mode 100644 server/management_test.go create mode 100644 tests/management_e2e.py diff --git a/AGENTS.md b/AGENTS.md index afd0adb..6ba9977 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -13,15 +13,17 @@ | 目录 | 作用 | 主要语言/格式 | | --- | --- | --- | -| `agent/` | 远程设备守护程序 ads-agent,注册设备、连接手机 ADB、建立转发并重连;源码在 `src/` | Rust、TOML | +| `agent/` | 无界面多手机守护程序 ads-agent、独立监督进程、私有 IPC 和远程管理;兼容单设备参数;源码在 `src/` | Rust、TOML | +| `ctl/` | adsctl 命令行管理端,本机/远程设备、配置、日志、重启及系统启动托管;源码在 `src/` | Rust、TOML | | `usb/` | 可选 ads-usb 本机虚拟 ADB USB 设备,提供回环 USB/IP、描述符和 ADB 帧适配;Windows 使用已签名 USBip 虚拟控制器 | Rust、TOML | | `client/` | 本地守护程序 ads-client,维护设备租约、监听本地端口并自动调用 adb connect;源码在 `src/` | Rust、TOML | | `server/` | ads-server 控制中心、旧协议兼容转发及独立 ads-relay 中继节点 | Go | | `server/cmd/ads-relay/` | 独立中继入口,提供短期票据认证、WSS 数据配对及可选 STUN | Go | | `server/internal/relay/` | 中继票据、数据通道、租约撤销、STUN 与测试 | Go | | `crates/` | Rust 工作区共享库容器 | Rust、TOML | +| `crates/ads-management/` | Agent 配置及管理协议、Windows 命名管道/Unix socket、私有文件与系统服务操作;源码在 `src/` | Rust、TOML | | `crates/ads-core/` | 共用网络与协议代码,包括 WSS、WebRTC P2P、节点回退、ADB smart socket 和流式转发;源码在 `src/` | Rust、TOML | -| `flutter_manager/` | 独立 Flutter Windows 管理中心,提供设备/中继总览、多设备连接、本机 ADB、托盘与独立 scrcpy 窗口 | Dart、C++、CMake、YAML、ICO | +| `flutter_manager/` | 独立 Flutter Windows 管理中心,提供设备/中继总览、Agent 管理、多设备连接、本机 ADB、托盘与独立 scrcpy 窗口 | Dart、C++、CMake、YAML、ICO | | `desktop/` | 跨平台桌面客户端及其测试项目、.NET SDK 配置与使用说明 | C#、Avalonia XAML(AXAML)、JSON、Markdown | | `desktop/Ads.Desktop/` | Avalonia 多设备工作台,管理独立连接、自动端口分配、USB 模拟、多个 scrcpy 窗口及托盘;`Assets/` 保存图标资源 | C#、AXAML、XML(csproj)、PNG | | `desktop/Ads.Desktop.Tests/` | 桌面客户端单元测试 | C#、XML(csproj) | diff --git a/Cargo.lock b/Cargo.lock index 343071a..f4f26e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,18 +4,24 @@ version = 4 [[package]] name = "ads-agent" -version = "0.1.0" +version = "0.2.0" dependencies = [ "ads-core", + "ads-management", "anyhow", "clap", + "libc", + "serde", + "serde_json", "tokio", "tracing", + "tracing-subscriber", + "windows-sys 0.61.2", ] [[package]] name = "ads-client" -version = "0.1.0" +version = "0.2.0" dependencies = [ "ads-core", "anyhow", @@ -26,7 +32,7 @@ dependencies = [ [[package]] name = "ads-core" -version = "0.1.0" +version = "0.2.0" dependencies = [ "anyhow", "bytes", @@ -43,9 +49,25 @@ dependencies = [ "webrtc", ] +[[package]] +name = "ads-management" +version = "0.2.0" +dependencies = [ + "ads-core", + "anyhow", + "futures-util", + "libc", + "serde", + "serde_json", + "sha2", + "tokio", + "tokio-tungstenite", + "windows-sys 0.61.2", +] + [[package]] name = "ads-usb" -version = "0.1.0" +version = "0.2.0" dependencies = [ "anyhow", "clap", @@ -54,6 +76,18 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "adsctl" +version = "0.2.0" +dependencies = [ + "ads-core", + "ads-management", + "anyhow", + "clap", + "serde_json", + "tokio", +] + [[package]] name = "aead" version = "0.5.2" diff --git a/Cargo.toml b/Cargo.toml index 521566a..86346d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,9 @@ [workspace] -members = ["crates/ads-core", "agent", "client", "usb"] +members = ["crates/ads-core", "agent", "client", "usb", "crates/ads-management", "ctl"] resolver = "2" [workspace.package] -version = "0.1.0" +version = "0.2.0" edition = "2021" license = "MIT" diff --git a/README.md b/README.md index 5def05f..f9590a0 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ relay-1 / relay-2 / … ``` +Agent 管理已同时接入 Avalonia 和 Flutter;服务器无需 GUI,可使用 `adsctl` 管理本机或远程 Agent。安装与命令示例见 [Agent 管理](docs/agent-management.md)。 + Windows 现支持可选的 [USB ADB 模拟](docs/windows-usb.md),默认网络模式保持不变。 已部署的集群和使用方法见 [集群部署与协议](docs/cluster.md)。不配置集群时保留原有 v1 单中继模式,供兼容旧客户端;以下原有单机启动命令也仍可使用。 @@ -20,15 +22,17 @@ Windows 现支持可选的 [USB ADB 模拟](docs/windows-usb.md),默认网络 | 目录 | 作用 | 主要语言/格式 | | --- | --- | --- | -| `agent/` | 远程设备守护程序 ads-agent,注册设备、连接手机 ADB、建立转发并重连;源码在 `src/` | Rust、TOML | +| `agent/` | 无界面多手机守护程序 ads-agent、独立监督进程、私有 IPC 和远程管理;兼容单设备参数;源码在 `src/` | Rust、TOML | +| `ctl/` | adsctl 命令行管理端,本机/远程设备、配置、日志、重启及系统启动托管;源码在 `src/` | Rust、TOML | | `usb/` | 可选 ads-usb 本机虚拟 ADB USB 设备,提供回环 USB/IP、描述符和 ADB 帧适配;Windows 使用已签名 USBip 虚拟控制器 | Rust、TOML | | `client/` | 本地守护程序 ads-client,维护设备租约、监听本地端口并自动调用 adb connect;源码在 `src/` | Rust、TOML | | `server/` | ads-server 控制中心、旧协议兼容转发及独立 ads-relay 中继节点 | Go | | `server/cmd/ads-relay/` | 独立中继入口,提供短期票据认证、WSS 数据配对及可选 STUN | Go | | `server/internal/relay/` | 中继票据、数据通道、租约撤销、STUN 与测试 | Go | | `crates/` | Rust 工作区共享库容器 | Rust、TOML | +| `crates/ads-management/` | Agent 配置及管理协议、Windows 命名管道/Unix socket、私有文件与系统服务操作;源码在 `src/` | Rust、TOML | | `crates/ads-core/` | 共用网络与协议代码,包括 WSS、WebRTC P2P、节点回退、ADB smart socket 和流式转发;源码在 `src/` | Rust、TOML | -| `flutter_manager/` | 独立 Flutter Windows 管理中心,提供设备/中继总览、多设备连接、本机 ADB、托盘与独立 scrcpy 窗口 | Dart、C++、CMake、YAML、ICO | +| `flutter_manager/` | 独立 Flutter Windows 管理中心,提供设备/中继总览、Agent 管理、多设备连接、本机 ADB、托盘与独立 scrcpy 窗口 | Dart、C++、CMake、YAML、ICO | | `desktop/` | 跨平台桌面客户端及其测试项目、.NET SDK 配置与使用说明 | C#、Avalonia XAML(AXAML)、JSON、Markdown | | `desktop/Ads.Desktop/` | Avalonia 多设备工作台,管理独立连接、自动端口分配、USB 模拟、多个 scrcpy 窗口及托盘;`Assets/` 保存图标资源 | C#、AXAML、XML(csproj)、PNG | | `desktop/Ads.Desktop.Tests/` | 桌面客户端单元测试 | C#、XML(csproj) | @@ -59,10 +63,11 @@ Windows 现支持可选的 [USB ADB 模拟](docs/windows-usb.md),默认网络 | `server/` | `ads-server` / `ads-relay` | 控制中心:认证、注册、设备独占、信令、健康检查;独立中继:数据转发 | | `agent/` | `ads-agent` | 远程电脑后台进程,主动注册设备、接通手机、断线重连 | | `client/` | `ads-client` | 本地后台进程,保持设备租约,提供本地端口,自动调用 `adb connect` | +| `crates/ads-management/` | Agent 配置及管理协议、Windows 命名管道/Unix socket、私有文件与系统服务操作;源码在 `src/` | Rust、TOML | | `crates/ads-core/` | 共用 Rust 库 | WSS、协议、ADB smart socket、流式转发 | | `deploy/` | 部署示例 | Caddy、systemd、launchd 示例 | -四个网络后端与可选 ads-usb 均为独立可执行文件;另有 Avalonia 桌面工作台,不依赖 Python 运行正式程序。两端守护程序当前作为前台长运行进程,启动安装交给各平台进程管理器;尚未实现原生 Windows Service 安装器。 +网络后端、adsctl 与可选 ads-usb 均为独立可执行文件,不依赖 Python 运行正式程序。Agent 可由 Windows 计划任务或 Linux systemd 托管;Avalonia 与 Flutter 管理界面是可选入口。Windows 使用计划任务,并非原生 SCM 服务。 ## 编译 @@ -218,3 +223,5 @@ python tests/real_device.py --adb-server 192.168.5.222:5037 --serial 192.168.100 ## 桌面客户端(Avalonia) 设备列表、配置、连接管理、托盘、运行日志及 scrcpy 独立窗口手机控制现已放在 `desktop/`。启动和工具路径配置见 [桌面客户端说明](desktop/README.md)。 + +Agent 管理发行入口:`scripts/build-desktop.ps1` 生成 `bin/desktop-win-x64/`,`scripts/build-flutter.ps1` 生成 `bin/flutter-win-x64/`,两者均包含 Agent/CLI,均不包含私有凭据。Avalonia 本机 Agent 托盘使用 `scripts/start-agent-tray.ps1`。 diff --git a/agent/Cargo.toml b/agent/Cargo.toml index 9d86cf3..88b78f8 100644 --- a/agent/Cargo.toml +++ b/agent/Cargo.toml @@ -10,3 +10,12 @@ anyhow.workspace = true clap.workspace = true tokio.workspace = true tracing.workspace = true +ads-management = { path = "../crates/ads-management" } +serde.workspace = true +serde_json.workspace = true +tracing-subscriber.workspace = true + +[target.'cfg(windows)'.dependencies] +windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_Security", "Win32_System_JobObjects", "Win32_System_Threading"] } +[target.'cfg(unix)'.dependencies] +libc = "0.2" diff --git a/agent/src/gateway.rs b/agent/src/gateway.rs new file mode 100644 index 0000000..370f2ae --- /dev/null +++ b/agent/src/gateway.rs @@ -0,0 +1,126 @@ +use ads_core::{adb_tcp_connect, dial, next_control, relay, tcp_connect, NetworkArgs}; +use anyhow::{bail, Context, Result}; +use std::{sync::Arc, time::Duration}; +use tokio::{net::TcpStream, task::JoinSet, time::sleep}; +use tracing::{info, warn}; + +#[derive(Clone)] +pub struct Gateway { + pub network: NetworkArgs, + pub token: String, + pub device: String, + pub target: Option, + pub adb_serial: Option, + pub adb_server: String, + pub device_port: u16, +} +#[derive(Default, Clone)] +pub struct Health { + pub connected: bool, + pub last_error: Option, +} +pub type WorkerHealth = Arc>; +async fn upstream(args: &Gateway) -> Result { + match &args.target { + Some(address) => tcp_connect(address).await, + None => { + adb_tcp_connect( + &args.adb_server, + args.adb_serial.as_deref().context("missing serial")?, + args.device_port, + ) + .await + } + } +} + +async fn serve_session( + args: Arc, + session: String, + signal_key: Option, +) -> Result<()> { + let tcp = upstream(&args).await.context("cannot reach phone adbd")?; + if let Some(key) = signal_key { + let signal = dial( + args.network + .url("/v2/signal", &[("session", &session), ("key", &key)])?, + &args.token, + args.network.connect_address.as_deref(), + ) + .await?; + return ads_core::transport::agent_session(tcp, signal, &args.network).await; + } + let ws = dial( + args.network.url( + "/v1/data", + &[("device", &args.device), ("session", &session)], + )?, + &args.token, + args.network.connect_address.as_deref(), + ) + .await?; + relay(tcp, ws).await +} + +async fn online(args: Arc, health: &Option) -> Result<()> { + let mut ws = dial( + args.network + .url("/v1/agent", &[("device", &args.device), ("v", "2")])?, + &args.token, + args.network.connect_address.as_deref(), + ) + .await?; + let hello = next_control(&mut ws).await?; + if hello.kind != "registered" { + bail!("expected registered response"); + } + if let Some(health) = health { + let mut state = health.lock().unwrap_or_else(|e| e.into_inner()); + state.connected = true; + state.last_error = None; + } + info!(device = %args.device, "gateway registered"); + let mut tasks = JoinSet::new(); + loop { + tokio::select! { + message = next_control(&mut ws) => { + let message = message?; + if message.kind != "open" && message.kind != "open_v2" { bail!("unexpected gateway control message"); } + let signal_key = if message.kind == "open_v2" { Some(message.signal_key.context("missing signal key")?) } else { None }; + let session = message.session.context("missing session ticket")?; + if tasks.len() >= 32 { warn!("gateway stream limit reached"); continue; } + let args = args.clone(); + let health = health.clone(); + tasks.spawn(async move { + let device=args.device.clone(); + if let Err(error) = serve_session(args, session, signal_key).await { + if let Some(health)=health {health.lock().unwrap_or_else(|e|e.into_inner()).last_error=Some(error.to_string());} + warn!(%device,%error, "device stream ended"); + } + }); + }, + _ = tasks.join_next(), if !tasks.is_empty() => {}, + } + } + // JoinSet drop aborts all streams when the control connection is lost. +} + +pub async fn run(args: Arc, health: Option) { + let mut delay = 1; + loop { + let started = std::time::Instant::now(); + if let Err(error) = online(args.clone(), &health).await { + if let Some(health) = &health { + let mut state = health.lock().unwrap_or_else(|e| e.into_inner()); + state.connected = false; + state.last_error = Some(error.to_string()); + } + warn!(device=%args.device,%error, "gateway offline; reconnecting"); + } + if started.elapsed() > Duration::from_secs(30) { + delay = 1; + } + sleep(Duration::from_secs(delay)).await; + delay = (delay * 2).min(30); + } +} diff --git a/agent/src/main.rs b/agent/src/main.rs index 8e803b1..84f55d2 100644 --- a/agent/src/main.rs +++ b/agent/src/main.rs @@ -1,34 +1,34 @@ -use ads_core::{ - adb_tcp_connect, dial, init_logging, next_control, relay, tcp_connect, valid_device_id, - validate_token, NetworkArgs, -}; +mod gateway; +mod managed; +mod supervisor; +use ads_core::{valid_device_id, validate_token, NetworkArgs}; +use ads_management::{credential, AgentConfig}; use anyhow::{bail, Context, Result}; use clap::Parser; -use std::{sync::Arc, time::Duration}; -use tokio::{net::TcpStream, task::JoinSet, time::sleep}; -use tracing::{info, warn}; +use std::{path::PathBuf, sync::Arc}; #[derive(Parser)] #[command( version, - about = "Remote device gateway: outbound-only connection to the ADS relay" + about = "ADS agent: managed multi-device gateway or legacy single-device worker" )] struct Args { #[command(flatten)] network: NetworkArgs, #[arg(long, env = "ADS_AGENT_TOKEN", hide_env_values = true)] - token: String, - /// Stable public device alias, e.g. home-phone + token: Option, #[arg(long)] - device: String, - /// Direct phone adbd address reachable from this gateway, e.g. 192.168.100.141:5555 - #[arg( - long, - required_unless_present = "adb_serial", - conflicts_with = "adb_serial" - )] + config: Option, + /// Supervise the headless child; required for management restart + #[arg(long, requires = "config")] + supervise: bool, + /// Retain managed logs in memory without console output + #[arg(long)] + quiet: bool, + #[arg(long)] + device: Option, + #[arg(long, conflicts_with = "adb_serial")] target: Option, - /// Alternative: choose a transport from an existing ADB server (phone must have TCP adbd enabled) #[arg(long)] adb_serial: Option, #[arg(long, default_value = "127.0.0.1:5037")] @@ -36,105 +36,49 @@ struct Args { #[arg(long, default_value_t = 5555)] device_port: u16, } - -async fn upstream(args: &Args) -> Result { - match &args.target { - Some(address) => tcp_connect(address).await, - None => { - adb_tcp_connect( - &args.adb_server, - args.adb_serial.as_deref().context("missing serial")?, - args.device_port, - ) - .await - } - } -} - -async fn serve_session(args: Arc, session: String, signal_key: Option) -> Result<()> { - let tcp = upstream(&args).await.context("cannot reach phone adbd")?; - if let Some(key) = signal_key { - let signal = dial( - args.network - .url("/v2/signal", &[("session", &session), ("key", &key)])?, - &args.token, - args.network.connect_address.as_deref(), - ) - .await?; - return ads_core::transport::agent_session(tcp, signal, &args.network).await; - } - let ws = dial( - args.network.url( - "/v1/data", - &[("device", &args.device), ("session", &session)], - )?, - &args.token, - args.network.connect_address.as_deref(), - ) - .await?; - relay(tcp, ws).await -} - -async fn online(args: Arc) -> Result<()> { - let mut ws = dial( - args.network - .url("/v1/agent", &[("device", &args.device), ("v", "2")])?, - &args.token, - args.network.connect_address.as_deref(), - ) - .await?; - let hello = next_control(&mut ws).await?; - if hello.kind != "registered" { - bail!("expected registered response"); - } - info!(device = %args.device, "gateway registered"); - let mut tasks = JoinSet::new(); - loop { - tokio::select! { - message = next_control(&mut ws) => { - let message = message?; - if message.kind != "open" && message.kind != "open_v2" { bail!("unexpected gateway control message"); } - let signal_key = if message.kind == "open_v2" { Some(message.signal_key.context("missing signal key")?) } else { None }; - let session = message.session.context("missing session ticket")?; - if tasks.len() >= 32 { warn!("gateway stream limit reached"); continue; } - let args = args.clone(); - tasks.spawn(async move { - if let Err(error) = serve_session(args, session, signal_key).await { warn!(%error, "device stream ended"); } - }); - }, - _ = tasks.join_next(), if !tasks.is_empty() => {}, - } - } - // JoinSet drop aborts all streams when the control connection is lost. -} - -async fn run(args: Arc) { - let mut delay = 1; - loop { - let started = std::time::Instant::now(); - if let Err(error) = online(args.clone()).await { - warn!(%error, "gateway offline; reconnecting"); - } - if started.elapsed() > Duration::from_secs(30) { - delay = 1; - } - sleep(Duration::from_secs(delay)).await; - delay = (delay * 2).min(30); - } -} - #[tokio::main] async fn main() -> Result<()> { - init_logging(); let args = Args::parse(); - validate_token(&args.token)?; - if !valid_device_id(&args.device) { - bail!("invalid device ID (use letters, digits, -, _, .; max 128 bytes)"); + if args.quiet { + std::env::set_var("ADS_LOG_STDOUT", "0"); } - args.network.url("/v1/agent", &[])?; - tokio::select! { - _ = run(Arc::new(args)) => {}, - result = tokio::signal::ctrl_c() => { result?; info!("gateway stopping"); }, + if let Some(path) = args.config { + if args.device.is_some() || args.target.is_some() || args.adb_serial.is_some() { + bail!("managed config cannot be combined with single-device arguments") + } + let path = std::fs::canonicalize(path).context("agent config not found")?; + if args.supervise { + return supervisor::run(path).await; + } + let config = AgentConfig::load(&path)?; + let token = match args.token { + Some(value) => value, + None => credential(&path.with_file_name("agent.env"), "ADS_AGENT_TOKEN")?, + }; + validate_token(&token)?; + let restart = managed::run(path, config, token).await?; + if restart { + std::process::exit(75) + } + } else { + ads_core::init_logging(); + let token = args.token.context("provide ADS_AGENT_TOKEN")?; + validate_token(&token)?; + let device = args.device.context("provide --device or --config")?; + if !valid_device_id(&device) || args.target.is_none() == args.adb_serial.is_none() { + bail!("valid device and exactly one of --target / --adb-serial are required") + } + args.network.url("/v1/agent", &[])?; + let worker = gateway::Gateway { + network: args.network, + token, + device, + target: args.target, + adb_serial: args.adb_serial, + adb_server: args.adb_server, + device_port: args.device_port, + }; + tokio::select! {_=gateway::run(Arc::new(worker),None)=>{},result=tokio::signal::ctrl_c()=>{result?;}} } Ok(()) } diff --git a/agent/src/managed.rs b/agent/src/managed.rs new file mode 100644 index 0000000..d8a9d46 --- /dev/null +++ b/agent/src/managed.rs @@ -0,0 +1,374 @@ +use crate::gateway::{self, Gateway, Health, WorkerHealth}; +use ads_management::{ + now, read_ws, write_ws, AgentConfig, AgentStatus, DeviceStatus, LogEntry, LogPage, Request, + Response, +}; +use anyhow::{bail, Context, Result}; +use serde::Deserialize; +use std::{ + collections::{BTreeMap, VecDeque}, + path::PathBuf, + sync::{Arc, Mutex}, + time::{Duration, Instant}, +}; +use tokio::{ + sync::{watch, Mutex as AsyncMutex}, + task::JoinHandle, + time::{interval, sleep}, +}; +use tracing_subscriber::{ + layer::{Context as LayerContext, SubscriberExt}, + util::SubscriberInitExt, + Layer, +}; +struct Logs { + entries: VecDeque, + next: u64, + secret: String, + instance: String, +} +#[derive(Clone)] +struct LogLayer(Arc>); +struct Visitor(String); +impl tracing::field::Visit for Visitor { + fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) { + if !self.0.is_empty() { + self.0.push(' ') + }; + self.0.push_str(field.name()); + self.0.push('='); + self.0.push_str(&format!("{value:?}")); + } +} +impl Layer for LogLayer { + fn on_event(&self, event: &tracing::Event<'_>, _: LayerContext<'_, S>) { + let mut visitor = Visitor(String::new()); + event.record(&mut visitor); + let mut logs = self.0.lock().unwrap_or_else(|e| e.into_inner()); + let message = visitor + .0 + .replace(&logs.secret, "[REDACTED]") + .chars() + .take(1024) + .collect(); + let entry = LogEntry { + seq: logs.next, + time: now(), + level: event.metadata().level().to_string(), + message, + }; + logs.next += 1; + logs.entries.push_back(entry); + while logs.entries.len() > 500 { + logs.entries.pop_front(); + } + } +} +struct Worker { + config: ads_management::DeviceConfig, + health: WorkerHealth, + task: JoinHandle<()>, +} +impl Drop for Worker { + fn drop(&mut self) { + self.task.abort(); + } +} +struct State { + config: AgentConfig, + workers: BTreeMap, + revision: u64, +} +struct Runtime { + path: PathBuf, + state: AsyncMutex, + token: String, + logs: Arc>, + started: Instant, + restart: watch::Sender, + supervised: bool, + instance: String, +} +impl Runtime { + fn reconcile(&self, state: &mut State) { + let network = state.config.network(); + let adb_server = state.config.adb_server.clone(); + state.workers.retain(|id, worker| { + state + .config + .devices + .iter() + .any(|d| d.enabled && d.id == *id && *d == worker.config) + }); + for device in &state.config.devices { + if !device.enabled || state.workers.contains_key(&device.id) { + continue; + } + let health = Arc::new(Mutex::new(Health::default())); + let gateway = Gateway { + network: network.clone(), + token: self.token.clone(), + device: device.id.clone(), + target: device.target.clone(), + adb_serial: device.adb_serial.clone(), + adb_server: device + .adb_server + .clone() + .unwrap_or_else(|| adb_server.clone()), + device_port: device.device_port, + }; + let task = tokio::spawn(gateway::run(Arc::new(gateway), Some(health.clone()))); + state.workers.insert( + device.id.clone(), + Worker { + config: device.clone(), + health, + task, + }, + ); + } + } + async fn status(&self) -> AgentStatus { + let state = self.state.lock().await; + let devices = state + .config + .devices + .iter() + .map(|config| { + let health = state + .workers + .get(&config.id) + .map(|w| w.health.lock().unwrap_or_else(|e| e.into_inner()).clone()) + .unwrap_or_default(); + DeviceStatus { + config: config.clone(), + connected: health.connected, + last_error: health + .last_error + .map(|e| e.replace(&self.token, "[REDACTED]")), + } + }) + .collect(); + AgentStatus { + id: state.config.id.clone(), + name: state.config.name.clone(), + hostname: std::env::var("COMPUTERNAME") + .or_else(|_| std::env::var("HOSTNAME")) + .unwrap_or_else(|_| "unknown".into()), + os: std::env::consts::OS.into(), + arch: std::env::consts::ARCH.into(), + version: env!("CARGO_PKG_VERSION").into(), + pid: std::process::id(), + uptime_seconds: self.started.elapsed().as_secs(), + supervised: self.supervised, + revision: state.revision, + instance: self.instance.clone(), + devices, + } + } + async fn execute(&self, request: Request) -> Result { + match request { + Request::Status => Ok(Response::success(self.status().await)), + Request::DevicesList => Ok(Response::success(self.status().await.devices)), + Request::ConfigGet => Ok(Response::success(&self.state.lock().await.config)), + Request::Logs { after, limit } => { + let logs = self.logs.lock().unwrap_or_else(|e| e.into_inner()); + let after = if after >= logs.next { 0 } else { after }; + let entries: Vec<_> = logs + .entries + .iter() + .filter(|e| e.seq > after) + .take(if limit == 0 { 50 } else { limit.min(50) } as usize) + .cloned() + .collect(); + Ok(Response::success(LogPage { + instance: logs.instance.clone(), + next: entries.last().map(|e| e.seq).unwrap_or(after), + entries, + })) + } + Request::Restart => { + if !self.supervised { + bail!("restart requires --supervise or an installed managed service") + }; + let sender = self.restart.clone(); + tokio::spawn(async move { + sleep(Duration::from_millis(500)).await; + let _ = sender.send(true); + }); + Ok(Response::success(serde_json::json!({"restarting":true}))) + } + mutation => { + let mut state = self.state.lock().await; + let mut config = state.config.clone(); + match mutation { + Request::DeviceAdd { device } => { + if config.devices.iter().any(|d| d.id == device.id) { + bail!("device ID already exists") + }; + config.devices.push(device); + } + Request::DeviceRemove { id } => { + let count = config.devices.len(); + config.devices.retain(|d| d.id != id); + if count == config.devices.len() { + bail!("device not found") + } + } + Request::DeviceEnable { id, enabled } => { + config + .devices + .iter_mut() + .find(|d| d.id == id) + .context("device not found")? + .enabled = enabled; + } + Request::ConfigSet { config: new } => { + if new.id != config.id { + bail!("agent ID is immutable; initialize a separate agent to change identity") + }; + config = new; + } + _ => bail!("unsupported operation"), + } + config.validate()?; + let changed_network = config.server != state.config.server + || config.connect_address != state.config.connect_address + || config.allow_insecure != state.config.allow_insecure + || config.adb_server != state.config.adb_server; + ads_management::save_private(&self.path, &serde_json::to_vec_pretty(&config)?)?; + if changed_network { + state.workers.clear(); + } + state.config = config; + state.revision += 1; + self.reconcile(&mut state); + tracing::info!(revision = state.revision, "agent configuration applied"); + Ok(Response::success( + serde_json::json!({"revision":state.revision}), + )) + } + } + } + async fn handle(&self, request: Request) -> Response { + match self.execute(request).await { + Ok(value) => value, + Err(error) => Response::failure(error.to_string().replace(&self.token, "[REDACTED]")), + } + } +} +#[derive(Deserialize)] +struct Incoming { + #[serde(rename = "type")] + kind: String, + #[serde(default)] + request_id: String, + request: Option, +} +async fn online(runtime: Arc) -> Result<()> { + let config = runtime.state.lock().await.config.clone(); + let network = config.network(); + let mut ws = ads_core::dial( + network.url("/v3/agents/register", &[("agent", &config.id)])?, + &runtime.token, + network.connect_address.as_deref(), + ) + .await?; + let hello: serde_json::Value = read_ws(&mut ws).await?; + if hello["type"] != "registered" { + bail!("invalid management registration") + } + tracing::info!(agent=%config.id,"agent management connected"); + let mut heartbeat = interval(Duration::from_secs(5)); + loop { + tokio::select! { + message=read_ws::(&mut ws)=>{ + let message=message?;if message.kind!="request"||message.request_id.is_empty(){bail!("unexpected management message")} + let response=runtime.handle(message.request.context("missing request")?).await; + write_ws(&mut ws,&serde_json::json!({"type":"result","request_id":message.request_id,"response":response})).await?; + }, + _=heartbeat.tick()=>{ + let latest=runtime.state.lock().await.config.clone(); + if latest.server!=config.server||latest.connect_address!=config.connect_address||latest.allow_insecure!=config.allow_insecure{bail!("management address changed")} + write_ws(&mut ws,&serde_json::json!({"type":"status","status":runtime.status().await})).await?; + } + } + } +} +pub async fn run(path: PathBuf, config: AgentConfig, token: String) -> Result { + let instance = format!( + "{}-{}-{}", + now(), + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH)? + .subsec_nanos() + ); + let logs = Arc::new(Mutex::new(Logs { + entries: VecDeque::new(), + next: 1, + secret: token.clone(), + instance: instance.clone(), + })); + tracing_subscriber::registry() + .with( + tracing_subscriber::EnvFilter::try_from_default_env() + .unwrap_or_else(|_| "ads_agent=info,ads_core=info,warn".into()), + ) + .with( + tracing_subscriber::fmt::layer() + .with_ansi(false) + .with_filter(tracing_subscriber::filter::filter_fn(|_| { + std::env::var("ADS_LOG_STDOUT").as_deref() != Ok("0") + })), + ) + .with(LogLayer(logs.clone())) + .init(); + let (restart, mut restarting) = watch::channel(false); + let runtime = Arc::new(Runtime { + path: path.clone(), + state: AsyncMutex::new(State { + config, + workers: BTreeMap::new(), + revision: 1, + }), + token, + logs, + started: Instant::now(), + restart, + supervised: std::env::var("ADS_SUPERVISED").as_deref() == Ok("1"), + instance, + }); + let handler: ads_management::ipc::Handler = { + let runtime = runtime.clone(); + Arc::new(move |request| { + let runtime = runtime.clone(); + Box::pin(async move { runtime.handle(request).await }) + }) + }; + // Bind IPC before starting phone workers, so a duplicate process cannot steal devices. + let (ready_tx, ready_rx) = tokio::sync::oneshot::channel(); + let ipc = ads_management::ipc::serve(&path, handler, ready_tx); + tokio::pin!(ipc); + let networking = async { + if ready_rx.await.is_err() { + std::future::pending::<()>().await; + } + { + let mut state = runtime.state.lock().await; + runtime.reconcile(&mut state); + } + let mut delay = 1; + loop { + if let Err(error) = online(runtime.clone()).await { + tracing::warn!(%error,"management offline; reconnecting") + }; + sleep(Duration::from_secs(delay)).await; + delay = (delay * 2).min(30); + } + }; + tokio::pin!(networking); + let restart = tokio::select! {result=&mut ipc=>{result?;false},_=&mut networking=>false,result=restarting.changed()=>{result?;sleep(Duration::from_millis(400)).await;true},result=crate::supervisor::shutdown()=>{result?;false}}; + runtime.state.lock().await.workers.clear(); + Ok(restart) +} diff --git a/agent/src/supervisor.rs b/agent/src/supervisor.rs new file mode 100644 index 0000000..408662a --- /dev/null +++ b/agent/src/supervisor.rs @@ -0,0 +1,120 @@ +use ads_management::AgentConfig; +#[cfg(windows)] +use anyhow::Context; +use anyhow::Result; +use std::{path::PathBuf, time::Duration}; +pub async fn shutdown() -> Result<()> { + #[cfg(unix)] + { + let mut term = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())?; + tokio::select! {value=tokio::signal::ctrl_c()=>{value?;},_=term.recv()=>{}} + } + #[cfg(windows)] + tokio::signal::ctrl_c().await?; + Ok(()) +} +pub async fn run(path: PathBuf) -> Result<()> { + AgentConfig::load(&path)?; + let executable = std::env::current_exe()?; + let mut delay = 1; + #[cfg(windows)] + let job = Job::new()?; + loop { + let mut command = tokio::process::Command::new(&executable); + command + .arg("--config") + .arg(&path) + .env("ADS_SUPERVISED", "1") + .env("ADS_SUPERVISOR_PID", std::process::id().to_string()) + .kill_on_drop(true); + #[cfg(windows)] + command.creation_flags(0x08000000); + #[cfg(target_os = "linux")] + { + let parent = std::process::id() as libc::pid_t; + unsafe { + command.pre_exec(move || { + if libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGTERM) != 0 { + return Err(std::io::Error::last_os_error()); + } + if libc::getppid() != parent { + return Err(std::io::Error::other( + "supervisor exited before child startup", + )); + } + Ok(()) + }); + } + } + let mut child = command.spawn()?; + #[cfg(windows)] + if let Err(error) = job.assign( + child + .raw_handle() + .context("child process handle unavailable")?, + ) { + let _ = child.kill().await; + return Err(error); + } + let result = tokio::select! { + result=child.wait()=>result?, + result=shutdown()=>{ + result?; + #[cfg(unix)]if let Some(pid)=child.id(){unsafe{libc::kill(pid as libc::pid_t,libc::SIGTERM);}} + #[cfg(unix)]if tokio::time::timeout(Duration::from_secs(3),child.wait()).await.is_ok(){return Ok(())} + let _=child.kill().await;return Ok(()) + } + }; + if result.success() { + return Ok(()); + } + if result.code() == Some(75) { + delay = 1; + continue; + } + tokio::time::sleep(Duration::from_secs(delay)).await; + delay = (delay * 2).min(30); + } +} +#[cfg(windows)] +struct Job(windows_sys::Win32::Foundation::HANDLE); +#[cfg(windows)] +impl Job { + fn new() -> Result { + use windows_sys::Win32::System::JobObjects::*; + let handle = unsafe { CreateJobObjectW(std::ptr::null(), std::ptr::null()) }; + if handle.is_null() { + return Err(std::io::Error::last_os_error().into()); + } + let job = Self(handle); + let mut info: JOBOBJECT_EXTENDED_LIMIT_INFORMATION = unsafe { std::mem::zeroed() }; + info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; + if unsafe { + SetInformationJobObject( + handle, + JobObjectExtendedLimitInformation, + &info as *const _ as _, + std::mem::size_of_val(&info) as u32, + ) + } == 0 + { + return Err(std::io::Error::last_os_error().into()); + }; + Ok(job) + } + fn assign(&self, child: std::os::windows::io::RawHandle) -> Result<()> { + if unsafe { + windows_sys::Win32::System::JobObjects::AssignProcessToJobObject(self.0, child as _) + } == 0 + { + return Err(std::io::Error::last_os_error().into()); + }; + Ok(()) + } +} +#[cfg(windows)] +impl Drop for Job { + fn drop(&mut self) { + unsafe { windows_sys::Win32::Foundation::CloseHandle(self.0) }; + } +} diff --git a/crates/ads-management/Cargo.toml b/crates/ads-management/Cargo.toml new file mode 100644 index 0000000..520ee9d --- /dev/null +++ b/crates/ads-management/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "ads-management" +version.workspace = true +edition.workspace = true +license.workspace = true + +[dependencies] +ads-core = { path = "../ads-core" } +anyhow.workspace = true +serde.workspace = true +serde_json.workspace = true +tokio.workspace = true +sha2 = "0.10" +futures-util.workspace = true +tokio-tungstenite.workspace = true + +[target.'cfg(unix)'.dependencies] +libc = "0.2" + +[target.'cfg(windows)'.dependencies] +windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_Security", "Win32_Security_Authorization", "Win32_System_Memory"] } diff --git a/crates/ads-management/src/ipc.rs b/crates/ads-management/src/ipc.rs new file mode 100644 index 0000000..47f8a3b --- /dev/null +++ b/crates/ads-management/src/ipc.rs @@ -0,0 +1,295 @@ +use crate::{read_frame, write_frame, Request, Response}; +use anyhow::{bail, Context, Result}; +use sha2::{Digest, Sha256}; +use std::{ + future::Future, + path::{Path, PathBuf}, + pin::Pin, + sync::Arc, + time::Duration, +}; +use tokio::{ + io::{AsyncRead, AsyncWrite}, + task::JoinSet, + time::timeout, +}; +pub type Handler = + Arc Pin + Send>> + Send + Sync>; +fn absolute(path: &Path) -> Result { + Ok( + std::fs::canonicalize(path.parent().context("missing config parent")?)? + .join(path.file_name().context("missing config filename")?), + ) +} +pub fn endpoint(config: &Path) -> Result { + let path = absolute(config)?; + if cfg!(windows) { + let normalized = path.to_string_lossy().to_lowercase(); + let hash = Sha256::digest(normalized.as_bytes()); + Ok(format!(r"\\.\pipe\ttstd-ads-{:x}", hash)) + } else { + let hash = format!( + "{:x}", + Sha256::digest(path.file_name().unwrap().as_encoded_bytes()) + ); + Ok(path + .parent() + .unwrap() + .join(format!("agent-{}.sock", &hash[..16])) + .to_string_lossy() + .into_owned()) + } +} +async fn connection( + mut socket: impl AsyncRead + AsyncWrite + Unpin, + handler: Handler, +) -> Result<()> { + let request = timeout(Duration::from_secs(5), read_frame(&mut socket)).await??; + let response = timeout(Duration::from_secs(12), handler(request)).await?; + timeout(Duration::from_secs(5), write_frame(&mut socket, &response)).await??; + Ok(()) +} +pub async fn request(config: &Path, value: &Request) -> Result { + let name = endpoint(config)?; + timeout(Duration::from_secs(15), async { + #[cfg(windows)] + let connect_deadline = std::time::Instant::now() + Duration::from_secs(3); + #[cfg(windows)] + let mut socket = loop { + match tokio::net::windows::named_pipe::ClientOptions::new().open(&name) { + Ok(socket) => break socket, + Err(error) + if matches!(error.raw_os_error(), Some(2 | 231)) + && std::time::Instant::now() < connect_deadline => + { + tokio::time::sleep(Duration::from_millis(50)).await + } + Err(error) => return Err(error.into()), + } + }; + #[cfg(unix)] + let mut socket = tokio::net::UnixStream::connect(&name) + .await + .context("agent is not running or local socket is inaccessible")?; + write_frame(&mut socket, value).await?; + read_frame(&mut socket).await + }) + .await + .context("local agent management timed out")? +} +#[cfg(unix)] +pub async fn serve( + config: &Path, + handler: Handler, + ready: tokio::sync::oneshot::Sender<()>, +) -> Result<()> { + use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt}; + let name = endpoint(config)?; + let path = PathBuf::from(&name); + let directory = std::fs::metadata(path.parent().unwrap())?; + if directory.mode() & 0o022 != 0 { + bail!("config directory must not be writable by other users") + } + let owner = directory.uid(); + if path.exists() { + if tokio::net::UnixStream::connect(&path).await.is_ok() { + bail!("agent already running") + } + let metadata = std::fs::symlink_metadata(&path)?; + if !metadata.file_type().is_socket() || metadata.uid() != owner { + bail!("refusing to remove a foreign socket path") + } + std::fs::remove_file(&path)?; + } + let listener = tokio::net::UnixListener::bind(&path)?; + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600))?; + let cpath = std::ffi::CString::new(name)?; + if unsafe { libc::geteuid() } == 0 + && unsafe { libc::chown(cpath.as_ptr(), owner, u32::MAX) } != 0 + { + bail!("cannot set local socket owner") + } + struct Cleanup(PathBuf); + impl Drop for Cleanup { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.0); + } + } + let _cleanup = Cleanup(path); + let _ = ready.send(()); + let mut tasks = JoinSet::new(); + loop { + tokio::select! { + accepted=listener.accept()=>{let(socket,_)=accepted?;if tasks.len()>=16{drop(socket);continue;}let handler=handler.clone();tasks.spawn(async move{let _=connection(socket,handler).await;});}, + _=tasks.join_next(),if !tasks.is_empty()=>{}, + } + } +} +#[cfg(windows)] +pub async fn serve( + config: &Path, + handler: Handler, + ready: tokio::sync::oneshot::Sender<()>, +) -> Result<()> { + use tokio::net::windows::named_pipe::ServerOptions; + let name = endpoint(config)?; + let security = Security::new(config.parent().context("missing config directory")?)?; + let create = |first: bool| -> Result<_> { + let mut attributes = security.attributes(); + Ok(unsafe { + ServerOptions::new() + .first_pipe_instance(first) + .reject_remote_clients(true) + .create_with_security_attributes_raw(&name, &mut attributes as *mut _ as *mut _) + }?) + }; + let mut server = create(true) + .context("cannot create private management pipe; agent may already be running")?; + let _ = ready.send(()); + let mut tasks = JoinSet::new(); + loop { + tokio::select! { + connected=server.connect()=>{connected?;let next=create(false)?;let socket=std::mem::replace(&mut server,next);if tasks.len()>=16{drop(socket);continue;}let handler=handler.clone();tasks.spawn(async move{let _=connection(socket,handler).await;});}, + _=tasks.join_next(),if !tasks.is_empty()=>{}, + } + } +} +#[cfg(windows)] +pub fn owner_sid(path: &Path) -> Result { + use std::os::windows::ffi::OsStrExt; + use windows_sys::Win32::{ + Foundation::LocalFree, + Security::{ + Authorization::{ConvertSidToStringSidW, GetNamedSecurityInfoW, SE_FILE_OBJECT}, + OWNER_SECURITY_INFORMATION, + }, + }; + let name: Vec = path.as_os_str().encode_wide().chain(Some(0)).collect(); + let mut owner = std::ptr::null_mut(); + let mut descriptor = std::ptr::null_mut(); + let code = unsafe { + GetNamedSecurityInfoW( + name.as_ptr(), + SE_FILE_OBJECT, + OWNER_SECURITY_INFORMATION, + &mut owner, + std::ptr::null_mut(), + std::ptr::null_mut(), + std::ptr::null_mut(), + &mut descriptor, + ) + }; + if code != 0 { + bail!("cannot read config directory owner: {code}") + } + let mut sid = std::ptr::null_mut(); + let result = unsafe { ConvertSidToStringSidW(owner, &mut sid) }; + if result == 0 { + unsafe { LocalFree(descriptor) }; + bail!("cannot format config owner SID") + } + let mut length = 0; + unsafe { + while *sid.add(length) != 0 { + length += 1; + } + } + let result = String::from_utf16_lossy(unsafe { std::slice::from_raw_parts(sid, length) }); + unsafe { + LocalFree(sid as _); + LocalFree(descriptor); + } + Ok(result) +} +#[cfg(windows)] +struct Security(*mut std::ffi::c_void); +#[cfg(windows)] +unsafe impl Send for Security {} +#[cfg(windows)] +impl Security { + fn new(directory: &Path) -> Result { + use windows_sys::Win32::Security::Authorization::ConvertStringSecurityDescriptorToSecurityDescriptorW; + let owner = owner_sid(directory)?; + let sddl: Vec = format!("D:P(A;;GA;;;SY)(A;;GA;;;{owner})") + .encode_utf16() + .chain(Some(0)) + .collect(); + let mut descriptor = std::ptr::null_mut(); + if unsafe { + ConvertStringSecurityDescriptorToSecurityDescriptorW( + sddl.as_ptr(), + 1, + &mut descriptor, + std::ptr::null_mut(), + ) + } == 0 + { + bail!("cannot create private pipe ACL") + }; + Ok(Self(descriptor)) + } + fn attributes(&self) -> windows_sys::Win32::Security::SECURITY_ATTRIBUTES { + windows_sys::Win32::Security::SECURITY_ATTRIBUTES { + nLength: std::mem::size_of::() + as u32, + lpSecurityDescriptor: self.0, + bInheritHandle: 0, + } + } +} +#[cfg(windows)] +impl Drop for Security { + fn drop(&mut self) { + unsafe { windows_sys::Win32::Foundation::LocalFree(self.0) }; + } +} +#[cfg(windows)] +pub fn protect_file(path: &Path, parent: &Path) -> Result<()> { + let sid = owner_sid(parent)?; + use std::os::windows::process::CommandExt; + let status = std::process::Command::new("icacls.exe") + .creation_flags(0x08000000) + .arg(path) + .args([ + "/inheritance:r", + "/grant:r", + &format!("*{sid}:(F)"), + "*S-1-5-18:(F)", + "/Q", + ]) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .status()?; + if !status.success() { + bail!("cannot restrict private file permissions") + }; + Ok(()) +} +#[cfg(unix)] +pub fn protect_file(path: &Path, parent: &Path) -> Result<()> { + use std::os::unix::ffi::OsStrExt; + use std::os::unix::fs::MetadataExt; + let metadata = std::fs::metadata(parent)?; + if metadata.mode() & 0o022 != 0 { + bail!("config directory must not be writable by other users") + } + if unsafe { libc::geteuid() } == 0 { + let path = std::ffi::CString::new(path.as_os_str().as_bytes())?; + if unsafe { libc::chown(path.as_ptr(), metadata.uid(), u32::MAX) } != 0 { + bail!("cannot preserve config owner") + } + } + Ok(()) +} + +#[cfg(test)] +mod tests { + #[test] + fn config_filenames_have_isolated_management_endpoints() { + let folder = std::env::temp_dir(); + assert_ne!( + super::endpoint(&folder.join("first-agent.json")).unwrap(), + super::endpoint(&folder.join("second-agent.json")).unwrap() + ); + } +} diff --git a/crates/ads-management/src/lib.rs b/crates/ads-management/src/lib.rs new file mode 100644 index 0000000..4a0a7c3 --- /dev/null +++ b/crates/ads-management/src/lib.rs @@ -0,0 +1,367 @@ +pub mod ipc; +pub mod service; +use ads_core::{valid_device_id, validate_token, NetworkArgs}; +use anyhow::{bail, Context, Result}; +use futures_util::{SinkExt, StreamExt}; +use serde::{Deserialize, Serialize}; +use std::{ + collections::HashSet, + path::{Path, PathBuf}, + time::{SystemTime, UNIX_EPOCH}, +}; +use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; +use tokio_tungstenite::tungstenite::Message; +pub const MAX_FRAME: usize = 128 * 1024; +pub fn now() -> u64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_secs() +} +fn yes() -> bool { + true +} +fn adb_port() -> u16 { + 5555 +} +fn adb_server() -> String { + "127.0.0.1:5037".into() +} +fn version() -> u32 { + 1 +} +#[derive(Clone, Serialize, Deserialize, PartialEq, Eq, Debug)] +#[serde(deny_unknown_fields)] +pub struct DeviceConfig { + pub id: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub target: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub adb_serial: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub adb_server: Option, + #[serde(default = "adb_port")] + pub device_port: u16, + #[serde(default = "yes")] + pub enabled: bool, +} +#[derive(Clone, Serialize, Deserialize, Debug)] +#[serde(deny_unknown_fields)] +pub struct AgentConfig { + #[serde(default = "version")] + pub version: u32, + pub id: String, + pub name: String, + pub server: String, + #[serde(default)] + pub connect_address: Option, + #[serde(default)] + pub allow_insecure: bool, + #[serde(default = "adb_server")] + pub adb_server: String, + #[serde(default)] + pub devices: Vec, +} +impl AgentConfig { + pub fn network(&self) -> NetworkArgs { + NetworkArgs { + server: self.server.clone(), + connect_address: self.connect_address.clone(), + allow_insecure: self.allow_insecure, + } + } + pub fn validate(&self) -> Result<()> { + if self.version != 1 + || !valid_device_id(&self.id) + || self.name.is_empty() + || self.name.len() > 128 + || self.name.chars().any(char::is_control) + { + bail!("invalid agent version, ID or name") + } + if serde_json::to_vec_pretty(self)?.len() > 64 * 1024 { + bail!("agent config exceeds 64 KiB") + } + self.network().url("/v3/agents/register", &[])?; + validate_address(&self.adb_server)?; + if let Some(address) = &self.connect_address { + validate_address(address)?; + } + if self.devices.len() > 128 { + bail!("at most 128 devices per agent") + } + let mut ids = HashSet::new(); + for device in &self.devices { + if !valid_device_id(&device.id) || !ids.insert(&device.id) || device.device_port == 0 { + bail!("invalid or duplicate device ID/port") + } + if device.target.is_some() == device.adb_serial.is_some() { + bail!("each device requires exactly one of target or adb_serial") + } + if let Some(target) = &device.target { + validate_address(target)?; + } + if let Some(server) = &device.adb_server { + validate_address(server)?; + } + if let Some(serial) = &device.adb_serial { + if serial.is_empty() || serial.len() > 256 || serial.chars().any(char::is_control) { + bail!("invalid adb_serial") + } + } + } + Ok(()) + } + pub fn load(path: &Path) -> Result { + let data = std::fs::read(path).context("cannot read agent config")?; + if data.len() > 64 * 1024 { + bail!("agent config exceeds 64 KiB") + }; + let config: Self = serde_json::from_slice(&data).context("invalid agent config JSON")?; + config.validate()?; + Ok(config) + } +} +fn validate_address(value: &str) -> Result<()> { + if value.is_empty() + || value.len() > 256 + || value.chars().any(|c| c.is_whitespace() || c.is_control()) + { + bail!("invalid host:port address") + } + let (host, port) = value + .rsplit_once(':') + .context("address requires host:port")?; + if host.is_empty() || port.parse::().unwrap_or(0) == 0 { + bail!("invalid host:port address") + }; + Ok(()) +} +#[derive(Clone, Serialize, Deserialize, Debug)] +#[serde(tag = "op", rename_all = "snake_case", deny_unknown_fields)] +pub enum Request { + Status, + DevicesList, + DeviceAdd { + device: DeviceConfig, + }, + DeviceRemove { + id: String, + }, + DeviceEnable { + id: String, + enabled: bool, + }, + ConfigGet, + ConfigSet { + config: AgentConfig, + }, + Logs { + #[serde(default)] + after: u64, + #[serde(default)] + limit: u32, + }, + Restart, +} +#[derive(Clone, Serialize, Deserialize, Debug)] +pub struct Response { + pub ok: bool, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub data: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub error: Option, +} +impl Response { + pub fn success(value: impl Serialize) -> Self { + Self { + ok: true, + data: Some(serde_json::to_value(value).unwrap_or_default()), + error: None, + } + } + pub fn failure(error: impl ToString) -> Self { + Self { + ok: false, + data: None, + error: Some(error.to_string()), + } + } + pub fn value(self) -> Result { + if !self.ok { + bail!( + "{}", + self.error + .unwrap_or_else(|| "management request failed".into()) + ) + }; + Ok(self.data.unwrap_or_default()) + } +} +#[derive(Clone, Serialize, Deserialize, Debug)] +pub struct DeviceStatus { + #[serde(flatten)] + pub config: DeviceConfig, + pub connected: bool, + pub last_error: Option, +} +#[derive(Clone, Serialize, Deserialize, Debug)] +pub struct AgentStatus { + pub id: String, + pub name: String, + pub hostname: String, + pub os: String, + pub arch: String, + pub version: String, + pub pid: u32, + pub uptime_seconds: u64, + pub supervised: bool, + pub revision: u64, + pub instance: String, + pub devices: Vec, +} +#[derive(Clone, Serialize, Deserialize, Debug)] +pub struct LogEntry { + pub seq: u64, + pub time: u64, + pub level: String, + pub message: String, +} +#[derive(Clone, Serialize, Deserialize, Debug)] +pub struct LogPage { + pub instance: String, + pub next: u64, + pub entries: Vec, +} +pub fn default_config() -> PathBuf { + if cfg!(windows) { + PathBuf::from(std::env::var_os("LOCALAPPDATA").unwrap_or_else(|| ".".into())) + .join("AndroidDeviceStreaming/agent/agent.json") + } else { + PathBuf::from( + std::env::var_os("XDG_CONFIG_HOME") + .map(PathBuf::from) + .unwrap_or_else(|| { + PathBuf::from(std::env::var_os("HOME").unwrap_or_else(|| ".".into())) + .join(".config") + }), + ) + .join("ads/agent.json") + } +} +pub fn credential(path: &Path, key: &str) -> Result { + if let Ok(value) = std::env::var(key) { + validate_token(&value)?; + return Ok(value); + } + let data = std::fs::read_to_string(path).context( + "credential file not found; set the role token in env or provide its private env file", + )?; + for line in data.lines() { + if let Some(value) = line.strip_prefix(&format!("{key}=")) { + validate_token(value)?; + return Ok(value.into()); + } + } + bail!("missing {key}") +} +pub fn save_private(path: &Path, data: &[u8]) -> Result<()> { + let parent = path + .parent() + .context("configuration requires a parent directory")?; + std::fs::create_dir_all(parent)?; + let temporary = parent.join(format!( + ".ads-{}-{}.tmp", + std::process::id(), + SystemTime::now().duration_since(UNIX_EPOCH)?.as_nanos() + )); + use std::io::Write; + let mut options = std::fs::OpenOptions::new(); + options.write(true).create_new(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + options.mode(0o600); + } + let result = (|| -> Result<()> { + let mut file = options.open(&temporary)?; + ipc::protect_file(&temporary, parent)?; + file.write_all(data)?; + file.sync_all()?; + drop(file); + std::fs::rename(&temporary, path)?; + Ok(()) + })(); + if result.is_err() { + let _ = std::fs::remove_file(&temporary); + } + result +} +pub async fn read_frame Deserialize<'de>>( + stream: &mut (impl AsyncRead + Unpin), +) -> Result { + let size = stream.read_u32_le().await? as usize; + if size == 0 || size > MAX_FRAME { + bail!("invalid management frame length") + }; + let mut data = vec![0; size]; + stream.read_exact(&mut data).await?; + Ok(serde_json::from_slice(&data)?) +} +pub async fn write_frame( + stream: &mut (impl AsyncWrite + Unpin), + value: &impl Serialize, +) -> Result<()> { + let data = serde_json::to_vec(value)?; + if data.len() > MAX_FRAME { + bail!("management response too large") + }; + stream.write_u32_le(data.len() as u32).await?; + stream.write_all(&data).await?; + stream.flush().await?; + Ok(()) +} +pub async fn read_ws Deserialize<'de>>(ws: &mut ads_core::Ws) -> Result { + loop { + match ws.next().await.context("management connection closed")?? { + Message::Text(data) => return Ok(serde_json::from_str(&data)?), + Message::Ping(data) => ws.send(Message::Pong(data)).await?, + Message::Pong(_) => {} + _ => bail!("management connection closed or invalid message"), + } + } +} +pub async fn write_ws(ws: &mut ads_core::Ws, value: &impl Serialize) -> Result<()> { + let data = serde_json::to_string(value)?; + if data.len() > MAX_FRAME { + bail!("management response too large") + }; + ws.send(Message::Text(data.into())).await?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn accepted_config_fits_the_restart_loader_limit() { + let mut cfg: AgentConfig = serde_json::from_value(serde_json::json!({ + "id":"host", "name":"Host", "server":"wss://example.com/ads", "devices":[] + })) + .unwrap(); + cfg.validate().unwrap(); + let host = format!("{}:5555", "a".repeat(240)); + for index in 0..128 { + cfg.devices.push(DeviceConfig { + id: format!("phone-{index}"), + target: Some(host.clone()), + adb_serial: None, + adb_server: Some(host.clone()), + device_port: 5555, + enabled: true, + }); + } + assert!(serde_json::to_vec_pretty(&cfg).unwrap().len() > 64 * 1024); + assert!(cfg.validate().unwrap_err().to_string().contains("64 KiB")); + } +} diff --git a/crates/ads-management/src/service.rs b/crates/ads-management/src/service.rs new file mode 100644 index 0000000..24b905c --- /dev/null +++ b/crates/ads-management/src/service.rs @@ -0,0 +1,170 @@ +#[cfg(unix)] +use crate::save_private; +use crate::AgentConfig; +use anyhow::{bail, Context, Result}; +use std::path::Path; +#[cfg(unix)] +use std::path::PathBuf; +#[derive(Clone, Copy)] +pub enum Action { + Install, + Start, + Stop, + Restart, + Status, + Uninstall, +} +fn safe_path(path: &Path) -> Result { + let text = path.to_str().context("service paths must be valid UTF-8")?; + #[cfg(windows)] + let text = text.strip_prefix(r"\\?\").unwrap_or(text); + if text.chars().any(char::is_control) { + bail!("service path contains control characters") + }; + Ok(text.into()) +} +#[cfg(windows)] +fn ps(value: &str) -> String { + format!("'{}'", value.replace(char::from(39), "''")) +} +#[cfg(windows)] +pub fn run(action: Action, config: &Path, _system: bool, start_now: bool) -> Result<()> { + let cfg = AgentConfig::load(config)?; + let config = std::fs::canonicalize(config)?; + let executable = std::env::current_exe()?.with_file_name("ads-agent.exe"); + let executable = + std::fs::canonicalize(executable).context("place ads-agent.exe next to adsctl.exe")?; + let name = format!("ADS-Agent-{}", cfg.id); + let executable = safe_path(&executable)?; + let path = safe_path(&config)?; + let arguments = format!("--quiet --supervise --config \"{path}\""); + let guard=format!("$task=Get-ScheduledTask -TaskName {} -ErrorAction SilentlyContinue; if($task -and -not (@($task.Actions | Where-Object {{ $_.Execute -eq {} -and $_.Arguments -eq {} }}).Count)) {{ throw 'A foreign task owns this name' }};",ps(&name),ps(&executable),ps(&arguments)); + let body=match action{ + Action::Install=>format!("$action=New-ScheduledTaskAction -Execute {} -Argument {}; $trigger=New-ScheduledTaskTrigger -AtStartup; $settings=New-ScheduledTaskSettingsSet -RestartCount 999 -RestartInterval (New-TimeSpan -Minutes 1) -ExecutionTimeLimit ([TimeSpan]::Zero) -StartWhenAvailable -MultipleInstances IgnoreNew; Register-ScheduledTask -TaskName {} -Action $action -Trigger $trigger -Settings $settings -User SYSTEM -RunLevel Highest -Force | Out-Null; {}",ps(&executable),ps(&arguments),ps(&name),if start_now {format!("Start-ScheduledTask -TaskName {};",ps(&name))} else {String::new()}), + Action::Start=>format!("Start-ScheduledTask -TaskName {};",ps(&name)), + Action::Stop=>format!("Stop-ScheduledTask -TaskName {};",ps(&name)), + Action::Restart=>format!("Stop-ScheduledTask -TaskName {}; Start-Sleep -Seconds 1; Start-ScheduledTask -TaskName {};",ps(&name),ps(&name)), + Action::Status=>format!("Get-ScheduledTask -TaskName {} | Select-Object TaskName,State; Get-ScheduledTaskInfo -TaskName {};",ps(&name),ps(&name)), + Action::Uninstall=>format!("Stop-ScheduledTask -TaskName {} -ErrorAction SilentlyContinue; Unregister-ScheduledTask -TaskName {} -Confirm:$false;",ps(&name),ps(&name)), + }; + use std::os::windows::process::CommandExt; + let output = std::process::Command::new("powershell.exe") + .creation_flags(0x08000000) + .args([ + "-NoProfile", + "-NonInteractive", + "-Command", + &format!("$ErrorActionPreference='Stop';{guard}{body}"), + ]) + .output()?; + eprint!( + "{}{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + if !output.status.success() { + bail!("Windows service operation failed; install/uninstall require an elevated administrator terminal") + }; + Ok(()) +} +#[cfg(unix)] +pub fn run(action: Action, config: &Path, system: bool, start_now: bool) -> Result<()> { + if !cfg!(target_os = "linux") { + bail!("native service install currently supports Linux systemd and Windows Task Scheduler; run ads-agent --supervise manually on this platform") + } + let cfg = AgentConfig::load(config)?; + let config = std::fs::canonicalize(config)?; + let executable = std::fs::canonicalize(std::env::current_exe()?.with_file_name("ads-agent")) + .context("place ads-agent next to adsctl")?; + let unit = format!("ads-agent-{}.service", cfg.id); + let folder = if system { + PathBuf::from("/etc/systemd/system") + } else { + PathBuf::from(std::env::var_os("HOME").context("HOME is missing")?) + .join(".config/systemd/user") + }; + let path = folder.join(&unit); + let marker = format!( + "# Managed by ADSCTL {}\n# Config: {}\n# Executable: {}\n", + cfg.id, + safe_path(&config)?, + safe_path(&executable)? + ); + let systemctl = |args: &[&str]| -> Result<()> { + let mut command = std::process::Command::new("systemctl"); + if !system { + command.arg("--user"); + } + let output = command.args(args).output()?; + eprint!( + "{}{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + if !output.status.success() { + bail!("systemctl failed; check service privileges and availability") + }; + Ok(()) + }; + let guard = || -> Result<()> { + if path.exists() && !std::fs::read_to_string(&path)?.starts_with(&marker) { + bail!("a foreign systemd unit owns this name") + }; + Ok(()) + }; + guard()?; + match action { + Action::Install => { + let mut user = String::new(); + if system { + use std::os::unix::fs::MetadataExt; + let owner = std::fs::metadata(config.parent().unwrap())?.uid(); + let output = std::process::Command::new("id") + .args(["-un", &owner.to_string()]) + .output()?; + if !output.status.success() { + bail!("cannot resolve service config owner") + }; + let name = String::from_utf8(output.stdout)?.trim().to_string(); + if name.is_empty() + || !name + .bytes() + .all(|c| c.is_ascii_alphanumeric() || b"_-.".contains(&c)) + { + bail!("unsupported service account name") + }; + user = format!("User={name}\n"); + } + fn quote(path: &Path) -> Result { + Ok(format!( + "\"{}\"", + safe_path(path)? + .replace('%', "%%") + .replace('\\', "\\\\") + .replace('\"', "\\\"") + )) + } + let body=format!("{marker}[Unit]\nDescription=ADS managed agent {}\nAfter=network-online.target\nWants=network-online.target\n[Service]\nType=simple\n{user}ExecStart={} --quiet --supervise --config {}\nRestart=on-failure\nRestartSec=3\nNoNewPrivileges=true\nPrivateTmp=true\n[Install]\nWantedBy={}\n",cfg.id,quote(&executable)?,quote(&config)?,if system{"multi-user.target"}else{"default.target"}); + save_private(&path, body.as_bytes())?; + systemctl(&["daemon-reload"])?; + if start_now { + systemctl(&["enable", "--now", &unit])?; + } else { + systemctl(&["enable", &unit])?; + } + } + Action::Start => systemctl(&["start", &unit])?, + Action::Stop => systemctl(&["stop", &unit])?, + Action::Restart => systemctl(&["restart", &unit])?, + Action::Status => systemctl(&["status", "--no-pager", &unit])?, + Action::Uninstall => { + guard()?; + systemctl(&["disable", "--now", &unit])?; + if path.exists() { + std::fs::remove_file(path)?; + } + systemctl(&["daemon-reload"])?; + } + } + Ok(()) +} diff --git a/ctl/Cargo.toml b/ctl/Cargo.toml new file mode 100644 index 0000000..af1cac0 --- /dev/null +++ b/ctl/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "adsctl" +version.workspace = true +edition.workspace = true +license.workspace = true +[dependencies] +ads-core = { path = "../crates/ads-core" } +ads-management = { path = "../crates/ads-management" } +anyhow.workspace = true +clap.workspace = true +serde_json.workspace = true +tokio.workspace = true diff --git a/ctl/src/main.rs b/ctl/src/main.rs new file mode 100644 index 0000000..2c78830 --- /dev/null +++ b/ctl/src/main.rs @@ -0,0 +1,342 @@ +use ads_core::{validate_token, NetworkArgs}; +use ads_management::{AgentConfig, DeviceConfig, LogPage, Request, Response}; +use anyhow::{bail, Context, Result}; +use clap::{Parser, Subcommand}; +use std::{path::PathBuf, time::Duration}; +#[derive(Parser)] +#[command(version, about = "ADS headless agent management CLI")] +struct Args { + #[command(flatten)] + network: NetworkArgs, + #[arg(long, global = true)] + config: Option, + /// Remote agent ID; omit for local named pipe / Unix socket + #[arg(long, global = true)] + agent: Option, + #[arg(long, global = true)] + credentials: Option, + #[arg(long, global = true)] + json: bool, + #[command(subcommand)] + command: Command, +} +#[derive(Subcommand, Clone)] +enum Command { + Init { + #[arg(long)] + id: String, + #[arg(long)] + name: Option, + }, + Status, + Restart, + /// Read one structured request from stdin (for GUI and automation clients) + Request, + Agents { + #[command(subcommand)] + command: Agents, + }, + Devices { + #[command(subcommand)] + command: Devices, + }, + Config { + #[command(subcommand)] + command: Config, + }, + Logs { + #[arg(long)] + follow: bool, + #[arg(long, default_value_t = 0)] + after: u64, + #[arg(long, default_value_t = 50)] + limit: u32, + }, + Service { + #[command(subcommand)] + command: Service, + #[arg(long, global = true)] + system: bool, + }, +} +#[derive(Subcommand, Clone)] +enum Agents { + List, +} +#[derive(Subcommand, Clone)] +enum Devices { + List, + Add { + #[arg(long)] + id: String, + #[arg( + long, + conflicts_with = "adb_serial", + required_unless_present = "adb_serial" + )] + target: Option, + #[arg(long)] + adb_serial: Option, + #[arg(long)] + adb_server: Option, + #[arg(long, default_value_t = 5555)] + device_port: u16, + }, + Remove { + id: String, + }, + Enable { + id: String, + }, + Disable { + id: String, + }, +} +#[derive(Subcommand, Clone)] +enum Config { + Get, + Validate { + #[arg(long)] + file: Option, + }, + Set { + #[arg(long)] + file: PathBuf, + }, +} +#[derive(Subcommand, Clone)] +enum Service { + Install, + Start, + Stop, + Restart, + Status, + Uninstall, +} +fn print_value(value: &serde_json::Value, json: bool) -> Result<()> { + if json { + println!("{}", serde_json::to_string(value)?) + } else { + println!("{}", serde_json::to_string_pretty(value)?) + }; + Ok(()) +} +async fn invoke(args: &Args, request: &Request) -> Result { + if let Some(id) = &args.agent { + if !ads_core::valid_device_id(id) { + bail!("invalid remote agent ID") + } + let credential = args + .credentials + .clone() + .unwrap_or_else(|| ads_management::default_config().with_file_name("admin.env")); + let token = ads_management::credential(&credential, "ADS_ADMIN_TOKEN")?; + let mut ws = ads_core::dial( + args.network.url("/v3/rpc", &[("agent", id)])?, + &token, + args.network.connect_address.as_deref(), + ) + .await?; + ads_management::write_ws(&mut ws, request).await?; + tokio::time::timeout(Duration::from_secs(20), ads_management::read_ws(&mut ws)) + .await + .context("remote management request timed out")? + } else { + ads_management::ipc::request( + &args + .config + .clone() + .unwrap_or_else(ads_management::default_config), + request, + ) + .await + } +} +#[tokio::main] +async fn main() -> Result<()> { + let mut args = Args::parse(); + // A private admin.env may carry the same optional TCP origin override as client.env. + if args.network.connect_address.is_none() { + if let Some(path) = &args.credentials { + if let Ok(data) = std::fs::read_to_string(path) { + for line in data.lines() { + if let Some(value) = line.strip_prefix("ADS_CONNECT_ADDRESS=") { + args.network.connect_address = Some(value.into()); + } + } + } + } + } + let config = args + .config + .clone() + .unwrap_or_else(ads_management::default_config); + match &args.command { + Command::Init { id, name } => { + if args.agent.is_some() { + bail!("init is a local operation") + }; + if config.exists() { + bail!("config already exists; use config set to update it") + } + let cfg = AgentConfig { + version: 1, + id: id.clone(), + name: name.clone().unwrap_or_else(|| id.clone()), + server: args.network.server.clone(), + connect_address: args.network.connect_address.clone(), + allow_insecure: args.network.allow_insecure, + adb_server: "127.0.0.1:5037".into(), + devices: vec![], + }; + cfg.validate()?; + let source = args + .credentials + .clone() + .unwrap_or_else(|| config.with_file_name("agent.env")); + let token = ads_management::credential(&source, "ADS_AGENT_TOKEN")?; + validate_token(&token)?; + ads_management::save_private( + &config.with_file_name("agent.env"), + format!("ADS_AGENT_TOKEN={token}\n").as_bytes(), + )?; + ads_management::save_private(&config, &serde_json::to_vec_pretty(&cfg)?)?; + return print_value(&serde_json::json!({"config":config,"agent":id}), args.json); + } + Command::Agents { .. } => { + let path = args + .credentials + .clone() + .unwrap_or_else(|| config.with_file_name("admin.env")); + let token = ads_management::credential(&path, "ADS_ADMIN_TOKEN")?; + let mut ws = ads_core::dial( + args.network.url("/v3/agents", &[])?, + &token, + args.network.connect_address.as_deref(), + ) + .await?; + let value: serde_json::Value = ads_management::read_ws(&mut ws).await?; + return print_value(&value, args.json); + } + Command::Config { + command: Config::Validate { file }, + } => { + let cfg = if args.agent.is_some() && file.is_none() { + let value = invoke(&args, &Request::ConfigGet).await?.value()?; + let value: AgentConfig = serde_json::from_value(value)?; + value.validate()?; + value + } else { + AgentConfig::load(file.as_ref().unwrap_or(&config))? + }; + return print_value( + &serde_json::json!({"valid":true,"agent":cfg.id,"devices":cfg.devices.len()}), + args.json, + ); + } + Command::Service { command, system } => { + if args.agent.is_some() { + bail!("service installation and OS service operations are local only; use restart for remote managed restart") + } + use ads_management::service::Action; + let action = match command { + Service::Install => Action::Install, + Service::Start => Action::Start, + Service::Stop => Action::Stop, + Service::Restart => Action::Restart, + Service::Status => Action::Status, + Service::Uninstall => Action::Uninstall, + }; + let start_now = !matches!(action, Action::Install) + || ads_management::ipc::request(&config, &Request::Status) + .await + .is_err(); + ads_management::service::run(action, &config, *system, start_now)?; + return print_value(&serde_json::json!({"ok":true}), args.json); + } + Command::Logs { + follow, + after, + limit, + } => { + let mut cursor = *after; + let mut instance = String::new(); + loop { + let value = invoke( + &args, + &Request::Logs { + after: cursor, + limit: *limit, + }, + ) + .await? + .value()?; + let page: LogPage = serde_json::from_value(value.clone())?; + if !instance.is_empty() && instance != page.instance { + cursor = 0; + instance = page.instance; + continue; + } + cursor = page.next; + instance = page.instance; + if args.json { + print_value(&value, true)? + } else { + for entry in page.entries { + println!("{} {} {}", entry.time, entry.level, entry.message) + } + } + if !follow { + return Ok(()); + } + tokio::select! {_=tokio::time::sleep(Duration::from_secs(1))=>{},_=tokio::signal::ctrl_c()=>return Ok(())} + } + } + _ => {} + } + let request = match args.command.clone() { + Command::Request => { + use std::io::Read; + let mut input = Vec::new(); + std::io::stdin().take(65537).read_to_end(&mut input)?; + if input.len() > 65536 { + bail!("request exceeds 64 KiB") + }; + serde_json::from_slice::(&input)? + } + Command::Status => Request::Status, + Command::Restart => Request::Restart, + Command::Devices { command } => match command { + Devices::List => Request::DevicesList, + Devices::Add { + id, + target, + adb_serial, + adb_server, + device_port, + } => Request::DeviceAdd { + device: DeviceConfig { + id, + target, + adb_serial, + adb_server, + device_port, + enabled: true, + }, + }, + Devices::Remove { id } => Request::DeviceRemove { id }, + Devices::Enable { id } => Request::DeviceEnable { id, enabled: true }, + Devices::Disable { id } => Request::DeviceEnable { id, enabled: false }, + }, + Command::Config { + command: Config::Get, + } => Request::ConfigGet, + Command::Config { + command: Config::Set { ref file }, + } => Request::ConfigSet { + config: AgentConfig::load(file)?, + }, + _ => unreachable!(), + }; + print_value(&invoke(&args, &request).await?.value()?, args.json) +} diff --git a/deploy/windows/README.md b/deploy/windows/README.md index 66f221e..ee1dcac 100644 --- a/deploy/windows/README.md +++ b/deploy/windows/README.md @@ -54,3 +54,19 @@ Get-Content "$env:LOCALAPPDATA\AndroidDeviceStreaming\agent\logs\agent.log" -Tai Start-ScheduledTask -TaskName ADS-Agent Stop-ScheduledTask -TaskName ADS-Agent ``` + +## Managed multi-phone Agent (0.2) + +The deployed computer has migrated to Agent ID `home-pc` (家里电脑), retaining `home-phone` and its TCP endpoint. `ADS-Agent-home-pc` runs the native supervised Agent as SYSTEM at startup; the old `ADS-Agent` task is disabled. The migration keeps a timestamped backup under the protected Agent directory and restores the legacy files/task on failure. No reboot was performed. + +`migrate-managed.ps1` migrates the earlier TCP single-phone installation. Stage the new binaries as `ads-agent.exe.new` and `adsctl.exe.new` in its directory, then run the script in an elevated PowerShell 7 terminal. It verifies the legacy task belongs to that directory before stopping it. Parameters customize directory, Agent ID, display name and old task name. New installations should use `adsctl init` and `adsctl service install` instead. + +For current status and logs, use: + +```powershell +& "$env:LOCALAPPDATA/AndroidDeviceStreaming/agent/adsctl.exe" status +& "$env:LOCALAPPDATA/AndroidDeviceStreaming/agent/adsctl.exe" logs --follow +Get-ScheduledTask -TaskName ADS-Agent-home-pc +``` + +The managed log buffer is in memory; the old runner log is historical. See [Agent management](../../docs/agent-management.md) for Linux and Windows headless setup, CLI commands, role credentials and both GUIs. diff --git a/deploy/windows/migrate-managed.ps1 b/deploy/windows/migrate-managed.ps1 new file mode 100644 index 0000000..5ef0ae6 --- /dev/null +++ b/deploy/windows/migrate-managed.ps1 @@ -0,0 +1,67 @@ +param( + [string]$Directory = (Join-Path $env:LOCALAPPDATA 'AndroidDeviceStreaming/agent'), + [string]$Id = 'home-pc', + [string]$Name = '家里电脑', + [string]$LegacyTask = 'ADS-Agent' +) +$ErrorActionPreference = 'Stop' +$ProgressPreference = 'SilentlyContinue' +$root = [IO.Path]::GetFullPath($Directory).TrimEnd('\') +$configPath = Join-Path $root 'agent.json' +$exe = Join-Path $root 'ads-agent.exe' +$ctl = Join-Path $root 'adsctl.exe' +$old = Get-Content -LiteralPath $configPath -Raw | ConvertFrom-Json +if ($old.devices -or $old.id) { throw 'Already managed; use adsctl config commands instead' } +if (-not $old.device -or -not $old.target) { throw 'Expected legacy TCP phone configuration' } +$task = Get-ScheduledTask -TaskName $LegacyTask -ErrorAction Stop +if (-not @($task.Actions | Where-Object { $_.Arguments -and $_.Arguments.Contains($root) }).Count) { throw 'Legacy task does not belong to the requested Agent directory' } +if (Get-ScheduledTask -TaskName "ADS-Agent-$Id" -ErrorAction SilentlyContinue) { throw 'Managed task already exists' } +foreach ($file in @('ads-agent.exe.new', 'adsctl.exe.new')) { + if (-not (Test-Path -LiteralPath (Join-Path $root $file))) { throw "Missing staged $file" } +} +$backup = Join-Path $root ('backup-management-' + (Get-Date -Format yyyyMMdd-HHmmss)) +New-Item -ItemType Directory -Path $backup | Out-Null +foreach ($file in @('agent.json','ads-agent.exe','adsctl.exe')) { + $path = Join-Path $root $file + if (Test-Path -LiteralPath $path) { Copy-Item -LiteralPath $path -Destination $backup } +} +Export-ScheduledTask -TaskName $LegacyTask | Set-Content -LiteralPath (Join-Path $backup 'legacy-task.xml') -Encoding utf8 +$next = [ordered]@{version=1;id=$Id;name=$Name;server=$old.server;connect_address=$old.connect_address;adb_server='127.0.0.1:5037';devices=@(@{id=$old.device;target=$old.target;enabled=$true})} +try { + Disable-ScheduledTask -TaskName $LegacyTask | Out-Null + Stop-ScheduledTask -TaskName $LegacyTask + Get-CimInstance Win32_Process -Filter "Name='ads-agent.exe'" | Where-Object { $_.ExecutablePath -eq $exe } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force } + Start-Sleep -Seconds 1 + Move-Item -LiteralPath (Join-Path $root 'ads-agent.exe.new') -Destination $exe -Force + Move-Item -LiteralPath (Join-Path $root 'adsctl.exe.new') -Destination $ctl -Force + $next | ConvertTo-Json -Depth 10 | Set-Content -LiteralPath $configPath -Encoding utf8NoBOM + & $ctl --config $configPath config validate --file $configPath + if ($LASTEXITCODE) { throw 'Managed config validation failed' } + & $ctl --config $configPath service install + if ($LASTEXITCODE) { throw 'Managed service installation failed' } + $ready=$false + for ($i=0; $i -lt 10; $i++) { + Start-Sleep -Seconds 1 + $raw = & $ctl --json --config $configPath status 2>$null + if ($LASTEXITCODE -eq 0 -and ($raw | ConvertFrom-Json).id -eq $Id) { $ready=$true;break } + } + if (-not $ready) { throw 'Managed Agent did not become ready' } + Write-Output "Migration succeeded; backup: $backup" + & $ctl --json --config $configPath status +} catch { + $failure=$_ + $managed=Get-ScheduledTask -TaskName "ADS-Agent-$Id" -ErrorAction SilentlyContinue + if ($managed -and @($managed.Actions | Where-Object { $_.Execute -eq $exe -and $_.Arguments.Contains($configPath) }).Count) { + Stop-ScheduledTask -TaskName "ADS-Agent-$Id" -ErrorAction SilentlyContinue + Unregister-ScheduledTask -TaskName "ADS-Agent-$Id" -Confirm:$false + } + Get-CimInstance Win32_Process -Filter "Name='ads-agent.exe'" | Where-Object { $_.ExecutablePath -eq $exe } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue } + Start-Sleep -Seconds 1 + foreach ($file in @('agent.json','ads-agent.exe','adsctl.exe')) { + $saved=Join-Path $backup $file + if (Test-Path -LiteralPath $saved) { Copy-Item -LiteralPath $saved -Destination (Join-Path $root $file) -Force } + } + Enable-ScheduledTask -TaskName $LegacyTask | Out-Null + Start-ScheduledTask -TaskName $LegacyTask + throw $failure +} diff --git a/deploy/windows/run-agent.ps1 b/deploy/windows/run-agent.ps1 index d281493..617c52d 100644 --- a/deploy/windows/run-agent.ps1 +++ b/deploy/windows/run-agent.ps1 @@ -14,8 +14,8 @@ $logFile = Join-Path $logDirectory 'agent.log' if ((Test-Path -LiteralPath $logFile) -and (Get-Item -LiteralPath $logFile).Length -gt 10MB) { Move-Item -LiteralPath $logFile -Destination ($logFile + '.1') -Force } -$arguments = @('--server', [string]$config.server, '--device', [string]$config.device, '--target', [string]$config.target) -if ($config.connect_address) { $arguments += @('--connect-address', [string]$config.connect_address) } +$arguments = if ($config.id -and $config.devices -ne $null) { @('--quiet', '--supervise', '--config', (Join-Path $root 'agent.json')) } else { @('--server', [string]$config.server, '--device', [string]$config.device, '--target', [string]$config.target) } +if (-not $config.id -and $config.connect_address) { $arguments += @('--connect-address', [string]$config.connect_address) } # Keep the child in this task's process tree. Task Scheduler owns its lifecycle. # Tracing writes to stdout; stderr is also retained for startup diagnostics. $ErrorActionPreference = 'Continue' diff --git a/desktop/Ads.Desktop.Smoke/Program.cs b/desktop/Ads.Desktop.Smoke/Program.cs index 2f48cb0..f0376bd 100644 --- a/desktop/Ads.Desktop.Smoke/Program.cs +++ b/desktop/Ads.Desktop.Smoke/Program.cs @@ -24,7 +24,7 @@ internal static class Program Exception? failure = null; Dispatcher.UIThread.Post(async () => { - try { if (args.Contains("--multi")) await RunMultiAsync(args.Contains("--usb")); else await RunAsync(args.Contains("--live"), args.Contains("--screen"), args.Contains("--usb")); } + try { if (args.Contains("--agents")) await RunAgentsAsync(args.Contains("--local-agent")); else if (args.Contains("--multi")) await RunMultiAsync(args.Contains("--usb")); else await RunAsync(args.Contains("--live"), args.Contains("--screen"), args.Contains("--usb")); } catch (Exception error) { failure = error; } finally { finished.Cancel(); } }); @@ -48,6 +48,45 @@ internal static class Program } } + private static async Task RunAgentsAsync(bool local) + { + var root=DesktopPaths.ProjectRoot()!; + using var vm=new MainViewModel(Path.Combine(root,"work","agent-gui-smoke",Guid.NewGuid().ToString("N")),backgroundMonitoring:false,loadLocalCredentials:true); + var m=vm.AgentManager;m.AutoRefresh=false;m.LocalMode=local; + if(local)m.ConfigPath=Environment.GetEnvironmentVariable("ADS_TEST_AGENT_CONFIG")!; + var id=Environment.GetEnvironmentVariable("ADS_TEST_MANAGED_AGENT")!; + var window=new MainWindow{DataContext=vm};window.Show(); + window.FindControl("MainTabs")!.SelectedItem=window.FindControl("AgentManagementTab"); + async Task WaitState(Func predicate,string message){var deadline=DateTime.UtcNow.AddSeconds(50);while(!predicate()){await m.RefreshAgentsCommand.ExecuteAsync(null);m.SelectedAgent=m.Agents.FirstOrDefault(a=>a.Id==id);if(DateTime.UtcNow>deadline)throw new InvalidOperationException(message+": "+m.Notice);await Task.Delay(300);}} + try { + await WaitState(()=>m.SelectedAgent?.Id==id&&m.SelectedAgent.Online,"Agent not discovered"); + await m.LoadConfigCommand.ExecuteAsync(null); + using(var config=System.Text.Json.JsonDocument.Parse(m.ConfigJson))Require(config.RootElement.GetProperty("id").GetString()==id,"Wrong agent configuration"); + var node=System.Text.Json.Nodes.JsonNode.Parse(m.ConfigJson)!;node["name"]="GUI managed test";m.ConfigJson=node.ToJsonString();await m.SaveConfigCommand.ExecuteAsync(null); + await WaitState(()=>m.SelectedAgent?.Status.Name=="GUI managed test","Config mutation not applied"); + m.DeviceId=id+"-phone";m.Target="127.0.0.1:9";await m.AddDeviceCommand.ExecuteAsync(null); + await WaitState(()=>m.AgentDevices.Any(d=>d.Id==m.DeviceId),"Phone add not visible"); + m.SelectedManagedDevice=m.AgentDevices.Single(d=>d.Id==m.DeviceId);await m.DisableDeviceCommand.ExecuteAsync(null); + await WaitState(()=>m.AgentDevices.Any(d=>d.Id==m.DeviceId&&!d.Enabled),"Phone disable failed"); + await m.EnableDeviceCommand.ExecuteAsync(null);await WaitState(()=>m.AgentDevices.Any(d=>d.Id==m.DeviceId&&d.Enabled),"Phone enable failed"); + await m.ReadLogsCommand.ExecuteAsync(null);Require(m.RemoteLogText.Length>0,"Managed logs were empty"); + var view=window.GetVisualDescendants().OfType().Single(); + Require(view.FindControl("AgentConfigBox")?.Text==m.ConfigJson,"Agent config binding failed"); + using(var frame=window.CaptureRenderedFrame())frame?.Save(Path.Combine(root,"work",local?"agent-local.png":"agent-management.png")); + await m.RemoveDeviceCommand.ExecuteAsync(null);await WaitState(()=>m.AgentDevices.All(d=>d.Id!=m.DeviceId),"Phone removal failed"); + var pid=m.SelectedAgent!.Status.Pid;await m.RestartAgentCommand.ExecuteAsync(null); + await WaitState(()=>m.SelectedAgent?.Online==true&&m.SelectedAgent.Status.Pid>0&&m.SelectedAgent.Status.Pid!=pid,"Supervisor restart failed"); + Require(!m.ConfigJson.Contains(m.AdminToken)||m.AdminToken.Length==0,"Admin token leaked to config"); + Require(!m.RemoteLogText.Contains(m.AdminToken)||m.AdminToken.Length==0,"Admin token leaked to logs"); + var oldPath=m.ConfigPath;var oldServer=vm.Server; + if(local)m.ConfigPath+=".different";else vm.Server="wss://changed-center.invalid/ads"; + await m.LoadConfigCommand.ExecuteAsync(null); + Require(m.Notice.Contains("先刷新"),"A stale agent list allowed a request to a different source"); + m.ConfigPath=oldPath;vm.Server=oldServer; + Console.WriteLine("PASS: "+(local?"local IPC":"remote admin")+" GUI status, config, device add/enable/disable/remove, logs, supervised restart and bindings"); + } finally {await vm.ShutdownAsync();window.Close();} + } + private static async Task RunMultiAsync(bool usb) { var root = DesktopPaths.ProjectRoot()!; diff --git a/desktop/Ads.Desktop/AgentManagement.cs b/desktop/Ads.Desktop/AgentManagement.cs new file mode 100644 index 0000000..0c9d79f --- /dev/null +++ b/desktop/Ads.Desktop/AgentManagement.cs @@ -0,0 +1,54 @@ +using System.Net.Http.Headers; +using System.Net.Http.Json; +using System.Text.Json; +using System.Text.Json.Serialization; +namespace Ads.Desktop; +public sealed record ManagedDevice( + string Id, string? Target, [property:JsonPropertyName("adb_serial")] string? AdbSerial, + [property:JsonPropertyName("adb_server")] string? AdbServer, + [property:JsonPropertyName("device_port")] int DevicePort, bool Enabled, bool Connected, + [property:JsonPropertyName("last_error")] string? LastError) +{ + public string Description => $"{Id} · {(Enabled ? Connected ? LastError==null ? "已注册" : "转发异常" : "等待注册" : "已停用")} · {Target ?? AdbSerial}"; +} +public sealed record ManagedStatus(string Id, string Name, string Hostname, string Os, string Arch, string Version, int Pid, + [property:JsonPropertyName("uptime_seconds")] long UptimeSeconds, bool Supervised, long Revision, string Instance, ManagedDevice[] Devices); +public sealed record ManagedAgent(string Id, bool Online, [property:JsonPropertyName("last_seen")] long LastSeen, ManagedStatus Status) +{ + public string Description => $"{(string.IsNullOrWhiteSpace(Status.Name) ? Id : Status.Name)} · {(Online ? "在线" : "离线")} · {Status.Os} · {Status.Devices.Length} 台手机"; + public string Details => $"{Id} · {Status.Hostname} · {Status.Os}/{Status.Arch} · v{Status.Version} · PID {Status.Pid} · 配置版本 {Status.Revision}"; +} +public sealed record ManagementResponse(bool Ok, JsonElement? Data, string? Error) +{ + public JsonElement Value() => Ok && Data.HasValue ? Data.Value : throw new InvalidOperationException(Error ?? "Agent 管理请求失败。"); +} +public static class AgentManagementApi +{ + private static readonly JsonSerializerOptions Json = new(JsonSerializerDefaults.Web); + private static Uri UriFor(RelayOptions options, string endpoint) => new UriBuilder(options.DevicesUri) { + Path = options.BaseUri.AbsolutePath.TrimEnd('/') + endpoint + }.Uri; + public static async Task ListAsync(RelayOptions options, CancellationToken cancellation) + { + using var http = RelayApi.CreateClient(options); + using var request = new HttpRequestMessage(HttpMethod.Get, UriFor(options,"/v3/agents")); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer",options.Token); + using var response = await http.SendAsync(request,cancellation); Check(response); + return await response.Content.ReadFromJsonAsync(Json,cancellation) ?? []; + } + public static async Task CallAsync(RelayOptions options, string agent, object operation, CancellationToken cancellation) + { + using var http = RelayApi.CreateClient(options); http.Timeout=TimeSpan.FromSeconds(20); + var uri = new UriBuilder(UriFor(options,"/v3/rpc")) { Query="agent="+Uri.EscapeDataString(agent) }.Uri; + using var request = new HttpRequestMessage(HttpMethod.Post,uri) { Content=JsonContent.Create(operation) }; + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer",options.Token); + using var response = await http.SendAsync(request,cancellation); Check(response); + return (await response.Content.ReadFromJsonAsync(Json,cancellation) ?? throw new InvalidOperationException("管理响应为空。")).Value(); + } + private static void Check(HttpResponseMessage response) + { + if (response.StatusCode==System.Net.HttpStatusCode.Unauthorized) throw new InvalidOperationException("管理员凭据无效,需要 admin.env;手机连接令牌不具有管理权限。"); + if (response.StatusCode==System.Net.HttpStatusCode.ServiceUnavailable) throw new InvalidOperationException("控制中心尚未配置 ADS_ADMIN_TOKEN。"); + response.EnsureSuccessStatusCode(); + } +} diff --git a/desktop/Ads.Desktop/AgentManagementView.axaml b/desktop/Ads.Desktop/AgentManagementView.axaml new file mode 100644 index 0000000..27e20cb --- /dev/null +++ b/desktop/Ads.Desktop/AgentManagementView.axaml @@ -0,0 +1,33 @@ + + + + + + + + +