dsh-mobile-plugin 0.5.81 → 0.5.82
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/lib/frpc/provision.cjs +21 -17
- package/lib/frpc/runner.cjs +193 -165
- package/lib/impl.js +53 -80
- package/lib/mirror/pusher.cjs +60 -10
- 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/mux.js +148 -21
- package/lib/mobile/paired-devices.js +2 -1
- package/lib/mobile/pairing.js +3 -2
- package/lib/mobile/relay-control.js +27 -33
- 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 +17 -15
- package/lib/relay/blob-upload.cjs +103 -0
- package/lib/relay/config-store.js +81 -0
- package/lib/relay/control.js +94 -46
- package/lib/relay/core.js +9 -13
- package/lib/relay/lan-proxy.js +44 -7
- 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/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 +25 -17
- package/lib/routes/mobile-api.js +21 -18
- 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/sidecar-facade-worker.cjs +156 -112
- 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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//
|
|
1
|
+
// 远程电脑状态保留环境诊断,助手换代主动撤下隧道并恢复现有访问。
|
|
2
2
|
// 2026-11-19:桌面窗口改成多窗口集合(只有最后一个窗口退出才回到未打开),进程还在就显示正在打开 /
|
|
3
3
|
// 已打开;换网按 lost 与非 lost 分流,lost 只断开已打开设备的连接、不改状态也不探活,非 lost 让所有
|
|
4
4
|
// 绑定(含没打开过的)都重选线路;connect 回调记下 connection.route 作为「实际在用线路」供面板展示;
|
|
@@ -18,7 +18,11 @@ const REMOTE_ERROR_TEXT = {
|
|
|
18
18
|
REMOTE_REVOKED: '这台电脑的授权已被撤销,请重新绑定',
|
|
19
19
|
REMOTE_VERSION: '对方插件版本不兼容,请升级到支持远程电脑的版本',
|
|
20
20
|
REMOTE_EXPIRED: '授权已到期,请重新打开',
|
|
21
|
-
REMOTE_TIMEOUT: '
|
|
21
|
+
REMOTE_TIMEOUT: '连接远程电脑超时,请检查对方在线状态;受限网络也可能需要代理或先完成网络登录',
|
|
22
|
+
REMOTE_PROXY: '网络返回代理认证要求,请完成代理配置或更换网络后重试',
|
|
23
|
+
REMOTE_PROXY_CONFIGURED: '本机配置了出站代理,当前远程连接使用直连;请确认网络允许直连或更换网络',
|
|
24
|
+
REMOTE_INTERCEPTED: '连接收到了网页而非远程协议,请检查网络登录页或线路地址',
|
|
25
|
+
REMOTE_HELPER_RESTARTING: '本机隧道助手正在恢复,请稍后重试',
|
|
22
26
|
REMOTE_CONNECT: '暂时无法连接远程电脑,请检查网络和对方是否在线',
|
|
23
27
|
REMOTE_UNAVAILABLE: '对方电脑暂时不可用,请确认它已打开 DSH 后重试',
|
|
24
28
|
REMOTE_NOT_FOUND: '找不到这台远程电脑,它可能已被删除,请刷新后重试',
|
|
@@ -124,6 +128,32 @@ function createRemoteController({ store, transport, name, tunnels, onBindingsCha
|
|
|
124
128
|
item.router = routerFor(item);
|
|
125
129
|
}
|
|
126
130
|
|
|
131
|
+
const unsubscribeTunnels = tunnels?.onChange?.(state => {
|
|
132
|
+
if (closed) return;
|
|
133
|
+
for (const item of devices.values()) {
|
|
134
|
+
const hadTunnel = ['tunnel', 'p2p'].includes(item.activeRoute?.channel || item.router.snapshot().route?.channel);
|
|
135
|
+
if (!state.ready) {
|
|
136
|
+
item.helperRecovery = Boolean(item.helperRecovery || check(item)
|
|
137
|
+
&& item.router.snapshot().routes.some(route => ['tunnel', 'p2p'].includes(route.channel)));
|
|
138
|
+
item.router.invalidateTunnels();
|
|
139
|
+
if (hadTunnel) {
|
|
140
|
+
item.gateway?.disconnect?.(); item.activeRoute = null;
|
|
141
|
+
item.lastErrorCode = 'REMOTE_HELPER_RESTARTING';
|
|
142
|
+
item.lastError = REMOTE_ERROR_TEXT.REMOTE_HELPER_RESTARTING;
|
|
143
|
+
if (check(item)) item.state = 'connecting';
|
|
144
|
+
}
|
|
145
|
+
} else if (item.helperRecovery && check(item) && !['expired', 'revoked'].includes(item.state)) {
|
|
146
|
+
item.helperRecovery = false;
|
|
147
|
+
const router = item.router;
|
|
148
|
+
void router.refresh(true).then(() => router.probe()).then(() => {
|
|
149
|
+
if (!closed && item.router === router && check(item)) {
|
|
150
|
+
item.state = 'connected'; item.lastError = ''; item.lastErrorCode = '';
|
|
151
|
+
}
|
|
152
|
+
}).catch(error => { if (!closed && item.router === router && check(item)) fault(item, error); });
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
|
|
127
157
|
const network = createNetworkMonitor({
|
|
128
158
|
async onTick(time) {
|
|
129
159
|
const active = [...devices.values()].filter((item) => check(item));
|
|
@@ -272,7 +302,7 @@ function createRemoteController({ store, transport, name, tunnels, onBindingsCha
|
|
|
272
302
|
},
|
|
273
303
|
async close() {
|
|
274
304
|
if (closed) return;
|
|
275
|
-
closed = true; network.close();
|
|
305
|
+
closed = true; network.close(); unsubscribeTunnels?.();
|
|
276
306
|
await Promise.all([...devices.values()].map(async (item) => {
|
|
277
307
|
item.gateway?.close(); item.gateway = null; item.router.close();
|
|
278
308
|
await tunnels?.close?.(item.binding.id);
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
// IPv6 候选按真实监听端口生成,独立于 IPv4 局域网候选开关。
|
|
1
2
|
'use strict';
|
|
2
3
|
// 电脑间已认证控制信令只提供隧道会合,目标固定为本机插件入口。
|
|
3
4
|
const { relayAddressesOf } = require('../relay/core.js');
|
|
4
|
-
const { advertisedCandidates, phoneCandidates, ipv6Candidates, withManualIpv6Candidate } = require('../relay/lan-network.cjs');
|
|
5
|
+
const { advertisedCandidates, phoneCandidates, ipv6Candidates, withManualIpv6Candidate, tcpDirectTarget } = require('../relay/lan-network.cjs');
|
|
5
6
|
|
|
6
7
|
function routesOf({ state, lanPortInUse, lanAvailable = () => state.lanEnabled, interfaces }) {
|
|
7
8
|
const routes = [];
|
|
@@ -11,12 +12,20 @@ function routesOf({ state, lanPortInUse, lanAvailable = () => state.lanEnabled,
|
|
|
11
12
|
if (!['http:', 'https:'].includes(parsed.protocol) || parsed.username || parsed.password || parsed.search || parsed.hash) return;
|
|
12
13
|
routes.push({ url: parsed.href.replace(/\/$/, ''), channel });
|
|
13
14
|
};
|
|
15
|
+
const port = Number(lanPortInUse());
|
|
14
16
|
if (lanAvailable()) {
|
|
15
|
-
const port = lanPortInUse();
|
|
16
17
|
for (const item of phoneCandidates(advertisedCandidates(port, interfaces), { manual: state.manualLanUrl })) push(item.url, 'lan');
|
|
18
|
+
}
|
|
19
|
+
// lanPortInUse 为 0 表示没有监听,不能把配置端口或中转回环口发布成 v6 直连。
|
|
20
|
+
if (Number.isInteger(port) && port > 0) {
|
|
17
21
|
for (const item of withManualIpv6Candidate(ipv6Candidates(port, interfaces), state.manualIpv6Url)) push(item.url, 'ipv6');
|
|
18
22
|
}
|
|
19
23
|
for (const url of relayAddressesOf(state).relayUrls) push(url, 'relay');
|
|
24
|
+
// 「TCP 直连」目标:公网可达、手机靠裸 TCP 拨的那条,电脑对电脑同样能走(明文 http 转发到本机入口)。
|
|
25
|
+
// 与自建中转 / 直连同属「公网可达的自建」,配了之后公用中转会被停用(见 relay/core.publicRelayBlocked),
|
|
26
|
+
// 所以这里必须把它也当候选,否则远程电脑反而少一条可用的公网线路。
|
|
27
|
+
const tcp = tcpDirectTarget(state.manualTcpUrl);
|
|
28
|
+
if (tcp) push(`http://${tcp}`, 'tcp');
|
|
20
29
|
return routes;
|
|
21
30
|
}
|
|
22
31
|
function candidates(value) {
|
package/lib/remote/router.cjs
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
//
|
|
1
|
+
// 取消/关停不带错误码时用 REMOTE_CONNECT,别让 DOMException 的数字 code(20) 漏给调用方。
|
|
2
|
+
// 每台电脑按scope独立记选路证据,常态轮次与认证探测、真实业务结果分账。
|
|
3
|
+
// 助手退出立即撤下旧回环候选,诊断保留真实失败证据。
|
|
2
4
|
// 连接预算不写死:升级阶段按该线路的实测耗时(没有就用宽松默认)给上限,握手期间只要还在
|
|
3
5
|
// 收到消息就继续等;失败只影响先试谁,不缩短愿意等的时间。
|
|
4
6
|
// 2026-11-19:换网后没有当前线路时按通道各取一条候选(中转必进)、候选并发各探一发先答者用;
|
|
@@ -25,6 +27,8 @@ const backoffThresholdOf = (channel) => ROUTE_CHANNELS.find((row) => row.channel
|
|
|
25
27
|
const budgetOf = (channel) => ROUTE_CHANNELS.find((row) => row.channel === channel)?.budget;
|
|
26
28
|
const keyOf = (route) => `${route.channel}|${route.url}`;
|
|
27
29
|
const terminal = (error) => ['REMOTE_REVOKED', 'REMOTE_EXPIRED'].includes(error?.code);
|
|
30
|
+
const diagnostic = error => ['REMOTE_PROXY', 'REMOTE_INTERCEPTED', 'REMOTE_PROXY_CONFIGURED'].includes(error?.code);
|
|
31
|
+
const preferredError = (current, next) => terminal(next) || !current || diagnostic(next) && !diagnostic(current) ? next : current;
|
|
28
32
|
|
|
29
33
|
// 轮转切一段候选:每一轮从游标往后取,保证别的候选迟早各轮到一次。
|
|
30
34
|
function rotated(list, offset) {
|
|
@@ -35,6 +39,7 @@ function rotated(list, offset) {
|
|
|
35
39
|
|
|
36
40
|
function createRemoteRouter({ binding, transport, tunnels, now = Date.now, onError = () => {}, onRoutes = async () => {} }) {
|
|
37
41
|
const policy = createRoutePolicy();
|
|
42
|
+
const scope = binding.instanceId || binding.id || '';
|
|
38
43
|
const accounts = new Map();
|
|
39
44
|
let routes = safeRoutes(binding.routes);
|
|
40
45
|
let selected = null;
|
|
@@ -42,9 +47,10 @@ function createRemoteRouter({ binding, transport, tunnels, now = Date.now, onErr
|
|
|
42
47
|
let probing = null;
|
|
43
48
|
let refreshing = null;
|
|
44
49
|
let generation = 0;
|
|
50
|
+
let tunnelGeneration = 0;
|
|
45
51
|
let closed = false;
|
|
46
52
|
let probeCursor = 0;
|
|
47
|
-
const pending = new
|
|
53
|
+
const pending = new Map();
|
|
48
54
|
|
|
49
55
|
async function connectionInfo(info) {
|
|
50
56
|
if (closed || !Array.isArray(info?.routes)) return;
|
|
@@ -68,19 +74,33 @@ function createRemoteRouter({ binding, transport, tunnels, now = Date.now, onErr
|
|
|
68
74
|
item.failures = Math.min(64, item.failures + 1);
|
|
69
75
|
const threshold = backoffThresholdOf(route.channel);
|
|
70
76
|
if (item.failures >= threshold) item.until = now() + Math.min(30000, 1000 * 2 ** Math.min(5, item.failures - threshold));
|
|
71
|
-
if (penalize) policy.noteRouteFailure(route.url);
|
|
72
|
-
|
|
77
|
+
if (penalize) policy.noteRouteFailure(route.url, scope);
|
|
78
|
+
// REMOTE_AUTH 也可能是凭据或解密失败,不能据此断言地址属于另一台电脑。
|
|
73
79
|
}
|
|
74
80
|
|
|
75
|
-
function proven(route) {
|
|
81
|
+
function proven(route) {
|
|
82
|
+
accounts.set(keyOf(route), { failures: 0, until: 0, proven: true });
|
|
83
|
+
policy.noteRouteProven(route.url, scope);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function verified(route, ms) {
|
|
87
|
+
// probeRemote 只有通过绑定密钥的 challenge/ready 与预期实例校验才成功;不是裸 health 成功。
|
|
88
|
+
policy.noteRouteLatency(route.url, ms, undefined, scope);
|
|
89
|
+
policy.noteRouteVerified(route.url, scope);
|
|
90
|
+
}
|
|
76
91
|
|
|
77
|
-
function ordered() {
|
|
92
|
+
function ordered(advance = false) {
|
|
78
93
|
const scoped = mode === 'auto' ? routes : routes.filter((route) => route.channel === mode);
|
|
79
94
|
// 固定挡位一条候选都没有(那条路不存在了):按 auto 兜底,别让用户卡在「没有可用的远程线路」;
|
|
80
95
|
// 真正全部探测失败才由 open / probe 报错。
|
|
81
96
|
const candidates = scoped.length ? scoped : routes;
|
|
97
|
+
if (advance) {
|
|
98
|
+
const channels = new Set(candidates.map(route => route.channel)).size;
|
|
99
|
+
const probeLimit = selected ? Math.min(candidates.length, 4) : Math.min(candidates.length, Math.max(3, channels));
|
|
100
|
+
policy.advanceRouteEvidenceRound(candidates.length, probeLimit, scope);
|
|
101
|
+
}
|
|
82
102
|
const suspended = candidates.filter((route) => account(route).until > now()).map((route) => route.url);
|
|
83
|
-
return policy.orderRouteCandidates(candidates, selected?.url || '', suspended);
|
|
103
|
+
return policy.orderRouteCandidates(candidates, selected?.url || '', suspended, scope);
|
|
84
104
|
}
|
|
85
105
|
|
|
86
106
|
function refresh(networkChanged = false) {
|
|
@@ -90,13 +110,14 @@ function createRemoteRouter({ binding, transport, tunnels, now = Date.now, onErr
|
|
|
90
110
|
}
|
|
91
111
|
if (refreshing && !networkChanged) return refreshing;
|
|
92
112
|
const ticket = generation;
|
|
113
|
+
const tunnelTicket = tunnelGeneration;
|
|
93
114
|
const work = (async () => {
|
|
94
115
|
let extra = [];
|
|
95
116
|
try {
|
|
96
117
|
const read = networkChanged && tunnels?.refresh ? tunnels.refresh(binding) : tunnels?.routesFor?.(binding);
|
|
97
118
|
extra = await read || [];
|
|
98
119
|
} catch (error) { if (terminal(error)) { onError(error); throw error; } }
|
|
99
|
-
if (closed || ticket !== generation) return routes;
|
|
120
|
+
if (closed || ticket !== generation || tunnelTicket !== tunnelGeneration) return routes;
|
|
100
121
|
routes = safeRoutes([...(binding.routes || []), ...extra]).sort((a, b) => priorityIndex(a.channel) - priorityIndex(b.channel));
|
|
101
122
|
return routes;
|
|
102
123
|
})();
|
|
@@ -107,15 +128,20 @@ function createRemoteRouter({ binding, transport, tunnels, now = Date.now, onErr
|
|
|
107
128
|
|
|
108
129
|
// 只负责取消,不再自己卡一个总时限:超时交给 transport 的两段式判定(升级上限 + 握手沉默窗口)。
|
|
109
130
|
// 取消(关停 / 换网)时也要把自定义 transport 里挂住的 Promise 一并拒掉,晚到的结果回收掉。
|
|
110
|
-
async function timed(operation) {
|
|
131
|
+
async function timed(operation, route) {
|
|
111
132
|
const abort = new AbortController();
|
|
112
|
-
pending.
|
|
133
|
+
pending.set(abort, route);
|
|
113
134
|
let settled = false;
|
|
114
135
|
const result = Promise.resolve().then(() => operation(abort.signal));
|
|
115
136
|
result.then((value) => { if (abort.signal.aborted) value?.stream?.destroy(); }, () => {});
|
|
116
137
|
const cancelled = new Promise((_resolve, reject) => {
|
|
117
138
|
abort.signal.addEventListener('abort', () => {
|
|
118
|
-
|
|
139
|
+
// `abort()` 不带 reason 时 `reason` 是 DOMException,它的 `code` 是数字 20(ABORT_ERR),
|
|
140
|
+
// 不是我们的字符串错误码。只有主动带 reason(如 REMOTE_HELPER_RESTARTING)才取它,
|
|
141
|
+
// 否则一律按取消 = REMOTE_CONNECT,别把数字 20 漏到调用方。
|
|
142
|
+
const reasonCode = abort.signal.reason?.code;
|
|
143
|
+
const code = typeof reasonCode === 'string' ? reasonCode : 'REMOTE_CONNECT';
|
|
144
|
+
if (!settled) reject(Object.assign(new Error('远程连接已取消'), { code }));
|
|
119
145
|
}, { once: true });
|
|
120
146
|
});
|
|
121
147
|
try {
|
|
@@ -135,7 +161,7 @@ function createRemoteRouter({ binding, transport, tunnels, now = Date.now, onErr
|
|
|
135
161
|
|
|
136
162
|
/** 升级阶段的上限:有实测握手耗时就在它上面加余量;没有就按线路类型给宽松默认。 */
|
|
137
163
|
function connectBudget(route) {
|
|
138
|
-
const measured = policy.routeLatencyOf(route.url);
|
|
164
|
+
const measured = policy.routeLatencyOf(route.url, scope);
|
|
139
165
|
if (typeof measured === 'number' && Number.isFinite(measured) && measured >= 0) {
|
|
140
166
|
return Math.min(MAX_CONNECT_MS, Math.max(MIN_CONNECT_MS, Math.round(measured + 5_000)));
|
|
141
167
|
}
|
|
@@ -144,7 +170,7 @@ function createRemoteRouter({ binding, transport, tunnels, now = Date.now, onErr
|
|
|
144
170
|
|
|
145
171
|
/** 握手期间允许的沉默时长:有实测就跟着放大,没有给一个宽松默认。 */
|
|
146
172
|
function idleBudget(route) {
|
|
147
|
-
const measured = policy.routeLatencyOf(route.url);
|
|
173
|
+
const measured = policy.routeLatencyOf(route.url, scope);
|
|
148
174
|
if (typeof measured === 'number' && Number.isFinite(measured) && measured >= 0) {
|
|
149
175
|
return Math.min(MAX_IDLE_MS, Math.max(MIN_IDLE_MS, Math.round(measured + 5_000)));
|
|
150
176
|
}
|
|
@@ -154,29 +180,33 @@ function createRemoteRouter({ binding, transport, tunnels, now = Date.now, onErr
|
|
|
154
180
|
// 换网后没有当前线路:候选并发各发一发探测,先成功的那条直接拿去 open;落后者不取消,
|
|
155
181
|
// 结果照旧喂给选路账本。稳态(已有当前线路)不走这条,仍按顺序串行试。
|
|
156
182
|
function raceCandidates(candidates, expiresAt) {
|
|
183
|
+
const ticket = generation;
|
|
157
184
|
return new Promise((resolve) => {
|
|
158
185
|
let settled = false;
|
|
159
186
|
let remaining = candidates.length;
|
|
187
|
+
let lastError;
|
|
160
188
|
const finish = (value) => { if (!settled) { settled = true; resolve(value); } };
|
|
161
189
|
for (const route of candidates) {
|
|
162
190
|
const left = expiresAt ? Math.max(1, expiresAt - now()) : Infinity;
|
|
163
191
|
const connectTimeoutMs = Math.min(connectBudget(route), left);
|
|
164
192
|
const idleTimeoutMs = Math.min(idleBudget(route), left);
|
|
165
|
-
timed((signal) => transport.probeRemote(binding, route, { connectTimeoutMs, idleTimeoutMs, signal, onConnectionInfo: connectionInfo }))
|
|
193
|
+
timed((signal) => transport.probeRemote(binding, route, { connectTimeoutMs, idleTimeoutMs, signal, onConnectionInfo: connectionInfo }), route)
|
|
166
194
|
.then((ms) => {
|
|
167
|
-
if (closed) return;
|
|
195
|
+
if (closed || ticket !== generation) return;
|
|
168
196
|
if (typeof ms !== 'number' || !Number.isFinite(ms) || ms < 0) throw new Error('远程探测没有返回延迟');
|
|
169
|
-
|
|
197
|
+
verified(route, ms);
|
|
170
198
|
finish({ route });
|
|
171
199
|
})
|
|
172
200
|
.catch((error) => {
|
|
173
|
-
if (closed) return;
|
|
174
|
-
|
|
201
|
+
if (closed || ticket !== generation) return;
|
|
202
|
+
if (error?.code === 'REMOTE_HELPER_RESTARTING') return;
|
|
203
|
+
failure(route, error, false);
|
|
204
|
+
lastError = preferredError(lastError, error);
|
|
175
205
|
if (terminal(error)) { onError(error); finish({ error }); }
|
|
176
206
|
})
|
|
177
207
|
.finally(() => {
|
|
178
208
|
remaining -= 1;
|
|
179
|
-
if (remaining === 0) finish(null);
|
|
209
|
+
if (remaining === 0) finish(lastError ? { error: lastError } : null);
|
|
180
210
|
});
|
|
181
211
|
}
|
|
182
212
|
if (!candidates.length) finish(null);
|
|
@@ -185,6 +215,7 @@ function createRemoteRouter({ binding, transport, tunnels, now = Date.now, onErr
|
|
|
185
215
|
|
|
186
216
|
async function open({ expiresAt } = {}) {
|
|
187
217
|
if (closed) throw new Error('远程连接已停止');
|
|
218
|
+
const ticket = generation;
|
|
188
219
|
const refreshing = refresh();
|
|
189
220
|
if (!routes.length) await refreshing;
|
|
190
221
|
else void refreshing.catch(() => {});
|
|
@@ -195,25 +226,29 @@ function createRemoteRouter({ binding, transport, tunnels, now = Date.now, onErr
|
|
|
195
226
|
if (!selected && queue.length > 1) {
|
|
196
227
|
const raced = await raceCandidates(queue, expiresAt);
|
|
197
228
|
if (raced?.error) throw raced.error;
|
|
198
|
-
if (closed) throw Object.assign(new Error('远程连接已停止'), { code: 'REMOTE_CONNECT' });
|
|
229
|
+
if (closed || ticket !== generation) throw Object.assign(new Error('远程连接已停止'), { code: 'REMOTE_CONNECT' });
|
|
199
230
|
if (!raced) throw Object.assign(new Error('没有可用的远程线路'), { code: 'REMOTE_CONNECT' });
|
|
200
231
|
queue = [raced.route, ...ordered().filter((route) => route !== raced.route)];
|
|
201
232
|
}
|
|
202
233
|
for (const route of queue) {
|
|
203
|
-
if (closed) throw Object.assign(new Error('远程连接已停止'), { code: 'REMOTE_CONNECT' });
|
|
234
|
+
if (closed || ticket !== generation) throw Object.assign(new Error('远程连接已停止'), { code: 'REMOTE_CONNECT' });
|
|
235
|
+
if (['tunnel', 'p2p'].includes(route.channel) && !routes.some(item => keyOf(item) === keyOf(route))) continue;
|
|
204
236
|
if (expiresAt && now() >= expiresAt) throw Object.assign(new Error('访问已到期'), { code: 'REMOTE_EXPIRED' });
|
|
205
237
|
const remaining = expiresAt ? Math.max(1, expiresAt - now()) : Infinity;
|
|
206
238
|
const connectTimeoutMs = Math.min(connectBudget(route), remaining);
|
|
207
239
|
const idleTimeoutMs = Math.min(idleBudget(route), remaining);
|
|
208
240
|
try {
|
|
209
|
-
const result = await timed((signal) => transport.openRemoteStream(binding, route, { connectTimeoutMs, idleTimeoutMs, expiresAt, signal, onConnectionInfo: connectionInfo }));
|
|
210
|
-
if (closed ||
|
|
241
|
+
const result = await timed((signal) => transport.openRemoteStream(binding, route, { connectTimeoutMs, idleTimeoutMs, expiresAt, signal, onConnectionInfo: connectionInfo }), route);
|
|
242
|
+
if (closed || ticket !== generation) { result.stream.destroy(); throw Object.assign(new Error('远程连接已取消'), { code: 'REMOTE_CONNECT' }); }
|
|
243
|
+
if (expiresAt && now() >= expiresAt) { result.stream.destroy(); throw Object.assign(new Error('访问已到期'), { code: 'REMOTE_EXPIRED' }); }
|
|
211
244
|
selected = route; proven(route);
|
|
212
245
|
await connectionInfo(result.host?.connectionInfo).catch(() => {});
|
|
213
246
|
// controller 在 connect 回调里读它记「实际在用线路」,一定带上。
|
|
214
247
|
return { ...result, route };
|
|
215
248
|
} catch (error) {
|
|
216
|
-
|
|
249
|
+
if (closed || ticket !== generation) throw error;
|
|
250
|
+
if (error?.code !== 'REMOTE_HELPER_RESTARTING') failure(route, error);
|
|
251
|
+
last = preferredError(last, error);
|
|
217
252
|
if (terminal(error)) { onError(error); throw error; }
|
|
218
253
|
}
|
|
219
254
|
}
|
|
@@ -227,7 +262,9 @@ function createRemoteRouter({ binding, transport, tunnels, now = Date.now, onErr
|
|
|
227
262
|
const warming = refresh();
|
|
228
263
|
if (!routes.length) await warming;
|
|
229
264
|
else void warming.catch(() => {});
|
|
230
|
-
|
|
265
|
+
if (closed || ticket !== generation) return snapshot();
|
|
266
|
+
// 常态轮次开始、排序前拨一次证据钟;首连赛跑、业务和诊断密采不拨钟。
|
|
267
|
+
const candidates = ordered(true);
|
|
231
268
|
const current = candidates.find((route) => route.url === selected?.url);
|
|
232
269
|
let queue;
|
|
233
270
|
if (current) {
|
|
@@ -255,16 +292,18 @@ function createRemoteRouter({ binding, transport, tunnels, now = Date.now, onErr
|
|
|
255
292
|
const worker = async () => {
|
|
256
293
|
while (!closed && ticket === generation && index < queue.length && !fatal) {
|
|
257
294
|
const route = queue[index++];
|
|
295
|
+
if (['tunnel', 'p2p'].includes(route.channel) && !routes.some(item => keyOf(item) === keyOf(route))) continue;
|
|
258
296
|
const connectTimeoutMs = connectBudget(route);
|
|
259
297
|
const idleTimeoutMs = idleBudget(route);
|
|
260
298
|
try {
|
|
261
|
-
const ms = await timed((signal) => transport.probeRemote(binding, route, { connectTimeoutMs, idleTimeoutMs, signal, onConnectionInfo: connectionInfo }));
|
|
299
|
+
const ms = await timed((signal) => transport.probeRemote(binding, route, { connectTimeoutMs, idleTimeoutMs, signal, onConnectionInfo: connectionInfo }), route);
|
|
262
300
|
if (closed || ticket !== generation) return;
|
|
263
301
|
if (typeof ms !== 'number' || !Number.isFinite(ms) || ms < 0) throw new Error('远程探测没有返回延迟');
|
|
264
|
-
|
|
302
|
+
verified(route, ms); successes += 1;
|
|
265
303
|
} catch (error) {
|
|
266
304
|
if (ticket !== generation || closed) return;
|
|
267
|
-
|
|
305
|
+
if (error?.code !== 'REMOTE_HELPER_RESTARTING') failure(route, error, false);
|
|
306
|
+
lastError = preferredError(lastError, error);
|
|
268
307
|
if (terminal(error)) { fatal = error; onError(error); }
|
|
269
308
|
}
|
|
270
309
|
}
|
|
@@ -300,7 +339,7 @@ function createRemoteRouter({ binding, transport, tunnels, now = Date.now, onErr
|
|
|
300
339
|
timer.unref?.();
|
|
301
340
|
});
|
|
302
341
|
return Promise.race([probe, cap]);
|
|
303
|
-
});
|
|
342
|
+
}, route);
|
|
304
343
|
const latencyMs = typeof ms === 'number' && Number.isFinite(ms) && ms >= 0 ? Math.round(ms) : null;
|
|
305
344
|
return { channel: route.channel, url: route.url, ok: latencyMs !== null, latencyMs, errorCode: null };
|
|
306
345
|
} catch (error) {
|
|
@@ -313,11 +352,22 @@ function createRemoteRouter({ binding, transport, tunnels, now = Date.now, onErr
|
|
|
313
352
|
}
|
|
314
353
|
|
|
315
354
|
function snapshot() {
|
|
316
|
-
return { route: selected, routes: [...routes], latencyMs: selected ? policy.routeLatencyOf(selected.url) : null };
|
|
355
|
+
return { route: selected, routes: [...routes], latencyMs: selected ? policy.routeLatencyOf(selected.url, scope) : null };
|
|
317
356
|
}
|
|
318
357
|
|
|
319
358
|
return {
|
|
320
359
|
open, probe, refresh, snapshot, diagnose,
|
|
360
|
+
invalidateTunnels() {
|
|
361
|
+
tunnelGeneration += 1;
|
|
362
|
+
for (const [abort, route] of pending) {
|
|
363
|
+
if (['tunnel', 'p2p'].includes(route?.channel)) {
|
|
364
|
+
abort.abort(Object.assign(new Error('隧道助手正在恢复'), { code: 'REMOTE_HELPER_RESTARTING' }));
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
routes = routes.filter(route => !['tunnel', 'p2p'].includes(route.channel));
|
|
368
|
+
if (['tunnel', 'p2p'].includes(selected?.channel)) selected = null;
|
|
369
|
+
refreshing = null;
|
|
370
|
+
},
|
|
321
371
|
setMode(value) {
|
|
322
372
|
// 带错误码:静态档位白名单删掉后,这条是唯一的存在性判据,界面要能给出具体提示而不是兜底文案。
|
|
323
373
|
if (value !== 'auto' && !routes.some((route) => route.channel === value)) throw Object.assign(new Error('这条线路当前不可用'), { code: 'bad-mode' });
|
|
@@ -325,15 +375,15 @@ function createRemoteRouter({ binding, transport, tunnels, now = Date.now, onErr
|
|
|
325
375
|
},
|
|
326
376
|
failure(error) { failure(selected, error); },
|
|
327
377
|
async networkChanged() {
|
|
328
|
-
generation += 1; policy.forgetRouteFacts(); accounts.clear(); selected = null;
|
|
329
|
-
for (const abort of pending) abort.abort();
|
|
378
|
+
generation += 1; policy.forgetRouteFacts(scope); accounts.clear(); selected = null;
|
|
379
|
+
for (const abort of pending.keys()) abort.abort();
|
|
330
380
|
const previous = probing;
|
|
331
381
|
void refresh(true).catch((error) => onError(error));
|
|
332
382
|
if (previous) await previous.catch(() => {});
|
|
333
383
|
if (!closed) return probe();
|
|
334
384
|
return snapshot();
|
|
335
385
|
},
|
|
336
|
-
close() { closed = true; generation += 1; for (const abort of pending) abort.abort(); policy.forgetRouteFacts(); accounts.clear(); },
|
|
386
|
+
close() { closed = true; generation += 1; for (const abort of pending.keys()) abort.abort(); policy.forgetRouteFacts(scope); accounts.clear(); },
|
|
337
387
|
};
|
|
338
388
|
}
|
|
339
389
|
|
package/lib/remote/transport.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//
|
|
1
|
+
// 远程加密传输保留真实 HTTP 代理与非协议页面证据,网络诊断不泄露响应内容。
|
|
2
2
|
// 握手超时按「有没有进展」判:升级阶段给一个上限,升级后每收到一条消息就重置沉默窗口,
|
|
3
3
|
// 慢网只要还在往前推就一直等,只有彻底没声音的线路才会被判死。
|
|
4
4
|
'use strict';
|
|
@@ -51,6 +51,23 @@ function parseInvitation(text) {
|
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
+
function networkFailure(error, route) {
|
|
55
|
+
if (error?.code?.startsWith('REMOTE_')) return error;
|
|
56
|
+
const protocol = c.endpoint(route, PAIR_PATH).protocol;
|
|
57
|
+
const proxy = protocol === 'https:' ? process.env.HTTPS_PROXY || process.env.https_proxy
|
|
58
|
+
|| process.env.HTTP_PROXY || process.env.http_proxy : process.env.HTTP_PROXY || process.env.http_proxy;
|
|
59
|
+
return c.error(proxy ? 'REMOTE_PROXY_CONFIGURED' : 'REMOTE_CONNECT', 'Computer connection failed');
|
|
60
|
+
}
|
|
61
|
+
function responseFailure(response, bytes) {
|
|
62
|
+
if (response.statusCode === 407) return c.error('REMOTE_PROXY', 'Proxy authentication is required');
|
|
63
|
+
const type = String(response.headers['content-type'] || '');
|
|
64
|
+
if (/text\/html|application\/xhtml\+xml/i.test(type)
|
|
65
|
+
|| /^\s*(?:<!doctype\s+html|<html[\s>])/i.test(bytes?.toString('utf8', 0, 256) || '')) {
|
|
66
|
+
return c.error('REMOTE_INTERCEPTED', 'Expected remote protocol but received a web page');
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
|
|
54
71
|
function post(route, path, instanceId, body, timeoutMs) {
|
|
55
72
|
return new Promise((resolve, reject) => {
|
|
56
73
|
const bytes = c.json(body);
|
|
@@ -65,6 +82,9 @@ function post(route, path, instanceId, body, timeoutMs) {
|
|
|
65
82
|
});
|
|
66
83
|
response.on('error', reject);
|
|
67
84
|
response.on('end', () => {
|
|
85
|
+
const bytes = Buffer.concat(chunks);
|
|
86
|
+
const intercepted = responseFailure(response, bytes);
|
|
87
|
+
if (intercepted) { reject(intercepted); return; }
|
|
68
88
|
if (response.statusCode !== 200) { reject(c.error(response.statusCode === 401 ? 'REMOTE_AUTH' : 'REMOTE_CONNECT', 'Computer pairing was refused')); return; }
|
|
69
89
|
try { resolve(JSON.parse(Buffer.concat(chunks).toString('utf8'))); } catch { reject(c.error('REMOTE_AUTH', 'Invalid pair response')); }
|
|
70
90
|
});
|
|
@@ -72,7 +92,7 @@ function post(route, path, instanceId, body, timeoutMs) {
|
|
|
72
92
|
const timer = setTimeout(() => request.destroy(c.error('REMOTE_TIMEOUT', 'Computer pairing timed out')), timeoutMs);
|
|
73
93
|
timer.unref?.();
|
|
74
94
|
request.on('close', () => clearTimeout(timer));
|
|
75
|
-
request.on('error', (err) => reject(
|
|
95
|
+
request.on('error', (err) => reject(networkFailure(err, route)));
|
|
76
96
|
request.end(bytes);
|
|
77
97
|
});
|
|
78
98
|
}
|
|
@@ -97,8 +117,10 @@ async function pairComputer(textOrObject, options = {}) {
|
|
|
97
117
|
for (const candidate of binding.routes) c.endpoint(candidate, TUNNEL_PATH, true);
|
|
98
118
|
return { instanceId: binding.instanceId, name: binding.name, deviceId, credential: binding.credential, deviceKey, keyId, routes: binding.routes, createdAt: binding.createdAt, ...(binding.connectionInfo ? { connectionInfo: binding.connectionInfo } : {}) };
|
|
99
119
|
} catch (err) {
|
|
100
|
-
|
|
101
|
-
|
|
120
|
+
const next = err.code?.startsWith('REMOTE_') ? err : c.error('REMOTE_AUTH', 'Computer pairing authentication failed');
|
|
121
|
+
const evidence = error => ['REMOTE_PROXY', 'REMOTE_PROXY_CONFIGURED', 'REMOTE_INTERCEPTED'].includes(error?.code);
|
|
122
|
+
if (!evidence(lastError) || evidence(next) || !['REMOTE_CONNECT', 'REMOTE_TIMEOUT'].includes(next.code)) lastError = next;
|
|
123
|
+
if (!['REMOTE_CONNECT', 'REMOTE_TIMEOUT', 'REMOTE_PROXY', 'REMOTE_PROXY_CONFIGURED', 'REMOTE_INTERCEPTED'].includes(lastError.code)) throw lastError;
|
|
102
124
|
}
|
|
103
125
|
}
|
|
104
126
|
throw lastError || c.error('REMOTE_CONNECT', 'No computer route is available');
|
|
@@ -198,7 +220,12 @@ function openRemoteStream(binding, route, options = {}) {
|
|
|
198
220
|
ws.on('message', receive);
|
|
199
221
|
// 升级完成即进入「沉默才算死」阶段;此后的握手步骤只要在推进就继续等。
|
|
200
222
|
ws.on('open', () => arm(idleWindow));
|
|
201
|
-
ws.on('
|
|
223
|
+
ws.on('unexpected-response', (_request, response) => {
|
|
224
|
+
const intercepted = responseFailure(response);
|
|
225
|
+
response.resume();
|
|
226
|
+
fail(intercepted || c.error('REMOTE_CONNECT', 'Computer upgrade was refused'));
|
|
227
|
+
});
|
|
228
|
+
ws.on('error', error => fail(networkFailure(error, route)));
|
|
202
229
|
ws.on('close', () => fail(c.error('REMOTE_CONNECT', 'Computer connection closed')));
|
|
203
230
|
} catch (err) { fail(err); }
|
|
204
231
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//
|
|
1
|
+
// 桌面助手 RPC 按请求关联,意外退出后退避重拉并通知候选持有者。
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
const { spawn } = require('node:child_process');
|
|
@@ -7,6 +7,7 @@ const fs = require('node:fs');
|
|
|
7
7
|
const path = require('node:path');
|
|
8
8
|
const { resolveBinary, platformId, IPC_VERSION } = require('../tailcat/host-process.cjs');
|
|
9
9
|
const MAX_LINE = 64 * 1024;
|
|
10
|
+
const BACKOFF_MS = [1000, 2000, 5000, 10000, 30000];
|
|
10
11
|
|
|
11
12
|
function failure(code) {
|
|
12
13
|
const error = new Error(code);
|
|
@@ -37,12 +38,24 @@ function createRemoteTunnelHelper(options = {}) {
|
|
|
37
38
|
const binary = (options.resolve || resolveRemoteBinary)();
|
|
38
39
|
const launch = options.spawn || spawn;
|
|
39
40
|
const pending = new Map();
|
|
41
|
+
const listeners = new Set();
|
|
40
42
|
let child;
|
|
41
43
|
let ready;
|
|
42
44
|
let closed = false;
|
|
43
45
|
let unsupported = false;
|
|
44
46
|
let generation = 0;
|
|
45
|
-
|
|
47
|
+
let restartTimer;
|
|
48
|
+
let backoff = 0;
|
|
49
|
+
let readyAt = 0;
|
|
50
|
+
let lastError = '';
|
|
51
|
+
const status = () => ({ available: Boolean(binary) && !unsupported && !closed,
|
|
52
|
+
running: Boolean(child), ready: readyAt > 0, restarting: Boolean(restartTimer), generation, error: lastError });
|
|
53
|
+
function notify() {
|
|
54
|
+
const snapshot = status();
|
|
55
|
+
for (const listener of listeners) {
|
|
56
|
+
try { Promise.resolve(listener(snapshot)).catch(() => {}); } catch { /* 观察者不影响助手。 */ }
|
|
57
|
+
}
|
|
58
|
+
}
|
|
46
59
|
function rejectPending(code) {
|
|
47
60
|
for (const item of pending.values()) {
|
|
48
61
|
clearTimeout(item.timer);
|
|
@@ -50,17 +63,31 @@ function createRemoteTunnelHelper(options = {}) {
|
|
|
50
63
|
}
|
|
51
64
|
pending.clear();
|
|
52
65
|
}
|
|
53
|
-
|
|
66
|
+
function scheduleRestart() {
|
|
67
|
+
if (closed || unsupported || !binary || restartTimer) return;
|
|
68
|
+
const delay = BACKOFF_MS[Math.min(backoff++, BACKOFF_MS.length - 1)];
|
|
69
|
+
restartTimer = setTimeout(() => {
|
|
70
|
+
restartTimer = undefined;
|
|
71
|
+
void start().catch(() => {});
|
|
72
|
+
}, delay);
|
|
73
|
+
restartTimer.unref?.();
|
|
74
|
+
}
|
|
54
75
|
function disable(code) {
|
|
76
|
+
if (closed) return;
|
|
55
77
|
unsupported = code === 'remote-helper-unsupported';
|
|
78
|
+
if (readyAt && performance.now() - readyAt >= 30000) backoff = 0;
|
|
79
|
+
lastError = code;
|
|
80
|
+
readyAt = 0;
|
|
56
81
|
rejectPending(code);
|
|
57
82
|
const proc = child;
|
|
58
83
|
child = undefined;
|
|
59
84
|
ready = undefined;
|
|
60
85
|
proc?.stdin?.end();
|
|
61
86
|
proc?.kill();
|
|
87
|
+
if (unsupported && restartTimer) { clearTimeout(restartTimer); restartTimer = undefined; }
|
|
88
|
+
else scheduleRestart();
|
|
89
|
+
notify();
|
|
62
90
|
}
|
|
63
|
-
|
|
64
91
|
function handleLine(line) {
|
|
65
92
|
if (Buffer.byteLength(line) > MAX_LINE) return disable('remote-helper-protocol');
|
|
66
93
|
let value;
|
|
@@ -78,42 +105,38 @@ function createRemoteTunnelHelper(options = {}) {
|
|
|
78
105
|
if (value.ok === true && value.result && typeof value.result === 'object') item.resolve(value.result);
|
|
79
106
|
else item.reject(failure('remote-helper-request'));
|
|
80
107
|
}
|
|
81
|
-
|
|
82
108
|
function send(command, fields = {}, timeoutMs = options.timeoutMs || 15000) {
|
|
83
109
|
if (!child?.stdin?.writable) return Promise.reject(failure('remote-helper-closed'));
|
|
110
|
+
const proc = child;
|
|
84
111
|
const requestId = randomUUID();
|
|
85
112
|
return new Promise((resolve, reject) => {
|
|
86
113
|
const timer = setTimeout(() => {
|
|
87
114
|
pending.delete(requestId);
|
|
88
115
|
const code = command === 'remoteCapabilities' ? 'remote-helper-unsupported' : 'remote-helper-timeout';
|
|
89
116
|
reject(failure(code));
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
child.stdin.write(`${JSON.stringify({ command: 'remoteClose', requestId: randomUUID(), deviceId: fields.deviceId })}\n`);
|
|
117
|
+
if (fields.deviceId && child === proc && proc.stdin?.writable) {
|
|
118
|
+
proc.stdin.write(`${JSON.stringify({ command: 'remoteClose', requestId: randomUUID(), deviceId: fields.deviceId })}\n`);
|
|
93
119
|
} else disable(code);
|
|
94
120
|
}, timeoutMs);
|
|
95
121
|
pending.set(requestId, { command, resolve, reject, timer });
|
|
96
122
|
const text = JSON.stringify({ ...fields, command, requestId });
|
|
97
123
|
if (Buffer.byteLength(text) > MAX_LINE) {
|
|
98
|
-
clearTimeout(timer);
|
|
99
|
-
pending.delete(requestId);
|
|
100
|
-
reject(failure('remote-helper-request'));
|
|
101
|
-
return;
|
|
124
|
+
clearTimeout(timer); pending.delete(requestId); reject(failure('remote-helper-request')); return;
|
|
102
125
|
}
|
|
103
|
-
|
|
104
|
-
if (error && pending.has(requestId)) disable('remote-helper-closed');
|
|
126
|
+
proc.stdin.write(`${text}\n`, error => {
|
|
127
|
+
if (error && child === proc && pending.has(requestId)) disable('remote-helper-closed');
|
|
105
128
|
});
|
|
106
129
|
});
|
|
107
130
|
}
|
|
108
|
-
|
|
109
131
|
function start() {
|
|
110
132
|
if (closed) return Promise.reject(failure('remote-helper-closed'));
|
|
111
133
|
if (!binary) return Promise.reject(failure('remote-helper-unavailable'));
|
|
112
134
|
if (unsupported) return Promise.reject(failure('remote-helper-unsupported'));
|
|
135
|
+
if (restartTimer) return Promise.reject(failure('remote-helper-restarting'));
|
|
113
136
|
if (ready) return ready;
|
|
114
137
|
let proc;
|
|
115
138
|
try { proc = launch(binary, [], { stdio: ['pipe', 'pipe', 'ignore'], windowsHide: true }); }
|
|
116
|
-
catch { return Promise.reject(failure('remote-helper-unavailable')); }
|
|
139
|
+
catch { disable('remote-helper-unavailable'); return Promise.reject(failure('remote-helper-unavailable')); }
|
|
117
140
|
child = proc;
|
|
118
141
|
generation += 1;
|
|
119
142
|
let buffer = '';
|
|
@@ -124,36 +147,39 @@ function createRemoteTunnelHelper(options = {}) {
|
|
|
124
147
|
if (child !== proc) return;
|
|
125
148
|
buffer += chunk;
|
|
126
149
|
for (let at = buffer.indexOf('\n'); at >= 0; at = buffer.indexOf('\n')) {
|
|
127
|
-
const line = buffer.slice(0, at);
|
|
128
|
-
buffer = buffer.slice(at + 1);
|
|
129
|
-
handleLine(line);
|
|
150
|
+
const line = buffer.slice(0, at); buffer = buffer.slice(at + 1); handleLine(line);
|
|
130
151
|
}
|
|
131
152
|
if (Buffer.byteLength(buffer) > MAX_LINE) disable('remote-helper-protocol');
|
|
132
153
|
});
|
|
133
154
|
proc.on('exit', () => { if (child === proc) disable('remote-helper-closed'); });
|
|
134
155
|
proc.stdin.write(`${JSON.stringify({ version: IPC_VERSION, localPort: 19449, name: 'remote-computer-client', state: null, allowedClients: [] })}\n`);
|
|
135
156
|
ready = send('remoteCapabilities', {}, options.capabilityTimeoutMs || 3000).then(result => {
|
|
157
|
+
if (child !== proc || closed) throw failure('remote-helper-closed');
|
|
136
158
|
if (result.version !== 1 || !result.tailcat || !result.p2p) {
|
|
137
|
-
disable('remote-helper-unsupported');
|
|
138
|
-
throw failure('remote-helper-unsupported');
|
|
159
|
+
disable('remote-helper-unsupported'); throw failure('remote-helper-unsupported');
|
|
139
160
|
}
|
|
161
|
+
readyAt = performance.now() || Number.EPSILON;
|
|
162
|
+
lastError = '';
|
|
163
|
+
notify();
|
|
140
164
|
return result;
|
|
141
165
|
});
|
|
142
166
|
return ready;
|
|
143
167
|
}
|
|
144
|
-
|
|
145
168
|
return {
|
|
146
|
-
status
|
|
169
|
+
status,
|
|
170
|
+
onChange(listener) { listeners.add(listener); return () => listeners.delete(listener); },
|
|
147
171
|
async request(command, fields = {}) { await start(); return send(command, fields); },
|
|
148
172
|
async closeDevice(deviceId) {
|
|
149
|
-
if (child && !closed && !unsupported) await send('remoteClose', { deviceId });
|
|
173
|
+
if (child && !closed && !unsupported && readyAt) await send('remoteClose', { deviceId });
|
|
150
174
|
},
|
|
151
175
|
async close() {
|
|
152
176
|
if (closed) return;
|
|
153
177
|
closed = true;
|
|
178
|
+
if (restartTimer) clearTimeout(restartTimer);
|
|
179
|
+
restartTimer = undefined; listeners.clear();
|
|
154
180
|
rejectPending('remote-helper-closed');
|
|
155
181
|
const proc = child;
|
|
156
|
-
child = undefined;
|
|
182
|
+
child = undefined; readyAt = 0;
|
|
157
183
|
if (!proc || proc.exitCode !== null) return;
|
|
158
184
|
await new Promise(resolve => {
|
|
159
185
|
const timer = setTimeout(() => { proc.kill(); resolve(); }, 3000);
|