dsh-xdpool 2.7.3 → 2.7.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +77 -0
- package/lib/client.js +282 -6
- package/lib/index.d.ts +12 -0
- package/lib/index.js +485 -67
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
2
|
import { execFile, execFileSync, spawn, spawnSync } from "node:child_process";
|
|
3
|
-
import { appendFileSync, cpSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, statSync, truncateSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { appendFileSync, cpSync, createWriteStream, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, truncateSync, writeFileSync } from "node:fs";
|
|
4
4
|
import { homedir, hostname, tmpdir } from "node:os";
|
|
5
5
|
import { basename, dirname, join, resolve } from "node:path";
|
|
6
6
|
import crypto$1, { createDecipheriv, createHash, randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
|
|
@@ -998,7 +998,7 @@ const BRIEF_LIST_ENDPOINT = "https://www.doubao.com/alice/slot/action_bar_v3/bri
|
|
|
998
998
|
/** The bot whose roster is being asked for — the default Doubao assistant. */
|
|
999
999
|
const BOT_ID = "7338286299411103781";
|
|
1000
1000
|
/** How long a live listing is reused. */
|
|
1001
|
-
const TTL_MS = 6e5;
|
|
1001
|
+
const TTL_MS$1 = 6e5;
|
|
1002
1002
|
/** The query the endpoint expects. */
|
|
1003
1003
|
const CLIENT_PARAMS$2 = {
|
|
1004
1004
|
aid: "497858",
|
|
@@ -1018,7 +1018,7 @@ const CLIENT_PARAMS$2 = {
|
|
|
1018
1018
|
samantha_web: "1",
|
|
1019
1019
|
"use-olympus-account": "1"
|
|
1020
1020
|
};
|
|
1021
|
-
let cached$
|
|
1021
|
+
let cached$2;
|
|
1022
1022
|
/**
|
|
1023
1023
|
* Read the roster from the account.
|
|
1024
1024
|
*
|
|
@@ -1026,7 +1026,7 @@ let cached$1;
|
|
|
1026
1026
|
*/
|
|
1027
1027
|
async function fetchLiveModels(session, options = {}) {
|
|
1028
1028
|
const now = options.now ?? Date.now();
|
|
1029
|
-
if (options.force !== true && cached$
|
|
1029
|
+
if (options.force !== true && cached$2 !== void 0 && now - cached$2.at < TTL_MS$1) return cached$2.models;
|
|
1030
1030
|
if (session === void 0 || !sessionUsable(session)) return DOUBAO_MODELS;
|
|
1031
1031
|
const params = new URLSearchParams({
|
|
1032
1032
|
...CLIENT_PARAMS$2,
|
|
@@ -1086,7 +1086,7 @@ async function fetchLiveModels(session, options = {}) {
|
|
|
1086
1086
|
byId.set(model.id, model);
|
|
1087
1087
|
}
|
|
1088
1088
|
const models = [...byId.values()];
|
|
1089
|
-
cached$
|
|
1089
|
+
cached$2 = {
|
|
1090
1090
|
at: now,
|
|
1091
1091
|
models
|
|
1092
1092
|
};
|
|
@@ -2466,6 +2466,77 @@ function createDoubaoLlm(options) {
|
|
|
2466
2466
|
};
|
|
2467
2467
|
}
|
|
2468
2468
|
//#endregion
|
|
2469
|
+
//#region src/origin-trust.ts
|
|
2470
|
+
/** Whether a hostname is the local machine. */
|
|
2471
|
+
function isLoopback(hostname) {
|
|
2472
|
+
const bare = hostname.startsWith("[") && hostname.endsWith("]") ? hostname.slice(1, -1) : hostname;
|
|
2473
|
+
return bare === "localhost" || bare === "127.0.0.1" || bare === "::1" || bare.startsWith("127.");
|
|
2474
|
+
}
|
|
2475
|
+
/**
|
|
2476
|
+
* A `trustedHosts` entry and an origin, compared the way the host compares them.
|
|
2477
|
+
*
|
|
2478
|
+
* An entry WITH a port matches exactly; a port-less entry matches the hostname on any port. The
|
|
2479
|
+
* host's own `isTrustedAuthority` works this way, and agreeing with it matters more than picking
|
|
2480
|
+
* a rule of our own — two components disagreeing about trust is how this defect happened.
|
|
2481
|
+
*/
|
|
2482
|
+
function matchesTrusted(origin, trusted) {
|
|
2483
|
+
for (const entry of trusted) {
|
|
2484
|
+
if (typeof entry !== "string" || entry.trim() === "") continue;
|
|
2485
|
+
const wanted = entry.trim().toLowerCase();
|
|
2486
|
+
if (/:\d+$/u.test(wanted)) {
|
|
2487
|
+
if (origin.host.toLowerCase() === wanted) return true;
|
|
2488
|
+
continue;
|
|
2489
|
+
}
|
|
2490
|
+
if (origin.hostname.toLowerCase() === wanted) return true;
|
|
2491
|
+
}
|
|
2492
|
+
return false;
|
|
2493
|
+
}
|
|
2494
|
+
/** The `Host` header's hostname, or undefined when it is absent or unparseable. */
|
|
2495
|
+
function hostHeaderName(req) {
|
|
2496
|
+
const host = req.headers.host;
|
|
2497
|
+
if (typeof host !== "string" || host === "") return void 0;
|
|
2498
|
+
try {
|
|
2499
|
+
return new URL(`http://${host}`).hostname.toLowerCase();
|
|
2500
|
+
} catch {
|
|
2501
|
+
return;
|
|
2502
|
+
}
|
|
2503
|
+
}
|
|
2504
|
+
/**
|
|
2505
|
+
* Decide whether `req` may act.
|
|
2506
|
+
*
|
|
2507
|
+
* `trusted` is read fresh on each call so a configuration change reaches the next request
|
|
2508
|
+
* without a plugin restart — the host's own bridge does the same.
|
|
2509
|
+
*/
|
|
2510
|
+
function originTrusted(req, trusted = []) {
|
|
2511
|
+
const origin = req.headers.origin;
|
|
2512
|
+
/**
|
|
2513
|
+
* No `Origin` at all.
|
|
2514
|
+
*
|
|
2515
|
+
* A same-origin `fetch()` in Chromium omits it for GET/HEAD, and the card's own reads are
|
|
2516
|
+
* exactly that. Refusing this would break the page on the machine it was installed on.
|
|
2517
|
+
*/
|
|
2518
|
+
if (typeof origin !== "string" || origin === "") return true;
|
|
2519
|
+
let url;
|
|
2520
|
+
try {
|
|
2521
|
+
url = new URL(origin);
|
|
2522
|
+
} catch {
|
|
2523
|
+
return false;
|
|
2524
|
+
}
|
|
2525
|
+
if (isLoopback(url.hostname)) return true;
|
|
2526
|
+
if (matchesTrusted(url, trusted)) return true;
|
|
2527
|
+
/**
|
|
2528
|
+
* Same-origin, judged by the browser's own two headers.
|
|
2529
|
+
*
|
|
2530
|
+
* This is the case a reverse proxy produces: the page is served from
|
|
2531
|
+
* `https://node.tailXXXX.ts.net`, so that is what `Origin` says, and the request arrives at a
|
|
2532
|
+
* plugin route on that same authority — so `Host` says the same thing. A malicious page on
|
|
2533
|
+
* another site cannot make these agree.
|
|
2534
|
+
*/
|
|
2535
|
+
const host = hostHeaderName(req);
|
|
2536
|
+
if (host !== void 0 && url.hostname.toLowerCase() === host) return true;
|
|
2537
|
+
return false;
|
|
2538
|
+
}
|
|
2539
|
+
//#endregion
|
|
2469
2540
|
//#region src/status-paths.ts
|
|
2470
2541
|
/**
|
|
2471
2542
|
* Node-free constants and types shared by the Host and browser halves of the
|
|
@@ -2510,6 +2581,10 @@ const POOL_ACCOUNT_DISABLE_PATH = "/plugins/dsh-xdpool/accounts/disabled";
|
|
|
2510
2581
|
const POOL_ACCOUNT_IGNORE_PATH = "/plugins/dsh-xdpool/accounts/ignored";
|
|
2511
2582
|
/** Run one automation job immediately, so the card can verify it on demand. */
|
|
2512
2583
|
const POOL_AUTOMATION_RUN_PATH = "/plugins/dsh-xdpool/automation/run";
|
|
2584
|
+
/** Is there a newer build of this plugin? */
|
|
2585
|
+
const POOL_UPDATE_CHECK_PATH = "/plugins/dsh-xdpool/update";
|
|
2586
|
+
/** Install the newest build. The host applies it on the next start. */
|
|
2587
|
+
const POOL_UPDATE_INSTALL_PATH = "/plugins/dsh-xdpool/update/install";
|
|
2513
2588
|
/** Set or clear one account's reserved-credit floor. */
|
|
2514
2589
|
const POOL_CREDIT_RESERVE_PATH = "/plugins/dsh-xdpool/accounts/credit-reserve";
|
|
2515
2590
|
/**
|
|
@@ -3115,7 +3190,7 @@ function evaluateQuotaExhausted(data) {
|
|
|
3115
3190
|
}
|
|
3116
3191
|
async function fetchQuota(options = {}) {
|
|
3117
3192
|
const now = Date.now();
|
|
3118
|
-
if (!options.force && cached && now - cached.fetchedAt < 3e5) return cached;
|
|
3193
|
+
if (!options.force && cached$1 && now - cached$1.fetchedAt < 3e5) return cached$1;
|
|
3119
3194
|
if (!options.force && cachedFailure !== void 0 && now - cachedFailureAt < QUOTA_FAILURE_TTL_MS) return cachedFailure;
|
|
3120
3195
|
/**
|
|
3121
3196
|
* No session is a quotable answer, not a throw.
|
|
@@ -3162,7 +3237,7 @@ async function fetchQuota(options = {}) {
|
|
|
3162
3237
|
const plan = payload.data?.current_subscription?.display?.short_name;
|
|
3163
3238
|
if (plan) quota.plan = plan;
|
|
3164
3239
|
if (payload.data?.member_info?.hasActiveSubscription !== void 0) quota.planActive = payload.data.member_info.hasActiveSubscription;
|
|
3165
|
-
cached = quota;
|
|
3240
|
+
cached$1 = quota;
|
|
3166
3241
|
cachedFailure = void 0;
|
|
3167
3242
|
return quota;
|
|
3168
3243
|
} catch (error) {
|
|
@@ -3174,7 +3249,7 @@ async function fetchQuota(options = {}) {
|
|
|
3174
3249
|
});
|
|
3175
3250
|
}
|
|
3176
3251
|
}
|
|
3177
|
-
let cached;
|
|
3252
|
+
let cached$1;
|
|
3178
3253
|
/** The last FAILED reading, reused for {@link QUOTA_FAILURE_TTL_MS}. */
|
|
3179
3254
|
let cachedFailure;
|
|
3180
3255
|
let cachedFailureAt = 0;
|
|
@@ -3198,25 +3273,6 @@ function json$1(res, status, body) {
|
|
|
3198
3273
|
res.end(JSON.stringify(body));
|
|
3199
3274
|
}
|
|
3200
3275
|
/**
|
|
3201
|
-
* Whether the request came from the host's own page.
|
|
3202
|
-
*
|
|
3203
|
-
* An ABSENT `Origin` is allowed and is the common case: only cross-origin requests carry the
|
|
3204
|
-
* header, so a same-origin request from the card sends none. Requiring it refused every call
|
|
3205
|
-
* the UI made — the paste arrived, was rejected `403` before any parsing ran, and the card
|
|
3206
|
-
* reported it as a bad cookie. A page in the user's browser driving this endpoint is
|
|
3207
|
-
* cross-origin by construction and always sends `Origin`, so the hostname check covers it.
|
|
3208
|
-
*/
|
|
3209
|
-
function loopbackOrigin$1(req) {
|
|
3210
|
-
const origin = req.headers.origin;
|
|
3211
|
-
if (typeof origin !== "string" || origin === "") return true;
|
|
3212
|
-
try {
|
|
3213
|
-
const url = new URL(origin);
|
|
3214
|
-
return url.hostname === "127.0.0.1" || url.hostname === "localhost" || url.hostname === "::1";
|
|
3215
|
-
} catch {
|
|
3216
|
-
return false;
|
|
3217
|
-
}
|
|
3218
|
-
}
|
|
3219
|
-
/**
|
|
3220
3276
|
* Mount the Doubao routes.
|
|
3221
3277
|
*
|
|
3222
3278
|
* The sign-in route answers `signedIn: false` rather than an error when the user closes the
|
|
@@ -3228,7 +3284,7 @@ function registerDoubaoRoutes(ctx, deps) {
|
|
|
3228
3284
|
kind: "exact",
|
|
3229
3285
|
path: POOL_DOUBAO_SESSION_PATH,
|
|
3230
3286
|
handler: async (req, res) => {
|
|
3231
|
-
if (!
|
|
3287
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json$1(res, 403, { error: "origin-not-trusted" });
|
|
3232
3288
|
try {
|
|
3233
3289
|
if (req.method === "DELETE") {
|
|
3234
3290
|
deps.clear();
|
|
@@ -3305,7 +3361,7 @@ function registerDoubaoRoutes(ctx, deps) {
|
|
|
3305
3361
|
path: POOL_DOUBAO_QUOTA_PATH,
|
|
3306
3362
|
handler: async (req, res) => {
|
|
3307
3363
|
if (req.method !== "GET") return json$1(res, 405, { error: "method not allowed" });
|
|
3308
|
-
if (!
|
|
3364
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json$1(res, 403, { error: "origin-not-trusted" });
|
|
3309
3365
|
const session = deps.session();
|
|
3310
3366
|
if (session === void 0 || !sessionUsable(session)) return json$1(res, 200, {
|
|
3311
3367
|
ok: false,
|
|
@@ -3337,7 +3393,7 @@ function registerDoubaoRoutes(ctx, deps) {
|
|
|
3337
3393
|
path: POOL_DOUBAO_MODELS_PATH,
|
|
3338
3394
|
handler: async (req, res) => {
|
|
3339
3395
|
if (req.method !== "GET") return json$1(res, 405, { error: "method not allowed" });
|
|
3340
|
-
if (!
|
|
3396
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json$1(res, 403, { error: "origin-not-trusted" });
|
|
3341
3397
|
/**
|
|
3342
3398
|
* The same list the picker gets.
|
|
3343
3399
|
*
|
|
@@ -3364,7 +3420,7 @@ function registerDoubaoRoutes(ctx, deps) {
|
|
|
3364
3420
|
path: POOL_DOUBAO_CAMPAIGN_PATH,
|
|
3365
3421
|
handler: async (req, res) => {
|
|
3366
3422
|
if (req.method !== "GET") return json$1(res, 405, { error: "method not allowed" });
|
|
3367
|
-
if (!
|
|
3423
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json$1(res, 403, { error: "origin-not-trusted" });
|
|
3368
3424
|
const session = deps.session();
|
|
3369
3425
|
const campaign = await fetchCampaign(session);
|
|
3370
3426
|
return json$1(res, 200, {
|
|
@@ -3379,7 +3435,7 @@ function registerDoubaoRoutes(ctx, deps) {
|
|
|
3379
3435
|
path: POOL_DOUBAO_CLAIM_PATH,
|
|
3380
3436
|
handler: async (req, res) => {
|
|
3381
3437
|
if (req.method !== "POST") return json$1(res, 405, { error: "method not allowed" });
|
|
3382
|
-
if (!
|
|
3438
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json$1(res, 403, { error: "origin-not-trusted" });
|
|
3383
3439
|
const result = await claimTasks(deps.session());
|
|
3384
3440
|
return json$1(res, 200, {
|
|
3385
3441
|
ok: result.failed === 0,
|
|
@@ -3391,7 +3447,7 @@ function registerDoubaoRoutes(ctx, deps) {
|
|
|
3391
3447
|
kind: "exact",
|
|
3392
3448
|
path: POOL_DOUBAO_AUTOMATION_PATH,
|
|
3393
3449
|
handler: async (req, res) => {
|
|
3394
|
-
if (!
|
|
3450
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json$1(res, 403, { error: "origin-not-trusted" });
|
|
3395
3451
|
const automation = deps.automation();
|
|
3396
3452
|
try {
|
|
3397
3453
|
if (req.method === "POST") {
|
|
@@ -19664,6 +19720,149 @@ function parseBody(body) {
|
|
|
19664
19720
|
throw new Error("the request body was not a JSON object");
|
|
19665
19721
|
}
|
|
19666
19722
|
//#endregion
|
|
19723
|
+
//#region src/version.ts
|
|
19724
|
+
/**
|
|
19725
|
+
* The plugin's own version, hand-maintained for the browser bundle.
|
|
19726
|
+
*
|
|
19727
|
+
* The card ships as `lib/client.js`, served to the host dialog as a browser
|
|
19728
|
+
* script: `package.json` is neither reachable nor guaranteed to be the file
|
|
19729
|
+
* the running build came from. A manifest lying next to the code is worse than
|
|
19730
|
+
* a number a test keeps honest.
|
|
19731
|
+
*
|
|
19732
|
+
* `tests/version-sync.test.ts` fails when this drifts from `package.json`, so
|
|
19733
|
+
* the release checklist cannot ship a card that calls itself the wrong version.
|
|
19734
|
+
*
|
|
19735
|
+
* @module dsh-xdpool/version
|
|
19736
|
+
*/
|
|
19737
|
+
const POOL_VERSION = "2.7.5";
|
|
19738
|
+
//#endregion
|
|
19739
|
+
//#region src/update-check.ts
|
|
19740
|
+
/**
|
|
19741
|
+
* Whether a newer build of this plugin exists.
|
|
19742
|
+
*
|
|
19743
|
+
* WHY IT LIVES IN THE PLUGIN
|
|
19744
|
+
*
|
|
19745
|
+
* Updating meant watching the repository by hand or asking an agent to check — neither is
|
|
19746
|
+
* something a user should have to do to stay current. A version read from the registry on demand
|
|
19747
|
+
* answers the only question worth asking ("is there something newer than what I run?") without a
|
|
19748
|
+
* background task that phones home on its own schedule.
|
|
19749
|
+
*
|
|
19750
|
+
* WHAT IT ASKS
|
|
19751
|
+
*
|
|
19752
|
+
* The npm registry's abbreviated document for this package: `dist-tags.latest` and the publish
|
|
19753
|
+
* time of that version. Nothing else is read, nothing is sent beyond the package name, and the
|
|
19754
|
+
* answer is cached for a few minutes so opening the panel repeatedly is one request.
|
|
19755
|
+
*
|
|
19756
|
+
* A FAILURE IS AN ORDINARY ANSWER
|
|
19757
|
+
*
|
|
19758
|
+
* A machine behind a proxy, offline, or with no registry access still has a working plugin. The
|
|
19759
|
+
* result carries `error` and the card says "could not check" rather than hiding the control or
|
|
19760
|
+
* claiming there is nothing new — telling a user they are current when the check did not run is
|
|
19761
|
+
* the one wrong answer here.
|
|
19762
|
+
*
|
|
19763
|
+
* @module dsh-xdpool/update-check
|
|
19764
|
+
*/
|
|
19765
|
+
/** The package name this plugin publishes under. */
|
|
19766
|
+
const PACKAGE$1 = "dsh-xdpool";
|
|
19767
|
+
/** The registries tried, in order. npm first: it is the canonical one. */
|
|
19768
|
+
const REGISTRIES = [`https://registry.npmjs.org/${PACKAGE$1}/latest`, `https://registry.npmjs.org/${PACKAGE$1}`];
|
|
19769
|
+
/** How long an answer is reused. */
|
|
19770
|
+
const TTL_MS = 6e5;
|
|
19771
|
+
let cached;
|
|
19772
|
+
/**
|
|
19773
|
+
* Compare two dotted versions.
|
|
19774
|
+
*
|
|
19775
|
+
* Numeric segments compare as numbers and everything else as text, which is the ordering npm
|
|
19776
|
+
* itself uses for the plain `x.y.z` releases this package publishes. A prerelease tag is older
|
|
19777
|
+
* than its release (`2.7.3-rc.1 < 2.7.3`), which is what the `-` check below states.
|
|
19778
|
+
*/
|
|
19779
|
+
function compareVersions(left, right) {
|
|
19780
|
+
const parse = (value) => {
|
|
19781
|
+
const [core, ...rest] = value.trim().replace(/^v/u, "").split("-");
|
|
19782
|
+
return {
|
|
19783
|
+
parts: (core ?? "").split(".").map((segment) => {
|
|
19784
|
+
const number = Number.parseInt(segment, 10);
|
|
19785
|
+
return Number.isFinite(number) ? number : 0;
|
|
19786
|
+
}),
|
|
19787
|
+
pre: rest.join("-")
|
|
19788
|
+
};
|
|
19789
|
+
};
|
|
19790
|
+
const a = parse(left);
|
|
19791
|
+
const b = parse(right);
|
|
19792
|
+
const length = Math.max(a.parts.length, b.parts.length);
|
|
19793
|
+
for (let i = 0; i < length; i += 1) {
|
|
19794
|
+
const left1 = a.parts[i] ?? 0;
|
|
19795
|
+
const right1 = b.parts[i] ?? 0;
|
|
19796
|
+
if (left1 !== right1) return left1 < right1 ? -1 : 1;
|
|
19797
|
+
}
|
|
19798
|
+
if (a.pre === "" && b.pre !== "") return 1;
|
|
19799
|
+
if (a.pre !== "" && b.pre === "") return -1;
|
|
19800
|
+
return a.pre === b.pre ? 0 : a.pre < b.pre ? -1 : 1;
|
|
19801
|
+
}
|
|
19802
|
+
/** Read `dist-tags.latest` and its publish time from a registry document. */
|
|
19803
|
+
function parseRegistry(body) {
|
|
19804
|
+
if (body === null || typeof body !== "object") return {};
|
|
19805
|
+
const record = body;
|
|
19806
|
+
const direct = record["version"];
|
|
19807
|
+
if (typeof direct === "string" && direct !== "") return { latest: direct };
|
|
19808
|
+
const tags = record["dist-tags"];
|
|
19809
|
+
if (tags !== null && typeof tags === "object") {
|
|
19810
|
+
const latest = tags["latest"];
|
|
19811
|
+
if (typeof latest === "string" && latest !== "") {
|
|
19812
|
+
const times = record["time"];
|
|
19813
|
+
const when = times !== null && typeof times === "object" ? times[latest] : void 0;
|
|
19814
|
+
return {
|
|
19815
|
+
latest,
|
|
19816
|
+
...typeof when === "string" ? { publishedAt: when } : {}
|
|
19817
|
+
};
|
|
19818
|
+
}
|
|
19819
|
+
}
|
|
19820
|
+
return {};
|
|
19821
|
+
}
|
|
19822
|
+
/**
|
|
19823
|
+
* Ask the registry, or answer from the cache.
|
|
19824
|
+
*
|
|
19825
|
+
* `force` skips the cache — that is what the card's button does, and a button that silently
|
|
19826
|
+
* returns a ten-minute-old answer is indistinguishable from one that did nothing.
|
|
19827
|
+
*/
|
|
19828
|
+
async function checkForUpdate(options = {}) {
|
|
19829
|
+
const now = options.now ?? Date.now();
|
|
19830
|
+
if (options.force !== true && cached !== void 0 && now - cached.at < TTL_MS) return cached.status;
|
|
19831
|
+
const checkedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
19832
|
+
for (const url of REGISTRIES) try {
|
|
19833
|
+
const response = await fetch(url, {
|
|
19834
|
+
headers: { accept: "application/vnd.npm.install-v1+json, application/json" },
|
|
19835
|
+
signal: AbortSignal.timeout(8e3)
|
|
19836
|
+
});
|
|
19837
|
+
if (!response.ok) continue;
|
|
19838
|
+
const parsed = parseRegistry(await response.json());
|
|
19839
|
+
if (parsed.latest === void 0) continue;
|
|
19840
|
+
const status = {
|
|
19841
|
+
current: POOL_VERSION,
|
|
19842
|
+
latest: parsed.latest,
|
|
19843
|
+
newer: compareVersions(parsed.latest, POOL_VERSION) > 0,
|
|
19844
|
+
...parsed.publishedAt === void 0 ? {} : { publishedAt: parsed.publishedAt },
|
|
19845
|
+
checkedAt
|
|
19846
|
+
};
|
|
19847
|
+
cached = {
|
|
19848
|
+
at: now,
|
|
19849
|
+
status
|
|
19850
|
+
};
|
|
19851
|
+
return status;
|
|
19852
|
+
} catch {}
|
|
19853
|
+
const status = {
|
|
19854
|
+
current: POOL_VERSION,
|
|
19855
|
+
newer: false,
|
|
19856
|
+
error: "无法访问 npm registry(离线或代理未放行)",
|
|
19857
|
+
checkedAt
|
|
19858
|
+
};
|
|
19859
|
+
cached = {
|
|
19860
|
+
at: now - TTL_MS + 6e4,
|
|
19861
|
+
status
|
|
19862
|
+
};
|
|
19863
|
+
return status;
|
|
19864
|
+
}
|
|
19865
|
+
//#endregion
|
|
19667
19866
|
//#region src/task-events.ts
|
|
19668
19867
|
/**
|
|
19669
19868
|
* The app the buddy chain enters.
|
|
@@ -21459,6 +21658,164 @@ const PROVIDER_NOTES_ZH = Object.freeze({
|
|
|
21459
21658
|
"zenmux": "max_tokens 实际按 max_completion_tokens 发送"
|
|
21460
21659
|
});
|
|
21461
21660
|
//#endregion
|
|
21661
|
+
//#region src/update-install.ts
|
|
21662
|
+
/**
|
|
21663
|
+
* Installing a newer build of this plugin.
|
|
21664
|
+
*
|
|
21665
|
+
* HOW THE HOST UPDATES A PLUGIN
|
|
21666
|
+
*
|
|
21667
|
+
* There is no install API to call. A profile's `package.json` names each plugin by a spec, and
|
|
21668
|
+
* the host resolves that spec when it starts — so "update" means "change the spec, then restart".
|
|
21669
|
+
* That is the whole mechanism, and it is what every plugin that self-updates is really doing.
|
|
21670
|
+
*
|
|
21671
|
+
* WHAT THIS DOES
|
|
21672
|
+
*
|
|
21673
|
+
* A newer version is downloaded from the npm registry to a local file, and the profile's
|
|
21674
|
+
* dependency is pointed at it with a `file:` spec. On the next start the host installs from that
|
|
21675
|
+
* file. Nothing is written outside the profile's own directory, and nothing is changed if the
|
|
21676
|
+
* download fails — a half-applied update is worse than an unapplied one.
|
|
21677
|
+
*
|
|
21678
|
+
* WHY `file:` AND NOT THE PUBLISHED VERSION
|
|
21679
|
+
*
|
|
21680
|
+
* Naming the version (`"dsh-xdpool": "2.7.5"`) makes the host fetch from the registry on every
|
|
21681
|
+
* start, which fails on a machine whose registry is proxied or slow — and the failure mode is a
|
|
21682
|
+
* plugin that does not load. Pinning the tarball that was just verified to download keeps the
|
|
21683
|
+
* update as local as the install it replaces.
|
|
21684
|
+
*
|
|
21685
|
+
* A RESTART IS REQUIRED, AND SAYS SO
|
|
21686
|
+
*
|
|
21687
|
+
* Modules already loaded cannot be swapped in place. The panel reports `restartRequired` and the
|
|
21688
|
+
* user restarts when convenient — claiming the update took effect immediately would be a lie the
|
|
21689
|
+
* user discovers on the next crash.
|
|
21690
|
+
*
|
|
21691
|
+
* @module dsh-xdpool/update-install
|
|
21692
|
+
*/
|
|
21693
|
+
/** The package this plugin publishes under. */
|
|
21694
|
+
const PACKAGE = "dsh-xdpool";
|
|
21695
|
+
/** The profile directory whose `package.json` names this plugin. */
|
|
21696
|
+
function profileDir() {
|
|
21697
|
+
const candidates = [
|
|
21698
|
+
process.env["DSH_PROFILE_DIR"],
|
|
21699
|
+
process.env["DSH_HOME"] === void 0 ? void 0 : join(process.env["DSH_HOME"], "profiles", "desktop"),
|
|
21700
|
+
join(homedir(), ".dsh", "profiles", "desktop")
|
|
21701
|
+
];
|
|
21702
|
+
for (const candidate of candidates) {
|
|
21703
|
+
if (candidate === void 0) continue;
|
|
21704
|
+
if (existsSync(join(candidate, "package.json"))) return candidate;
|
|
21705
|
+
}
|
|
21706
|
+
}
|
|
21707
|
+
/**
|
|
21708
|
+
* Download a version and point the profile at it.
|
|
21709
|
+
*
|
|
21710
|
+
* The order matters: download first, verify it is a gzip tarball, and only then touch
|
|
21711
|
+
* `package.json`. A failed download must leave a working install alone.
|
|
21712
|
+
*/
|
|
21713
|
+
async function installVersion(version) {
|
|
21714
|
+
const profile = profileDir();
|
|
21715
|
+
if (profile === void 0) return {
|
|
21716
|
+
ok: false,
|
|
21717
|
+
restartRequired: false,
|
|
21718
|
+
error: "找不到插件所在的 profile 目录,无法自动更新;请在 DSH 的插件设置里更新"
|
|
21719
|
+
};
|
|
21720
|
+
const target = join(tmpdir(), `${PACKAGE}-${version}.tgz`);
|
|
21721
|
+
try {
|
|
21722
|
+
const response = await fetch(`https://registry.npmjs.org/${PACKAGE}/-/${PACKAGE}-${version}.tgz`, { signal: AbortSignal.timeout(12e4) });
|
|
21723
|
+
if (!response.ok || response.body === null) return {
|
|
21724
|
+
ok: false,
|
|
21725
|
+
restartRequired: false,
|
|
21726
|
+
error: `下载失败:HTTP ${String(response.status)}`
|
|
21727
|
+
};
|
|
21728
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
21729
|
+
const partial = `${target}.part`;
|
|
21730
|
+
const sink = createWriteStream(partial);
|
|
21731
|
+
for await (const chunk of response.body) sink.write(chunk);
|
|
21732
|
+
await new Promise((resolve, reject) => {
|
|
21733
|
+
sink.end(() => {
|
|
21734
|
+
resolve();
|
|
21735
|
+
});
|
|
21736
|
+
sink.on("error", reject);
|
|
21737
|
+
});
|
|
21738
|
+
const head = readFileSync(partial).subarray(0, 2);
|
|
21739
|
+
if (head[0] !== 31 || head[1] !== 139) {
|
|
21740
|
+
rmSync(partial, { force: true });
|
|
21741
|
+
return {
|
|
21742
|
+
ok: false,
|
|
21743
|
+
restartRequired: false,
|
|
21744
|
+
error: "下载到的不是压缩包(可能被代理拦截)"
|
|
21745
|
+
};
|
|
21746
|
+
}
|
|
21747
|
+
renameSync(partial, target);
|
|
21748
|
+
} catch (error) {
|
|
21749
|
+
return {
|
|
21750
|
+
ok: false,
|
|
21751
|
+
restartRequired: false,
|
|
21752
|
+
error: error instanceof Error ? error.message : String(error)
|
|
21753
|
+
};
|
|
21754
|
+
}
|
|
21755
|
+
try {
|
|
21756
|
+
const pkgPath = join(profile, "package.json");
|
|
21757
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
|
|
21758
|
+
let changed = false;
|
|
21759
|
+
for (const key of [
|
|
21760
|
+
"dependencies",
|
|
21761
|
+
"devDependencies",
|
|
21762
|
+
"optionalDependencies"
|
|
21763
|
+
]) {
|
|
21764
|
+
const block = pkg[key];
|
|
21765
|
+
if (block === null || typeof block !== "object") continue;
|
|
21766
|
+
const record = block;
|
|
21767
|
+
if (!Object.hasOwn(record, PACKAGE)) continue;
|
|
21768
|
+
record[PACKAGE] = `file:${target.replaceAll("\\", "/")}`;
|
|
21769
|
+
changed = true;
|
|
21770
|
+
}
|
|
21771
|
+
if (!changed) return {
|
|
21772
|
+
ok: false,
|
|
21773
|
+
restartRequired: false,
|
|
21774
|
+
path: target,
|
|
21775
|
+
error: `profile 的 package.json 里没有 ${PACKAGE},无法自动更新`
|
|
21776
|
+
};
|
|
21777
|
+
writeFileSync(pkgPath, `${JSON.stringify(pkg, null, 2)}\n`, "utf8");
|
|
21778
|
+
return {
|
|
21779
|
+
ok: true,
|
|
21780
|
+
version,
|
|
21781
|
+
path: target,
|
|
21782
|
+
restartRequired: true
|
|
21783
|
+
};
|
|
21784
|
+
} catch (error) {
|
|
21785
|
+
return {
|
|
21786
|
+
ok: false,
|
|
21787
|
+
restartRequired: false,
|
|
21788
|
+
path: target,
|
|
21789
|
+
error: error instanceof Error ? error.message : String(error)
|
|
21790
|
+
};
|
|
21791
|
+
}
|
|
21792
|
+
}
|
|
21793
|
+
/**
|
|
21794
|
+
* Check, then install if there is something newer.
|
|
21795
|
+
*
|
|
21796
|
+
* Kept together so the route cannot install a version the check never saw — the alternative is a
|
|
21797
|
+
* caller passing an arbitrary version string to an endpoint that writes `package.json`.
|
|
21798
|
+
*/
|
|
21799
|
+
async function updateToLatest() {
|
|
21800
|
+
const status = await checkForUpdate({ force: true });
|
|
21801
|
+
if (status.error !== void 0) return {
|
|
21802
|
+
ok: false,
|
|
21803
|
+
restartRequired: false,
|
|
21804
|
+
error: status.error
|
|
21805
|
+
};
|
|
21806
|
+
if (status.latest === void 0) return {
|
|
21807
|
+
ok: false,
|
|
21808
|
+
restartRequired: false,
|
|
21809
|
+
error: "没有读到最新版本号"
|
|
21810
|
+
};
|
|
21811
|
+
if (!status.newer) return {
|
|
21812
|
+
ok: true,
|
|
21813
|
+
version: status.current,
|
|
21814
|
+
restartRequired: false
|
|
21815
|
+
};
|
|
21816
|
+
return await installVersion(status.latest);
|
|
21817
|
+
}
|
|
21818
|
+
//#endregion
|
|
21462
21819
|
//#region src/web-status.ts
|
|
21463
21820
|
/** Redact token-like content before it crosses to the browser. */
|
|
21464
21821
|
/**
|
|
@@ -21487,17 +21844,6 @@ function json(res, status, body) {
|
|
|
21487
21844
|
});
|
|
21488
21845
|
res.end(payload);
|
|
21489
21846
|
}
|
|
21490
|
-
/** Loopback browser origins only; other devices are refused. */
|
|
21491
|
-
function loopbackOrigin(req) {
|
|
21492
|
-
const origin = req.headers.origin;
|
|
21493
|
-
if (origin === void 0) return true;
|
|
21494
|
-
try {
|
|
21495
|
-
const { hostname } = new URL(origin);
|
|
21496
|
-
return hostname === "localhost" || hostname === "127.0.0.1" || hostname === "[::1]" || hostname === "::1";
|
|
21497
|
-
} catch {
|
|
21498
|
-
return false;
|
|
21499
|
-
}
|
|
21500
|
-
}
|
|
21501
21847
|
/** Smallest possible POST body reader, capped so a hung or oversized body
|
|
21502
21848
|
* cannot pin memory on the Host. Returns `{}` for an empty body. */
|
|
21503
21849
|
function readJsonBody(req) {
|
|
@@ -22044,7 +22390,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22044
22390
|
path: POOL_STATUS_PATH,
|
|
22045
22391
|
handler: async (req, res) => {
|
|
22046
22392
|
if (req.method !== "GET") return json(res, 405, { error: "method not allowed" });
|
|
22047
|
-
if (!
|
|
22393
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22048
22394
|
try {
|
|
22049
22395
|
json(res, 200, await poolWebStatus(deps, new URL(req.url ?? "/", "http://localhost").searchParams.get("region") === "global" ? "global" : "cn"));
|
|
22050
22396
|
} catch (error) {
|
|
@@ -22064,7 +22410,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22064
22410
|
path: POOL_USAGE_PATH,
|
|
22065
22411
|
handler: async (req, res) => {
|
|
22066
22412
|
if (req.method !== "GET") return json(res, 405, { error: "method not allowed" });
|
|
22067
|
-
if (!
|
|
22413
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22068
22414
|
if (deps.usage === void 0) return json(res, 503, { error: "usage accounting is not available in this build" });
|
|
22069
22415
|
try {
|
|
22070
22416
|
json(res, 200, deps.usage());
|
|
@@ -22090,7 +22436,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22090
22436
|
path: POOL_CATALOG_REFRESH_PATH,
|
|
22091
22437
|
handler: async (req, res) => {
|
|
22092
22438
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22093
|
-
if (!
|
|
22439
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22094
22440
|
if (deps.refreshCatalog === void 0) return json(res, 503, { error: "catalog refresh is not available in this build" });
|
|
22095
22441
|
try {
|
|
22096
22442
|
json(res, 200, {
|
|
@@ -22107,7 +22453,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22107
22453
|
path: POOL_RESCAN_PATH,
|
|
22108
22454
|
handler: async (req, res) => {
|
|
22109
22455
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22110
|
-
if (!
|
|
22456
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22111
22457
|
try {
|
|
22112
22458
|
json(res, 200, { accounts: (await deps.pool.scan()).length });
|
|
22113
22459
|
} catch (error) {
|
|
@@ -22120,7 +22466,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22120
22466
|
path: POOL_RESET_COOLDOWN_PATH,
|
|
22121
22467
|
handler: async (req, res) => {
|
|
22122
22468
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22123
|
-
if (!
|
|
22469
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22124
22470
|
deps.pool.resetCooldowns();
|
|
22125
22471
|
json(res, 200, { ok: true });
|
|
22126
22472
|
}
|
|
@@ -22130,7 +22476,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22130
22476
|
path: POOL_CHECKIN_PATH,
|
|
22131
22477
|
handler: async (req, res) => {
|
|
22132
22478
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22133
|
-
if (!
|
|
22479
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22134
22480
|
try {
|
|
22135
22481
|
const body = await readJsonBody(req);
|
|
22136
22482
|
const accountId = typeof body["accountId"] === "string" ? body["accountId"] : "";
|
|
@@ -22162,7 +22508,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22162
22508
|
path: POOL_MODELS_SAVE_PATH,
|
|
22163
22509
|
handler: async (req, res) => {
|
|
22164
22510
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22165
|
-
if (!
|
|
22511
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22166
22512
|
if (deps.saveSelection === void 0) return json(res, 503, { error: "settings service unavailable; model selection cannot be saved" });
|
|
22167
22513
|
try {
|
|
22168
22514
|
const body = await readJsonBody(req);
|
|
@@ -22185,7 +22531,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22185
22531
|
path: POOL_ACCOUNT_DISABLE_PATH,
|
|
22186
22532
|
handler: async (req, res) => {
|
|
22187
22533
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22188
|
-
if (!
|
|
22534
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22189
22535
|
if (deps.setAccountDisabled === void 0) return json(res, 503, { error: "settings service unavailable; the account switch cannot be saved" });
|
|
22190
22536
|
try {
|
|
22191
22537
|
const body = await readJsonBody(req);
|
|
@@ -22220,7 +22566,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22220
22566
|
path: POOL_ACCOUNT_IGNORE_PATH,
|
|
22221
22567
|
handler: async (req, res) => {
|
|
22222
22568
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22223
|
-
if (!
|
|
22569
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22224
22570
|
if (deps.setAccountIgnored === void 0) return json(res, 503, { error: "settings service unavailable; the ignore list cannot be saved" });
|
|
22225
22571
|
try {
|
|
22226
22572
|
const body = await readJsonBody(req);
|
|
@@ -22243,7 +22589,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22243
22589
|
path: POOL_ACCOUNT_PREFER_PATH,
|
|
22244
22590
|
handler: async (req, res) => {
|
|
22245
22591
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22246
|
-
if (!
|
|
22592
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22247
22593
|
if (deps.setPreferredAccount === void 0) return json(res, 503, { error: "the preferred account cannot be saved in this build" });
|
|
22248
22594
|
try {
|
|
22249
22595
|
const body = await readJsonBody(req);
|
|
@@ -22272,7 +22618,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22272
22618
|
path: POOL_AUTOMATION_RUN_PATH,
|
|
22273
22619
|
handler: async (req, res) => {
|
|
22274
22620
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22275
|
-
if (!
|
|
22621
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22276
22622
|
if (deps.runAutomation === void 0) return json(res, 503, { error: "automation is not available in this build" });
|
|
22277
22623
|
try {
|
|
22278
22624
|
const run = parseAutomationRun(await readJsonBody(req));
|
|
@@ -22300,7 +22646,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22300
22646
|
path: POOL_CREDIT_RESERVE_PATH,
|
|
22301
22647
|
handler: async (req, res) => {
|
|
22302
22648
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22303
|
-
if (!
|
|
22649
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22304
22650
|
if (deps.setCreditReserve === void 0) return json(res, 503, { error: "settings service unavailable; the reserve cannot be saved" });
|
|
22305
22651
|
try {
|
|
22306
22652
|
const body = await readJsonBody(req);
|
|
@@ -22335,7 +22681,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22335
22681
|
path: POOL_AGGREGATE_PATH,
|
|
22336
22682
|
handler: async (req, res) => {
|
|
22337
22683
|
if (req.method !== "GET") return json(res, 405, { error: "method not allowed" });
|
|
22338
|
-
if (!
|
|
22684
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22339
22685
|
if (deps.aggregateDocument === void 0) return json(res, 503, { error: "the aggregate lane is not available in this build" });
|
|
22340
22686
|
try {
|
|
22341
22687
|
json(res, 200, await deps.aggregateDocument());
|
|
@@ -22361,7 +22707,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22361
22707
|
path: POOL_AGGREGATE_SAVE_PATH,
|
|
22362
22708
|
handler: async (req, res) => {
|
|
22363
22709
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22364
|
-
if (!
|
|
22710
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22365
22711
|
if (deps.saveAggregate === void 0) return json(res, 503, { error: "the aggregate lane is not available in this build" });
|
|
22366
22712
|
try {
|
|
22367
22713
|
const patch = parseAggregatePatch(await readJsonBody(req));
|
|
@@ -22393,7 +22739,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22393
22739
|
path: POOL_AGGREGATE_TEST_PATH,
|
|
22394
22740
|
handler: async (req, res) => {
|
|
22395
22741
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22396
|
-
if (!
|
|
22742
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22397
22743
|
if (deps.probeAggregate === void 0) return json(res, 503, { error: "the aggregate lane is not available in this build" });
|
|
22398
22744
|
try {
|
|
22399
22745
|
const providerId = parseProviderId(await readJsonBody(req));
|
|
@@ -22422,7 +22768,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22422
22768
|
path: POOL_MODEL_PROBE_PATH,
|
|
22423
22769
|
handler: async (req, res) => {
|
|
22424
22770
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22425
|
-
if (!
|
|
22771
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22426
22772
|
if (deps.probeModel === void 0) return json(res, 503, { error: "the connectivity probe is not available in this build" });
|
|
22427
22773
|
const controller = new AbortController();
|
|
22428
22774
|
req.on("aborted", () => {
|
|
@@ -22470,7 +22816,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22470
22816
|
path: POOL_CHANNEL_MODELS_PATH,
|
|
22471
22817
|
handler: async (req, res) => {
|
|
22472
22818
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22473
|
-
if (!
|
|
22819
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22474
22820
|
if (deps.channelModels === void 0) return json(res, 503, { error: "the login channels are not available in this build" });
|
|
22475
22821
|
try {
|
|
22476
22822
|
const body = await readJsonBody(req);
|
|
@@ -22487,7 +22833,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22487
22833
|
path: POOL_AGGREGATE_DISCOVER_PATH,
|
|
22488
22834
|
handler: async (req, res) => {
|
|
22489
22835
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22490
|
-
if (!
|
|
22836
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22491
22837
|
if (deps.discoverAggregate === void 0) return json(res, 503, { error: "the aggregate lane is not available in this build" });
|
|
22492
22838
|
try {
|
|
22493
22839
|
const providerId = parseProviderId(await readJsonBody(req));
|
|
@@ -22503,7 +22849,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22503
22849
|
path: POOL_CHANNELS_PATH,
|
|
22504
22850
|
handler: (req, res) => {
|
|
22505
22851
|
if (req.method !== "GET") return json(res, 405, { error: "method not allowed" });
|
|
22506
|
-
if (!
|
|
22852
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22507
22853
|
if (deps.channelsDocument === void 0) return json(res, 503, { error: "the login channels are not available in this build" });
|
|
22508
22854
|
try {
|
|
22509
22855
|
json(res, 200, deps.channelsDocument());
|
|
@@ -22525,7 +22871,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22525
22871
|
path: POOL_COMBOS_PATH,
|
|
22526
22872
|
handler: async (req, res) => {
|
|
22527
22873
|
if (req.method !== "GET") return json(res, 405, { error: "method not allowed" });
|
|
22528
|
-
if (!
|
|
22874
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22529
22875
|
if (deps.combosDocument === void 0) return json(res, 503, { error: "the routing chains are not available in this build" });
|
|
22530
22876
|
try {
|
|
22531
22877
|
json(res, 200, await deps.combosDocument());
|
|
@@ -22546,7 +22892,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22546
22892
|
path: POOL_CREDITS_SPEND_PATH,
|
|
22547
22893
|
handler: async (req, res) => {
|
|
22548
22894
|
if (req.method !== "GET") return json(res, 405, { error: "method not allowed" });
|
|
22549
|
-
if (!
|
|
22895
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22550
22896
|
if (deps.creditsSpend === void 0) return json(res, 503, { error: "credit accounting is not available in this build" });
|
|
22551
22897
|
try {
|
|
22552
22898
|
const raw = new URL(req.url ?? "/", "http://127.0.0.1").searchParams.get("days");
|
|
@@ -22569,7 +22915,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22569
22915
|
path: POOL_COMBOS_SAVE_PATH,
|
|
22570
22916
|
handler: async (req, res) => {
|
|
22571
22917
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22572
|
-
if (!
|
|
22918
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22573
22919
|
if (deps.saveCombos === void 0) return json(res, 503, { error: "the routing chains are not available in this build" });
|
|
22574
22920
|
try {
|
|
22575
22921
|
const body = await readJsonBody(req);
|
|
@@ -22595,7 +22941,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22595
22941
|
path: POOL_RELAY_PATH,
|
|
22596
22942
|
handler: async (req, res) => {
|
|
22597
22943
|
if (req.method !== "GET" && req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22598
|
-
if (!
|
|
22944
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22599
22945
|
if (deps.relayDocument === void 0 || deps.saveRelay === void 0) return json(res, 503, { error: "the LAN relay is not available in this build" });
|
|
22600
22946
|
try {
|
|
22601
22947
|
if (req.method === "GET") {
|
|
@@ -22609,7 +22955,57 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22609
22955
|
}
|
|
22610
22956
|
}
|
|
22611
22957
|
});
|
|
22958
|
+
/**
|
|
22959
|
+
* Is there a newer build?
|
|
22960
|
+
*
|
|
22961
|
+
* `POST` forces a fresh read — that is the panel's button, and a button that silently
|
|
22962
|
+
* returns a cached answer is indistinguishable from one that did nothing.
|
|
22963
|
+
*/
|
|
22964
|
+
const disposeUpdate = ctx.webServer.register({
|
|
22965
|
+
kind: "exact",
|
|
22966
|
+
path: POOL_UPDATE_CHECK_PATH,
|
|
22967
|
+
handler: async (req, res) => {
|
|
22968
|
+
if (req.method !== "GET" && req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22969
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22970
|
+
try {
|
|
22971
|
+
return json(res, 200, await checkForUpdate({ force: req.method === "POST" }));
|
|
22972
|
+
} catch (error) {
|
|
22973
|
+
return json(res, 200, {
|
|
22974
|
+
current: "unknown",
|
|
22975
|
+
newer: false,
|
|
22976
|
+
error: safeMessage(error),
|
|
22977
|
+
checkedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
22978
|
+
});
|
|
22979
|
+
}
|
|
22980
|
+
}
|
|
22981
|
+
});
|
|
22982
|
+
/**
|
|
22983
|
+
* Install the newest build.
|
|
22984
|
+
*
|
|
22985
|
+
* `POST` only, and the BODY is ignored: the version comes from the same check the panel
|
|
22986
|
+
* shows, never from the caller. An endpoint that writes `package.json` must not take a
|
|
22987
|
+
* version string on trust.
|
|
22988
|
+
*/
|
|
22989
|
+
const disposeInstall = ctx.webServer.register({
|
|
22990
|
+
kind: "exact",
|
|
22991
|
+
path: POOL_UPDATE_INSTALL_PATH,
|
|
22992
|
+
handler: async (req, res) => {
|
|
22993
|
+
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22994
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22995
|
+
try {
|
|
22996
|
+
return json(res, 200, await updateToLatest());
|
|
22997
|
+
} catch (error) {
|
|
22998
|
+
return json(res, 200, {
|
|
22999
|
+
ok: false,
|
|
23000
|
+
restartRequired: false,
|
|
23001
|
+
error: safeMessage(error)
|
|
23002
|
+
});
|
|
23003
|
+
}
|
|
23004
|
+
}
|
|
23005
|
+
});
|
|
22612
23006
|
return () => {
|
|
23007
|
+
disposeInstall();
|
|
23008
|
+
disposeUpdate();
|
|
22613
23009
|
disposeRelay();
|
|
22614
23010
|
disposeCombosSave();
|
|
22615
23011
|
disposeCombos();
|
|
@@ -27566,6 +27962,26 @@ function apply(ctx, config = {}) {
|
|
|
27566
27962
|
rateOf
|
|
27567
27963
|
});
|
|
27568
27964
|
};
|
|
27965
|
+
/**
|
|
27966
|
+
* The authorities this deployment serves, read per request.
|
|
27967
|
+
*
|
|
27968
|
+
* `connection.trustedHosts` is what the host's own `/api` bridge trusts — Tailscale serve, a
|
|
27969
|
+
* LAN hostname, a reverse proxy. The plugin's routes used to accept loopback only, so behind
|
|
27970
|
+
* any of those the card displayed its data and refused every button (#164). Reading it fresh
|
|
27971
|
+
* means a configuration change reaches the next request without a restart.
|
|
27972
|
+
*
|
|
27973
|
+
* A missing service is not an error: it simply means nothing beyond loopback is trusted, which
|
|
27974
|
+
* is the desktop default.
|
|
27975
|
+
*/
|
|
27976
|
+
const trustedHosts = () => {
|
|
27977
|
+
try {
|
|
27978
|
+
const list = ctx.connection?.config?.trustedHosts;
|
|
27979
|
+
if (!Array.isArray(list)) return [];
|
|
27980
|
+
return list.filter((entry) => typeof entry === "string");
|
|
27981
|
+
} catch {
|
|
27982
|
+
return [];
|
|
27983
|
+
}
|
|
27984
|
+
};
|
|
27569
27985
|
ctx.inject(["webServer"], (webCtx) => {
|
|
27570
27986
|
/**
|
|
27571
27987
|
* Doubao's three routes.
|
|
@@ -27576,6 +27992,7 @@ function apply(ctx, config = {}) {
|
|
|
27576
27992
|
*/
|
|
27577
27993
|
registerDoubaoRoutes(webCtx, {
|
|
27578
27994
|
session: () => doubaoSession,
|
|
27995
|
+
trustedHosts,
|
|
27579
27996
|
save: (session) => {
|
|
27580
27997
|
adoptDoubaoSession(session);
|
|
27581
27998
|
writeStoredSession(session);
|
|
@@ -27588,6 +28005,7 @@ function apply(ctx, config = {}) {
|
|
|
27588
28005
|
});
|
|
27589
28006
|
return registerPoolStatusRoute(webCtx, {
|
|
27590
28007
|
pool: core.pool,
|
|
28008
|
+
trustedHosts,
|
|
27591
28009
|
catalogs: core.catalogs,
|
|
27592
28010
|
client: core.client,
|
|
27593
28011
|
usage: () => usageRecorder.summary(),
|