dsh-mobile-plugin 0.5.100 → 0.5.102
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/panel/index.html +13 -13
- package/lib/relay/status.js +5 -0
- package/lib/relay/traffic-stats.cjs +84 -0
- package/lib/remote/loading-page.cjs +103 -0
- package/lib/remote/local-upstream.cjs +8 -1
- package/lib/remote/local.cjs +41 -2
- package/lib/routes/control-plane.js +21 -0
- package/lib/routes/mobile-api.js +50 -2
- package/lib/shell/runtime.js +3 -0
- package/lib/shell/transport-host.js +1 -1
- package/lib/zeronews/account.cjs +278 -0
- package/lib/zeronews/provision.cjs +32 -10
- package/lib/zeronews/wiring.cjs +64 -9
- package/package.json +1 -1
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
// 只读账号快照:把 CLI 的 status / show 三张清单归一成面板能直接画的结构,不写配置、不建隧道。
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* ZeroNews 是账号制内网穿透,客户端一登录就能通过官方 CLI 读到「这台机器 + 整个账号」的现状:
|
|
6
|
+
* · `status --json` —— 登录 / 连接状态、client_id、节点、主机、版本、最近心跳、套餐名、账号;
|
|
7
|
+
* · `show devices --json` —— 账号下所有设备(在线、版本、系统、IP、接入时间……);
|
|
8
|
+
* · `show tunnels --json` —— 账号下所有隧道(域名、协议、带宽、状态、转发到本机哪个端口……);
|
|
9
|
+
* · `show domains --json` —— 账号下的域名(默认域名 / 自有域名)。
|
|
10
|
+
*
|
|
11
|
+
* 这里只做「读 + 归一」,全部字段按官方 JSON 的真实形状挑,缺字段就给空值,不让界面出现 undefined。
|
|
12
|
+
* 定位可执行文件走 provision 的 locate(只找不下),客户端没装就如实回 available:false,绝不顺手下载。
|
|
13
|
+
*
|
|
14
|
+
* 官方 CLI 与本地 API 都没暴露「套餐配额上限」(能建多少条隧道 / 多少台设备),只有套餐名与已用数量,
|
|
15
|
+
* 所以这里也只给「套餐名 + 已用数量」,不编造上限。所有命令 / 定位都可注入,守卫用假体走完整条分支。
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const { execFile } = require('node:child_process');
|
|
19
|
+
const path = require('node:path');
|
|
20
|
+
|
|
21
|
+
const { locateZeroNews } = require('./provision.cjs');
|
|
22
|
+
|
|
23
|
+
const DEFAULT_TIMEOUT_MS = 15_000;
|
|
24
|
+
|
|
25
|
+
/** 统一跑一条 CLI 命令:拿到 stdout / stderr / 退出码,不抛异常(登录过期等按空结果处理)。 */
|
|
26
|
+
function defaultRun(binary, args, opts = {}) {
|
|
27
|
+
return new Promise((resolve) => {
|
|
28
|
+
execFile(binary, args, {
|
|
29
|
+
timeout: opts.timeoutMs || DEFAULT_TIMEOUT_MS,
|
|
30
|
+
cwd: opts.cwd || path.dirname(binary),
|
|
31
|
+
maxBuffer: 1 << 20,
|
|
32
|
+
windowsHide: true,
|
|
33
|
+
}, (err, stdout, stderr) => {
|
|
34
|
+
const code = err ? (typeof err.code === 'number' ? err.code : 1) : 0;
|
|
35
|
+
resolve({ code, stdout: String(stdout || ''), stderr: String(stderr || '') });
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 命令行里可能夹着日志行;只取第一对花括号之间的 JSON。解析不出就 null。 */
|
|
41
|
+
function parseJson(text) {
|
|
42
|
+
const raw = String(text || '').trim();
|
|
43
|
+
const start = raw.indexOf('{');
|
|
44
|
+
const end = raw.lastIndexOf('}');
|
|
45
|
+
if (start < 0 || end <= start) return null;
|
|
46
|
+
try { return JSON.parse(raw.slice(start, end + 1)); } catch { return null; }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function str(value) {
|
|
50
|
+
return typeof value === 'string' ? value : (value === null || value === undefined ? '' : String(value));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function num(value) {
|
|
54
|
+
const n = Number(value);
|
|
55
|
+
return Number.isFinite(n) ? n : 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function bool(value) {
|
|
59
|
+
return value === true || value === 'true';
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** `{total, items}` 这一层的 items,认不出就给空数组。 */
|
|
63
|
+
function itemsOf(page) {
|
|
64
|
+
if (!page || typeof page !== 'object') return [];
|
|
65
|
+
return Array.isArray(page.items) ? page.items : [];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** 总数优先用 CLI 的 total(分页时它才是全量),没有再退回本页条数。 */
|
|
69
|
+
function totalOf(page, items) {
|
|
70
|
+
if (page && typeof page === 'object' && page.total !== undefined) {
|
|
71
|
+
const n = Number(page.total);
|
|
72
|
+
if (Number.isFinite(n)) return n;
|
|
73
|
+
}
|
|
74
|
+
return items.length;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** status --json 归一到面板用的字段。 */
|
|
78
|
+
function normalizeClient(raw) {
|
|
79
|
+
if (!raw || typeof raw !== 'object') return null;
|
|
80
|
+
return {
|
|
81
|
+
login: bool(raw.login),
|
|
82
|
+
loginPending: bool(raw.login_pending),
|
|
83
|
+
configured: bool(raw.configured),
|
|
84
|
+
connected: bool(raw.connected),
|
|
85
|
+
clientId: str(raw.client_id),
|
|
86
|
+
node: str(raw.cloud_server_addr),
|
|
87
|
+
hostname: str(raw.hostname),
|
|
88
|
+
os: str(raw.os),
|
|
89
|
+
arch: str(raw.arch),
|
|
90
|
+
version: str(raw.version),
|
|
91
|
+
endpointCount: num(raw.endpoint_count),
|
|
92
|
+
lastPongAt: str(raw.last_pong_at),
|
|
93
|
+
clientEnabled: raw.client_enabled !== false,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function normalizeAccount(raw) {
|
|
98
|
+
if (!raw || typeof raw !== 'object') return { planName: '', userId: '', userTel: '' };
|
|
99
|
+
return {
|
|
100
|
+
planName: str(raw.plan_name),
|
|
101
|
+
userId: str(raw.user_id),
|
|
102
|
+
userTel: str(raw.user_tel),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function normalizeDevice(row) {
|
|
107
|
+
if (!row || typeof row !== 'object') return null;
|
|
108
|
+
const id = str(row.id);
|
|
109
|
+
if (!id) return null;
|
|
110
|
+
return {
|
|
111
|
+
id,
|
|
112
|
+
name: str(row.custom_name) || str(row.name),
|
|
113
|
+
hostname: str(row.hostname),
|
|
114
|
+
os: str(row.os),
|
|
115
|
+
arch: str(row.arch),
|
|
116
|
+
version: str(row.version),
|
|
117
|
+
ip: str(row.ip),
|
|
118
|
+
online: bool(row.is_online),
|
|
119
|
+
enabled: bool(row.enabled),
|
|
120
|
+
createdAt: str(row.created_at),
|
|
121
|
+
edgeNode: str(row.edge_node_name),
|
|
122
|
+
workspace: str(row.workspace && row.workspace.name),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** 隧道第一个转发后端指向的本机地址(面板只显示一条,够用且不吓人)。 */
|
|
127
|
+
function tunnelLocal(row) {
|
|
128
|
+
const rules = Array.isArray(row.route_rules) ? row.route_rules : [];
|
|
129
|
+
for (const rule of rules) {
|
|
130
|
+
const forwards = Array.isArray(rule.forward_rules) ? rule.forward_rules : [];
|
|
131
|
+
for (const fwd of forwards) {
|
|
132
|
+
const endpoints = fwd && fwd.load_balance_policy && fwd.load_balance_policy.backend_endpoints;
|
|
133
|
+
if (!Array.isArray(endpoints)) continue;
|
|
134
|
+
for (const ep of endpoints) {
|
|
135
|
+
const port = Number(ep && ep.local_port);
|
|
136
|
+
if (Number.isFinite(port) && port > 0) {
|
|
137
|
+
const ip = str(ep.local_ip) || '127.0.0.1';
|
|
138
|
+
return `${ip}:${port}`;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return '';
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** 隧道的公网地址:自有域名优先,否则子域 + 根域。 */
|
|
147
|
+
function tunnelDomain(row) {
|
|
148
|
+
const self = str(row.self_hosted_domain);
|
|
149
|
+
if (self) return self;
|
|
150
|
+
const sub = str(row.subdomain);
|
|
151
|
+
const root = str(row.root_domain && row.root_domain.domain);
|
|
152
|
+
if (sub && root) return `${sub}.${root}`;
|
|
153
|
+
return sub || root;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function normalizeTunnel(row) {
|
|
157
|
+
if (!row || typeof row !== 'object') return null;
|
|
158
|
+
const id = str(row.id);
|
|
159
|
+
if (!id) return null;
|
|
160
|
+
return {
|
|
161
|
+
id,
|
|
162
|
+
type: str(row.type).toUpperCase(),
|
|
163
|
+
feature: str(row.feature),
|
|
164
|
+
mode: str(row.mode),
|
|
165
|
+
domain: tunnelDomain(row),
|
|
166
|
+
enabled: bool(row.enabled),
|
|
167
|
+
bw: num(row.bw),
|
|
168
|
+
status: str(row.status),
|
|
169
|
+
local: tunnelLocal(row),
|
|
170
|
+
createdAt: str(row.created_at),
|
|
171
|
+
lastSyncedAt: str(row.last_synced_at),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function normalizeDomain(row) {
|
|
176
|
+
if (!row || typeof row !== 'object') return null;
|
|
177
|
+
const id = str(row.id);
|
|
178
|
+
if (!id) return null;
|
|
179
|
+
const sub = str(row.subdomain);
|
|
180
|
+
const root = str(row.root_domain && row.root_domain.domain);
|
|
181
|
+
return {
|
|
182
|
+
id,
|
|
183
|
+
fqdn: str(row.cname_domain) || (sub && root ? `${sub}.${root}` : (sub || root)),
|
|
184
|
+
type: str(row.type),
|
|
185
|
+
status: str(row.status),
|
|
186
|
+
enabled: bool(row.enabled),
|
|
187
|
+
portCount: Array.isArray(row.ports) ? row.ports.length : 0,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function compact(list) {
|
|
192
|
+
return list.filter(Boolean);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** 客户端没装 / 没登录时的统一空壳:字段齐全,让界面照常渲染。 */
|
|
196
|
+
function emptySnapshot(extra = {}) {
|
|
197
|
+
return {
|
|
198
|
+
ok: true,
|
|
199
|
+
available: true,
|
|
200
|
+
login: false,
|
|
201
|
+
resourcesLoaded: false,
|
|
202
|
+
error: '',
|
|
203
|
+
client: null,
|
|
204
|
+
account: { planName: '', userId: '', userTel: '' },
|
|
205
|
+
counts: { tunnels: 0, devices: 0, domains: 0 },
|
|
206
|
+
tunnels: [],
|
|
207
|
+
devices: [],
|
|
208
|
+
domains: [],
|
|
209
|
+
...extra,
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* @param {object} [options]
|
|
215
|
+
* @param {() => Promise<{ok:boolean,path:string}>} [options.locate] 定位已装客户端(只找不下)
|
|
216
|
+
* @param {(bin:string,args:string[],opts:object)=>Promise<{code:number,stdout:string,stderr:string}>} [options.run] 命令执行注入
|
|
217
|
+
* @param {number} [options.timeoutMs] 单条命令超时
|
|
218
|
+
*/
|
|
219
|
+
function createZeroNewsAccountReader(options = {}) {
|
|
220
|
+
const locate = typeof options.locate === 'function' ? options.locate : locateZeroNews;
|
|
221
|
+
const run = typeof options.run === 'function' ? options.run : defaultRun;
|
|
222
|
+
const timeoutMs = Number(options.timeoutMs) > 0 ? Number(options.timeoutMs) : DEFAULT_TIMEOUT_MS;
|
|
223
|
+
|
|
224
|
+
async function runJson(binary, args) {
|
|
225
|
+
const result = await run(binary, args, { timeoutMs });
|
|
226
|
+
return { parsed: parseJson(result && result.stdout), result };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async function read() {
|
|
230
|
+
let found = null;
|
|
231
|
+
try { found = await locate(); } catch { found = null; }
|
|
232
|
+
if (!found || !found.ok || !found.path) {
|
|
233
|
+
return emptySnapshot({ available: false, error: '' });
|
|
234
|
+
}
|
|
235
|
+
const binary = found.path;
|
|
236
|
+
const { parsed: status } = await runJson(binary, ['status', '--json']);
|
|
237
|
+
const client = normalizeClient(status);
|
|
238
|
+
const account = normalizeAccount(status);
|
|
239
|
+
const loggedIn = Boolean(client && client.login);
|
|
240
|
+
if (!loggedIn) {
|
|
241
|
+
return emptySnapshot({ available: true, login: false, client, account, resourcesLoaded: false });
|
|
242
|
+
}
|
|
243
|
+
// 三张清单彼此独立,一起拉;某一张读失败就空着,不把整页带崩。
|
|
244
|
+
const [devicesRes, tunnelsRes, domainsRes] = await Promise.all([
|
|
245
|
+
runJson(binary, ['show', 'devices', '--json']),
|
|
246
|
+
runJson(binary, ['show', 'tunnels', '--json']),
|
|
247
|
+
runJson(binary, ['show', 'domains', '--json']),
|
|
248
|
+
]);
|
|
249
|
+
const deviceItems = itemsOf(devicesRes.parsed);
|
|
250
|
+
const tunnelItems = itemsOf(tunnelsRes.parsed);
|
|
251
|
+
const domainItems = itemsOf(domainsRes.parsed);
|
|
252
|
+
const failed = [devicesRes, tunnelsRes, domainsRes].some((r) => !r.parsed);
|
|
253
|
+
return {
|
|
254
|
+
ok: true,
|
|
255
|
+
available: true,
|
|
256
|
+
login: true,
|
|
257
|
+
resourcesLoaded: !failed,
|
|
258
|
+
error: failed ? 'part of account data could not be loaded' : '',
|
|
259
|
+
client,
|
|
260
|
+
account,
|
|
261
|
+
counts: {
|
|
262
|
+
devices: totalOf(devicesRes.parsed, deviceItems),
|
|
263
|
+
tunnels: totalOf(tunnelsRes.parsed, tunnelItems),
|
|
264
|
+
domains: totalOf(domainsRes.parsed, domainItems),
|
|
265
|
+
},
|
|
266
|
+
devices: compact(deviceItems.map(normalizeDevice)),
|
|
267
|
+
tunnels: compact(tunnelItems.map(normalizeTunnel)),
|
|
268
|
+
domains: compact(domainItems.map(normalizeDomain)),
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return { read };
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
module.exports = {
|
|
276
|
+
DEFAULT_TIMEOUT_MS, parseJson, normalizeClient, normalizeAccount, normalizeDevice,
|
|
277
|
+
normalizeTunnel, normalizeDomain, tunnelLocal, tunnelDomain, createZeroNewsAccountReader,
|
|
278
|
+
};
|
|
@@ -67,27 +67,25 @@ function extractFromTar(buf) {
|
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
/**
|
|
70
|
+
* 只「找」一份已经在本机的 zeronews,绝不下下载:只读状态 / 账号数据那条路要用它,
|
|
71
|
+
* 面板打开一下不该顺手把几十兆客户端拉下来。来源顺序与 ensure 一致:显式 / PATH / 系统安装 / 缓存。
|
|
72
|
+
*
|
|
70
73
|
* @param {object} options
|
|
71
74
|
* @param {() => string} [options.env] 读环境变量(PATH / DSHM_ZERONEWS_BIN)
|
|
72
75
|
* @param {object} [options.fsImpl] 文件系统注入
|
|
73
|
-
* @param {(url: string) => Promise<{ok:boolean, arrayBuffer:()=>Promise<ArrayBuffer>}>} [options.fetchImpl]
|
|
74
76
|
* @param {object} [options.platform] { platform, arch }
|
|
75
77
|
* @param {string} [options.root] 运行目录
|
|
78
|
+
* @returns {{ok:boolean, path:string, source:string, code?:string}}
|
|
76
79
|
*/
|
|
77
|
-
|
|
80
|
+
function locateZeroNews(options = {}) {
|
|
78
81
|
const fsImpl = options.fsImpl || fs;
|
|
79
82
|
const readEnv = typeof options.env === 'function' ? options.env : (key) => process.env[key] || '';
|
|
80
|
-
const doFetch = typeof options.fetchImpl === 'function'
|
|
81
|
-
? options.fetchImpl
|
|
82
|
-
: (typeof globalThis.fetch === 'function' ? globalThis.fetch : null);
|
|
83
83
|
const plat = options.platform || {};
|
|
84
|
-
const key = platformId(plat.platform, plat.arch);
|
|
85
84
|
const dir = runDir(options.root);
|
|
86
85
|
const binName = (plat.platform || process.platform) === 'win32' ? 'zeronews.exe' : 'zeronews';
|
|
87
86
|
const cached = path.join(dir, binName);
|
|
88
87
|
const exists = (p) => { try { return fsImpl.statSync(p).isFile(); } catch { return false; } };
|
|
89
88
|
|
|
90
|
-
// ① 用户显式指定 / PATH 上已有,直接用。
|
|
91
89
|
const explicit = readEnv('DSHM_ZERONEWS_BIN');
|
|
92
90
|
if (explicit && exists(explicit)) return { ok: true, path: explicit, source: 'env' };
|
|
93
91
|
// macOS 上官方默认装到 /Applications/zeronews/zeronews(那份可能已经登录),优先复用,别另起一份未登录的。
|
|
@@ -99,9 +97,33 @@ async function ensureZeroNews(options = {}) {
|
|
|
99
97
|
const candidate = path.join(entry, binName);
|
|
100
98
|
if (exists(candidate)) return { ok: true, path: candidate, source: 'path' };
|
|
101
99
|
}
|
|
102
|
-
// ② 以前下载过的缓存。
|
|
103
100
|
if (exists(cached)) return { ok: true, path: cached, source: 'cache' };
|
|
104
|
-
|
|
101
|
+
return { ok: false, path: '', source: '', code: 'not-installed' };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* @param {object} options
|
|
106
|
+
* @param {() => string} [options.env] 读环境变量(PATH / DSHM_ZERONEWS_BIN)
|
|
107
|
+
* @param {object} [options.fsImpl] 文件系统注入
|
|
108
|
+
* @param {(url: string) => Promise<{ok:boolean, arrayBuffer:()=>Promise<ArrayBuffer>}>} [options.fetchImpl]
|
|
109
|
+
* @param {object} [options.platform] { platform, arch }
|
|
110
|
+
* @param {string} [options.root] 运行目录
|
|
111
|
+
*/
|
|
112
|
+
async function ensureZeroNews(options = {}) {
|
|
113
|
+
const doFetch = typeof options.fetchImpl === 'function'
|
|
114
|
+
? options.fetchImpl
|
|
115
|
+
: (typeof globalThis.fetch === 'function' ? globalThis.fetch : null);
|
|
116
|
+
const plat = options.platform || {};
|
|
117
|
+
const key = platformId(plat.platform, plat.arch);
|
|
118
|
+
const dir = runDir(options.root);
|
|
119
|
+
const binName = (plat.platform || process.platform) === 'win32' ? 'zeronews.exe' : 'zeronews';
|
|
120
|
+
const cached = path.join(dir, binName);
|
|
121
|
+
const fsImpl = options.fsImpl || fs;
|
|
122
|
+
|
|
123
|
+
// ① 用户显式指定 / PATH 上已有 / 系统安装 / 缓存,直接用。
|
|
124
|
+
const located = locateZeroNews(options);
|
|
125
|
+
if (located.ok) return located;
|
|
126
|
+
// ② 都不支持或没网,如实说。
|
|
105
127
|
if (!key) return { ok: false, code: 'unsupported-platform', path: '', source: '' };
|
|
106
128
|
if (!doFetch) return { ok: false, code: 'download-failed', path: '', source: '' };
|
|
107
129
|
|
|
@@ -124,5 +146,5 @@ async function ensureZeroNews(options = {}) {
|
|
|
124
146
|
}
|
|
125
147
|
|
|
126
148
|
module.exports = {
|
|
127
|
-
VERSION, DOWNLOADS, platformId, downloadUrlOf, runDir, extractFromTar, ensureZeroNews,
|
|
149
|
+
VERSION, DOWNLOADS, platformId, downloadUrlOf, runDir, extractFromTar, locateZeroNews, ensureZeroNews,
|
|
128
150
|
};
|
package/lib/zeronews/wiring.cjs
CHANGED
|
@@ -15,12 +15,47 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
const { execFile } = require('node:child_process');
|
|
18
|
+
const path = require('node:path');
|
|
18
19
|
|
|
19
20
|
const { createZeroNewsRunner } = require('./runner.cjs');
|
|
20
21
|
const { ensureZeroNews } = require('./provision.cjs');
|
|
22
|
+
const { createZeroNewsAccountReader } = require('./account.cjs');
|
|
21
23
|
|
|
22
|
-
/**
|
|
23
|
-
|
|
24
|
+
/** 取命令行输出里的第一条 http(s) 链接:`zeronews login` 会把这台机器的确认页打在这一行。 */
|
|
25
|
+
function firstHttpUrl(text) {
|
|
26
|
+
for (const line of String(text || '').split(/\r?\n/)) {
|
|
27
|
+
const trimmed = line.trim();
|
|
28
|
+
if (trimmed.startsWith('http://') || trimmed.startsWith('https://')) return trimmed;
|
|
29
|
+
}
|
|
30
|
+
return '';
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 跑 `zeronews login`:官方流程是「打印一个确认链接 → 用户在浏览器点确认 → 客户端拿到身份」,
|
|
35
|
+
* 全程不需要复制 token。命令打印完链接就返回,登录状态由 status --json 的 login 字段回答。
|
|
36
|
+
* 已登录时命令会直接说 already logged in,给它一个 already 标记,面板据此跳过「等确认」。
|
|
37
|
+
*/
|
|
38
|
+
function defaultLogin(binary, clientLabel, exec = execFile) {
|
|
39
|
+
return new Promise((resolve) => {
|
|
40
|
+
const args = ['login'];
|
|
41
|
+
if (clientLabel) args.push('--client', clientLabel);
|
|
42
|
+
try {
|
|
43
|
+
exec(binary, args, {
|
|
44
|
+
timeout: 20_000,
|
|
45
|
+
cwd: path.dirname(binary),
|
|
46
|
+
windowsHide: true,
|
|
47
|
+
env: { ...process.env, NO_TTY: '1' },
|
|
48
|
+
}, (err, stdout, stderr) => {
|
|
49
|
+
const combined = `${stdout || ''}\n${stderr || ''}`;
|
|
50
|
+
if (/already logged in/i.test(combined)) { resolve({ already: true, url: '', error: '' }); return; }
|
|
51
|
+
const url = firstHttpUrl(stdout) || firstHttpUrl(stderr);
|
|
52
|
+
resolve({ already: false, url, error: url ? '' : String(stderr || '').trim() });
|
|
53
|
+
});
|
|
54
|
+
} catch {
|
|
55
|
+
resolve({ already: false, url: '', error: 'could not start login' });
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
24
59
|
|
|
25
60
|
/**
|
|
26
61
|
* 用系统默认浏览器打开一个地址。
|
|
@@ -53,14 +88,20 @@ const facade = {
|
|
|
53
88
|
stop: () => (readCurrent() ? readCurrent().stop() : Promise.resolve({ phase: 'off' })),
|
|
54
89
|
status: () => (readCurrent() ? readCurrent().status() : { available: false, running: false, phase: 'off', url: '', error: '', configured: false }),
|
|
55
90
|
prepare: () => (readCurrent() ? readCurrent().prepare() : Promise.resolve({ ok: false, error: '这台宿主没有接入 ZeroNews 线路能力' })),
|
|
91
|
+
account: () => (readCurrent() ? readCurrent().account() : Promise.resolve({ ok: false, available: false, error: '这台宿主没有接入 ZeroNews 线路能力' })),
|
|
56
92
|
};
|
|
57
93
|
|
|
58
|
-
/** @param {{mount:Function, settings:object, getState:Function, control:object, warn:Function, ctx:object, name:string, portOf?:Function, runnerFactory?:Function, ensure?:Function}} options */
|
|
94
|
+
/** @param {{mount:Function, settings:object, getState:Function, control:object, warn:Function, ctx:object, name:string, portOf?:Function, runnerFactory?:Function, ensure?:Function, accountFactory?:Function, loginFlow?:Function, loginClient?:string, openUrl?:Function}} options */
|
|
59
95
|
function mountZeroNews(options) {
|
|
60
96
|
const { mount, settings, getState, control, warn, ctx, name } = options;
|
|
61
97
|
const makeRunner = typeof options.runnerFactory === 'function' ? options.runnerFactory : createZeroNewsRunner;
|
|
62
98
|
const provision = typeof options.ensure === 'function' ? options.ensure : ensureZeroNews;
|
|
63
99
|
const exec = typeof options.execFile === 'function' ? options.execFile : execFile;
|
|
100
|
+
const makeAccount = typeof options.accountFactory === 'function' ? options.accountFactory : () => createZeroNewsAccountReader();
|
|
101
|
+
const accountReader = makeAccount();
|
|
102
|
+
const loginFlow = typeof options.loginFlow === 'function'
|
|
103
|
+
? options.loginFlow
|
|
104
|
+
: (binary) => defaultLogin(binary, options.loginClient, exec);
|
|
64
105
|
// 自愈:客户端意外退出 / 启动失败时,只要还「配置过」,就按退避自动重连,别让线路卡死在错误上。
|
|
65
106
|
// 插件自动更新 / 就地重载后由新实例重新挂载,这条重连负责把上一轮断掉的客户端重新接上。
|
|
66
107
|
const retryBaseMs = Number(options.retryBaseMs) > 0 ? Number(options.retryBaseMs) : 3000;
|
|
@@ -118,10 +159,12 @@ function mountZeroNews(options) {
|
|
|
118
159
|
};
|
|
119
160
|
|
|
120
161
|
/**
|
|
121
|
-
*
|
|
162
|
+
* 第一次接入:把客户端装好,再走官方 `login` 拉起浏览器确认。
|
|
122
163
|
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
164
|
+
* 全部自动:命令会打印这台机器的确认链接,我们直接用系统浏览器打开;用户只需要在页面点一次
|
|
165
|
+
* 「确认登录」。这里不等确认就返回(下载可能要几十秒,等确认不该占着一个请求),面板随后轮询
|
|
166
|
+
* 登录状态,`login` 变真就自动接着起客户端、建隧道。已经登录过的机器命令会直接说 already,
|
|
167
|
+
* 面板据此跳过等待——这也是老用户从「粘 token」切到新版本时无感的来源:他们的身份早就落盘了。
|
|
125
168
|
*/
|
|
126
169
|
const prepare = async () => {
|
|
127
170
|
let got;
|
|
@@ -133,8 +176,15 @@ function mountZeroNews(options) {
|
|
|
133
176
|
if (!got || !got.ok) {
|
|
134
177
|
return { ok: false, error: got && got.code === 'unsupported-platform' ? '这台系统不支持自动安装 ZeroNews 客户端' : '下载 ZeroNews 客户端失败,请稍后重试' };
|
|
135
178
|
}
|
|
136
|
-
const
|
|
137
|
-
|
|
179
|
+
const started = await loginFlow(got.path);
|
|
180
|
+
if (started && started.already) {
|
|
181
|
+
return { ok: true, downloaded: got.source === 'downloaded', opened: false, already: true };
|
|
182
|
+
}
|
|
183
|
+
if (!started || !started.url) {
|
|
184
|
+
return { ok: false, downloaded: got.source === 'downloaded', error: (started && started.error) || '没能开始登录,请稍后重试' };
|
|
185
|
+
}
|
|
186
|
+
const opened = await (typeof options.openUrl === 'function' ? options.openUrl(started.url) : openInBrowser(started.url, exec));
|
|
187
|
+
return { ok: true, downloaded: got.source === 'downloaded', opened: opened === true, url: started.url, loginPending: true };
|
|
138
188
|
};
|
|
139
189
|
|
|
140
190
|
const stop = async () => {
|
|
@@ -145,7 +195,12 @@ function mountZeroNews(options) {
|
|
|
145
195
|
return result;
|
|
146
196
|
};
|
|
147
197
|
|
|
148
|
-
|
|
198
|
+
const account = async () => {
|
|
199
|
+
try { return await accountReader.read(); }
|
|
200
|
+
catch (error) { return { ok: false, available: false, error: error && error.message ? error.message : String(error) }; }
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
globalThis[GLOBAL_KEY] = { start, stop, status: statusOf, prepare, account };
|
|
149
204
|
|
|
150
205
|
mount('ZeroNews 线路', () => ctx.effect(() => {
|
|
151
206
|
// 只要「配置过」就自动重连:zeroNewsEnabled 为真,或者状态里还留着上次连上的隧道地址
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-mobile-plugin",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.102",
|
|
4
4
|
"description": "Mobile API (/api/mobile/v1) and relay client for the DSH mobile app — desktop profile build (dsh 0.2.x, peers resolved by the host)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"icon": "./icon.png",
|