Control a real USB-connected iPhone from Linux (no Mac, no paid developer account) and expose it to AI agents as MCP tools
Rust ai-agentautomationclaudeclaude-codedockeriosiphonelinuxmcpmcp-servermodel-context-protocolrustwebdriveragent
docs: a guide to the viewer and remote use; bring the rest up to date (#84)
Qinghe Li6f6aaa5c3ef4
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex 50cf2f8..c4280fe 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -49,6 +49,16 @@ export MCP_CMD="$PWD/target/release/iphone-control mcp" # your build; the daem`run.py` prints every result with its wall time, ends with a line counting calls and failures, saves screenshots to `./shots` and exits with 1 if any call failed. `MCP_CMD` defaults to `iphone-control mcp` on your `PATH`. To test your branch, run its daemon too (stop the installed service with `iphone-control service stop`, then start `target/release/iphone-control daemon` in another terminal; `service start` afterwards). What a pull request has to run on the device is in [Required tests](#required-tests).+AI tasks (anything in `crates/ictl-daemon/src/tasks.rs`, `task`, the viewer's task API, the MCP server's task handling) have their own scenarios in `tests/device/tasks.py`, each check a PASS / FAIL line:++```bash+export ICTL_BIN="$PWD/target/release/iphone-control" # your build (the daemon too)+.venv/bin/python tests/device/tasks.py # every scenario but restart+.venv/bin/python tests/device/tasks.py pause confirm # only these+```++With `MCP_URL` (the viewer's `/mcp`) the agents connect over HTTP instead of stdio, and `ICTL_VIEWER_TOKEN` is the viewer's login token (`iphone-control viewer token`); `ICTL_VIEWER` (the viewer's address) adds the checks through its API, and the `remote` scenario, which plays a platform on another machine, needs all three. `MCP_CA` trusts the viewer's self-signed certificate for an `https://` address. The scenarios are harmless (Home, Settings, Clock, screenshots) and stop any task they leave behind.+On macOS, the full checklist (setup, `debug` checks, coexistence with Finder, reconnect) is in [docs/dev/macos-verification.md](docs/dev/macos-verification.md).Notes:diff --git a/README.md b/README.mdindex b31f9a3..c0223f2 100644--- a/README.md+++ b/README.md@@ -98,7 +98,7 @@ iphone-controlThe first time, it walks you through setup in the terminal, one step at a time: language, proxy, the background service, pairing, Developer Mode, Apple ID, signing and installing WebDriverAgent, the optional auto-unlock and notifications, and registering with Claude Code and Codex. Do what it asks on the phone. Steps already done are skipped; afterwards, `iphone-control` just shows the status.-Then open a new Claude Code or Codex session and ask, for example, *“Open Clock, switch to the stopwatch and start it.”* Or run `iphone-control viewer` for the control page in the browser (http://127.0.0.1:8200), where you can act on the live screen and hand tasks to a local Claude Code or Codex.+Then open a new Claude Code or Codex session and ask, for example, *“Open Clock, switch to the stopwatch and start it.”* Or run `iphone-control viewer` for the control page in the browser (http://127.0.0.1:8200; log in with the link `iphone-control viewer token` prints), where you can act on the live screen and hand tasks to a local Claude Code or Codex; it works from the local network and other machines too, see [Viewer and Remote Use](https://derrors.github.io/iphone-control/en/guide/remote).More: [step-by-step guide](https://derrors.github.io/iphone-control/en/guide/getting-started) · [build from source](https://derrors.github.io/iphone-control/en/deploy/source) · [macOS guide](https://derrors.github.io/iphone-control/en/deploy/macos) · [other MCP clients](https://derrors.github.io/iphone-control/en/guide/mcp-clients).@@ -146,7 +146,8 @@ Tool texts, results, CLI output and logs are in Simplified Chinese by default; s| `notify-test [event]` | Send test notifications || `mcp-config` | Print the command that registers the MCP server || `wda-bridge [--port]` | Expose WebDriverAgent on 127.0.0.1 for other tools; stop it when done |-| `viewer [--port]` | Control page on 127.0.0.1:8200: live screen you can tap, swipe and type on, device panel, and AI tasks run by a local Claude Code or Codex; stop it when done |+| `viewer [--port] [--lan]` | Control page on 127.0.0.1:8200, behind a login (`viewer token` shows the token and a link; read-only tokens too): live screen you can tap, swipe and type on, device panel, and AI tasks run by a local Claude Code or Codex. `--lan` opens it to the local network (HTTPS), and MCP over HTTP is on the same port (`/mcp`) |+| `task run [--url <viewer>] -- <command>` | AI tasks for other agent platforms; with `--url` the phone is on another machine. `task list`, `pause`, `approve`… follow and control tasks |All commands and options: [CLI reference](https://derrors.github.io/iphone-control/en/guide/cli).diff --git a/README.zh-CN.md b/README.zh-CN.mdindex 56faba9..a970146 100644--- a/README.zh-CN.md+++ b/README.zh-CN.md@@ -100,7 +100,7 @@ iphone-control第一次运行时,它在终端里一步步带你完成设置:语言、代理、后台服务、配对、开发者模式、Apple ID、签名并安装 WebDriverAgent,可选的自动解锁和通知,最后注册到 Claude Code 和 Codex。按提示在手机上操作即可。做过的步骤会跳过;设置好以后再运行 `iphone-control` 只显示状态。-然后打开一个新的 Claude Code 或 Codex 会话,试着说:「打开时钟,切到秒表并启动」。也可以运行 `iphone-control viewer` 打开浏览器里的控制台(http://127.0.0.1:8200),在实时画面上直接操作,或把任务交给本机的 Claude Code 或 Codex。+然后打开一个新的 Claude Code 或 Codex 会话,试着说:「打开时钟,切到秒表并启动」。也可以运行 `iphone-control viewer` 打开浏览器里的控制台(http://127.0.0.1:8200,用 `iphone-control viewer token` 打印的链接登录),在实时画面上直接操作,或把任务交给本机的 Claude Code 或 Codex;局域网和别的机器也能用,见[控制台与远程使用](https://derrors.github.io/iphone-control/guide/remote)。更多:[分步指南](https://derrors.github.io/iphone-control/guide/getting-started) · [从源码编译](https://derrors.github.io/iphone-control/deploy/source) · [macOS 指南](https://derrors.github.io/iphone-control/deploy/macos) · [其他 MCP 客户端](https://derrors.github.io/iphone-control/guide/mcp-clients)@@ -148,7 +148,8 @@ iphone-control| `notify-test [事件]` | 发送测试通知 || `mcp-config` | 打印注册 MCP 的命令 || `wda-bridge [--port]` | 把 WebDriverAgent 桥接到 127.0.0.1 给别的工具用,用完就停 |-| `viewer [--port]` | 控制台页面,地址 127.0.0.1:8200:实时画面可直接点击、滑动、输入,设备面板,还能把任务交给本机的 Claude Code 或 Codex 执行;用完就停 |+| `viewer [--port] [--lan]` | 控制台页面,地址 127.0.0.1:8200,要登录(`viewer token` 显示令牌和登录链接,可以加只读令牌):实时画面可直接点击、滑动、输入,设备面板,还能把任务交给本机的 Claude Code 或 Codex 执行。`--lan` 对局域网开放(HTTPS),同一个端口上还有 MCP over HTTP(`/mcp`) |+| `task run [--url <控制台>] -- <命令>` | 给别的 agent 平台用的 AI 任务;`--url` 时手机在另一台机器上。`task list`、`pause`、`approve` 等查看和控制任务 |全部命令和参数见[命令行](https://derrors.github.io/iphone-control/guide/cli)。diff --git a/SECURITY.md b/SECURITY.mdindex 4c8ca4e..24497c4 100644--- a/SECURITY.md+++ b/SECURITY.md@@ -14,7 +14,7 @@ iphone-control is, by design, a remote control for a phone. Anyone who can talk|---|---|---|| Phone control (taps, typing, screenshots, apps) | Control socket: `$XDG_RUNTIME_DIR/iphone-control/ctl.sock` on Linux, `~/.local/share/iphone-control/ctl.sock` on macOS; mode 0600 | The user the daemon runs as, and root || WebDriverAgent (no authentication) | On the phone, reached through usbmuxd; nothing listens on the network unless you run `wda-bridge` | With `wda-bridge`: **every local user** on the host, since the port is on 127.0.0.1. Browsers are blocked by a Host-header check (DNS rebinding), other local processes are not |-| Live viewer (`iphone-control viewer`, opt-in) | A page on `127.0.0.1` (port 8200 by default); nothing listens unless you run it. Follows the device the daemon controls. Its AI tasks run Claude Code or Codex as the user who started the viewer, and their MCP servers report to it over a Unix socket next to the control socket (`viewer-<port>.sock`, mode 0600, a secret token per task) | **Every local user** on the host can watch the screen, control the phone and start AI tasks (which use that user's agent login) through the port: it has no login. Other web pages in a browser are blocked by the `Host`, `Origin`, JSON and `Sec-Fetch-Site` checks, other local processes are not |+| Live viewer (`iphone-control viewer`, opt-in; also as a service) | A page on `127.0.0.1` (port 8200 by default), with `--lan` also on the local network over HTTPS (connections only from loopback, private, link-local and 100.64/10 addresses); MCP over HTTP on the same port (`/mcp`); nothing listens unless you run it. Its AI tasks run Claude Code or Codex as the user who started the viewer. Login tokens in `viewer-tokens.json` in the data directory (0600); the self-signed HTTPS key in `viewer-tls/` (key 0600) | Whoever has a token: every HTTP request needs one, from `127.0.0.1` too (a reverse proxy on the host makes visitors look local). An operating token can watch the screen, control the phone and start AI tasks (which use that user's agent login); a read-only token can only watch. Wrong tokens are rate-limited (20 a minute for the whole viewer). Other web pages in a browser are blocked by the `Host`, `Origin`, JSON and `Sec-Fetch-Site` checks. An agent working for a task gets only the task's token: it acts for that task and can't pause, resume or confirm it || Apple ID session tokens, development signing key and certificate | `account/` in the data directory (0700, key 0600) | Whoever can read the data directory. The Apple ID password itself is never stored || Pairing records | The system's `/var/lib/lockdown` (Linux, kept by usbmuxd) or `/var/db/lockdown` (macOS, shared with Finder) | Root on the host. A pairing record lets its holder talk to the phone over USB without another *Trust* prompt || Lock-screen passcodes | `secrets/passcodes/<UDID>` in the data directory (0600), one file per device, **plain text**, only if you ran `passcode set`. A `secrets/passcode` file from an older version is moved to the device's file when exactly one device is connected, and stays where it is until then | Whoever can read the data directory |@@ -24,7 +24,7 @@ The data directory is `~/.local/share/iphone-control`. Everything runs as your r## Design choices that limit exposure-- No network listener by default. WebDriverAgent is reached through usbmuxd. `wda-bridge` and `viewer` are opt-in and bind 127.0.0.1 only, and both are meant to be stopped after use. `wda-bridge` only forwards requests whose Host header is a loopback name. `viewer` checks the Host header the same way; its actions also need an `Origin` of the viewer page itself and a JSON body, so other web pages can't act through it, reads a browser marks as coming from another site (`Sec-Fetch-Site`) are refused, and each action carries the UDID of the device the page follows, so the daemon refuses it once another device is controlled.+- No network listener by default. WebDriverAgent is reached through usbmuxd. `wda-bridge` and `viewer` are opt-in and bind 127.0.0.1 only (`viewer --lan` also listens on the local network, over HTTPS); `wda-bridge` is meant to be stopped after use. `wda-bridge` only forwards requests whose Host header is a loopback name. `viewer` requires a login token for every HTTP request and checks the Host header the same way (its own names and addresses with `--lan`); its actions also need an `Origin` of the viewer page itself and a JSON body, so other web pages can't act through it, reads a browser marks as coming from another site (`Sec-Fetch-Site`) are refused, and each action carries the UDID of the device the page follows, so the daemon refuses it once another device is controlled.- The control socket is a Unix socket with mode 0600, not TCP.- The tunnel to the phone uses a user-space TCP stack, so no network interface or routing changes on the host, and the daemon needs no root.- Auto-unlock is off by default. When enabled, it stops for good after a single wrong attempt (it writes a stop marker, `logs/UNLOCK-DISABLED`, before entering the second digit) and sends a notification; it never retries a passcode that might be wrong. The marker is global: a failure on one device stops auto-unlock on all of them until you run `unlock-reset`. It does not work before the first unlock after a reboot (BFU) — that always needs a person.diff --git a/docs/architecture.md b/docs/architecture.mdindex 306d3a6..3f49a13 100644--- a/docs/architecture.md+++ b/docs/architecture.md@@ -44,7 +44,7 @@- MCP 进程由 AI 客户端直接调用二进制拉起,短命且可能同时有多个;设备连接(尤其是 HID 需要的显示媒体流、WDA 的 XCTest 会话)只能有一个持有者。- 不开 TCP 端口,避免重蹈 `ios forward` 监听所有网卡的覆辙。WDA 本身也通过 idevice 的 provider 直连设备端口,**本机不再监听 8100**。如需给外部工具用,`iphone-control wda-bridge` 显式开启,只绑 127.0.0.1,且只转发 Host 头是 `localhost`/`127.0.0.1`/`[::1]` 的请求(挡住 DNS rebinding)。注意**本机的任何用户都能连上 bridge**,而 WDA 没有认证,用完就停。-- `iphone-control viewer` 同样显式开启、只绑 127.0.0.1、检查 Host 头:页面看 WDA 的 MJPEG 流(手机上的 9100 端口,经 usbmuxd,只读);点击、滑动、输入、按键是 POST,还要检查 `Origin` 是页面本身、内容类型是 JSON,然后作为普通请求发给守护进程,和 agent 的调用排队、自动解锁。它跟着守护进程控制的设备:在本页、别的页面、`iphone-control select`、MCP 工具里切换,或拔掉后自动换了一台,`/api/info` 的 `udid` 都会变,页面最迟在下一次 10 秒轮询时发现,把画面(`/stream?udid=`)和各面板换过去。每个操作带上页面显示的那台的 UDID(`X-Ictl-Device`),守护进程控制的不是这台就拒绝,不会发到用户没在看的手机上。还没装 WDA 的设备,登录了 Apple ID 时页面上可以直接签名安装(`/api/sign`,即 `Request::Resign { force: true }`,时限 16 分钟)。AI 任务执行中不能切换设备,设了 `IPHONE_UDID` 时也不能。+- `iphone-control viewer` 同样显式开启、默认只绑 127.0.0.1、检查 Host 头;每个 HTTP 请求都要登录令牌(本机也一样),`--lan` 时也对局域网开放、走 HTTPS,同一个端口上还有 `/mcp`(MCP over HTTP)和给外部平台开任务的接口(见 [task-control.md](task-control.md) 第 7 节、[D3](decisions.md)):页面看 WDA 的 MJPEG 流(手机上的 9100 端口,经 usbmuxd,只读);点击、滑动、输入、按键是 POST,还要检查 `Origin` 是页面本身、内容类型是 JSON,然后作为普通请求发给守护进程,和 agent 的调用排队、自动解锁。它跟着守护进程控制的设备:在本页、别的页面、`iphone-control select`、MCP 工具里切换,或拔掉后自动换了一台,`/api/info` 的 `udid` 都会变,页面最迟在下一次 10 秒轮询时发现,把画面(`/stream?udid=`)和各面板换过去。每个操作带上页面显示的那台的 UDID(`X-Ictl-Device`),守护进程控制的不是这台就拒绝,不会发到用户没在看的手机上。还没装 WDA 的设备,登录了 Apple ID 时页面上可以直接签名安装(`/api/sign`,即 `Request::Resign { force: true }`,时限 16 分钟)。AI 任务执行中不能切换设备,设了 `IPHONE_UDID` 时也不能。- viewer 的画面像触摸屏:iOS 27 起按下、移动、松开各发一个 `Request::Touch`(`prepare`/`down`/`move`/`up`),守护进程在已开的 HID 会话上发一个 touchscreen 报告(约 2 毫秒);解锁、核对方向只在 `down` 时做一次。页面一次只发一个请求,没发出去的移动只留最新的一个,按住不动时每 100 毫秒再报一次位置。滚轮、触控板换成同样的手指拖动。iOS 27 以下松开后发整段手势(WDA)。画面的 MJPEG 流在 `WdaClient::fast_stream` 下最多每秒 60 帧、原尺寸、不转正:缩放或转正时 WDA 要逐帧解码、处理、重新编码,CPU 超过 iOS 的上限(60 秒内平均 80%)会被杀掉;这样设置时它直接转发 testmanagerd 用硬件编好的 JPEG。iPhone 横屏时这些帧仍是竖的:JPEG 带 EXIF 方向,浏览器显示 MJPEG 的 img 时自己转正;本机编码的 H.264 不带方向,页面按界面方向(`/api/info` 的 `display.rotation`)在 canvas 上转正,和手机自己的视频一样。页面的文字中英两套,请求带 `X-Ictl-Lang`,viewer 在这种语言下处理,AI 任务记下开始时的语言。- viewer 的画面编码(`video/`):macOS 上和装了 ffmpeg 时页面拉 `/video`,是 H.264。每个连接读手机的 MJPEG,丢掉和上一帧相同的(静止时 WDA 每秒重复约 25 次),交给一个 ffmpeg 子进程(`mpjpeg` 进,FLV 出;FLV 每帧带长度,收完最后一个字节就能发,裸 H.264 要等下一帧开头才知道这一帧结束),再按「1 字节类型 + 4 字节长度 + 内容」发给页面,页面用 WebCodecs 解码画到 canvas。发出去的解码配置先改写 SPS,写明不重排帧(`video/sps.rs`):没写的话 Chrome 的硬件解码器按级别的最坏情况扣着几帧不出(990×2151 时 4 帧),画面滞后,静止后停在动作中间。macOS 上先用本程序直接调的 VideoToolbox(`video/vt.rs`,每个连接一个线程:VideoToolbox 的 JPEG 解码器把 JPEG 解成 NV12,奇数尺寸顺带拉成偶数,直接交给 H.264 编码器;不重排、不扣帧,每帧编完才取下一帧,不用补帧;输出本来就是 avcC 格式,avcC 取自格式描述;qp 用 `MinAllowedFrameQP`/`MaxAllowedFrameQP` 定住,macOS 12 以前的系统按 `Quality` 近似;macOS 11 以后才有的键运行时用 `dlsym` 找,免得在旧系统上起不来),不需要 ffmpeg。其余编码器在 `viewer` 启动时按顺序试(NVENC、各渲染节点上的 VAAPI(先显卡解 JPEG,再 CPU 解)、QSV、V4L2 M2M、ffmpeg 的 VideoToolbox(原生的备用)、x264),处理真实画面失败就换下一个。ffmpeg 会扣住最新的一帧等下一帧来(AMD 780M 的 VAAPI 是 1 帧),画面静止时最后一次变化就出不来,所以每帧后面紧跟着把它再写 K 遍,K 在探测时测出(写入的帧数减去出来的帧数),这几份副本各编成约 100 字节。流量控制:系统和 SSH 隧道的缓冲能吞下几 MB,光看队列满不满判断不了页面跟不跟得上(每秒读 30 KB 的客户端曾落后 40 秒),所以页面每 100 毫秒 `POST /api/video/ack` 报告收到多少字节;最早一个没确认的消息比平常的往返时间晚 300 毫秒以上,或者在路上的字节超过「链路速度 ×(往返时间 + 300 毫秒)」时,新画面先不交给编码器,追上后只交最新一张。编好的帧一个不丢,帧链完整,不需要关键帧。没有 ffmpeg 或浏览器不支持 WebCodecs 时用 `/stream`:同样丢掉重复帧、只发最新一张,每帧后面紧跟下一部分的分隔线和头部(Chrome 要看到这些才显示上一帧)。- 手机自己的视频(iOS 27 及以上,页面优先用它):CoreDevice 屏幕媒体流本来就由 HID 会话持有(HID 事件要它在才生效)。`ictl-device` 的 `media.rs` 不再丢掉 RTP:用上游的 `HevcDepacketizer` 还原成 Annex-B,按 RTP marker 组成一帧,记下 VPS/SPS/PPS 补到手机按请求发来、不带参数集的关键帧前面,经 broadcast 发给订阅者(`ScreenFeed`);并照 Device Hub 回传 RTCP(每帧一个帧确认、每 50 毫秒一个 RCTL、每秒一个接收报告),按需发 PLI+FIR 要关键帧(最多每 300 毫秒一次)。守护进程的 `screen.rs` 处理流式请求 `ScreenStream`:先回一行 JSON,之后在同一连接上发「类型 + 长度 + 内容」的消息(0 解码配置、1 关键帧、2 其他帧、3 界面方向),客户端写 `K` 要关键帧;还没收到关键帧时每 250 毫秒再要一次(刚开流时要的那次手机可能不理);界面方向每秒问一次 WDA。有页面在看时(`screen_viewers`),HID 会话不因空闲关闭。viewer 的 `video/device.rs` 转发到 `/video?src=device`,流量控制沿用页面的回报:落后时丢掉非关键帧、再要一个关键帧。页面不带 description 配置 HEVC 解码器(Annex-B),按 `/api/info` 的屏幕尺寸裁掉宽高补到 16 倍数的边(1320×2868 编成 1328×2896),按方向转正;`isConfigSupported` 不支持或从没出过画面就退回 H.264,再退回 MJPEG。diff --git a/docs/decisions.md b/docs/decisions.mdindex 09e9a9c..9b85523 100644--- a/docs/decisions.md+++ b/docs/decisions.md@@ -21,6 +21,7 @@- **理由**:WDA 没有认证;toolkit 早期用 `ios forward` 监听了所有网卡,局域网内任何人都能控制手机。- **补充**:`iphone-control viewer` 用同样的保护模型(显式开启、只绑 127.0.0.1、检查 `Host`),另加操作的 `Origin` 和 JSON 内容类型检查、读接口的 `Sec-Fetch-Site` 检查,并只作用于页面跟着的那台设备。它的 AI 任务不开新端口:agent 的 MCP 服务经控制接口旁边的 Unix socket(0600,每个任务一个口令)回报 viewer。- **补充(2026-11-13)**:`viewer --lan` 让控制台也对局域网开放:同一个端口,仍是 HTTP,**还没有登录**(用户决定先这样,登录以后再加,见 task-control.md 7.2)。没有登录时能做的防护都做了:显式开启、只接受回环和局域网地址(私有网段、链路本地、100.64/10)来的连接、`Host` 只认本机的名字和地址、操作的 `Origin` 必须是页面自己。代价是局域网里能连上这台机器的人都能操作手机、用本机的 Claude Code / Codex 开任务;从局域网地址打开时浏览器不提供 WebCodecs,画面退回 MJPEG。默认仍只绑 127.0.0.1。+- **补充(2026-11-13,后来)**:上一条的两个代价都去掉了。`--lan` 时同一个端口也讲 HTTPS(按连接的第一个字节分辨,默认自签证书、显示指纹,task-control.md 7.3);所有 HTTP 访问都要登录令牌,**本机也一样**,因为本机上的反向代理(frp、cloudflared 等)会让外来的访问看起来就是 `127.0.0.1`(7.5)。令牌有名字,可以只读(7.6)。默认仍只绑 127.0.0.1,局域网仍要显式开启。## D4:WDA 仍然是 Objective-C,在 macOS CI 上编译@@ -150,4 +151,5 @@- **对 D2、D13 的修改**:D2 里「MCP 是薄客户端」不变,但守护进程从此管任务这层语义,不再只是设备驱动。D13「socket 权限才是边界」不变:任务口令用来区分调用方、让出错的调用方停下来,不是权限控制。同一用户的进程能拿到口令,也能直接发控制请求。要真正隔离,需要让 Agent 换 uid 运行或进沙箱,并用 `SO_PEERCRED` 认调用方(task-control.md 阶段三、四)。- **代价**:守护进程多了一个模块和几种请求。有任务时,不带口令的动作会被拒绝:这正是要的效果,但以前「任务进行中用命令行点一下」的做法不能用了,要先接管。任务日志写在数据目录(`tasks/`,最多 50 个)。- **重新评估**:需要无人值守、Agent 跑在沙箱里时,再做口令分开和 `SO_PEERCRED`;需要跨主机时,再做带认证的 HTTP 控制面。+- **补充(2026-11-13,后来)**:这两条都已部分做了。同一台机器上不发控制口令,而是按口令和进程关系(`SO_PEERCRED`)拒绝 Agent 控制自己的任务(task-control.md 5.6)。跨主机时控制台就是 HTTP 控制面:平台用登录令牌开任务、控制任务,Agent 只拿任务口令,`/mcp` 认它,别的接口不认,所以跨主机时执行和控制是真正分开的(7.6、7.7)。- **补充(2026-11-13)**:任务范围先做了(task-control.md 5.5):开任务时限定 App,越界的操作停下来问人。和口令不同,它在同一个用户下也有用:防的是走错路的 Agent,不是有意绕过的程序。拒绝只拒这一步而不结束任务,是因为越界多半是 Agent 误点进了别的 App,让它退回来比整个任务作废更合用;看不出前台 App 时不操作,是和整个任务机制一样出错就停。diff --git a/docs/dev/regression-cases.md b/docs/dev/regression-cases.mdindex 42a5d45..a45aba3 100644--- a/docs/dev/regression-cases.md+++ b/docs/dev/regression-cases.md@@ -1,6 +1,6 @@# 必须覆盖的回归用例-#1~#34 来自 toolkit 和本项目早期实际踩过的坑,#35~#45 是多设备和 HID 横屏,#46~#58 是实时画面、开发者工具和英文界面,#59~#63 是 iPhone 18 Pro Max(iOS 27.2.1)真机全量测试发现的问题,#64 起是 viewer 的 AI 任务和新页面。能单测的写单测,不能单测的进真机回归清单。**每个 PR 都必须保持这里的全部用例通过**:单测的由 `cargo test` 覆盖;改到的范围里测试方式是真机、无头 Chrome 或手动测量的,要重跑并在 PR 里列出编号,写明设备和 iOS 版本;修了清单里没有的问题,要加一行。完整要求见 [CONTRIBUTING.md 的 Required tests](../../CONTRIBUTING.md#required-tests)。+#1~#34 来自 toolkit 和本项目早期实际踩过的坑,#35~#45 是多设备和 HID 横屏,#46~#58 是实时画面、开发者工具和英文界面,#59~#63 是 iPhone 18 Pro Max(iOS 27.2.1)真机全量测试发现的问题,#64 起是 viewer 的 AI 任务和新页面,#116~#122 是任务的控制权、控制台的局域网访问与登录、跨主机。能单测的写单测,不能单测的进真机回归清单。**每个 PR 都必须保持这里的全部用例通过**:单测的由 `cargo test` 覆盖;改到的范围里测试方式是真机、无头 Chrome 或手动测量的,要重跑并在 PR 里列出编号,写明设备和 iOS 版本;修了清单里没有的问题,要加一行。完整要求见 [CONTRIBUTING.md 的 Required tests](../../CONTRIBUTING.md#required-tests)。| # | 坑 | 正确行为 | 测试方式 ||---|---|---|---|@@ -119,3 +119,10 @@| 113 | 在控制台画面上拖动、滑动时,触控板双指一碰就变成了缩放画面(双指捏合在浏览器里是按住 Ctrl 的滚轮) | 画面只用上方的加减号缩放;画面区域里的 Ctrl/⌘ + 滚轮、Safari 的 gesture 事件都拦下,既不缩放画面也不缩放整页;触屏上画面周围不允许双指缩放(`touch-action: pan-x pan-y`) | 页面测试(`tests/viewer/zoom.test.mjs`)+ 真机(在画面上双指捏合,缩放比例不变) || 114 | 设置菜单不能用方向键,长按方向键时重画期间会打开回显,取消后可能留下终端输入模式 | 菜单从第一帧到退出都关闭输入回显;方向键和编号选择一致,Esc/Ctrl+C 恢复原模式;基础终端保留逐行输入 | 菜单状态单测 + PTY 回归(含长按 ↓、输入输出在不同终端、取消和外部信号) || 115 | 生成 Shell 补全触发配置读取或设备维护锁,脚本与 CLI 定义不同步 | Bash/Zsh/Fish 从实际命令树生成,生成路径不读取设置、不连接设备、不申请维护锁 | 补全单测、CLI 子进程回归、Bash/Fish 候选断言及 Zsh 交互验证 |+| 116 | AI 任务的 Agent 用自己的 Bash 跑 `iphone-control task approve <id>`,就能批准它本该问人的越界确认和 `iphone_confirm`,或在用户暂停后自己继续 | 带任务口令、或来自开任务进程之下(Agent 及其子进程)的控制请求一律拒绝,停止除外;控制台接口在 Linux 上按调用进程同样拒绝 | 单测 `the_agent_cant_control_its_own_task`;真机 `tasks.py self` |+| 117 | `task run` 下的命令自己 `task finish` 后正常退出,却被立刻发 SIGTERM,`task run` 退出码变成 1,平台当成失败 | 任务是命令自己结束的(completed/failed/rejected)就给它 5 秒退出,退出码用它自己的;在别处停止的照旧马上停 | 单测 `a_command_that_finished_its_task_is_given_time_to_exit`、`the_exit_code_is_the_command_s_or_the_task_s_result`;真机 `tasks.py run` |+| 118 | `IPHONE_CONTROL_VIEWER_LAN=1`(文档写的值)让控制台启动就退出:`invalid value '1' for '--lan'` | 布尔设置接受 1/0、yes/no、on/off、true/false | 单测 `viewer_settings_from_the_environment` |+| 119 | 从局域网地址(HTTP)打开控制台一直停在「正在连接」:不是安全上下文,没有 `crypto.randomUUID` | 页面不用只在安全上下文里才有的接口生成画面编号(`getRandomValues`) | 页面测试 `lan.test.mjs` |+| 120 | 带请求体的请求被提前拒绝(没登录 401、403、413)时,控制台没读请求体就关连接,TCP 发 RST,客户端收不到回复(`connection reset by peer`,复用连接的程序约三次一次) | 回复后先关写方向,读完对方剩下的数据(最多 1 秒、1 MB)再关 | 单测 `every_request_needs_the_login`(256 KB 请求体仍收到 401);真机 `tasks.py remote` 连跑 8 次 |+| 121 | 任务事件流(SSE)的一块数据断在中文字符中间时,先整体转成文字会把这个字变成乱码 | 在字节里找事件边界,只转换完整的事件 | 单测 `remote::reads_server_sent_events` |+| 122 | 升级时运行新程序的 `--version` 偶尔失败:`Text file busy`(同一进程的其他线程 fork 时,新程序还以写方式打开) | 遇到 `ETXTBSY` 等 50 毫秒重试,最多 20 次 | 单测 `the_candidate_runs_once_it_is_no_longer_open_for_writing`(修复前稳定复现) |diff --git a/docs/task-control.md b/docs/task-control.mdindex 216bdc9..a67d285 100644--- a/docs/task-control.md+++ b/docs/task-control.md@@ -214,12 +214,12 @@ Running → Waiting(等确认)→ Running和第 2 节的目标架构相比:-- **口令只有一种**(2.5):开任务的一方拿到的口令同时交给 Agent,没有单独的控制口令;改为按口令和进程拒绝执行方的控制请求(5.6)。等 Agent 跑进沙箱、换了 uid,或者有了跨主机的控制面时,再发真正的控制口令。+- **口令只有一种**(2.5):同一台机器上,开任务的一方拿到的口令同时交给 Agent,没有单独的控制口令;改为按口令和进程拒绝执行方的控制请求(5.6)。跨主机时登录令牌就是控制口令,Agent 只拿任务口令(7.6)。要在同一台机器上真正隔离,仍要 Agent 换 uid 或进沙箱。- **任务范围已有,策略还没有**(2.6):限定 App 后越界会停下来问人(5.5);但范围内的敏感动作(在微信里发消息)仍然靠 Agent 调 `iphone_confirm`。控制台上开任务时也能设范围(Agent 旁边的 App 按钮,2026-11-13)。- **观察版本是简化版**(2.7):只有一个「需要重新看」的标记,在交还、或别人在接管期间动过手机后置上,Agent 截图、读元素或等文字之后清掉。动作不带「基于哪次观察」,所以 Agent 自己连续操作时不做这项检查。- **事件不带编号**(2.10):重连时从头重放,没有从断点继续。任务事件全是文字,从头重放代价不大。- **设备拔掉时**(2.9):任务不中断,它的请求会被拒绝(找不到绑定的设备),插回同一台手机后可以继续;只有换成另一台设备才中断。-- **单设备**(D11)、**只在本机**(没有 HTTP 控制面):按原计划留给阶段四。+- **单设备**(D11):仍是。**只在本机**已经不是:控制台就是带登录的 HTTP 控制面,局域网和别的机器都能开任务、控制任务、用 MCP(第 7 节)。## 7. 阶段四的需求diff --git a/website/.vitepress/config.mts b/website/.vitepress/config.mtsindex 08a20d6..b3760f2 100644--- a/website/.vitepress/config.mts+++ b/website/.vitepress/config.mts@@ -35,6 +35,7 @@ export default defineConfig({{text: '使用',items: [+ { text: '控制台与远程使用', link: '/guide/remote' },{ text: '配置', link: '/guide/configuration' },{ text: '命令行', link: '/guide/cli' },{ text: '多台设备', link: '/guide/multi-device' },@@ -99,6 +100,7 @@ export default defineConfig({{text: 'Usage',items: [+ { text: 'Viewer and Remote Use', link: '/en/guide/remote' },{ text: 'Configuration', link: '/en/guide/configuration' },{ text: 'CLI', link: '/en/guide/cli' },{ text: 'Several Devices', link: '/en/guide/multi-device' },diff --git a/website/en/guide/cli.md b/website/en/guide/cli.mdindex 685940a..3e4c224 100644--- a/website/en/guide/cli.md+++ b/website/en/guide/cli.md@@ -130,7 +130,7 @@ See [Auto-unlock](./auto-unlock). `--device` takes a UDID or name, and the devic|---|---|| `notify-test [event]` | Send test notifications: one per event type, or one type (`resign`, `session`, `pin`, `pin-recovered`, `unlock`, `offline`, `online`) || `wda-bridge [--port <port>]` | Expose WDA on `127.0.0.1` (default port 8100) for other tools. WDA has no authentication and every local user can reach it, so stop it when done |-| `viewer [--port <port>] [--lan] [--lan-host <name>…] [--tls auto\|on\|off] [--tls-cert <PEM> --tls-key <PEM>] [--mcp on\|off]` | Control page in the browser, **behind a login**: every HTTP visit (from this machine too; one forwarded by frp or another reverse proxy looks just like it) needs the token, and `viewer token` shows it with links that log in; a browser stays logged in for 30 days, programs and MCP clients send `Authorization: Bearer <token>`. (and MCP over HTTP at `/mcp` on the same port, see [Connect an AI Client](./mcp-clients#connect-by-url-http); `--mcp off` turns it off) at `http://127.0.0.1:8200/` (default port); with `--lan` also at `https://<this machine's address>:8200/` on the local network (same port; `http://` from the network is sent to `https://`). The certificate is a self-signed one made on this machine (kept in `viewer-tls/` in the data directory and reused), so the first time the browser warns that it isn't trusted: check that the SHA-256 fingerprint matches the one printed at startup or by `service status`, then go on. With a certificate of your own (`--tls-cert`/`--tls-key`: mkcert, `tailscale cert`…) there is no warning. With `--tls off` the network gets plain HTTP and the picture falls back to MJPEG (browsers only offer WebCodecs on localhost or HTTPS), in English or Chinese (follows `IPHONE_CONTROL_LANG`, switchable on the page). **Screen:** the live picture works like a touchscreen: click to tap, drag to swipe, hold to long-press, scroll with the mouse wheel or trackpad, plus a text field and the hardware buttons. On iOS 27 and later the finger follows the pointer as it moves (over CoreDevice HID, which keeps the phone awake until 30 seconds after the last touch); older versions get the whole gesture on release. Everything goes through the daemon like an MCP call, so it queues with the agent and unlocks the phone first. On iOS 27 and later the picture is the phone's own video; otherwise it comes from WDA (none until WDA is ready), as H.264 or MJPEG; see [Picture](#viewer-picture) below. Touch is available when the frame matches the measured size and orientation. After rotating or switching devices, start a new gesture on the updated picture. Settings at the bottom of the sidebar controls the theme and text size. The picture has its own zoom controls, the − and + above it (pinching doesn't zoom, so a drag can't turn into one). **Device panel:** state and readiness checks, installed apps, on-screen elements. The page follows the device the daemon controls: switch on the page or anywhere else, and the picture and panels move over; see [Several Devices](./multi-device#control-page). **AI tasks:** type a task and a local agent carries it out (Claude Code or Codex, whichever is on `PATH`; picked on the page). Before it starts you can limit it to some apps with the App button next to the agent; in any other app each step waits for you. You can pause, take over and hand back, or stop it, and it asks on the page before paying, deleting or sending messages. The token is the key: keep it to yourself |+| `viewer [--port <port>] [--lan] [--lan-host <name>…] [--tls auto\|on\|off] [--tls-cert <PEM> --tls-key <PEM>] [--mcp on\|off]` | Control page in the browser, **behind a login**: every HTTP visit (from this machine too; one forwarded by frp or another reverse proxy looks just like it) needs the token, and `viewer token` shows it with links that log in; a browser stays logged in for 30 days, programs and MCP clients send `Authorization: Bearer <token>`. (and MCP over HTTP at `/mcp` on the same port, see [Connect an AI Client](./mcp-clients#connect-by-url-http); `--mcp off` turns it off) at `http://127.0.0.1:8200/` (default port); with `--lan` also at `https://<this machine's address>:8200/` on the local network (same port; `http://` from the network is sent to `https://`). The certificate is a self-signed one made on this machine (kept in `viewer-tls/` in the data directory and reused), so the first time the browser warns that it isn't trusted: check that the SHA-256 fingerprint matches the one printed at startup or by `service status`, then go on. With a certificate of your own (`--tls-cert`/`--tls-key`: mkcert, `tailscale cert`…) there is no warning. With `--tls off` the network gets plain HTTP and the picture falls back to MJPEG (browsers only offer WebCodecs on localhost or HTTPS), in English or Chinese (follows `IPHONE_CONTROL_LANG`, switchable on the page). **Screen:** the live picture works like a touchscreen: click to tap, drag to swipe, hold to long-press, scroll with the mouse wheel or trackpad, plus a text field and the hardware buttons. On iOS 27 and later the finger follows the pointer as it moves (over CoreDevice HID, which keeps the phone awake until 30 seconds after the last touch); older versions get the whole gesture on release. Everything goes through the daemon like an MCP call, so it queues with the agent and unlocks the phone first. On iOS 27 and later the picture is the phone's own video; otherwise it comes from WDA (none until WDA is ready), as H.264 or MJPEG; see [Picture](#viewer-picture) below. Touch is available when the frame matches the measured size and orientation. After rotating or switching devices, start a new gesture on the updated picture. Settings at the bottom of the sidebar controls the theme and text size. The picture has its own zoom controls, the − and + above it (pinching doesn't zoom, so a drag can't turn into one). **Device panel:** state and readiness checks, installed apps, on-screen elements. The page follows the device the daemon controls: switch on the page or anywhere else, and the picture and panels move over; see [Several Devices](./multi-device#control-page). **AI tasks:** type a task and a local agent carries it out (Claude Code or Codex, whichever is on `PATH`; picked on the page). Before it starts you can limit it to some apps with the App button next to the agent; in any other app each step waits for you. You can pause, take over and hand back, or stop it, and it asks on the page before paying, deleting or sending messages. The token is the key: keep it to yourself. See [Viewer and Remote Use](./remote) || `viewer token [--new]` | Show the viewer's `owner` login token, links that log in on this machine and the local network, and the command to connect to its HTTP MCP; `--new` replaces it: browsers and MCP clients that used the old one need the new one. Tokens are kept in `viewer-tokens.json` in the data directory (0600); changes take effect at once, without restarting the viewer || `viewer token add <name> [--read-only]`, `list`, `show <name> [--new]`, `remove <name>` | Named tokens, one per person or device, say; the page's task log records which token paused, took over or confirmed. A `--read-only` token can watch the picture, the device's state and tasks; every action is refused and the page hides them. `list` doesn't show the tokens themselves; the last token that may operate stays || `task run [--title <title>] [--app <app>…] -- <command>` | AI tasks for other agent platforms: opens a task on the controlled device and runs the command in it. An `iphone-control mcp` the command starts takes part (through `IPHONE_CONTROL_TASK_TOKEN`): the control page shows every step and can pause, take over and stop it, and it waits for you before paying or sending a message. The task ends when the command does; when the task is stopped elsewhere, so is the command; Ctrl-C stops the task. While a task runs, other clients can look but not act on the phone. `--app` (repeatable, name or bundle ID) limits the task to those apps: in any other app, or to open a link that isn't a web page, each step waits for you first. **With the phone on another machine**, add `--url https://<that machine>:8200` (and `--ca cert.pem` for a self-signed certificate) and put the login token in `IPHONE_CONTROL_VIEWER_TOKEN`: the task is opened through that machine's viewer, and the command gets the task's token and `IPHONE_CONTROL_MCP_URL`, for its agent to connect to MCP over HTTP (see [Connect an AI Client](./mcp-clients#connect-by-url-http)). The login token isn't passed to the command, so the agent can't pause or confirm its own task; `task finish` in the command works as usual |diff --git a/website/en/guide/getting-started.md b/website/en/guide/getting-started.mdindex 34e0f3d..5bd34e0 100644--- a/website/en/guide/getting-started.md+++ b/website/en/guide/getting-started.md@@ -111,9 +111,12 @@ The agent will use `iphone_launch_app`, `iphone_tap_element`, `iphone_screenshotYou can also watch and control the phone in a browser, or hand tasks to a local Claude Code or Codex there:```bash-iphone-control viewer # then open http://127.0.0.1:8200+iphone-control viewer # the control page, on http://127.0.0.1:8200+iphone-control viewer token # in another terminal: open the link it prints to log in```+The page needs a login, on this machine too; a browser stays logged in for 30 days. The local network, read-only tokens and use from other machines: [Viewer and Remote Use](./remote).+## Checking on it```bashdiff --git a/website/en/guide/remote.md b/website/en/guide/remote.mdnew file mode 100644index 0000000..9c71ab7--- /dev/null+++ b/website/en/guide/remote.md@@ -0,0 +1,121 @@+# Viewer and Remote Use++`iphone-control viewer` is the control page in a browser: watch the live picture, tap, swipe and type on it, see the device panel, hand tasks to a local Claude Code or Codex, and pause, take over or confirm each step. This page covers opening it, using it from the local network and other machines, and how other agent platforms connect. Every command and option is listed in [CLI](./cli).++## Opening the viewer++```bash+iphone-control viewer # in this terminal; Ctrl-C stops it+iphone-control viewer token # in another one: the login token and a link that logs in+```++By default the viewer listens on `127.0.0.1:8200` only (change the port with `--port` or `IPHONE_CONTROL_VIEWER_PORT`). **Every HTTP visit needs a login, from this machine too**: the viewer can't tell your own browser from a visit forwarded by a reverse proxy such as frp, nginx or cloudflared, since both come from `127.0.0.1`.++- **Browsers**: open the link `viewer token` prints (`http://127.0.0.1:8200/?token=…`), or paste the token on the login page. A browser stays logged in for 30 days, and the token leaves the address bar.+- **Programs**: send `Authorization: Bearer <token>`.+- The CLI, `iphone-control mcp` over stdio and `task run` use the local Unix socket and need no login.++Tokens are kept in `viewer-tokens.json` in the data directory (0600). A token is a key: don't paste it into chats or configuration that gets shared.++## Tokens: named and read-only++The first token, `owner`, may do everything. Add named tokens, one per person or device, say:++```bash+iphone-control viewer token add living-room-tv --read-only # read-only; shows it and links that log in+iphone-control viewer token add alex # may operate, like owner+iphone-control viewer token list # names and roles (not the tokens)+iphone-control viewer token show alex --new # replace it+iphone-control viewer token remove living-room-tv # browsers that used it are out at once+```++- A **read-only token** can watch the picture, the device's state, apps, elements, and tasks with their steps; taps, typing, starting, pausing or confirming tasks, switching devices and `/mcp` are refused. The page shows "Read-only" in its header and hides the text field and buttons.+- The task log records which token controlled a task, e.g. `viewer:alex`.+- Adding, replacing and removing tokens **takes effect at once**, without restarting the viewer; a browser that used a removed or replaced token is back at the login page on its next request.+- The last token that may operate can't be removed; replace it with `show <name> --new`.++## The local network and HTTPS++```bash+iphone-control viewer --lan # or IPHONE_CONTROL_VIEWER_LAN=1 in the settings file+```++- Same port, listening on the local network too. Only connections from loopback, private ranges (10/8, 172.16/12, 192.168/16), link-local and 100.64/10 (Tailscale) are accepted; `Host` must be one of this machine's names or addresses, and `--lan-host` adds another name.+- **HTTPS on the network**: browsers only offer WebCodecs on HTTPS or localhost, so only over HTTPS can the picture be H.264 or the phone's own HEVC (over HTTP it falls back to MJPEG at several MB/s), and the token and picture are encrypted. `http://` from the network is redirected to `https://`; `http://127.0.0.1` on this machine works as before.+- **The certificate** is a self-signed one made on this machine (in `viewer-tls/` in the data directory, kept; made again when a new address shows up). The first time, the browser warns that it isn't trusted: in the warning's certificate details, check that the SHA-256 fingerprint matches the one printed at startup or by `iphone-control service status`, then go on.+- **No warning**: use a certificate of your own with `--tls-cert`/`--tls-key` (or `IPHONE_CONTROL_VIEWER_TLS_CERT`/`_KEY`), such as one from a local CA made with mkcert and installed on your computer, or a real one from `tailscale cert`.+- `--tls off` turns HTTPS off; see [Configuration](./configuration).++Without `--lan`, an SSH tunnel works too: `ssh -L 8200:127.0.0.1:8200 <user>@<host>`, then open `http://127.0.0.1:8200/`.++## As a background service++```bash+iphone-control service viewer install # starts at login; reads IPHONE_CONTROL_VIEWER_* from the settings file+iphone-control service status # whether it runs, and the HTTPS certificate's fingerprint+```++Installing keeps this shell's `PATH` so the viewer finds `claude` and `codex`; install again if `PATH` changes. After changing `IPHONE_CONTROL_VIEWER_*` settings, `service viewer restart`. An update stops the viewer first and starts it again afterwards; an automatic update waits while an AI task runs.++## From another machine++The phone is connected to one machine (a NAS, say) and you use it from another:++| To | Do |+|---|---|+| Watch and act in a browser | Open `https://<that machine>:8200/?token=…` (the viewer runs with `--lan`) |+| Use an MCP client that takes a URL | `https://<that machine>:8200/mcp` with `Authorization: Bearer <token>`; see [Connect an AI Client](./mcp-clients#connect-by-url-http) |+| Use an MCP client that only starts a command | Install iphone-control on this machine and use `iphone-control mcp --url https://<that machine>:8200` (plus `--ca cert.pem`), with the login token in `IPHONE_CONTROL_VIEWER_TOKEN`. It bridges stdio to that machine's `/mcp` |+| Start AI tasks on that phone | `iphone-control task run --url https://<that machine>:8200 -- <command>`; see the next section |+| Follow and control tasks there | `iphone-control task list`, `events <id>`, `pause`, `approve <id>`… all with `--url` (or `IPHONE_CONTROL_URL`) |++With a self-signed certificate, the other machine has to trust it: copy `~/.local/share/iphone-control/viewer-tls/cert.pem` from the machine with the phone, give iphone-control commands `--ca cert.pem` (or `IPHONE_CONTROL_CA`), and start Node-based clients with `NODE_EXTRA_CA_CERTS=cert.pem`. If that computer uses a proxy, add this machine's address to `NO_PROXY`.++## For agent platforms: AI tasks++An AI task lets the control page show each step of an agent and pause, take over or stop it, and the agent waits for you before paying or sending a message; while a task runs, other clients can look but not act on the phone.++**On the same machine**:++```bash+iphone-control task run --title "Tidy the photos" [--app Photos] -- <command that starts the agent>+```++`task run` opens the task, holds its lease and runs the command with the task's token (`IPHONE_CONTROL_TASK_TOKEN`); an `iphone-control mcp` the command starts takes part in the task. The task ends when the command does; when the task is stopped elsewhere, so is the command. `--app` limits the task to those apps: in any other app each step waits for you first.++**On another machine**: add `--url`, with the login token in `IPHONE_CONTROL_VIEWER_TOKEN`:++```bash+export IPHONE_CONTROL_VIEWER_TOKEN=<token>+iphone-control task run --url https://<that machine>:8200 --ca cert.pem -- <command that starts the agent>+```++The command gets the task's token, `IPHONE_CONTROL_URL` and `IPHONE_CONTROL_MCP_URL`, and **not the login**. Its agent can keep a plain `iphone-control mcp`: seeing `IPHONE_CONTROL_URL`, it bridges to that machine with the task's token only. It can also use `$IPHONE_CONTROL_MCP_URL` with the header `Iphone-Control-Task-Token` over HTTP directly.++**Acting and controlling apart**: the agent has only the task's token, so it acts on the phone for that task but can't pause, resume, take over or approve its own confirmations; a person does that on the control page, or the platform with its login. On the same machine, control requests from the agent's processes (and those they start) are refused as well.++**A platform without iphone-control** calls the viewer's API (all with the login token, except the last two):++| Endpoint | What |+|---|---|+| `POST /api/agent/tasks/open`, `{title, owner?, apps?}` | Opens a task. Answers with an event stream: `opened` with `{id, token}` first, then the task's events. **The open connection is the lease**: when it closes, the task is abandoned; a comment every 15 s keeps proxies from closing it |+| `GET /api/agent/tasks` | Recent tasks |+| `GET /api/agent/tasks/<id>/events` | A task's events (replayed, then followed) |+| `POST /api/agent/tasks/<id>/pause` (`resume`, `takeover`, `handback`, `stop`) | Controls the task |+| `POST /api/agent/tasks/<id>/confirm`, `{"approve": true}` | Answers a confirmation |+| `/mcp` with the header `Iphone-Control-Task-Token` | For the agent: its task's token alone, no login |+| `POST /api/agent/tasks/finish`, `{ok, summary}`, the same header | For the agent: ends the task with a summary |++## Through a reverse proxy or tunnel++When frp, nginx or cloudflared forwards the viewer to the internet:++- The login still applies; a token is 256 random bits, and after 20 wrong tokens in a minute none are checked for the rest of it.+- Use HTTPS (a real certificate on the proxy), or the token and cookie cross the internet in the clear.+- Something like Tailscale, which only lets your own devices in, is much safer than exposing the port to the internet.++## Security++- A token that may operate can watch the screen, control the phone and run tasks with this machine's Claude Code / Codex login: give it only to people you trust, and if it leaks, `viewer token show <name> --new` or `remove` it at once. Give a read-only token to people who only need to watch.+- Processes of the same system user can read each other's files: an agent on this machine can read the token file, but its control requests are refused by their process. This isn't a strict boundary; for real isolation, run the agent as another system user or in a sandbox.+- See also [Security and Privacy](./security).diff --git a/website/en/guide/tools.md b/website/en/guide/tools.mdindex d6e1f4f..3b1f7df 100644--- a/website/en/guide/tools.md+++ b/website/en/guide/tools.md@@ -76,4 +76,8 @@ When WDA can't be reached, screenshots, taps, swipes and buttons go through Core- **Screen size:** while WDA is down, taps and swipes return an error if there is no record of this device's size. Wait for WDA to come up once, or set [`SCREEN_W`/`SCREEN_H`](./configuration#advanced).- **No redo:** if a request already reached WDA and the connection then dropped or the response was incomplete, it doesn't fall back to HID, because the action may have happened and redoing it would tap or type twice. The tool returns an error; the agent should take a screenshot first.+## In an AI task: `iphone_confirm`++When the agent works in an [AI task](./remote#for-agent-platforms-ai-tasks) (one the control page gave it, or one `task run` started), its tool list has one more, `iphone_confirm`: before paying, deleting, sending a message or changing account or privacy settings it asks you with it and waits for your answer, on the control page or with `iphone-control task approve`/`reject`; when you reject, it stops and doesn't do the step. A task limited to some apps also stops and asks in any other app. While a task runs, other clients can look but not act on the phone.+All parameters: [docs/tools.md](https://github.com/Derrors/iphone-control/blob/main/docs/tools.md) in the repository.diff --git a/website/en/guide/what-is-iphone-control.md b/website/en/guide/what-is-iphone-control.mdindex 1b55508..412ab62 100644--- a/website/en/guide/what-is-iphone-control.md+++ b/website/en/guide/what-is-iphone-control.md@@ -9,6 +9,7 @@ It works as a set of [MCP](https://modelcontextprotocol.io) (Model Context Proto- Let an agent get things done on the phone: look things up, fill in forms, change settings, play games.- Test your own app or website on a real device; a Linux box will do, no Mac needed.- Turn a spare iPhone into a device you can script remotely.+- Watch and act on the phone from a control page in the browser, or hand tasks to a local Claude Code or Codex and pause, take over or confirm each step; from the local network and other machines too, see [Viewer and Remote Use](./remote).## How it worksdiff --git a/website/en/troubleshoot/faq.md b/website/en/troubleshoot/faq.mdindex 6673c80..2b8b950 100644--- a/website/en/troubleshoot/faq.md+++ b/website/en/troubleshoot/faq.md@@ -58,6 +58,21 @@ Messages are in Simplified Chinese by default, and in English with `IPHONE_CONTR- **Taps do nothing:** a system alert may be covering the screen; the result says so (「屏幕上有系统弹窗…」). Dismiss it first.- **Typed text is incomplete:** `iphone_type_text` reads the field back and fixes it; if it still reports a mismatch, the app rewrites the field while typing — type shorter pieces.+## The control page asks for a login, or says the token is wrong++Every HTTP visit needs a login, on this machine too. On the machine with the phone run `iphone-control viewer token` and open the link it prints (or paste the token on the login page). Back at the login page after being logged in: the token was replaced or removed (`viewer token show <name> --new`, `remove`). After 20 wrong tokens in a minute none are checked for the rest of it (you are asked to try again later). A read-only token can watch but not act; the page says "Read-only". See [Viewer and Remote Use](../guide/remote).++## The browser warns about the certificate on the local network++With `--lan` the viewer uses a self-signed certificate made on that machine, so the browser warns the first time. In the warning's certificate details, check that the SHA-256 fingerprint matches what `iphone-control service status` (or the viewer at startup) shows, then go on. A new IP address for the machine makes a new certificate, to check again. To skip the warning, use a certificate of your own with `--tls-cert`/`--tls-key` (mkcert, `tailscale cert`).++## Can't reach the viewer, `/mcp` or `task --url` from another machine++- 401: no login token, or a wrong one. The CLI and `mcp --url` read `IPHONE_CONTROL_VIEWER_TOKEN`; MCP clients send `Authorization: Bearer <token>`; an agent with only its task's token gets 401 once the task ended.+- A certificate error: a self-signed certificate has to be trusted. Give iphone-control commands `--ca cert.pem` (`IPHONE_CONTROL_CA`) and Node-based clients `NODE_EXTRA_CA_CERTS=cert.pem`; the certificate is `~/.local/share/iphone-control/viewer-tls/cert.pem` on the machine with the phone.+- Refused or timing out: the viewer needs `--lan`, and only takes connections from local-network addresses (Tailscale's 100.64/10 included). If this computer uses a proxy, add that machine's address to `NO_PROXY`.+- 403: a read-only token can't act, or `Host` isn't one of that machine's names or addresses (add one with `--lan-host`).+## No picture on the control page after switching devicesThe picture area says why: not connected; pairing or Developer Mode still needed (run `iphone-control setup --reconfigure` and pick *Phone*); WDA starting (usually 10–60 seconds); or it needs unlocking. When the new device has no WDA yet and an Apple ID is signed in, click *Sign and install WDA*; it takes about 1–2 minutes, keep the phone unlocked. Without an Apple ID, run `iphone-control setup --reconfigure` and pick *Apple ID*. Switching is refused while an AI task runs, and when `IPHONE_UDID` is set. See [Several devices](../guide/multi-device).diff --git a/website/guide/cli.md b/website/guide/cli.mdindex 51fea16..7cdd351 100644--- a/website/guide/cli.md+++ b/website/guide/cli.md@@ -130,7 +130,7 @@ Viewer 只监听本机回环地址。同机其他用户也能查看安装信息|---|---|| `notify-test [事件]` | 发测试通知:每类事件各一条,或只发一类(`resign`、`session`、`pin`、`pin-recovered`、`unlock`、`offline`、`online`) || `wda-bridge [--port <端口>]` | 把 WDA 桥接到 `127.0.0.1`,端口默认 8100,给别的工具用。WDA 没有认证,本机任何用户都能连上,用完就停 |-| `viewer [--port <端口>] [--lan] [--lan-host <名字>…] [--tls auto\|on\|off] [--tls-cert <PEM> --tls-key <PEM>] [--mcp on\|off]` | 浏览器里的控制台,**要先登录**:所有经 HTTP 的访问(本机也一样,经 frp 等反向代理转发进来的访问看起来就是本机)都要令牌,`viewer token` 显示令牌和点开就能登录的链接;浏览器登录一次后记住 30 天,程序和 MCP 客户端用 `Authorization: Bearer <令牌>`。同一个端口的 `/mcp` 还提供 MCP over HTTP(见[接入 AI 客户端](./mcp-clients#通过-url-接入-http),`--mcp off` 关掉),地址 `http://127.0.0.1:8200/`(默认端口);加 `--lan` 时局域网里也能用 `https://<这台机器的 IP>:8200/` 打开(同一个端口;局域网来的 `http://` 会跳到 `https://`)。证书默认是本机生成的自签证书(存在数据目录的 `viewer-tls/`,一直沿用),浏览器第一次打开会提示不受信任:核对启动时或 `service status` 显示的 SHA-256 指纹一致后继续。用 `--tls-cert`/`--tls-key` 换成自己的证书(mkcert、`tailscale cert` 等)就没有这个提示。`--tls off` 时局域网里是 HTTP,画面退回 MJPEG(浏览器只在 localhost 或 HTTPS 上提供 WebCodecs)。中英文(默认跟着 `IPHONE_CONTROL_LANG`,页面上可切换)。**画面:** 实时画面像触摸屏一样用:点一下是点击,拖动是滑动,按住是长按,滚动鼠标滚轮或触控板也能滚动页面,另有输入框和硬件键。iOS 27 起手指跟着鼠标实时移动(经 CoreDevice HID,最后一次触摸后 30 秒内手机保持亮屏);更低的版本在松开后发整段手势。操作都和 MCP 调用一样经守护进程,和 agent 的操作排队,并先自动解锁。iOS 27 及以上画面直接用手机自己的视频;其余来自 WDA(WDA 没就绪时没有画面),是 H.264 或 MJPEG,见下面的[画面编码](#viewer-picture)。画面与设备实测的尺寸、方向一致时可以点按。转屏或换机后,请在新画面上重新操作。侧栏底部的「设置」可调整主题和字号,画面缩放用画面上方的加减号调整(双指捏合不缩放,免得拖动时误触)。**设备面板:** 状态和就绪检查、已安装的 App、屏幕上的元素。页面跟着守护进程控制的设备走,在页面上或别处切换设备,画面和面板都会跟过去,见[多台设备](./multi-device#control-page)。**AI 任务:** 写下任务,交给本机的 agent 执行(PATH 里有的 Claude Code 或 Codex,在页面上选)。开任务前可以用 Agent 旁边的 App 按钮限定范围,到了别的 App 每一步都先等你确认。可以暂停、接管后交还、停止;付款、删除、发消息之前它会在页面上请你确认。令牌就是钥匙,别外传 |+| `viewer [--port <端口>] [--lan] [--lan-host <名字>…] [--tls auto\|on\|off] [--tls-cert <PEM> --tls-key <PEM>] [--mcp on\|off]` | 浏览器里的控制台,**要先登录**:所有经 HTTP 的访问(本机也一样,经 frp 等反向代理转发进来的访问看起来就是本机)都要令牌,`viewer token` 显示令牌和点开就能登录的链接;浏览器登录一次后记住 30 天,程序和 MCP 客户端用 `Authorization: Bearer <令牌>`。同一个端口的 `/mcp` 还提供 MCP over HTTP(见[接入 AI 客户端](./mcp-clients#通过-url-接入-http),`--mcp off` 关掉),地址 `http://127.0.0.1:8200/`(默认端口);加 `--lan` 时局域网里也能用 `https://<这台机器的 IP>:8200/` 打开(同一个端口;局域网来的 `http://` 会跳到 `https://`)。证书默认是本机生成的自签证书(存在数据目录的 `viewer-tls/`,一直沿用),浏览器第一次打开会提示不受信任:核对启动时或 `service status` 显示的 SHA-256 指纹一致后继续。用 `--tls-cert`/`--tls-key` 换成自己的证书(mkcert、`tailscale cert` 等)就没有这个提示。`--tls off` 时局域网里是 HTTP,画面退回 MJPEG(浏览器只在 localhost 或 HTTPS 上提供 WebCodecs)。中英文(默认跟着 `IPHONE_CONTROL_LANG`,页面上可切换)。**画面:** 实时画面像触摸屏一样用:点一下是点击,拖动是滑动,按住是长按,滚动鼠标滚轮或触控板也能滚动页面,另有输入框和硬件键。iOS 27 起手指跟着鼠标实时移动(经 CoreDevice HID,最后一次触摸后 30 秒内手机保持亮屏);更低的版本在松开后发整段手势。操作都和 MCP 调用一样经守护进程,和 agent 的操作排队,并先自动解锁。iOS 27 及以上画面直接用手机自己的视频;其余来自 WDA(WDA 没就绪时没有画面),是 H.264 或 MJPEG,见下面的[画面编码](#viewer-picture)。画面与设备实测的尺寸、方向一致时可以点按。转屏或换机后,请在新画面上重新操作。侧栏底部的「设置」可调整主题和字号,画面缩放用画面上方的加减号调整(双指捏合不缩放,免得拖动时误触)。**设备面板:** 状态和就绪检查、已安装的 App、屏幕上的元素。页面跟着守护进程控制的设备走,在页面上或别处切换设备,画面和面板都会跟过去,见[多台设备](./multi-device#control-page)。**AI 任务:** 写下任务,交给本机的 agent 执行(PATH 里有的 Claude Code 或 Codex,在页面上选)。开任务前可以用 Agent 旁边的 App 按钮限定范围,到了别的 App 每一步都先等你确认。可以暂停、接管后交还、停止;付款、删除、发消息之前它会在页面上请你确认。令牌就是钥匙,别外传。详见[控制台与远程使用](./remote) || `viewer token [--new]` | 显示控制台的 `owner` 登录令牌、本机和局域网的登录链接,以及接入 HTTP MCP 的命令;`--new` 换一个新的,用旧令牌登录的浏览器和 MCP 客户端都要用新的。令牌存在数据目录的 `viewer-tokens.json`(0600),改动马上生效,不用重启控制台 || `viewer token add <名字> [--read-only]`、`list`、`show <名字> [--new]`、`remove <名字>` | 有名字的令牌,比如每人一个、每台设备一个;控制台的任务记录里记下是哪个令牌暂停、接管、确认的。`--read-only` 的令牌只能看画面、设备状态和任务,所有操作都被拒绝,页面上也不显示操作入口。`list` 不显示令牌本身;最后一个能操作的令牌不能删 || `task run [--title <标题>] [--app <App>…] -- <命令>` | 给别的 agent 平台用的 AI 任务:在当前控制的设备上开一个任务,再运行这条命令。命令启动的 `iphone-control mcp` 自动加入任务(经环境变量 `IPHONE_CONTROL_TASK_TOKEN`),控制台上能看到每一步,可以暂停、接管、停止,付款、发消息之前会等你确认。命令结束,任务就结束;任务在别处被停止,命令也会被停止;按 Ctrl-C 等于停止任务。任务进行中,其他客户端只能看,不能操作手机。`--app`(可写多次,App 名或 Bundle ID)把任务限定在这些 App 里:到了别的 App、或者要打开网页以外的链接,每一步都先等你确认。**手机接在另一台机器上**时加 `--url https://<那台机器>:8200`(自签证书再加 `--ca cert.pem`),登录令牌放在环境变量 `IPHONE_CONTROL_VIEWER_TOKEN` 里:任务经那台机器的控制台开,命令拿到的是任务口令和 `IPHONE_CONTROL_MCP_URL`,它的 agent 用 HTTP 方式的 MCP 接入(见[接入 AI 客户端](./mcp-clients#通过-url-接入-http))。登录令牌不会传给命令,所以 agent 不能暂停、确认自己的任务;命令里的 `task finish` 照样能用 |diff --git a/website/guide/getting-started.md b/website/guide/getting-started.mdindex 8298ec0..7fd7c1c 100644--- a/website/guide/getting-started.md+++ b/website/guide/getting-started.md@@ -111,9 +111,12 @@ agent 会调用 `iphone_launch_app`、`iphone_tap_element`、`iphone_screenshot`也可以在浏览器里看画面、直接操作手机,或把任务交给本机的 Claude Code、Codex 执行:```bash-iphone-control viewer # 打开 http://127.0.0.1:8200+iphone-control viewer # 控制台,开在 http://127.0.0.1:8200+iphone-control viewer token # 另开一个终端:点开它打印的链接就登录了```+控制台要先登录,本机也一样;登录一次记住 30 天。局域网访问、只读令牌、从别的机器用,见[控制台与远程使用](./remote)。+## 检查状态```bashdiff --git a/website/guide/remote.md b/website/guide/remote.mdnew file mode 100644index 0000000..7c95eb7--- /dev/null+++ b/website/guide/remote.md@@ -0,0 +1,121 @@+# 控制台与远程使用++`iphone-control viewer` 是浏览器里的控制台:看实时画面、直接点按滑动输入、看设备面板,把任务交给本机的 Claude Code 或 Codex 执行,暂停、接管、确认它的每一步。这一页讲怎么打开它、怎么从局域网和别的机器用它,以及别的 agent 平台怎么接入。命令和参数的完整列表见[命令行](./cli)。++## 打开控制台++```bash+iphone-control viewer # 在这个终端里运行,Ctrl-C 停止+iphone-control viewer token # 另开一个终端:显示登录令牌和点开就能登录的链接+```++控制台默认只监听本机的 `127.0.0.1:8200`(端口用 `--port` 或 `IPHONE_CONTROL_VIEWER_PORT` 改)。**所有经 HTTP 的访问都要登录,本机也一样**:控制台分不清是你在本机的浏览器,还是经 frp、nginx、cloudflared 这类反向代理转发进来的访问,它们看起来都来自 `127.0.0.1`。++- **浏览器**:点开 `viewer token` 打印的链接(`http://127.0.0.1:8200/?token=…`),或者打开控制台后在登录页粘贴令牌。登录一次后记住 30 天,地址栏里的令牌会被去掉。+- **程序**:请求头带 `Authorization: Bearer <令牌>`。+- 命令行、stdio 方式的 `iphone-control mcp`、`task run` 走本机的 Unix socket,不用登录。++令牌存在数据目录的 `viewer-tokens.json`(0600)。令牌就是钥匙,别发到聊天里或写进会公开的配置。++## 令牌:有名字的、只读的++第一个令牌叫 `owner`,什么都能做。可以再加有名字的令牌,比如每人一个、每台设备一个:++```bash+iphone-control viewer token add 客厅电视 --read-only # 只读:显示令牌和登录链接+iphone-control viewer token add 小王 # 和 owner 一样能操作+iphone-control viewer token list # 名字和权限(不显示令牌本身)+iphone-control viewer token show 小王 --new # 换一个新的+iphone-control viewer token remove 客厅电视 # 用它登录的浏览器马上进不来+```++- **只读令牌**能看画面、设备状态、App 列表、元素、任务和它的每一步;点按、输入、开任务、暂停、确认、切换设备、`/mcp` 都会被拒绝。页面上顶栏会显示「只读」,输入框和按键都不显示。+- 任务记录里写下是哪个令牌做的控制,比如 `viewer:小王`。+- 增删、换令牌**马上生效**,不用重启控制台;被删或被换掉的令牌,用它登录的浏览器下一次请求就回到登录页。+- 最后一个能操作的令牌不能删;要换掉它用 `show <名字> --new`。++## 局域网访问和 HTTPS++```bash+iphone-control viewer --lan # 或设置文件里 IPHONE_CONTROL_VIEWER_LAN=1+```++- 还是同一个端口,同时监听局域网。只接受回环、私有网段(10/8、172.16/12、192.168/16)、链路本地和 100.64/10(Tailscale)来的连接;`Host` 只认本机的名字和地址,别的名字用 `--lan-host` 加。+- **局域网里用 HTTPS**:浏览器只在 HTTPS 或 localhost 上提供 WebCodecs,用 HTTPS 打开时画面才能是 H.264 或手机自己的 HEVC(HTTP 下只能退回每秒几 MB 的 MJPEG),令牌和画面也加了密。局域网来的 `http://` 会跳到 `https://`;本机的 `http://127.0.0.1` 照旧。+- **证书**:默认用本机生成的自签证书(数据目录的 `viewer-tls/`,一直沿用;出现新的 IP 时重新生成)。浏览器第一次打开会提示不受信任:在警告页的证书详情里核对 SHA-256 指纹,和启动时或 `iphone-control service status` 显示的一致就继续访问。+- **不想看到警告**:用自己的证书,`--tls-cert`/`--tls-key`(或 `IPHONE_CONTROL_VIEWER_TLS_CERT`/`_KEY`),比如 mkcert 生成、装到你电脑上的本地 CA,或者 `tailscale cert` 拿到的正式证书。+- `--tls off` 关掉 HTTPS,见[配置](./configuration)。++不开局域网也可以用 SSH 隧道:`ssh -L 8200:127.0.0.1:8200 <用户>@<主机>`,浏览器开 `http://127.0.0.1:8200/`。++## 装成后台服务++```bash+iphone-control service viewer install # 登录后自动启动;读设置文件里的 IPHONE_CONTROL_VIEWER_*+iphone-control service status # 是否在运行,以及 HTTPS 证书的指纹+```++安装时会记下当前终端的 `PATH`,好让控制台找到 `claude` 和 `codex`;`PATH` 变了再装一次。改了 `IPHONE_CONTROL_VIEWER_*` 设置后 `service viewer restart`。升级时会先停下控制台、升级完再启动;有 AI 任务在跑时,自动升级会推迟。++## 从别的机器用++手机接在一台机器上(比如 NAS),在别的机器上用它:++| 你要做的 | 怎么做 |+|---|---|+| 在浏览器里看、操作 | 打开 `https://<那台机器>:8200/?token=…`(控制台开着 `--lan`) |+| 只能填 URL 的 MCP 客户端 | `https://<那台机器>:8200/mcp`,请求头 `Authorization: Bearer <令牌>`,见[接入 AI 客户端](./mcp-clients#通过-url-接入-http) |+| 只能填命令的 MCP 客户端 | 在这台机器上装好 iphone-control,填 `iphone-control mcp --url https://<那台机器>:8200`(加 `--ca cert.pem`),登录令牌放在 `IPHONE_CONTROL_VIEWER_TOKEN`。它把 stdio 转到那台机器的 `/mcp` |+| 在那台手机上开 AI 任务 | `iphone-control task run --url https://<那台机器>:8200 -- <命令>`,见下一节 |+| 看、控制那台机器上的任务 | `iphone-control task list`、`events <id>`、`pause`、`approve <id>`… 都加 `--url`(或设 `IPHONE_CONTROL_URL`) |++自签证书时,别的机器要信任它:把那台机器上的 `~/.local/share/iphone-control/viewer-tls/cert.pem` 拷过来,iphone-control 的命令加 `--ca cert.pem`(或设 `IPHONE_CONTROL_CA`),Node 写的客户端用 `NODE_EXTRA_CA_CERTS=cert.pem` 启动。那台电脑配了代理时,把这台机器的 IP 加进 `NO_PROXY`。++## 给 agent 平台用:AI 任务++AI 任务让控制台看到 agent 的每一步,可以暂停、接管、停止,付款、发消息之前会等你确认;任务进行中,别的客户端只能看,不能操作手机。++**同一台机器上**:++```bash+iphone-control task run --title "整理相册" [--app 照片] -- <启动 agent 的命令>+```++`task run` 开任务、持有租约,再带着任务口令(`IPHONE_CONTROL_TASK_TOKEN`)运行命令;命令里启动的 `iphone-control mcp` 自动加入任务。命令结束,任务就结束;任务在别处被停止,命令也会被停止。`--app` 把任务限定在这些 App 里,到了别的 App 每一步都先等你确认。++**在另一台机器上**:加 `--url`,登录令牌放在 `IPHONE_CONTROL_VIEWER_TOKEN`:++```bash+export IPHONE_CONTROL_VIEWER_TOKEN=<令牌>+iphone-control task run --url https://<那台机器>:8200 --ca cert.pem -- <启动 agent 的命令>+```++命令拿到任务口令、`IPHONE_CONTROL_URL` 和 `IPHONE_CONTROL_MCP_URL`,**拿不到登录令牌**。里面的 agent 照常配置 `iphone-control mcp` 就行:它看到 `IPHONE_CONTROL_URL` 会自动转接到那台机器、只带任务口令;也可以直接用 `$IPHONE_CONTROL_MCP_URL` 加请求头 `Iphone-Control-Task-Token` 走 HTTP。++**执行和控制分开**:agent 只拿任务口令,只能为这个任务操作手机,不能暂停、继续、接管或批准自己的确认;这些由人在控制台上、或由平台用登录令牌来做。同一台机器上,agent 的进程(以及它启动的进程)发来的控制请求也会被拒绝。++**不装 iphone-control 的平台**直接调控制台的接口(都要登录令牌,除了标明的两个):++| 接口 | 作用 |+|---|---|+| `POST /api/agent/tasks/open`,`{title, owner?, apps?}` | 开任务。返回事件流:第一条 `opened` 带 `{id, token}`,之后是任务的事件。**连接保持着就是租约**,断开后任务记为 abandoned;每 15 秒有一条注释防代理掐断 |+| `GET /api/agent/tasks` | 最近的任务 |+| `GET /api/agent/tasks/<id>/events` | 任务的事件(先重放,再跟着推送) |+| `POST /api/agent/tasks/<id>/pause`(`resume`、`takeover`、`handback`、`stop`) | 控制任务 |+| `POST /api/agent/tasks/<id>/confirm`,`{"approve": true}` | 回答确认 |+| `/mcp`,请求头 `Iphone-Control-Task-Token` | agent 用:只凭任务口令,不用登录 |+| `POST /api/agent/tasks/finish`,`{ok, summary}`,同样的请求头 | agent 用:结束任务、写总结 |++## 经反向代理或隧道访问++用 frp、nginx、cloudflared 把控制台转发到公网时:++- 登录照样起作用,令牌是 256 位随机数;一分钟里错 20 次后,这一分钟内不再校验令牌。+- 用 HTTPS(反向代理那一侧配正式证书),否则令牌和 cookie 在公网上明文传输。+- 更推荐 Tailscale 这类只有你自己的设备连得进来的方式,比直接暴露在公网安全得多。++## 安全须知++- 有能操作的令牌,就能看屏幕、操作手机、用本机登录的 Claude Code / Codex 执行任务:只发给你信任的人,泄露了马上 `viewer token show <名字> --new` 或 `remove`。给只需要看的人发只读令牌。+- 同一个系统用户下,进程能读到彼此的文件:本机上的 agent 能读到令牌文件,但它的控制请求会按进程关系被拒绝。这不是严格的安全边界,要真正隔离,让 agent 换一个系统用户运行或放进沙箱。+- 更多见[安全与隐私](./security)。diff --git a/website/guide/tools.md b/website/guide/tools.mdindex b1b6752..6abfd91 100644--- a/website/guide/tools.md+++ b/website/guide/tools.md@@ -76,4 +76,8 @@ MCP 服务提供 17 个工具,开启[开发者工具](#开发者工具-显式- **屏幕尺寸**:WDA 不可用、又没有这台设备的尺寸记录时,点击和滑动报错。等 WDA 起来一次,或设 [`SCREEN_W`/`SCREEN_H`](./configuration#高级)。- **不会重做**:请求已经发给 WDA、连接才断开或响应不完整时,不退到 HID,因为动作可能已经做了,重做会点两次、输两遍。工具会报错,agent 要先截图确认再决定。+## AI 任务里:`iphone_confirm`++agent 在 [AI 任务](./remote#给-agent-平台用-ai-任务)里时(控制台交给它的任务,或 `task run` 启动的),工具列表里多一个 `iphone_confirm`:付款、删除、发消息、改账号或隐私设置之前,它用这个工具请你确认,等你在控制台上或用 `iphone-control task approve`/`reject` 回答;拒绝时它停下,不做这一步。任务限定了 App 时,到了别的 App 也会停下来问你。任务进行中,别的客户端只能看,操作手机会被拒绝。+完整参数见仓库里的 [docs/tools.md](https://github.com/Derrors/iphone-control/blob/main/docs/tools.md)(英文)。diff --git a/website/guide/what-is-iphone-control.md b/website/guide/what-is-iphone-control.mdindex 6313630..f2f6729 100644--- a/website/guide/what-is-iphone-control.md+++ b/website/guide/what-is-iphone-control.md@@ -9,6 +9,7 @@ iphone-control 让 AI agent(例如 Claude)操作一台用数据线接在 Lin- 让 agent 在手机上完成任务:查信息、填表、改设置、玩游戏。- 在真机上测试自己的 App 或网页;用 Linux 主机就行,不需要 Mac。- 把一台闲置 iPhone 变成可以远程脚本化的设备。+- 在浏览器的控制台里看画面、直接操作,或把任务交给本机的 Claude Code、Codex,每一步都能暂停、接管、确认;局域网和别的机器也能用,见[控制台与远程使用](./remote)。## 怎么工作diff --git a/website/troubleshoot/faq.md b/website/troubleshoot/faq.mdindex 6e91186..371e7d4 100644--- a/website/troubleshoot/faq.md+++ b/website/troubleshoot/faq.md@@ -56,6 +56,21 @@ iphone-control doctor # 逐层检查,每项失败都给出修复办法- **点了没反应:** 可能有系统弹窗挡着,工具结果里会提示「屏幕上有系统弹窗…」,先点掉它。- **输入的字不全:** `iphone_type_text` 会读回核对并补上;仍提示对不上时,说明 App 在输入过程中改写了输入框,分几段输入。+## 打开控制台要登录、提示「令牌不对」++所有经 HTTP 的访问都要登录,本机也一样。在接着手机的机器上运行 `iphone-control viewer token`,点开它打印的链接(或者在登录页粘贴令牌)。之前登录过、现在又回到登录页,是令牌被换掉或删掉了(`viewer token show <名字> --new`、`remove`)。一分钟里输错 20 次后,这一分钟内不再校验令牌(会提示稍后再试)。只读令牌能看不能操作,页面上会显示「只读」。见[控制台与远程使用](../guide/remote)。++## 局域网打开控制台时提示证书不受信任++`--lan` 时用的是本机生成的自签证书,第一次打开浏览器会警告。在警告页的证书详情里核对 SHA-256 指纹,和 `iphone-control service status`(或控制台启动时)显示的一致就继续访问。机器换了 IP 会重新生成证书,要再核对一次。不想看到警告,用 `--tls-cert`/`--tls-key` 换成自己的证书(mkcert、`tailscale cert`)。++## 从别的机器连不上控制台、`/mcp` 或 `task --url`++- 401:没带登录令牌或令牌不对。命令行和 `mcp --url` 读 `IPHONE_CONTROL_VIEWER_TOKEN`;MCP 客户端要带请求头 `Authorization: Bearer <令牌>`;Agent 只带任务口令时,任务结束后也会是 401。+- 证书错误:自签证书要信任它。iphone-control 的命令加 `--ca cert.pem`(`IPHONE_CONTROL_CA`),Node 写的客户端用 `NODE_EXTRA_CA_CERTS=cert.pem`;证书在接着手机的那台机器的 `~/.local/share/iphone-control/viewer-tls/cert.pem`。+- 连接被拒绝或超时:控制台要开 `--lan`;只接受局域网地址(含 Tailscale 的 100.64/10)来的连接。这台电脑配了代理时,把那台机器的 IP 加进 `NO_PROXY`。+- 403:只读令牌不能操作,或者 `Host` 不是那台机器的名字或地址(用 `--lan-host` 加名字)。+## 控制台切换设备后没有画面画面区域会写明原因:没连上、还要配对或打开开发者模式(运行 `iphone-control setup --reconfigure` 选「手机」)、WDA 正在启动(通常 10–60 秒)、要先解锁。新设备上还没装 WDA 时,已登录 Apple ID 的可以点「签名并安装 WDA」,大约 1–2 分钟,期间保持手机解锁;没登录的运行 `iphone-control setup --reconfigure` 选「Apple ID」。AI 任务执行中,或设了 `IPHONE_UDID` 时,不能切换设备。见[多台设备](../guide/multi-device)。