dsh-mobile-ios 0.2.17 → 0.2.19
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 +52 -0
- package/entries/plugin.mjs +25 -51
- package/entries/sidecar.mjs +12 -31
- package/lib/client.js +425 -19
- package/package.json +2 -2
- package/src/cloudflare.mjs +40 -3
- package/src/cloudflared-login.mjs +188 -0
- package/src/provision-local.mjs +367 -38
- package/src/provision-select.mjs +105 -0
- package/src/rest.mjs +99 -0
- package/src/server.mjs +5 -0
- package/src/tunnel-error.mjs +91 -0
- package/src/tunnel-state.mjs +122 -0
- package/src/zone-health.mjs +256 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// cloudflared-login.mjs — 面板里一键完成 `cloudflared tunnel login`
|
|
2
|
+
//
|
|
3
|
+
// 为什么必须做在插件里:固定地址写在 Cloudflare 的域名下,只有**持有该域名的账号**
|
|
4
|
+
// 的证书才写得进去。而 `cloudflared tunnel login` 需要「在浏览器里点授权」——这一步
|
|
5
|
+
// 人必须参与,但**不该要求用户先会开终端、会敲命令、会找 HOME**。
|
|
6
|
+
// 现场(2026-11-21)的结论很直接:新机上插件提示「账号不对」,用户重装、重启、
|
|
7
|
+
// 点重置都没用,唯一能修好的动作是一条终端命令,而面板只说了一句话。
|
|
8
|
+
//
|
|
9
|
+
// 这个模块只做三件事:起进程、把授权链接捞出来给面板、等进程结束并交代结果。
|
|
10
|
+
// 四个硬约束:
|
|
11
|
+
// 1. 幂等:等待中重复调用返回同一会话,不会起第二个浏览器授权;
|
|
12
|
+
// 2. 不静默:超时/被杀/退出码非 0/退出但没生成证书,都要有可读原因;
|
|
13
|
+
// 3. 不留孤儿:网关停止或取消时 kill 掉子进程;
|
|
14
|
+
// 4. 不写死路径:用调用方给的 executable(发现链解析结果),只覆盖 HOME。
|
|
15
|
+
|
|
16
|
+
import fs from 'node:fs';
|
|
17
|
+
import os from 'node:os';
|
|
18
|
+
import path from 'node:path';
|
|
19
|
+
import { spawn as nodeSpawn } from 'node:child_process';
|
|
20
|
+
|
|
21
|
+
const DEFAULT_TIMEOUT_MS = 5 * 60_000;
|
|
22
|
+
const LOG_CAP = 8 * 1024;
|
|
23
|
+
|
|
24
|
+
/** 从 cloudflared 的输出里捞出浏览器授权链接。 */
|
|
25
|
+
const AUTH_URL = /https:\/\/dash\.cloudflare\.com\/argotunnel\?[^\s"'<>]+/i;
|
|
26
|
+
|
|
27
|
+
/** 有用的最后几行(错误信息通常就在末尾,且不能像原来那样只留第一行)。 */
|
|
28
|
+
function tailLines(raw, max = 400) {
|
|
29
|
+
const lines = String(raw || '').split(/\r?\n/).map((s) => s.trim()).filter(Boolean);
|
|
30
|
+
const text = lines.slice(-4).join(' / ');
|
|
31
|
+
return text.length > max ? `${text.slice(0, max)}…` : text;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const LOGIN_HINT = {
|
|
35
|
+
idle: null,
|
|
36
|
+
waiting: '在浏览器里选择「持有该域名的账号」并点授权',
|
|
37
|
+
ok: '授权完成,本机证书已更新',
|
|
38
|
+
error: '授权没有完成',
|
|
39
|
+
canceled: '已取消授权',
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @param {object} opts
|
|
44
|
+
* @param {Function} [opts.spawn] 注入用(测试里给假进程)
|
|
45
|
+
* @param {Function} [opts.now]
|
|
46
|
+
* @param {Function} [opts.log]
|
|
47
|
+
* @param {number} [opts.timeoutMs]
|
|
48
|
+
* @param {object} [opts.fsImpl]
|
|
49
|
+
*/
|
|
50
|
+
export function createLoginManager({
|
|
51
|
+
spawn = nodeSpawn,
|
|
52
|
+
now = () => Date.now(),
|
|
53
|
+
log = () => {},
|
|
54
|
+
timeoutMs = DEFAULT_TIMEOUT_MS,
|
|
55
|
+
fsImpl = fs,
|
|
56
|
+
} = {}) {
|
|
57
|
+
let session = null; // { state, url, startedAt, finishedAt, pid, error, homeDir, child, certBefore }
|
|
58
|
+
|
|
59
|
+
const certFile = (homeDir) => path.join(homeDir || os.homedir(), '.cloudflared', 'cert.pem');
|
|
60
|
+
const certMtime = (homeDir) => {
|
|
61
|
+
try { return fsImpl.statSync(certFile(homeDir)).mtimeMs; } catch { return null; }
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
function snapshot() {
|
|
65
|
+
if (!session) {
|
|
66
|
+
return { state: 'idle', url: null, startedAt: null, finishedAt: null, pid: null, error: null, hint: null };
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
state: session.state,
|
|
70
|
+
url: session.url,
|
|
71
|
+
startedAt: session.startedAt,
|
|
72
|
+
finishedAt: session.finishedAt,
|
|
73
|
+
pid: session.pid,
|
|
74
|
+
error: session.error,
|
|
75
|
+
hint: LOGIN_HINT[session.state] ?? null,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function finish(state, error = null) {
|
|
80
|
+
if (!session) return;
|
|
81
|
+
session.state = state;
|
|
82
|
+
session.error = error;
|
|
83
|
+
session.finishedAt = new Date(now()).toISOString();
|
|
84
|
+
session.child = null;
|
|
85
|
+
if (session.timer) {
|
|
86
|
+
clearTimeout(session.timer);
|
|
87
|
+
session.timer = null;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 开始(或复用)一次授权。
|
|
93
|
+
* @returns {{state:string,url:string|null,startedAt:string|null,pid:number|null,error:string|null}}
|
|
94
|
+
*/
|
|
95
|
+
function start({ executable, homeDir = os.homedir() } = {}) {
|
|
96
|
+
if (session && session.state === 'waiting') return snapshot();
|
|
97
|
+
if (!executable) {
|
|
98
|
+
const err = new Error('这台主机还没有 cloudflared,无法发起授权');
|
|
99
|
+
err.code = 'NO_CLOUDFLARED';
|
|
100
|
+
throw err;
|
|
101
|
+
}
|
|
102
|
+
session = {
|
|
103
|
+
state: 'waiting',
|
|
104
|
+
url: null,
|
|
105
|
+
startedAt: new Date(now()).toISOString(),
|
|
106
|
+
finishedAt: null,
|
|
107
|
+
pid: null,
|
|
108
|
+
error: null,
|
|
109
|
+
homeDir,
|
|
110
|
+
child: null,
|
|
111
|
+
timer: null,
|
|
112
|
+
certBefore: certMtime(homeDir),
|
|
113
|
+
};
|
|
114
|
+
let raw = '';
|
|
115
|
+
let child;
|
|
116
|
+
try {
|
|
117
|
+
child = spawn(executable, ['tunnel', 'login'], {
|
|
118
|
+
// 只覆盖 HOME:证书一定落在本机真正使用的那份 ~/.cloudflared 里。
|
|
119
|
+
// (Windows 走 USERPROFILE,两者都设,避免 cloudflared 用另一套路径)
|
|
120
|
+
env: { ...process.env, HOME: homeDir, USERPROFILE: homeDir },
|
|
121
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
122
|
+
});
|
|
123
|
+
} catch (err) {
|
|
124
|
+
finish('error', `无法启动 cloudflared:${err?.message || err}`);
|
|
125
|
+
return snapshot();
|
|
126
|
+
}
|
|
127
|
+
session.child = child;
|
|
128
|
+
session.pid = child.pid ?? null;
|
|
129
|
+
log(`tunnel login 已启动 pid=${session.pid} exe=${executable}`);
|
|
130
|
+
|
|
131
|
+
const onChunk = (buf) => {
|
|
132
|
+
raw = (raw + String(buf)).slice(-LOG_CAP);
|
|
133
|
+
if (!session.url) {
|
|
134
|
+
const m = AUTH_URL.exec(raw);
|
|
135
|
+
if (m) {
|
|
136
|
+
// cloudflared 会把 & 原样打印;有些版本会加尾随标点,去掉
|
|
137
|
+
session.url = m[0].replace(/[.,;)]+$/, '');
|
|
138
|
+
log('tunnel login 已拿到授权链接');
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
child.stdout?.on?.('data', onChunk);
|
|
143
|
+
child.stderr?.on?.('data', onChunk);
|
|
144
|
+
|
|
145
|
+
child.on?.('error', (err) => {
|
|
146
|
+
finish('error', `无法启动 cloudflared:${err?.message || err}`);
|
|
147
|
+
});
|
|
148
|
+
child.on?.('close', (code, signal) => {
|
|
149
|
+
if (session.state !== 'waiting') return; // 已经被取消/超时收口
|
|
150
|
+
if (code === 0) {
|
|
151
|
+
const after = certMtime(session.homeDir);
|
|
152
|
+
const renewed = after !== null && (session.certBefore === null || after !== session.certBefore);
|
|
153
|
+
if (!renewed) {
|
|
154
|
+
finish('error', 'cloudflared 正常退出,但没有生成 ~/.cloudflared/cert.pem(授权可能未完成)');
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
finish('ok');
|
|
158
|
+
log('tunnel login 完成,证书已更新');
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const reason = tailLines(raw);
|
|
162
|
+
finish('error', `cloudflared 退出(${signal ? `signal ${signal}` : `code ${code}`})`
|
|
163
|
+
+ (reason ? `:${reason}` : ''));
|
|
164
|
+
log(`tunnel login 失败 code=${code} signal=${signal || '-'}`);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
session.timer = setTimeout(() => {
|
|
168
|
+
if (session?.state !== 'waiting') return;
|
|
169
|
+
try { session.child?.kill?.('SIGTERM'); } catch { /* 已退出 */ }
|
|
170
|
+
finish('error', `等待浏览器授权超时(${Math.round(timeoutMs / 1000)} 秒)。`
|
|
171
|
+
+ '可以重新发起,或在终端手动执行 cloudflared tunnel login。');
|
|
172
|
+
log('tunnel login 超时');
|
|
173
|
+
}, timeoutMs);
|
|
174
|
+
if (session.timer.unref) session.timer.unref();
|
|
175
|
+
|
|
176
|
+
return snapshot();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function cancel() {
|
|
180
|
+
if (!session || session.state !== 'waiting') return snapshot();
|
|
181
|
+
try { session.child?.kill?.('SIGTERM'); } catch { /* 已退出 */ }
|
|
182
|
+
finish('canceled');
|
|
183
|
+
log('tunnel login 已取消');
|
|
184
|
+
return snapshot();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return { start, cancel, status: snapshot };
|
|
188
|
+
}
|