homeagent-runner 0.4.11 → 0.4.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -0
- package/README.en.md +46 -23
- package/README.md +16 -12
- package/dist/runner/cli.js +6 -2
- package/dist/runner/config-location.js +2 -2
- package/dist/runner/config.js +31 -8
- package/dist/runner/credentials.js +15 -8
- package/dist/runner/interactive.js +396 -0
- package/dist/runner/policy.js +8 -4
- package/dist/runner/service.js +5 -5
- package/dist/runner/setup.js +1 -1
- package/dist/runner/terminal-input.js +137 -0
- package/dist/runner/tools.js +6 -2
- package/dist/runner/tui.js +587 -30
- package/dist/shared/effective-permissions.js +25 -0
- package/dist/shared/protocol.js +1 -1
- package/docs/DEPLOYMENT.md +14 -8
- package/docs/OPEN_SOURCE.md +9 -9
- package/docs/TOOLS.md +4 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,38 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.13 — Clear program permissions
|
|
4
|
+
|
|
5
|
+
- Expose an explicit “Allow all programs” control in both device projects and
|
|
6
|
+
client permissions; hide the program list when unrestricted execution is selected.
|
|
7
|
+
- Align MCP device discovery with the effective client permissions used by the
|
|
8
|
+
Runner, and explain whether execution or path restrictions come from the
|
|
9
|
+
project configuration or the client authorization.
|
|
10
|
+
- Verify live permission changes and external interpreter execution from working
|
|
11
|
+
directories outside the project without rotating client credentials.
|
|
12
|
+
- Include the HomeAgent icon in MCP server metadata.
|
|
13
|
+
|
|
14
|
+
## 0.4.12 — Full-screen interactive Runner
|
|
15
|
+
|
|
16
|
+
- Make `homeagent` the everyday entry point: detect local login automatically,
|
|
17
|
+
show a full-screen welcome page before login, guide Server and device setup,
|
|
18
|
+
and connect immediately after browser approval.
|
|
19
|
+
- Add interactive settings for device names, process concurrency, output and
|
|
20
|
+
history limits, configuration selection, Server switching, autostart and
|
|
21
|
+
local logout. Confirm changes that replace credentials or restart execution.
|
|
22
|
+
- Replace the startup card with a responsive full-screen interface, including
|
|
23
|
+
recent activity, connection state and reconnect countdown. Restore the shell,
|
|
24
|
+
cursor, input and mouse modes on exit.
|
|
25
|
+
- Support mouse clicks, wheel navigation and Unicode-aware input positioning.
|
|
26
|
+
Shade clickable controls on hover, use stronger feedback while pressed, and
|
|
27
|
+
provide a mouse toggle for terminal text selection and copying.
|
|
28
|
+
- Separate keyboard, mouse and bracketed-paste input so terminal reports and
|
|
29
|
+
pasted text cannot accidentally trigger shortcuts or submit forms.
|
|
30
|
+
- Cancel browser pairing without retaining the state lock, preserve existing
|
|
31
|
+
credentials on cancellation, and isolate credentials when switching Servers.
|
|
32
|
+
Keep scripts and services compatible, explicitly using `--plain` for autostart.
|
|
33
|
+
- Update Dashboard onboarding and bilingual installation documentation to use
|
|
34
|
+
the single-command flow; add interaction, terminal input and settings tests.
|
|
35
|
+
|
|
3
36
|
## 0.4.11 — Updated live terminal preview
|
|
4
37
|
|
|
5
38
|
- Refresh the Chinese and English README terminal screenshot from the latest
|
package/README.en.md
CHANGED
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
|
|
34
34
|
HomeAgent lets Claude, ChatGPT, Cursor and other MCP clients work on your own Linux / Windows development machines through a relay you host. Runners connect outbound and execute authorized file, command and Git operations locally, without opening inbound ports.
|
|
35
35
|
|
|
36
|
-
The execution system requires no model API key, hosted relay, telemetry or paid cloud service. Tool results are sent to the AI client you choose. Version **v0.4.
|
|
36
|
+
The execution system requires no model API key, hosted relay, telemetry or paid cloud service. Tool results are sent to the AI client you choose. Version **v0.4.13** supports one administrator and multiple devices.
|
|
37
37
|
|
|
38
38
|
## Why HomeAgent
|
|
39
39
|
|
|
@@ -117,7 +117,7 @@ Install Docker Engine, Compose v2 and Git. Point your domain at the server and
|
|
|
117
117
|
allow inbound TCP ports 80/443. Replace `mcp.example.com` below with your domain.
|
|
118
118
|
|
|
119
119
|
```sh
|
|
120
|
-
git clone --branch v0.4.
|
|
120
|
+
git clone --branch v0.4.13 --depth 1 https://github.com/jhxxr/HomeAgent.git
|
|
121
121
|
cd HomeAgent
|
|
122
122
|
|
|
123
123
|
# Generate random secrets without installing Node.js on the host.
|
|
@@ -178,17 +178,19 @@ a Linux user systemd example is in [native Server deployment](docs/DEPLOYMENT.md
|
|
|
178
178
|
### 2. Install and pair a Runner
|
|
179
179
|
|
|
180
180
|
Install Node.js 24 and npm on the development machine. Git operations also need
|
|
181
|
-
a local Git installation.
|
|
181
|
+
a local Git installation. Everyday use has a single entry point:
|
|
182
182
|
|
|
183
183
|
```sh
|
|
184
184
|
npm install -g homeagent-runner@latest
|
|
185
|
-
homeagent
|
|
185
|
+
homeagent
|
|
186
186
|
```
|
|
187
187
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
188
|
+
Even before login, the full-screen welcome page explains that the device is not
|
|
189
|
+
signed in. Press Enter to provide the Web URL and device name and create the
|
|
190
|
+
private `$HOME/.homeagent/runner.json`. Press `o` on the approval screen to open
|
|
191
|
+
the browser; the link is valid for 10 minutes. Confirm the device name and project
|
|
192
|
+
directory in your signed-in browser. After approval, the Runner connects
|
|
193
|
+
automatically. You can also add projects later in the Dashboard.
|
|
192
194
|
|
|
193
195
|
**The Server stores project directories and permissions.** New local Runner
|
|
194
196
|
configurations contain no `projects/root`. The local `stateDir` holds device
|
|
@@ -197,31 +199,52 @@ available. The first project setting is permanent by default; temporary access
|
|
|
197
199
|
remains available in the permission editor.
|
|
198
200
|
|
|
199
201
|
```sh
|
|
200
|
-
#
|
|
201
|
-
homeagent
|
|
202
|
+
# Use the same entry point next time; login is detected automatically:
|
|
203
|
+
homeagent
|
|
202
204
|
```
|
|
203
205
|
|
|
204
206
|
Successful login or startup remembers the configuration location, so
|
|
205
|
-
`homeagent
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
207
|
+
`homeagent` also works from another directory. Press `s` for settings: device name,
|
|
208
|
+
process limits, output and history limits, login or Server switching,
|
|
209
|
+
configuration selection, autostart and logout. Changes that replace credentials
|
|
210
|
+
or require reconnection ask for confirmation. Press `a` for activity, `o` for the
|
|
211
|
+
Dashboard, `p` to pause or reconnect, Esc to go back, and `q` or Ctrl+C to quit.
|
|
212
|
+
|
|
213
|
+
Terminals supporting SGR mouse reports can click the login entry, footer buttons
|
|
214
|
+
and menu choices, scroll long menus and activity with the wheel, and position the
|
|
215
|
+
input cursor by clicking. Press `m` outside text fields, or click the mouse toggle,
|
|
216
|
+
to disable tracking for normal terminal selection and copying. Set
|
|
217
|
+
`HOMEAGENT_MOUSE=0` to start with tracking disabled. Pasted text cannot trigger
|
|
218
|
+
shortcuts or submit a form. Keyboard navigation remains available.
|
|
219
|
+
|
|
220
|
+
Clickable controls receive a shaded background on hover and a stronger blue
|
|
221
|
+
background while pressed. Moving away restores their normal appearance;
|
|
222
|
+
monochrome terminals use inverse text instead.
|
|
223
|
+
|
|
224
|
+
Use `homeagent --config <path>` to select another configuration. Lookup order:
|
|
225
|
+
explicit path, saved location, `~/.homeagent/runner.json`. A stale location stays
|
|
226
|
+
in the full-screen app until you select a configuration explicitly.
|
|
227
|
+
The `login`, `run`, `check-config`, `service-install` and `service-uninstall`
|
|
228
|
+
commands remain available for scripts and services.
|
|
229
|
+
|
|
230
|
+
In an interactive terminal, startup enters a full-screen Runner interface that
|
|
231
|
+
temporarily hides earlier shell commands and output. It shows the app mark,
|
|
232
|
+
device and Server details, recent activity and live connection state. The layout
|
|
233
|
+
adapts to terminal resizing, and stopping with `Ctrl+C` restores the previous
|
|
234
|
+
shell screen. Piped output, services and `homeagent run --plain` keep ordinary
|
|
235
|
+
logs for systemd, scheduled tasks and scripts.
|
|
215
236
|
|
|
216
237
|

|
|
217
238
|
|
|
218
|
-
*
|
|
239
|
+
*The v0.4.11 startup card, before the full-screen redesign: actual terminal output from an isolated demo environment, using a demo device and project directory.*
|
|
219
240
|
|
|
220
241
|
Enter Windows project paths such as `E:/Projects/my-app` in the Dashboard.
|
|
221
242
|
Keep configuration and state outside every exposed project. Local settings accept only connection and runtime options; project policies
|
|
222
243
|
come exclusively from the Server.
|
|
223
244
|
|
|
224
|
-
For automatic startup,
|
|
245
|
+
For automatic startup, select Enable autostart in settings. After confirmation,
|
|
246
|
+
the app closes the foreground connection before handing over to the background
|
|
247
|
+
Runner. Advanced users can still stop the foreground Runner and use
|
|
225
248
|
`homeagent service-install`.
|
|
226
249
|
Linux installs a user systemd service; Windows installs a current-user login
|
|
227
250
|
task. The installer discovers the configuration and pins the service to that
|
|
@@ -345,7 +368,7 @@ helpers. Each package has an explicit file list and license notices.
|
|
|
345
368
|
See [release preparation](docs/OPEN_SOURCE.md) to generate a reviewed source
|
|
346
369
|
archive. Nothing in the release commands automatically publishes or pushes it.
|
|
347
370
|
|
|
348
|
-
The `v0.4.
|
|
371
|
+
The `v0.4.13` release configuration produces separate `homeagent-server` and
|
|
349
372
|
`homeagent-runner` packages; local release commands do not upload to npm or push a
|
|
350
373
|
GitHub Release. For subsequent releases, increment the version and update the
|
|
351
374
|
version files and changelog before pushing a matching `vX.Y.Z` tag. Configure
|
package/README.md
CHANGED
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
|
|
34
34
|
HomeAgent 让 Claude、ChatGPT、Cursor 等 MCP 客户端,通过你部署的 Server 操作自己的 Linux / Windows 开发机器。Runner 主动连出,在本地执行授权的文件、命令和 Git 操作,无需开放被控端入站端口。
|
|
35
35
|
|
|
36
|
-
系统运行无需模型 API Key、第三方中继、遥测或付费云服务;工具结果会返回给你选择的 AI 客户端。当前版本 **v0.4.
|
|
36
|
+
系统运行无需模型 API Key、第三方中继、遥测或付费云服务;工具结果会返回给你选择的 AI 客户端。当前版本 **v0.4.13**,支持单管理员、多设备。
|
|
37
37
|
|
|
38
38
|
## 为什么使用 HomeAgent
|
|
39
39
|
|
|
@@ -111,7 +111,7 @@ Server 和 Runner 已拆成两个 npm 包,分别安装在中继服务器和需
|
|
|
111
111
|
准备 Docker Engine、Compose v2 和 Git,将自己的域名解析到服务器,并放行 TCP 80/443。下面以 `mcp.example.com` 为例,执行前替换成自己的域名。
|
|
112
112
|
|
|
113
113
|
```sh
|
|
114
|
-
git clone --branch v0.4.
|
|
114
|
+
git clone --branch v0.4.13 --depth 1 https://github.com/jhxxr/HomeAgent.git
|
|
115
115
|
cd HomeAgent
|
|
116
116
|
|
|
117
117
|
# 用容器生成随机密码和会话密钥,无需在宿主机安装 Node.js
|
|
@@ -157,33 +157,37 @@ homeagent-server
|
|
|
157
157
|
|
|
158
158
|
### 2. 安装并配对 Runner
|
|
159
159
|
|
|
160
|
-
在需要被 AI 操作的开发机器上安装 Node.js 24、npm;Git 工具还需要本机 Git
|
|
160
|
+
在需要被 AI 操作的开发机器上安装 Node.js 24、npm;Git 工具还需要本机 Git。日常使用只需一个入口:
|
|
161
161
|
|
|
162
162
|
```sh
|
|
163
163
|
npm install -g homeagent-runner@latest
|
|
164
|
-
homeagent
|
|
164
|
+
homeagent
|
|
165
165
|
```
|
|
166
166
|
|
|
167
|
-
|
|
167
|
+
未登录时也会进入全屏欢迎页,提示“尚未登录”。按 Enter 填写 Web URL 和设备名称,自动生成 `$HOME/.homeagent/runner.json`;等待批准时按 `o` 打开浏览器登录页面,链接有效期为 10 分钟。在网页中核对设备名称、填写被控端的项目目录并批准后,Runner 自动开始连接。也可以稍后在 Dashboard 的“管理设备权限”中添加项目。
|
|
168
168
|
|
|
169
169
|
**项目目录和权限只由 Server 保存**,本地配置不接受 `projects/root`。本地 `stateDir` 仅用于保存设备凭据和任务状态,不是项目目录。未配置项目时,Runner 不开放任何项目访问。首次项目设置默认永久保存;临时开放可在权限编辑器中另外选择。
|
|
170
170
|
|
|
171
171
|
```sh
|
|
172
|
-
#
|
|
173
|
-
homeagent
|
|
172
|
+
# 以后仍使用同一个入口,自动检查登录并连接:
|
|
173
|
+
homeagent
|
|
174
174
|
```
|
|
175
175
|
|
|
176
|
-
Runner 自动记录成功登录或启动时的配置位置,切换工作目录后仍可直接使用 `homeagent
|
|
176
|
+
Runner 自动记录成功登录或启动时的配置位置,切换工作目录后仍可直接使用 `homeagent`。按 `s` 打开设置,可以修改设备名称、并发数、任务输出上限和历史记录数,重新登录或切换服务,选择配置文件、启停自启动和退出登录。需要重连或替换凭据的操作会先显示确认。按 `a` 浏览活动,`o` 打开网页,`p` 暂停或恢复连接,Esc 返回,`q` 或 Ctrl+C 退出。
|
|
177
177
|
|
|
178
|
-
|
|
178
|
+
支持 SGR 鼠标协议的终端可直接点击登录入口、底部按钮和菜单项,使用滚轮浏览长菜单与活动记录,点击输入框定位光标。鼠标经过可点击区域时显示灰蓝色底纹,按下时加深为蓝色,移开恢复原样;单色界面使用反色反馈。按 `m`(输入框以外)或点击“鼠标开/关”切换鼠标模式,关闭后可选择和复制终端文本;设置环境变量 `HOMEAGENT_MOUSE=0` 可默认关闭鼠标。粘贴到输入框的内容按文字处理,不会触发快捷键或自动提交。键盘操作始终可用。
|
|
179
|
+
|
|
180
|
+
也可通过 `homeagent --config <路径>` 选择配置。查找顺序仍是显式路径、已记录路径、`~/.homeagent/runner.json`;路径失效时在全屏界面中重新选择,不会静默切换设备。脚本和服务继续兼容 `login`、`run`、`check-config`、`service-install` 和 `service-uninstall` 子命令。
|
|
181
|
+
|
|
182
|
+
交互式终端启动时会进入全屏 Runner 界面,暂时隐藏此前的命令和输出,显示应用图标、设备与 Server、最近活动和实时连接状态。窗口缩放时自动调整布局,按 `Ctrl+C` 停止后恢复原终端画面。服务、管道输出或使用 `homeagent run --plain` 时保留普通日志,方便 systemd、计划任务和脚本采集。
|
|
179
183
|
|
|
180
184
|

|
|
181
185
|
|
|
182
|
-
*
|
|
186
|
+
*v0.4.11 的启动卡片(全屏改版前):隔离演示环境中的实际终端输出,设备与目录均为演示数据。*
|
|
183
187
|
|
|
184
188
|
Windows 的项目根目录可在网页中填为 `E:/Projects/my-app`。Runner 配置和 stateDir 都必须位于所有暴露项目之外。本地只维护连接信息、设备凭据和任务状态。
|
|
185
189
|
|
|
186
|
-
|
|
190
|
+
需要自启动时,在设置中选择“启用自启动”,确认后会先关闭前台连接,再由后台 Runner 接管。Linux 安装用户 systemd 服务;Windows 安装当前用户登录时启动的计划任务。安装时固定使用当前配置路径。高级用法仍可停止前台 Runner 后执行 `homeagent service-install`。
|
|
187
191
|
|
|
188
192
|
默认不允许执行命令或 Git 写入。需要这些能力时,在 Dashboard 的设备权限中开启,并给客户端对应 scope。命令别名、环境 profile 和 linked worktree 目录同样在网页设备权限中配置。清空项目或断线后会收回全部项目访问;Server 与 Runner 应升级到相同版本。完整示例见 [日常开发工作流](docs/DEVELOPMENT_WORKFLOW.md)。
|
|
189
193
|
|
|
@@ -288,4 +292,4 @@ npm audit --omit=dev --audit-level=high
|
|
|
288
292
|
|
|
289
293
|
发布前执行 [开源与发布检查](docs/OPEN_SOURCE.md)。npm 发布会生成两个包:`homeagent-server` 只包含 Server/Web Dashboard,`homeagent-runner` 只包含 Runner、共享协议和 Windows 辅助程序;两者都采用明确的发布文件白名单。
|
|
290
294
|
|
|
291
|
-
`v0.4.
|
|
295
|
+
`v0.4.13` 的发布配置会分别生成 `homeagent-server` 和 `homeagent-runner`;本地发布命令不会自动上传 npm 或推送 GitHub。后续发布递增版本,更新版本文件和 changelog 后推送匹配标签;预发布使用 npm `next`,正式版本使用 `latest`。Token 权限、OIDC 切换和重试行为见 [自动发布说明](docs/OPEN_SOURCE.md#github-actions-自动发布)。
|
package/dist/runner/cli.js
CHANGED
|
@@ -9,6 +9,7 @@ import { rememberRunnerConfigPath, resolveRunnerConfigPath } from './config-loca
|
|
|
9
9
|
import { RunnerTui, tuiEnabled } from './tui.js';
|
|
10
10
|
import { VERSION } from '../shared/protocol.js';
|
|
11
11
|
import { versionBanner } from '../shared/version.js';
|
|
12
|
+
import { runInteractiveApplication } from './interactive.js';
|
|
12
13
|
function shortenHome(filename) {
|
|
13
14
|
const home = os.homedir();
|
|
14
15
|
if (!filename.toLowerCase().startsWith(home.toLowerCase()))
|
|
@@ -52,7 +53,7 @@ try {
|
|
|
52
53
|
},
|
|
53
54
|
});
|
|
54
55
|
if (values.help) {
|
|
55
|
-
console.log(`HomeAgent Runner ${VERSION}\n login [--config <path>] [--server URL] [--name NAME] [--run] [--force] [--plain]\n run [--config <path>] [--plain]\n check-config [--config <path>]\n service-install [--config <path>]\n service-uninstall [--config <path>]\n --version\n\nConfiguration: explicit --config, saved location, ~/.homeagent/runner.json.\
|
|
56
|
+
console.log(`HomeAgent Runner ${VERSION}\n\n homeagent Open the interactive app (login, settings and connection)\n\nAdvanced commands:\n login [--config <path>] [--server URL] [--name NAME] [--run] [--force] [--plain]\n run [--config <path>] [--plain]\n check-config [--config <path>]\n service-install [--config <path>]\n service-uninstall [--config <path>]\n --version\n\nConfiguration: explicit --config, saved location, ~/.homeagent/runner.json.\nIn a terminal, homeagent detects login automatically and guides setup in full screen.\n--plain prints one line per event; scripts and services retain the command interface.\nProject directories and permissions are configured on the Web.`);
|
|
56
57
|
}
|
|
57
58
|
else if (values.version)
|
|
58
59
|
console.log(versionBanner('Runner'));
|
|
@@ -60,7 +61,10 @@ try {
|
|
|
60
61
|
const command = positionals[0] ?? 'run';
|
|
61
62
|
if (positionals.length > 1 || !['login', 'run', 'check-config', 'service-install', 'service-uninstall'].includes(command))
|
|
62
63
|
throw new Error(`Unknown command: ${positionals.join(' ')}. Use homeagent --help.`);
|
|
63
|
-
if (
|
|
64
|
+
if (!values.plain && process.stdin.isTTY && tuiEnabled(process.stdout) && ['login', 'run'].includes(command)) {
|
|
65
|
+
await runInteractiveApplication({ config: values.config, server: values.server, name: values.name, force: values.force, login: command === 'login' });
|
|
66
|
+
}
|
|
67
|
+
else if (command === 'login') {
|
|
64
68
|
console.log(versionBanner('Runner'));
|
|
65
69
|
const result = await interactiveLogin({ config: values.config, server: values.server, name: values.name, force: values.force });
|
|
66
70
|
if (values.run)
|
|
@@ -42,13 +42,13 @@ export async function resolveRunnerConfigPath(explicit, { home = os.homedir(), c
|
|
|
42
42
|
throw new Error(`Saved Runner configuration location is invalid: ${locationFile}. Use --config to select a file explicitly.`);
|
|
43
43
|
}
|
|
44
44
|
if (!(await exists(filename)))
|
|
45
|
-
throw new Error(`Saved Runner configuration was moved or removed: ${filename}. Use homeagent
|
|
45
|
+
throw new Error(`Saved Runner configuration was moved or removed: ${filename}. Use homeagent --config <path> to select a new location.`);
|
|
46
46
|
return filename;
|
|
47
47
|
}
|
|
48
48
|
const standard = defaultRunnerConfigPath(home);
|
|
49
49
|
if (await exists(standard))
|
|
50
50
|
return standard;
|
|
51
|
-
throw new AppError('config_not_found', 'No Runner configuration found. Run homeagent login
|
|
51
|
+
throw new AppError('config_not_found', 'No Runner configuration found. Run homeagent to open the interactive setup (or homeagent login for scripts), or specify --config <path>.');
|
|
52
52
|
}
|
|
53
53
|
/** Remember only the absolute configuration path; credentials remain in stateDir. */
|
|
54
54
|
export async function rememberRunnerConfigPath(filename, home = os.homedir()) {
|
package/dist/runner/config.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { readFile, realpath, mkdir } from 'node:fs/promises';
|
|
1
|
+
import { readFile, realpath, mkdir, writeFile, rename, unlink } from 'node:fs/promises';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
2
3
|
import path from 'node:path';
|
|
3
4
|
import os from 'node:os';
|
|
4
5
|
import { z } from 'zod';
|
|
@@ -17,6 +18,34 @@ export function inside(root, target) {
|
|
|
17
18
|
const relative = path.relative(root, target);
|
|
18
19
|
return !relative.startsWith(`..${path.sep}`) && relative !== '..' && !path.isAbsolute(relative);
|
|
19
20
|
}
|
|
21
|
+
export function parseRunnerSettings(input) {
|
|
22
|
+
const config = runnerConfigSchema.parse(input);
|
|
23
|
+
const server = new URL(config.server);
|
|
24
|
+
if (server.protocol !== 'https:' && !(server.protocol === 'http:' && ['localhost', '127.0.0.1', '[::1]'].includes(server.hostname)))
|
|
25
|
+
throw new Error('Runner server requires HTTPS except on loopback');
|
|
26
|
+
if (server.username || server.password || server.pathname !== '/' || server.search || server.hash)
|
|
27
|
+
throw new Error('Runner server must be a plain origin');
|
|
28
|
+
return { ...config, server: server.origin };
|
|
29
|
+
}
|
|
30
|
+
/** Save connection/runtime settings only; project grants never belong here. */
|
|
31
|
+
export async function saveRunnerSettings(filename, input, create = false) {
|
|
32
|
+
const config = parseRunnerSettings(input);
|
|
33
|
+
await mkdir(path.dirname(path.resolve(filename)), { recursive: true, mode: 0o700 });
|
|
34
|
+
const text = JSON.stringify(config, null, 2) + '\n';
|
|
35
|
+
if (create) {
|
|
36
|
+
await writeFile(filename, text, { flag: 'wx', mode: 0o600 });
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const temporary = `${filename}.${randomUUID()}.tmp`;
|
|
40
|
+
try {
|
|
41
|
+
await writeFile(temporary, text, { flag: 'wx', mode: 0o600 });
|
|
42
|
+
await rename(temporary, filename);
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
await unlink(temporary).catch(error => { if (error.code !== 'ENOENT')
|
|
46
|
+
throw error; });
|
|
47
|
+
}
|
|
48
|
+
}
|
|
20
49
|
export async function loadRunnerConfig(filename, env = process.env) {
|
|
21
50
|
const input = JSON.parse(await readFile(filename, 'utf8'));
|
|
22
51
|
if (env.HOMEAGENT_SERVER)
|
|
@@ -25,13 +54,7 @@ export async function loadRunnerConfig(filename, env = process.env) {
|
|
|
25
54
|
throw new Error('ALLOWED_ROOTS is no longer supported. Configure project directories in the Dashboard.');
|
|
26
55
|
if (Object.hasOwn(input, 'projects') || Object.hasOwn(input, 'allowRemotePermissions'))
|
|
27
56
|
throw new Error('Local project configuration is no longer supported. Remove projects and allowRemotePermissions from this file; manage projects in the Dashboard.');
|
|
28
|
-
const config =
|
|
29
|
-
const server = new URL(config.server);
|
|
30
|
-
if (server.protocol !== 'https:' && !(server.protocol === 'http:' && ['localhost', '127.0.0.1', '[::1]'].includes(server.hostname)))
|
|
31
|
-
throw new Error('Runner server requires HTTPS except on loopback');
|
|
32
|
-
if (server.username || server.password || server.pathname !== '/' || server.search || server.hash)
|
|
33
|
-
throw new Error('Runner server must be a plain origin');
|
|
34
|
-
config.server = server.origin;
|
|
57
|
+
const config = parseRunnerSettings(input);
|
|
35
58
|
const configDirectory = path.dirname(path.resolve(filename));
|
|
36
59
|
const configPaths = [path.resolve(filename), await realpath(filename)];
|
|
37
60
|
config.stateDir = path.resolve(configDirectory, config.stateDir);
|
|
@@ -3,6 +3,7 @@ import { execFile } from 'node:child_process';
|
|
|
3
3
|
import { promisify } from 'node:util';
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import { randomUUID } from 'node:crypto';
|
|
6
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
6
7
|
import { z } from 'zod';
|
|
7
8
|
import { AppError } from '../shared/protocol.js';
|
|
8
9
|
import { RunnerLock } from './lock.js';
|
|
@@ -23,43 +24,49 @@ export async function loadCredential(config) {
|
|
|
23
24
|
throw new AppError('server_changed', 'Credential is pinned to another server; pair again explicitly');
|
|
24
25
|
return credential;
|
|
25
26
|
}
|
|
26
|
-
async function json(url, body) {
|
|
27
|
-
const
|
|
27
|
+
async function json(url, body, signal) {
|
|
28
|
+
const timeout = AbortSignal.timeout(15000);
|
|
29
|
+
const response = await fetch(url, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body), redirect: 'error', signal: signal ? AbortSignal.any([signal, timeout]) : timeout });
|
|
28
30
|
const value = await response.json();
|
|
29
31
|
if (!response.ok)
|
|
30
32
|
throw new Error(`Pairing failed (${response.status}): ${String(value.error ?? 'unknown')}`);
|
|
31
33
|
return { status: response.status, value };
|
|
32
34
|
}
|
|
33
|
-
export async function pair(config, force = false, log = console.log) {
|
|
35
|
+
export async function pair(config, force = false, log = console.log, options = {}) {
|
|
36
|
+
options.signal?.throwIfAborted();
|
|
34
37
|
await secureStateDirectory(config.stateDir);
|
|
35
38
|
const lock = new RunnerLock(config.stateDir);
|
|
36
39
|
try {
|
|
37
40
|
if (!force) {
|
|
38
41
|
try {
|
|
39
42
|
await loadCredential(config);
|
|
40
|
-
throw new AppError('already_paired', 'Already logged in. Use homeagent
|
|
43
|
+
throw new AppError('already_paired', 'Already logged in. Use homeagent to start, or homeagent login --force to replace the credential');
|
|
41
44
|
}
|
|
42
45
|
catch (error) {
|
|
43
46
|
if (error.code !== 'ENOENT')
|
|
44
47
|
throw error;
|
|
45
48
|
}
|
|
46
49
|
}
|
|
47
|
-
const { value } = await json(`${config.server}/pair/start`, { name: config.name });
|
|
50
|
+
const { value } = await json(`${config.server}/pair/start`, { name: config.name }, options.signal);
|
|
48
51
|
const start = z.object({ id: z.string().uuid(), code: z.string(), pollToken: z.string(), expires: z.number(), verificationUrl: z.url() }).parse(value);
|
|
52
|
+
if (new URL(start.verificationUrl).origin !== new URL(config.server).origin)
|
|
53
|
+
throw new Error('Pairing link must belong to the configured Server');
|
|
54
|
+
options.onPairing?.(start);
|
|
49
55
|
log(`\n请在已登录 HomeAgent 的浏览器打开下面的链接:\n${start.verificationUrl}\n\n核对设备名称“${config.name}”后批准。备用配对码:${start.code}\n`);
|
|
50
56
|
while (Date.now() < start.expires) {
|
|
51
|
-
await
|
|
52
|
-
const response = await json(`${config.server}/pair/claim`, { id: start.id, pollToken: start.pollToken });
|
|
57
|
+
await delay(3000, undefined, { signal: options.signal });
|
|
58
|
+
const response = await json(`${config.server}/pair/claim`, { id: start.id, pollToken: start.pollToken }, options.signal);
|
|
53
59
|
if (response.status === 202)
|
|
54
60
|
continue;
|
|
55
61
|
const credential = credentialSchema.parse({ server: config.server, ...response.value });
|
|
62
|
+
options.signal?.throwIfAborted();
|
|
56
63
|
const temporary = path.join(config.stateDir, `${randomUUID()}.credential.tmp`);
|
|
57
64
|
await writeFile(temporary, JSON.stringify(credential), { mode: 0o600, flag: 'wx' });
|
|
58
65
|
await rename(temporary, path.join(config.stateDir, 'device.json'));
|
|
59
66
|
log(`配对完成,设备 ID:${credential.deviceId}`);
|
|
60
67
|
return credential;
|
|
61
68
|
}
|
|
62
|
-
throw new Error('Login link expired; run homeagent
|
|
69
|
+
throw new Error('Login link expired; run homeagent to start the setup again');
|
|
63
70
|
}
|
|
64
71
|
finally {
|
|
65
72
|
lock.close();
|