dsh-mobile-plugin 0.5.81 → 0.5.83
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/README.md +4 -0
- package/dsh-mobile-credentials/lib/credentials.js +5 -0
- package/lib/client.js +171 -5
- package/lib/frpc/provision.cjs +21 -17
- package/lib/frpc/runner.cjs +231 -168
- package/lib/impl.js +77 -107
- package/lib/index.js +1 -1
- package/lib/mirror/pusher.cjs +60 -10
- package/lib/mobile/approval-memory.js +2 -55
- package/lib/mobile/approvals.js +6 -82
- package/lib/mobile/events.js +2 -1
- package/lib/mobile/feedback.js +6 -5
- package/lib/mobile/file-read.js +4 -2
- package/lib/mobile/host-bridge.js +12 -1
- package/lib/mobile/mux.js +148 -21
- package/lib/mobile/paired-devices.js +2 -1
- package/lib/mobile/pairing.js +8 -7
- package/lib/mobile/relay-control.js +33 -38
- package/lib/mobile/session-changes.js +8 -2
- package/lib/mobile/session-delivery.js +5 -1
- package/lib/mobile/session-file.js +46 -25
- package/lib/mobile/session-head.js +7 -3
- package/lib/mobile/session-snapshot.js +5 -1
- package/lib/mobile/session-step.js +13 -7
- package/lib/mobile/session-stop-summary.js +6 -1
- package/lib/mobile/stream-registry.js +48 -53
- package/lib/mobile/stream.js +17 -2
- package/lib/mobile/uploads.js +190 -163
- package/lib/p2p/session.cjs +169 -70
- package/lib/p2p/wiring.cjs +4 -2
- package/lib/panel/index.html +44 -24
- package/lib/relay/blob-upload.cjs +103 -0
- package/lib/relay/config-store.js +79 -0
- package/lib/relay/control.js +131 -52
- package/lib/relay/core.js +55 -17
- package/lib/relay/lan-discovery.cjs +22 -14
- package/lib/relay/lan-port-policy.cjs +8 -2
- package/lib/relay/lan-proxy.js +47 -12
- package/lib/relay/pairing-payload.cjs +14 -5
- package/lib/relay/preview-webapp.cjs +150 -0
- package/lib/relay/preview.js +35 -8
- package/lib/relay/protocol.js +6 -4
- package/lib/relay/relay-probe.js +112 -21
- package/lib/relay/status.js +4 -7
- package/lib/relay/tunnel-forwarder.cjs +4 -2
- package/lib/relay/tunnel.js +39 -8
- package/lib/remote/controller.cjs +33 -3
- package/lib/remote/host-control.cjs +11 -2
- package/lib/remote/router.cjs +83 -33
- package/lib/remote/transport.cjs +32 -5
- package/lib/remote/tunnel-helper.cjs +51 -25
- package/lib/remote/tunnels.cjs +27 -3
- package/lib/routes/control-plane.js +41 -53
- package/lib/routes/mobile-api.js +21 -31
- package/lib/shared/fresh-request.js +32 -1
- package/lib/shared/route-order.cjs +118 -132
- package/lib/shared/sse-write.js +29 -10
- package/lib/shared/via-header.js +5 -12
- package/lib/shell/transport-host.js +9 -1
- package/lib/shell/updater.js +18 -6
- package/lib/sidecar-facade-worker.cjs +165 -118
- package/lib/sidecar-facade.cjs +6 -1
- package/lib/sidecar-lifecycle.cjs +6 -2
- package/lib/tailcat/host-process.cjs +32 -8
- package/lib/tailcat/session.cjs +27 -10
- package/package.json +6 -6
- package/lib/mobile/auto-review.js +0 -178
package/lib/frpc/runner.cjs
CHANGED
|
@@ -1,61 +1,65 @@
|
|
|
1
|
+
// FRP 等待真实监听端口并跟随运行中变更,异常退出有限重试,主动停止取消待启动工作。
|
|
1
2
|
'use strict';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 在本机跑一个 frpc 进程,让面板导入的 FRP 配置真正生效。
|
|
7
|
-
*
|
|
8
|
-
* 为什么单独一层:`provision.cjs` 只负责「弄到可执行文件」,这里负责「用一份配置把它跑起来
|
|
9
|
-
* 并管住进程」。两件事分开,测试也能各测各的(这里注入假 spawn / 假 ensure,不真起进程)。
|
|
10
|
-
*
|
|
11
|
-
* 关键一步是**本地端口对齐**:面板导入的是服务商生成的配置,里面的 `localPort` 未必等于插件
|
|
12
|
-
* 此刻真正监听的端口(端口被占用会退让、用户改过也另说)。照抄会让公网请求转到一个没人的端口。
|
|
13
|
-
* 所以每次启动前都把配置里的 `localPort` 改成当前实际端口,再写盘、再起 frpc。
|
|
14
|
-
*
|
|
15
|
-
* 进程退出/起不来一律记进 `error`,不抛给调用方;`status()` 回一份可渲染的事实。frpc 自己会
|
|
16
|
-
* 对 frps 重试,进程不会因为连不上服务器就退出,所以这里不做进程级自动重启。退出原因取自
|
|
17
|
-
* 两路输出的尾部(frpc 把致命原因写在 stdout),且等 `close` 收全流再拼,别让人只看到退出码。
|
|
18
|
-
*/
|
|
19
|
-
|
|
4
|
+
/** 本机 frpc 的配置准备、进程起停和服务端分配地址读取。 */
|
|
20
5
|
const fs = require('node:fs');
|
|
21
6
|
const path = require('node:path');
|
|
22
7
|
const { spawn } = require('node:child_process');
|
|
23
|
-
|
|
24
8
|
const { dshHome } = require('../shared/paths.js');
|
|
25
9
|
const { ensureFrpc } = require('./provision.cjs');
|
|
26
|
-
|
|
27
|
-
/** 本机 frpc 管理接口的默认端口:只监听回环,用来读它实际分到的公网地址(remote_addr)。 */
|
|
28
10
|
const DEFAULT_ADMIN_PORT = 17401;
|
|
11
|
+
const RESTART_DELAYS_MS = [1000, 2000, 4000, 8000, 16000];
|
|
12
|
+
|
|
13
|
+
/** INI 仍可在面板识别地址,但当前 frpc 不接受它作为运行配置。 */
|
|
14
|
+
function isIniConfig(text) {
|
|
15
|
+
return /^\s*\[common\]\s*$/im.test(String(text || '')) || /^\s*server_addr\s*=/im.test(String(text || ''));
|
|
16
|
+
}
|
|
29
17
|
|
|
30
|
-
/** 把配置里每一处 `localPort / local_port` 改成实际端口,其余原样。 */
|
|
31
18
|
function alignLocalPort(text, port) {
|
|
19
|
+
const source = String(text || '');
|
|
32
20
|
const value = Number(port);
|
|
33
|
-
if (!Number.isInteger(value) || value <= 0) return
|
|
34
|
-
|
|
21
|
+
if (!Number.isInteger(value) || value <= 0) return source;
|
|
22
|
+
if (source.trimStart().startsWith('{')) {
|
|
23
|
+
const config = JSON.parse(source);
|
|
24
|
+
if (Array.isArray(config.proxies)) {
|
|
25
|
+
for (const proxy of config.proxies) {
|
|
26
|
+
if (proxy && typeof proxy === 'object') {
|
|
27
|
+
proxy.localPort = value;
|
|
28
|
+
if ('local_port' in proxy) delete proxy.local_port;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return JSON.stringify(config, null, 2);
|
|
33
|
+
}
|
|
34
|
+
return source.replace(/^(\s*local(?:Port|_port)\s*[=:]\s*)["']?\d+["']?/gim, `$1${value}`);
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
/**
|
|
38
|
-
* 给配置补一个本机管理接口(`webServer`),好让插件能读到 frpc 实际分到的公网地址。
|
|
39
|
-
*
|
|
40
|
-
* 服务商配置常常不写 `remotePort`(由服务端分配),地址只能从 frpc 的管理接口读。用户配置里
|
|
41
|
-
* 已经有 `webServer` 就不动它,免得把人家自己的设置覆盖掉。只对带 `[[proxies]]` 的 TOML 生效
|
|
42
|
-
* (INI 没这类顶层写法,跳过)。
|
|
43
|
-
*/
|
|
37
|
+
/** 管理接口只在未配置时补齐;JSON 保持 JSON,并使用相应的配置扩展名。 */
|
|
44
38
|
function withAdminPort(text, port) {
|
|
45
39
|
const source = String(text || '');
|
|
46
|
-
if (
|
|
40
|
+
if (source.trimStart().startsWith('{')) {
|
|
41
|
+
const config = JSON.parse(source);
|
|
42
|
+
if (!config.webServer) config.webServer = { addr: '127.0.0.1', port };
|
|
43
|
+
return JSON.stringify(config, null, 2);
|
|
44
|
+
}
|
|
45
|
+
if (/(?:^|\n)\s*(?:webServer\s*[.=]|\[\s*webServer\s*\])/i.test(source)) return source;
|
|
47
46
|
if (!/\[\[\s*proxies\s*\]\]/i.test(source)) return source;
|
|
48
|
-
|
|
49
|
-
|
|
47
|
+
return `webServer.addr = "127.0.0.1"\nwebServer.port = ${port}\n\n${source}`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 从当前运行配置读取管理端口,支持 TOML 点键/表和 JSON,用户值优先。 */
|
|
51
|
+
function adminPortOf(text, fallback) {
|
|
52
|
+
const source = String(text || '');
|
|
53
|
+
if (source.trimStart().startsWith('{')) {
|
|
54
|
+
try { return Number(JSON.parse(source).webServer?.port) || fallback; } catch { return fallback; }
|
|
55
|
+
}
|
|
56
|
+
const dotted = /^\s*webServer\.port\s*=\s*(\d+)/im.exec(source);
|
|
57
|
+
if (dotted) return Number(dotted[1]);
|
|
58
|
+
const table = /(?:^|\n)\s*\[\s*webServer\s*\]\s*\r?\n([\s\S]*?)(?=\n\s*\[|$)/i.exec(source);
|
|
59
|
+
const entry = table && /^\s*port\s*=\s*(\d+)/im.exec(table[1]);
|
|
60
|
+
return entry ? Number(entry[1]) : fallback;
|
|
50
61
|
}
|
|
51
62
|
|
|
52
|
-
/**
|
|
53
|
-
* 把「进程起不来」的底层报错翻成用户能照着排查的话。
|
|
54
|
-
*
|
|
55
|
-
* 最常见的两种:Windows 上安全软件不让跑刚下载的 exe(Node 报 `spawn UNKNOWN`),以及文件
|
|
56
|
-
* 根本不是这台机器能执行的东西(`ENOEXEC`、架构不符)。这两种在 Node 里都只是一句干巴巴的
|
|
57
|
-
* `spawn UNKNOWN`,直接甩给用户等于什么也没说——所以带上文件位置,并指明该往哪查。
|
|
58
|
-
*/
|
|
59
63
|
function describeSpawnFailure(err, binary) {
|
|
60
64
|
const code = err && err.code ? String(err.code) : '';
|
|
61
65
|
const raw = err && err.message ? err.message : String(err || '未知错误');
|
|
@@ -63,83 +67,78 @@ function describeSpawnFailure(err, binary) {
|
|
|
63
67
|
if (code === 'UNKNOWN') {
|
|
64
68
|
return `系统拒绝运行 frpc${where}:多半是安全软件(如 Windows Defender)拦了这个程序,或下载到的版本与本机芯片不符。请把它加入杀毒/安全软件的信任名单,或换成与本机匹配的 frpc 版本。`;
|
|
65
69
|
}
|
|
66
|
-
if (code === 'ENOEXEC') {
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
if (code === 'EACCES' || code === 'EPERM') {
|
|
70
|
-
return `系统不允许运行 frpc${where}:权限被拒。请检查这个文件是否被禁止执行或被安全策略拦截。`;
|
|
71
|
-
}
|
|
72
|
-
if (code === 'ENOENT') {
|
|
73
|
-
return `找不到 frpc 可执行文件${where}:它可能已被删除或路径失效,请重新获取。`;
|
|
74
|
-
}
|
|
70
|
+
if (code === 'ENOEXEC') return `frpc${where} 不是这台机器能执行的程序:文件可能已损坏,或版本/架构不对。请重新获取一份与本机匹配的 frpc。`;
|
|
71
|
+
if (code === 'EACCES' || code === 'EPERM') return `系统不允许运行 frpc${where}:权限被拒。请检查这个文件是否被禁止执行或被安全策略拦截。`;
|
|
72
|
+
if (code === 'ENOENT') return `找不到 frpc 可执行文件${where}:它可能已被删除或路径失效,请重新获取。`;
|
|
75
73
|
return `启动 frpc 失败:${raw}`;
|
|
76
74
|
}
|
|
77
75
|
|
|
78
76
|
function frpcPaths(root) {
|
|
79
|
-
//
|
|
80
|
-
// 装好新二进制后会清掉那里除版本号外的条目——放一起会把正在用的 frpc.toml 一起删掉。
|
|
77
|
+
// 与 provision 缓存分开,避免换二进制时清掉运行配置。
|
|
81
78
|
const dir = root || path.join(dshHome(), 'frpc-run');
|
|
82
|
-
return { dir, config: path.join(dir, 'frpc.toml'), enabled: path.join(dir, 'frpc.enabled') };
|
|
79
|
+
return { dir, config: path.join(dir, 'frpc.toml'), jsonConfig: path.join(dir, 'frpc.json'), enabled: path.join(dir, 'frpc.enabled') };
|
|
83
80
|
}
|
|
84
81
|
|
|
85
|
-
/**
|
|
86
|
-
* 造一个 frpc 运行器。
|
|
87
|
-
*
|
|
88
|
-
* options(都可注入,测试用):
|
|
89
|
-
* root 缓存/配置目录(缺省 `<DSH 数据目录>/frpc`)
|
|
90
|
-
* portOf `() => number`:当前插件实际监听端口(启动前用它改 localPort)
|
|
91
|
-
* ensure `(opts) => Promise<{ok,path,message}>`:拿 frpc 可执行文件(缺省 provision.ensureFrpc)
|
|
92
|
-
* spawnImpl 子进程工厂(缺省 node:child_process.spawn)
|
|
93
|
-
* fsImpl 文件系统(缺省 node:fs,测试可造假)
|
|
94
|
-
*/
|
|
95
82
|
function createFrpcRunner(options = {}) {
|
|
96
83
|
const paths = frpcPaths(options.root);
|
|
97
84
|
const ensure = typeof options.ensure === 'function' ? options.ensure : ensureFrpc;
|
|
98
85
|
const spawnImpl = typeof options.spawnImpl === 'function' ? options.spawnImpl : spawn;
|
|
99
86
|
const portOf = typeof options.portOf === 'function' ? options.portOf : () => null;
|
|
100
87
|
const fsImpl = options.fsImpl || fs;
|
|
101
|
-
const fetchImpl = options.fetchImpl ||
|
|
88
|
+
const fetchImpl = options.fetchImpl || globalThis.fetch;
|
|
102
89
|
const adminPort = Number.isInteger(options.adminPort) ? options.adminPort : DEFAULT_ADMIN_PORT;
|
|
103
|
-
|
|
90
|
+
const setTimer = options.setTimeoutImpl || setTimeout;
|
|
91
|
+
const clearTimer = options.clearTimeoutImpl || clearTimeout;
|
|
104
92
|
let child = null;
|
|
105
93
|
let binary = '';
|
|
106
94
|
let error = '';
|
|
107
95
|
let startedAt = 0;
|
|
108
|
-
/** frpc 连上后服务端分给这条隧道的公网地址(`remote_addr`);没读到就是空串。 */
|
|
109
96
|
let remoteAddress = '';
|
|
110
|
-
|
|
97
|
+
let desired = false;
|
|
98
|
+
let generation = 0;
|
|
99
|
+
let starting = null;
|
|
100
|
+
let retryTimer = null;
|
|
101
|
+
let retries = 0;
|
|
102
|
+
let activeAdminPort = adminPort;
|
|
103
|
+
let requestedLocalPort = 0;
|
|
104
|
+
let runningLocalPort = 0;
|
|
105
|
+
let waitingForPort = false;
|
|
106
|
+
let discovery = null;
|
|
107
|
+
let addressListener = typeof options.onRemoteAddress === 'function' ? options.onRemoteAddress : null;
|
|
111
108
|
const exists = (p) => { try { return fsImpl.existsSync(p); } catch { return false; } };
|
|
112
|
-
const
|
|
109
|
+
const configPath = () => exists(paths.jsonConfig) ? paths.jsonConfig : paths.config;
|
|
110
|
+
const readConfig = () => { try { return fsImpl.readFileSync(configPath(), 'utf8'); } catch { return null; } };
|
|
111
|
+
const localPort = () => {
|
|
112
|
+
const port = Number(portOf());
|
|
113
|
+
return Number.isInteger(port) && port >= 1024 && port <= 65535 ? port : 0;
|
|
114
|
+
};
|
|
113
115
|
|
|
114
116
|
function status() {
|
|
115
|
-
const running = Boolean(child);
|
|
116
117
|
return {
|
|
117
|
-
available: Boolean(binary),
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
startedAt,
|
|
122
|
-
configPresent: exists(paths.config),
|
|
123
|
-
remoteAddress: running ? remoteAddress : '',
|
|
118
|
+
available: Boolean(binary), running: Boolean(child), pid: child && child.pid ? child.pid : null,
|
|
119
|
+
error, startedAt, configPresent: exists(paths.config) || exists(paths.jsonConfig),
|
|
120
|
+
enabled: exists(paths.enabled), starting: Boolean(starting && !child), retrying: retryTimer !== null,
|
|
121
|
+
waitingForPort, localPort: child ? runningLocalPort : 0, remoteAddress: child ? remoteAddress : '',
|
|
124
122
|
};
|
|
125
123
|
}
|
|
126
124
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const prepared = withAdminPort(alignLocalPort(text,
|
|
125
|
+
function writeRunConfig(text, port = localPort()) {
|
|
126
|
+
if (isIniConfig(text)) throw new Error('旧 INI 格式不能运行,请让服务商提供 TOML 或 JSON 配置');
|
|
127
|
+
const prepared = withAdminPort(alignLocalPort(text, port), adminPort);
|
|
128
|
+
const json = prepared.trimStart().startsWith('{');
|
|
129
|
+
const target = json ? paths.jsonConfig : paths.config;
|
|
130
130
|
fsImpl.mkdirSync(paths.dir, { recursive: true });
|
|
131
|
-
fsImpl.writeFileSync(
|
|
132
|
-
|
|
131
|
+
fsImpl.writeFileSync(target, prepared);
|
|
132
|
+
fsImpl.rmSync(json ? paths.config : paths.jsonConfig, { force: true });
|
|
133
|
+
return target;
|
|
133
134
|
}
|
|
134
135
|
|
|
135
|
-
/** 保存导入的配置(localPort 对齐、补管理接口)并记下「启用」,重启宿主后自动再拉起。 */
|
|
136
136
|
function saveConfig(text) {
|
|
137
|
-
writeRunConfig(text);
|
|
138
|
-
|
|
139
|
-
return
|
|
137
|
+
const target = writeRunConfig(text);
|
|
138
|
+
fsImpl.writeFileSync(paths.enabled, '1');
|
|
139
|
+
return target;
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
-
/** 从管理接口的 `/api/status` 里挑出第一条代理的 `remote_addr`。 */
|
|
143
142
|
function pickRemote(payload) {
|
|
144
143
|
if (!payload || typeof payload !== 'object') return '';
|
|
145
144
|
for (const group of Object.values(payload)) {
|
|
@@ -152,112 +151,176 @@ function createFrpcRunner(options = {}) {
|
|
|
152
151
|
return '';
|
|
153
152
|
}
|
|
154
153
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
154
|
+
async function readRemoteAddress(timeoutMs = 8000) {
|
|
155
|
+
if (typeof fetchImpl !== 'function') return '';
|
|
156
|
+
const token = generation;
|
|
157
|
+
const proc = child;
|
|
158
|
+
const deadline = performance.now() + Math.max(500, Number(timeoutMs) || 8000);
|
|
159
|
+
const url = `http://127.0.0.1:${activeAdminPort}/api/status`;
|
|
160
|
+
while (performance.now() < deadline) {
|
|
161
|
+
if (!proc || child !== proc || generation !== token) return '';
|
|
162
|
+
const controller = new AbortController();
|
|
163
|
+
const timer = setTimeout(() => controller.abort(), Math.min(1500, Math.max(1, deadline - performance.now())));
|
|
165
164
|
try {
|
|
166
|
-
const
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
if (
|
|
165
|
+
const response = await fetchImpl(url, { signal: controller.signal });
|
|
166
|
+
const addr = response && response.ok ? pickRemote(await response.json()) : '';
|
|
167
|
+
if (child !== proc || generation !== token) return '';
|
|
168
|
+
if (addr) {
|
|
169
|
+
remoteAddress = addr;
|
|
170
|
+
const isCurrent = () => desired && child === proc && generation === token;
|
|
171
|
+
if (addressListener && isCurrent()) await addressListener(addr, isCurrent);
|
|
172
|
+
return isCurrent() ? addr : '';
|
|
173
173
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
if (addr) { remoteAddress = addr; return addr; }
|
|
177
|
-
}
|
|
178
|
-
} catch { /* 还没起来,接着轮 */ }
|
|
174
|
+
} catch { /* 管理接口还没起来,继续等。 */ }
|
|
175
|
+
finally { clearTimeout(timer); }
|
|
179
176
|
await new Promise((resolve) => setTimeout(resolve, 350));
|
|
180
177
|
}
|
|
181
178
|
return '';
|
|
182
179
|
}
|
|
183
180
|
|
|
184
|
-
|
|
181
|
+
function discoverRemoteAddress(timeoutMs = 8000) {
|
|
182
|
+
if (discovery && discovery.proc === child) return discovery.promise;
|
|
183
|
+
const entry = { proc: child, promise: null };
|
|
184
|
+
entry.promise = readRemoteAddress(timeoutMs).finally(() => { if (discovery === entry) discovery = null; });
|
|
185
|
+
discovery = entry;
|
|
186
|
+
return entry.promise;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function scheduleRestart() {
|
|
190
|
+
if (!desired || !exists(paths.enabled) || retryTimer !== null || retries >= RESTART_DELAYS_MS.length) return;
|
|
191
|
+
const token = generation;
|
|
192
|
+
retryTimer = setTimer(() => {
|
|
193
|
+
retryTimer = null;
|
|
194
|
+
if (desired && generation === token && exists(paths.enabled)) void launch();
|
|
195
|
+
}, RESTART_DELAYS_MS[retries++]);
|
|
196
|
+
if (retryTimer && typeof retryTimer.unref === 'function') retryTimer.unref();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async function launch() {
|
|
185
200
|
if (child) return status();
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
201
|
+
if (starting) { await starting; return status(); }
|
|
202
|
+
const token = ++generation;
|
|
203
|
+
const run = (async () => {
|
|
204
|
+
const raw = readConfig();
|
|
205
|
+
if (!raw) { error = '还没有导入 frpc 配置'; return; }
|
|
206
|
+
requestedLocalPort = localPort();
|
|
207
|
+
waitingForPort = requestedLocalPort === 0;
|
|
208
|
+
if (waitingForPort) { error = ''; return; }
|
|
209
|
+
let got;
|
|
210
|
+
try { got = await ensure({}); }
|
|
211
|
+
catch (err) { got = { ok: false, message: err && err.message ? err.message : String(err) }; }
|
|
212
|
+
if (!desired || generation !== token) return;
|
|
213
|
+
if (!got || got.ok !== true) {
|
|
214
|
+
error = got && got.message ? got.message : '没拿到 frpc 可执行文件';
|
|
215
|
+
scheduleRestart();
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
// 获取二进制期间监听可能尚未就绪或已经换号,启动前只取这一刻的实绑端口。
|
|
219
|
+
const port = localPort();
|
|
220
|
+
requestedLocalPort = port;
|
|
221
|
+
waitingForPort = port === 0;
|
|
222
|
+
if (waitingForPort) { error = ''; return; }
|
|
223
|
+
let target;
|
|
224
|
+
try { target = writeRunConfig(raw, port); }
|
|
225
|
+
catch (err) { error = `写 frpc 配置失败:${err && err.message ? err.message : err}`; return; }
|
|
226
|
+
binary = got.path;
|
|
227
|
+
let proc;
|
|
228
|
+
try {
|
|
229
|
+
proc = spawnImpl(binary, ['-c', target], { stdio: ['ignore', 'pipe', 'pipe'], windowsHide: true });
|
|
230
|
+
} catch (err) {
|
|
231
|
+
error = describeSpawnFailure(err, binary);
|
|
232
|
+
scheduleRestart();
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
child = proc;
|
|
236
|
+
runningLocalPort = port;
|
|
237
|
+
activeAdminPort = adminPortOf(readConfig(), adminPort);
|
|
238
|
+
error = '';
|
|
239
|
+
remoteAddress = '';
|
|
240
|
+
startedAt = Date.now();
|
|
241
|
+
let tail = '';
|
|
242
|
+
for (const stream of [proc.stdout, proc.stderr]) {
|
|
243
|
+
if (!stream || typeof stream.on !== 'function') continue;
|
|
244
|
+
if (typeof stream.setEncoding === 'function') stream.setEncoding('utf8');
|
|
245
|
+
stream.on('data', (chunk) => { tail = (tail + String(chunk)).slice(-1500); });
|
|
246
|
+
}
|
|
247
|
+
if (typeof proc.on === 'function') {
|
|
248
|
+
proc.on('error', (err) => { if (child === proc) error = describeSpawnFailure(err, binary); });
|
|
249
|
+
// close 时两路输出已收全,保留 frpc 自己给的退出原因。
|
|
250
|
+
proc.on('close', (code, signal) => {
|
|
251
|
+
if (child !== proc) return;
|
|
252
|
+
child = null;
|
|
253
|
+
runningLocalPort = 0;
|
|
254
|
+
remoteAddress = '';
|
|
255
|
+
const lastLine = tail.trim() ? tail.trim().split('\n').pop() : '';
|
|
256
|
+
error = `frpc 已退出(code=${code}${signal ? `, ${signal}` : ''})${lastLine ? `:${lastLine}` : ''}`;
|
|
257
|
+
scheduleRestart();
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
// 每代启动都读取实际分配地址;回调拿代次谓词,停止或换代后不能写回旧地址。
|
|
261
|
+
if (addressListener) void discoverRemoteAddress().catch(() => '');
|
|
262
|
+
})();
|
|
263
|
+
starting = run;
|
|
264
|
+
try { await run; }
|
|
265
|
+
finally { if (starting === run) starting = null; }
|
|
232
266
|
return status();
|
|
233
267
|
}
|
|
234
268
|
|
|
235
|
-
async function
|
|
236
|
-
if (!
|
|
269
|
+
async function start() {
|
|
270
|
+
if (!desired) { desired = true; retries = 0; }
|
|
271
|
+
if (retryTimer !== null) { clearTimer(retryTimer); retryTimer = null; }
|
|
272
|
+
return launch();
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function stopProcess() {
|
|
276
|
+
generation += 1;
|
|
277
|
+
if (retryTimer !== null) clearTimer(retryTimer);
|
|
278
|
+
retryTimer = null;
|
|
279
|
+
starting = null;
|
|
237
280
|
const proc = child;
|
|
238
281
|
child = null;
|
|
239
|
-
|
|
282
|
+
runningLocalPort = 0;
|
|
283
|
+
remoteAddress = '';
|
|
240
284
|
error = '';
|
|
285
|
+
if (proc) { try { proc.kill('SIGTERM'); } catch { /* 已退出。 */ } }
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** 监听状态变化时同步;不会越过主动停止,也不为同一个端口重启。 */
|
|
289
|
+
async function syncLocalPort() {
|
|
290
|
+
if (!desired) return status();
|
|
291
|
+
const port = localPort();
|
|
292
|
+
if (!port) { waitingForPort = !child; return status(); }
|
|
293
|
+
if (port === requestedLocalPort && !waitingForPort) return status();
|
|
294
|
+
stopProcess();
|
|
295
|
+
retries = 0;
|
|
296
|
+
return launch();
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
async function stop() {
|
|
300
|
+
desired = false;
|
|
301
|
+
stopProcess();
|
|
302
|
+
requestedLocalPort = 0;
|
|
303
|
+
waitingForPort = false;
|
|
241
304
|
return status();
|
|
242
305
|
}
|
|
243
306
|
|
|
244
307
|
return {
|
|
245
|
-
status,
|
|
246
|
-
|
|
247
|
-
|
|
308
|
+
status, start, stop, syncLocalPort, saveConfig, discoverRemoteAddress,
|
|
309
|
+
get runId() { return generation; },
|
|
310
|
+
setAddressListener(listener) {
|
|
311
|
+
addressListener = typeof listener === 'function' ? listener : null;
|
|
312
|
+
if (child && addressListener) void discoverRemoteAddress().catch(() => '');
|
|
313
|
+
},
|
|
248
314
|
async restart() { await stop(); return start(); },
|
|
249
|
-
saveConfig,
|
|
250
|
-
discoverRemoteAddress,
|
|
251
|
-
/** 停掉进程并清掉「启用」标记(用户主动断开时用)。 */
|
|
252
315
|
async clear() {
|
|
253
|
-
|
|
254
|
-
|
|
316
|
+
await stop();
|
|
317
|
+
fsImpl.rmSync(paths.enabled, { force: true });
|
|
318
|
+
return status();
|
|
255
319
|
},
|
|
256
|
-
|
|
257
|
-
shouldAutoStart() { return exists(paths.enabled) && exists(paths.config); },
|
|
320
|
+
shouldAutoStart() { return exists(paths.enabled) && (exists(paths.config) || exists(paths.jsonConfig)); },
|
|
258
321
|
dispose() { return stop(); },
|
|
259
322
|
_paths: paths,
|
|
260
323
|
};
|
|
261
324
|
}
|
|
262
325
|
|
|
263
|
-
module.exports = { createFrpcRunner, alignLocalPort, withAdminPort, frpcPaths };
|
|
326
|
+
module.exports = { createFrpcRunner, alignLocalPort, withAdminPort, frpcPaths, isIniConfig, adminPortOf };
|