# ATX Agent 手机管理平台 - Phase 1 基于 [atx-agent](https://github.com/openatx/atx-agent) 方案的手机设备管理平台,纯 WebSocket 通信。 当前为 **Phase 1**:服务器 WS Hub、消息协议、设备注册/心跳、Shell 远程执行、设备信息查询、前端调试页、模拟设备脚本。 ## 目录结构 ``` server/ ├── main.py # FastAPI 入口:/ws/device(设备端)、/ws/ui(前端)、/(调试页) ├── core/ │ ├── protocol.py # 消息信封(id/type/action/deviceId/payload)、错误码 │ ├── hub.py # 连接池:注册、离线标记、心跳看门狗、前端会话广播 │ ├── device.py # 指令下发 + pending future + 超时管理(默认 30s) │ └── storage.py # SQLite:设备档案、上下线事件、指令审计日志 ├── scripts/ │ └── mock_device.py # 模拟 Android 设备(注册、心跳、shell.exec、device.info) ├── web/index.html # 前端调试页(设备列表、心跳、Shell 终端) ├── tests/test_phase1.py # 集成测试(真实 uvicorn 服务器 + 模拟设备) └── data/cp_agent.db # SQLite 数据库(运行时生成) ``` ## 快速开始 ```bash cd server pip install -r requirements.txt # 终端 1:启动服务器(默认 0.0.0.0:8000) python main.py # 终端 2:启动模拟设备(可选,用于无真机联调) python scripts/mock_device.py --device-id mock-001 --device-name "Mock Phone" ``` 浏览器打开 http://127.0.0.1:8000 即可看到调试页:设备列表、心跳数据、Shell 终端、设备信息。 ## 文件与应用管理 页面中的“文件管理”只允许访问设备 `/sdcard/Download` 及其子目录,支持目录浏览、新建目录、上传、分块下载、重命名和删除。路径在服务端和 Android Bridge 两端校验,拒绝绝对路径、路径穿越和逃出 Download 的符号链接。 “应用管理”独立于设备管理页,支持查看可启动应用、启动、上传 APK 安装和卸载。APK 先以 64 KiB 分块上传到 `/sdcard/Download`,校验长度和 SHA-256 后再安装;CP Bridge 自身不能被卸载。单文件传输上限为 512 MiB,失败或超时的临时文件会清理。 ## 环境变量 | 变量 | 默认 | 说明 | |------|------|------| | `PORT` | 8000 | 监听端口 | | `HOST` | 0.0.0.0 | 监听地址 | | `DEVICE_TOKEN` | 空 | 设备注册认证 token;为空时开发模式放行 | | `DB_PATH` | server/data/cp_agent.db | SQLite 路径 | | `RELOAD` | 空 | 调试热重载 | ## WebSocket 协议 ### 设备端 `/ws/device` 首条消息必须为 register: ```json {"type":"register","deviceId":"xxx","deviceName":"Pixel 7","osVersion":"Android 14","sdkVersion":34,"token":"..."} ``` 之后上行: - `response`:指令回执(id 对应 command id,status: success/error,payload 含结果) - `event`:主动推送,如 `event.heartbeat` `{"battery":85,"cpu":12,"memory":45,"storage":60}` 下行:`command`(action 见下方指令表,payload 为对应参数) ### 前端 `/ws/ui` 上行请求(response 按相同 id 返回): - `device.list` → `{"devices":[{deviceId, deviceName, online, heartbeat, ...}]}` - `device.detail`(deviceId)→ 设备档案 + 最近心跳 - `shell.exec`(deviceId + `{"cmd":"ls","timeout":10}`)→ `{"exitCode":0,"output":"..."}` - `device.info`(deviceId)→ 设备端返回的信息 - `screen.capture`(deviceId)→ `{"format":"png","image":""}` - `app.list`(deviceId)→ `{"pkg":{"name":"label"},...}` - `app.launch`(deviceId + `{"package":"..."}`)→ `{"launched":true}` - `atx.*` 命名空间(deviceId + 各自参数)→ Bridge App 映射 atx-agent HTTP 接口,响应原样透传 ### `atx.*` 指令表(映射 atx-agent HTTP API) | action | 参数 | 映射 | |---|---|---| | `atx.version` | - | GET /version | | `atx.rotation` | - | GET /info/rotation | | `atx.wlanIp` | - | GET /wlan/ip | | `atx.procList` | - | GET /proc/list | | `atx.webviews` | - | GET /webviews | | `atx.packages` | - | GET /packages | | `atx.dumpHierarchy` | - | GET /dump/hierarchy | | `atx.pidof` | package | GET /pidof/{pkg} | | `atx.procMem` | package | GET /proc/{pkg}/meminfo | | `atx.procCpu` | package | GET /proc/{pkg}/cpuinfo | | `atx.webviewsOf` | package | GET /webviews/{pkg} | | `atx.packageInfo` | package | GET /packages/{pkg}/info | | `atx.packageIcon` | package | GET /packages/{pkg}/icon → `{"format":"jpeg","image":""}` | | `atx.shellBackground` | command | POST /shell/background | | `atx.sessionStart` | package, flags?, timeout? | POST /session/{pkg} | | `atx.service` | name, op=status/start/stop | GET/POST/DELETE /services/{name} | | `atx.screenRecordStart` | - | POST /screenrecord | | `atx.screenRecordStop` | - | PUT /screenrecord | | `atx.jsonrpc` | method, params? | POST /jsonrpc/0(uiautomator jsonrpc 透传) | | `atx.fileInfo` | path | GET /finfo/{path} | | `atx.fileDownload` | path | GET /raw/{path} → `{"path","size","base64"}` | | `atx.fileUpload` | path, base64, mode? | POST /upload/{path}(multipart) | | `atx.appInstall` | url | POST /packages(URL 安装,异步返回 id) | | `atx.appInstallStatus` | id | GET /packages/{id} | | `atx.appUninstall` | package | shell `pm uninstall` | 管理页面动作(仅由控制台使用): | action | 说明 | |---|---| | `file.list` / `file.mkdir` / `file.rename` / `file.delete` | 管理 `/sdcard/Download` 下的目录和文件 | | `file.upload.start` / `file.upload.chunk` / `file.upload.finish` | 64 KiB 分块上传,完成后校验 SHA-256 | | `file.download.start` / `file.download.chunk` / `file.download.finish` | 64 KiB 分块下载,完成后清理设备端暂存文件 | | `app.install` | 安装 Download 下的 APK | | `app.uninstall` | 卸载指定包名(禁止卸载 CP Bridge) | 不在表内(流式/WebSocket,Phase 2):minitouch、minicap、term、shell/stream。 下行推送:`device.list`(连接时全量)、`device.online` / `device.offline` / `event.heartbeat` 等事件。 ### 错误码 见 `core/protocol.py` `ErrorCode`:1001 指令格式错误、1002 执行超时、2001 atx-agent 未运行(含设备离线)、3001 文件不存在、3002 权限不足(含 token 校验失败)。 ## 运行测试 ```bash cd server python -m pytest tests/ -v ``` 测试会启动真实 uvicorn 服务器(随机端口),用模拟设备验证注册、心跳、Shell 往返、超时、断线离线标记等 9 个场景。 ## 后续 Phase(未实现) - Phase 2:投屏(minicap 二进制帧转发)、触控注入(minitouch)、Android Bridge App - Phase 3:文件管理、应用管理、UI 自动化 - Phase 4:认证权限、任务调度、告警 - Phase 5:稳定性与部署 scp "E:/private/cp_agent/cp-agent-server.tar.gz" root@8.138.167.65:/tmp/ # 服务器上: pkill -f "uvicorn main:app" cd /opt/server && tar -xzf /tmp/cp-agent-server.tar.gz nohup python3 -m uvicorn main:app --host 0.0.0.0 --port 60030 > /tmp/cp-agent.log 2>&1 &