@gruu6y/page-clean-login 0.2.10 → 0.2.12
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/account-browser.mjs +459 -0
- package/lib/agent-cli.mjs +22 -0
- package/lib/agent-surface.mjs +15 -3
- package/lib/browser-launcher.mjs +142 -36
- package/lib/renew-hosting.mjs +33 -0
- package/lib/server.mjs +166 -14
- package/lib/signin-flow.mjs +12 -0
- package/package.json +2 -2
- package/ui/account-view.js +140 -1
- package/ui/app.js +403 -0
- package/ui/index.html +5 -0
- package/ui/style.css +33 -0
package/lib/server.mjs
CHANGED
|
@@ -41,11 +41,12 @@ import { MarketInstallError, createMarketInstaller } from "./market-install.mjs"
|
|
|
41
41
|
import { RenewHostingError, createRenewHosting, offView } from "./renew-hosting.mjs";
|
|
42
42
|
import { buildIssueRequest, isAllowedIssueUrl } from "./issue-report.mjs";
|
|
43
43
|
import { openExternal, OpenExternalError } from "./open-external.mjs";
|
|
44
|
-
import { createEnvironmentManager, EnvironmentError } from "./browser-environment.mjs";
|
|
44
|
+
import { EXECUTABLE_ENV, createEnvironmentManager, EnvironmentError } from "./browser-environment.mjs";
|
|
45
45
|
import { SIGNIN_TERMINAL_STATES, createSignInFlow, SignInFlowError } from "./signin-flow.mjs";
|
|
46
46
|
import { sanitizeSubmittedInputs } from "./method-inputs.mjs";
|
|
47
47
|
import { createNativeSignIn, NativeSignInError } from "./native-signin.mjs";
|
|
48
48
|
import { SignInBrowserError } from "./browser-launcher.mjs";
|
|
49
|
+
import { AccountBrowserError, createAccountBrowserController } from "./account-browser.mjs";
|
|
49
50
|
import { createProxyScore, ProxyScoreError } from "./proxy-score.mjs";
|
|
50
51
|
import {
|
|
51
52
|
AccountRecordError,
|
|
@@ -84,7 +85,7 @@ import {
|
|
|
84
85
|
resolveAccountProxyName,
|
|
85
86
|
resolveEffectiveAccountExit,
|
|
86
87
|
} from "./account-exit.mjs";
|
|
87
|
-
import { readAuthEntries, readAuthExpiries } from "./signin-writeback.mjs";
|
|
88
|
+
import { readAuthEntries, readAuthExpiries, signInKey } from "./signin-writeback.mjs";
|
|
88
89
|
import { createProviderRegistration } from "./provider-registration.mjs";
|
|
89
90
|
import {
|
|
90
91
|
agentDigest,
|
|
@@ -241,7 +242,8 @@ function sendError(res, error) {
|
|
|
241
242
|
error instanceof IdentityCheckError ||
|
|
242
243
|
error instanceof OpenExternalError ||
|
|
243
244
|
error instanceof MarketInstallError ||
|
|
244
|
-
error instanceof RenewHostingError
|
|
245
|
+
error instanceof RenewHostingError ||
|
|
246
|
+
error instanceof AccountBrowserError
|
|
245
247
|
) {
|
|
246
248
|
json(res, error.status, { error: error.code, message: error.message });
|
|
247
249
|
return;
|
|
@@ -374,6 +376,19 @@ function accountExitFromPath(path) {
|
|
|
374
376
|
}
|
|
375
377
|
}
|
|
376
378
|
|
|
379
|
+
// `/api/accounts/<provider>/<key>/browser`: open, inspect, or close the
|
|
380
|
+
// account's standalone fingerprint browser. Resolved BEFORE `accountKeyFromPath`
|
|
381
|
+
// so the three-segment path is never swallowed as an account key.
|
|
382
|
+
function accountBrowserFromPath(path) {
|
|
383
|
+
const match = /^\/api\/accounts\/([^/]+)\/([^/]+)\/browser$/.exec(path);
|
|
384
|
+
if (!match) return null;
|
|
385
|
+
try {
|
|
386
|
+
return { provider: decodeURIComponent(match[1]), key: decodeURIComponent(match[2]) };
|
|
387
|
+
} catch {
|
|
388
|
+
throw new HttpError(400, "invalid_account", "The account path is invalid.");
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
377
392
|
// Human-readable companions to the exit writer's machine reasons. The HTTP
|
|
378
393
|
// error code stays the reason itself, so the page can tailor its hint.
|
|
379
394
|
const EXIT_WRITE_MESSAGES = {
|
|
@@ -519,8 +534,13 @@ export async function startServer({
|
|
|
519
534
|
// The hosted-renewal controller (tests inject a fake one) so no test ever
|
|
520
535
|
// opens a background browser or touches a real credential.
|
|
521
536
|
renewHosting: renewHostingOption,
|
|
537
|
+
// The standalone account-browser controller (tests inject a fake one) and
|
|
538
|
+
// the launcher seam the real controller opens its browser through.
|
|
539
|
+
accountBrowser: accountBrowserOption,
|
|
540
|
+
accountBrowserLauncher,
|
|
522
541
|
} = {}) {
|
|
523
542
|
const token = randomBytes(32).toString("hex");
|
|
543
|
+
const effectiveEnv = env ?? process.env;
|
|
524
544
|
const store = createProxyStore({ dataDir });
|
|
525
545
|
// The asset registry shares the plugin's private data directory but keeps
|
|
526
546
|
// its own file, so it never has to migrate the proxy store's shape.
|
|
@@ -621,13 +641,53 @@ export async function startServer({
|
|
|
621
641
|
onCheck: (check) => recordCheck(check),
|
|
622
642
|
});
|
|
623
643
|
|
|
644
|
+
async function resolveBrowserExecutable() {
|
|
645
|
+
if (environment?.status) {
|
|
646
|
+
try {
|
|
647
|
+
const status = await environment.status();
|
|
648
|
+
if (typeof status?.executablePath === "string" && status.executablePath) {
|
|
649
|
+
return status.executablePath;
|
|
650
|
+
}
|
|
651
|
+
} catch {
|
|
652
|
+
// Fall through to the environment override.
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
return effectiveEnv?.[EXECUTABLE_ENV] || process.env[EXECUTABLE_ENV] || null;
|
|
656
|
+
}
|
|
657
|
+
|
|
624
658
|
// Hosted renewal watches the credentials it has been explicitly enabled for
|
|
625
659
|
// and re-runs those accounts' own sign-in, headless, through the exit they
|
|
626
660
|
// logged in with. It shares the sign-in controller, so a hosted renewal and
|
|
627
|
-
// an interactive login can never run at the same time.
|
|
661
|
+
// an interactive login can never run at the same time. The account-browser
|
|
662
|
+
// guard uses a lazy wrapper so renewHosting and accountBrowser never deadlock
|
|
663
|
+
// on mutual initialization.
|
|
664
|
+
let accountBrowser = null;
|
|
628
665
|
const renewHosting =
|
|
629
666
|
renewHostingOption ||
|
|
630
|
-
createRenewHosting({
|
|
667
|
+
createRenewHosting({
|
|
668
|
+
env: effectiveEnv,
|
|
669
|
+
dataDir,
|
|
670
|
+
signIn,
|
|
671
|
+
store,
|
|
672
|
+
identityCheck,
|
|
673
|
+
accountBrowser: {
|
|
674
|
+
isOpen: (provider, key) => accountBrowser?.isOpen?.(provider, key) === true,
|
|
675
|
+
},
|
|
676
|
+
});
|
|
677
|
+
|
|
678
|
+
// Standalone account-browser controller: lets the user open an account's own
|
|
679
|
+
// pinned Camoufox profile through its live effective exit without mutating
|
|
680
|
+
// identity or exit state.
|
|
681
|
+
accountBrowser =
|
|
682
|
+
accountBrowserOption ||
|
|
683
|
+
createAccountBrowserController({
|
|
684
|
+
env: effectiveEnv,
|
|
685
|
+
store,
|
|
686
|
+
signIn,
|
|
687
|
+
renewHosting,
|
|
688
|
+
resolveExecutable: resolveBrowserExecutable,
|
|
689
|
+
...(accountBrowserLauncher ? { launchBrowser: accountBrowserLauncher } : {}),
|
|
690
|
+
});
|
|
631
691
|
|
|
632
692
|
// A single account object, used both by the list and by the PATCH response.
|
|
633
693
|
// Optional fields are whitelisted projections of the ledger; the fingerprint
|
|
@@ -656,6 +716,14 @@ export async function startServer({
|
|
|
656
716
|
? entry.meta.methodLabel
|
|
657
717
|
: null;
|
|
658
718
|
const expiresAt = authExpiries?.[entry.key] ?? hosting?.expiresAt ?? null;
|
|
719
|
+
const browser =
|
|
720
|
+
typeof accountBrowser?.status === "function"
|
|
721
|
+
? await accountBrowser.status(
|
|
722
|
+
entry.provider,
|
|
723
|
+
entry.key,
|
|
724
|
+
authContext?.liveResolved ? { liveResolved: authContext.liveResolved } : {},
|
|
725
|
+
)
|
|
726
|
+
: { open: false, openedAt: null, exit: null, exitChanged: false };
|
|
659
727
|
return {
|
|
660
728
|
provider: entry.provider,
|
|
661
729
|
key: entry.key,
|
|
@@ -669,6 +737,7 @@ export async function startServer({
|
|
|
669
737
|
...extras,
|
|
670
738
|
fingerprint: await readFingerprintSummary(entry.dir),
|
|
671
739
|
hosting,
|
|
740
|
+
browser,
|
|
672
741
|
};
|
|
673
742
|
}
|
|
674
743
|
|
|
@@ -704,7 +773,6 @@ export async function startServer({
|
|
|
704
773
|
// The account orphan sweep: one run per process at startup, plus an on-demand
|
|
705
774
|
// run from POST /api/accounts/prune. The last report is summarized on
|
|
706
775
|
// /api/health. It is fire-and-forget and never throws.
|
|
707
|
-
const effectiveEnv = env ?? process.env;
|
|
708
776
|
let lastAccountPrune = null;
|
|
709
777
|
let lastAccountPruneAt = null;
|
|
710
778
|
let pruneRun = null;
|
|
@@ -761,6 +829,15 @@ export async function startServer({
|
|
|
761
829
|
if (state !== "idle" && !SIGNIN_TERMINAL_STATES.has(state) && flow?.provider && flow?.key) {
|
|
762
830
|
dirs.add(accountDir(effectiveEnv, flow.provider, flow.key));
|
|
763
831
|
}
|
|
832
|
+
try {
|
|
833
|
+
for (const open of accountBrowser?.listOpen?.() ?? []) {
|
|
834
|
+
if (open?.provider && open?.key) {
|
|
835
|
+
dirs.add(accountDir(effectiveEnv, open.provider, open.key));
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
} catch {
|
|
839
|
+
// Best effort.
|
|
840
|
+
}
|
|
764
841
|
return dirs;
|
|
765
842
|
}
|
|
766
843
|
|
|
@@ -894,9 +971,16 @@ export async function startServer({
|
|
|
894
971
|
nativeEntries,
|
|
895
972
|
providersDoc,
|
|
896
973
|
proxyByUrl,
|
|
897
|
-
|
|
974
|
+
// When the account's standalone browser is open, match open()'s
|
|
975
|
+
// fallbackToLedger: true resolution so exitChanged stays accurate even
|
|
976
|
+
// when providers.json is absent; accountExitProjection still only
|
|
977
|
+
// overrides `account.exit` for authoritative providers.json sources.
|
|
978
|
+
fallbackToLedger: accountBrowser?.isOpen?.(entry.provider, entry.key) === true,
|
|
898
979
|
});
|
|
899
|
-
return
|
|
980
|
+
return {
|
|
981
|
+
exit: accountExitProjection(resolved, accountExtras(entry.meta).exit),
|
|
982
|
+
resolved,
|
|
983
|
+
};
|
|
900
984
|
}
|
|
901
985
|
|
|
902
986
|
// Every known account with its ownership ledger fields and whether its key
|
|
@@ -930,14 +1014,18 @@ export async function startServer({
|
|
|
930
1014
|
const accounts = [];
|
|
931
1015
|
for (const entry of dirs) {
|
|
932
1016
|
if (!entry.meta) continue;
|
|
933
|
-
const liveExit = await liveAccountExit(entry, {
|
|
1017
|
+
const { exit: liveExit, resolved: liveResolved } = await liveAccountExit(entry, {
|
|
934
1018
|
authEntries,
|
|
935
1019
|
nativeEntries,
|
|
936
1020
|
providersDoc,
|
|
937
1021
|
proxyByUrl,
|
|
938
1022
|
});
|
|
939
1023
|
accounts.push(
|
|
940
|
-
await accountView(entry, keys, hostingById, liveExit, {
|
|
1024
|
+
await accountView(entry, keys, hostingById, liveExit, {
|
|
1025
|
+
authEntries,
|
|
1026
|
+
authExpiries,
|
|
1027
|
+
liveResolved,
|
|
1028
|
+
}),
|
|
941
1029
|
);
|
|
942
1030
|
}
|
|
943
1031
|
// Deterministic order: provider, then account key.
|
|
@@ -1952,7 +2040,7 @@ export async function startServer({
|
|
|
1952
2040
|
await writeAccountMeta(dir, merged);
|
|
1953
2041
|
return merged;
|
|
1954
2042
|
});
|
|
1955
|
-
const liveExit = await liveAccountExit(
|
|
2043
|
+
const { exit: liveExit, resolved: liveResolved } = await liveAccountExit(
|
|
1956
2044
|
{ provider, key, dir, meta: updatedMeta },
|
|
1957
2045
|
{ authEntries, nativeEntries },
|
|
1958
2046
|
);
|
|
@@ -1961,7 +2049,7 @@ export async function startServer({
|
|
|
1961
2049
|
keys,
|
|
1962
2050
|
null,
|
|
1963
2051
|
liveExit,
|
|
1964
|
-
{ authEntries },
|
|
2052
|
+
{ authEntries, liveResolved },
|
|
1965
2053
|
);
|
|
1966
2054
|
} catch {
|
|
1967
2055
|
// A ledger write failure must not change the reported exit change.
|
|
@@ -1976,6 +2064,40 @@ export async function startServer({
|
|
|
1976
2064
|
return methodNotAllowed(res, "GET, PATCH");
|
|
1977
2065
|
}
|
|
1978
2066
|
|
|
2067
|
+
// Standalone fingerprint browser per account. Resolved BEFORE
|
|
2068
|
+
// `accountKeyFromPath` so `/browser` is never swallowed as an account key.
|
|
2069
|
+
const browserTarget = accountBrowserFromPath(url.pathname);
|
|
2070
|
+
if (browserTarget) {
|
|
2071
|
+
const { provider, key } = browserTarget;
|
|
2072
|
+
if (req.method === "GET") {
|
|
2073
|
+
const meta = await readAccountMeta(accountDir(effectiveEnv, provider, key));
|
|
2074
|
+
if (!meta || meta.provider !== provider) {
|
|
2075
|
+
throw new HttpError(404, "not_found", "Account not found.");
|
|
2076
|
+
}
|
|
2077
|
+
json(res, 200, { ok: true, browser: await accountBrowser.status(provider, key) });
|
|
2078
|
+
return;
|
|
2079
|
+
}
|
|
2080
|
+
if (req.method === "POST") {
|
|
2081
|
+
const body = await readJson(req, { optional: true });
|
|
2082
|
+
if (body !== null && (typeof body !== "object" || Array.isArray(body))) {
|
|
2083
|
+
throw new HttpError(400, "invalid_body", "Request body must be a JSON object.");
|
|
2084
|
+
}
|
|
2085
|
+
const browser = await accountBrowser.open({ provider, key, url: body?.url });
|
|
2086
|
+
json(res, 200, { ok: true, browser });
|
|
2087
|
+
return;
|
|
2088
|
+
}
|
|
2089
|
+
if (req.method === "DELETE") {
|
|
2090
|
+
const meta = await readAccountMeta(accountDir(effectiveEnv, provider, key));
|
|
2091
|
+
if (!meta || meta.provider !== provider) {
|
|
2092
|
+
throw new HttpError(404, "not_found", "Account not found.");
|
|
2093
|
+
}
|
|
2094
|
+
const result = await accountBrowser.close(provider, key);
|
|
2095
|
+
json(res, 200, { ok: true, ...result });
|
|
2096
|
+
return;
|
|
2097
|
+
}
|
|
2098
|
+
return methodNotAllowed(res, "GET, POST, DELETE");
|
|
2099
|
+
}
|
|
2100
|
+
|
|
1979
2101
|
const accountTarget = accountKeyFromPath(url.pathname);
|
|
1980
2102
|
if (accountTarget) {
|
|
1981
2103
|
const dir = accountDir(effectiveEnv, accountTarget.provider, accountTarget.key);
|
|
@@ -1994,14 +2116,27 @@ export async function startServer({
|
|
|
1994
2116
|
return merged;
|
|
1995
2117
|
});
|
|
1996
2118
|
const keys = liveAccountKeys();
|
|
1997
|
-
const liveExit = await liveAccountExit({
|
|
2119
|
+
const { exit: liveExit, resolved: liveResolved } = await liveAccountExit({
|
|
2120
|
+
...accountTarget,
|
|
2121
|
+
dir,
|
|
2122
|
+
meta: updated,
|
|
2123
|
+
});
|
|
1998
2124
|
json(res, 200, {
|
|
1999
2125
|
ok: true,
|
|
2000
|
-
account: await accountView({ ...accountTarget, dir, meta: updated }, keys, null, liveExit
|
|
2126
|
+
account: await accountView({ ...accountTarget, dir, meta: updated }, keys, null, liveExit, {
|
|
2127
|
+
liveResolved,
|
|
2128
|
+
}),
|
|
2001
2129
|
});
|
|
2002
2130
|
return;
|
|
2003
2131
|
}
|
|
2004
2132
|
if (req.method !== "DELETE") return methodNotAllowed(res, "DELETE");
|
|
2133
|
+
if (accountBrowser.isOpen?.(accountTarget.provider, accountTarget.key)) {
|
|
2134
|
+
throw new HttpError(
|
|
2135
|
+
409,
|
|
2136
|
+
"browser_open",
|
|
2137
|
+
"Close this account's fingerprint browser before removing this account.",
|
|
2138
|
+
);
|
|
2139
|
+
}
|
|
2005
2140
|
const keyResult = liveAccountKeySet();
|
|
2006
2141
|
if (!keyResult.ok) {
|
|
2007
2142
|
// Without the key set we cannot tell a signed-in account from a
|
|
@@ -2081,6 +2216,21 @@ export async function startServer({
|
|
|
2081
2216
|
...(body.account.rotate === true ? { rotate: true } : {}),
|
|
2082
2217
|
}
|
|
2083
2218
|
: null;
|
|
2219
|
+
// Refuse to start a sign-in or fingerprint rotation against an
|
|
2220
|
+
// account whose standalone fingerprint browser holds the same
|
|
2221
|
+
// `profile/` directory.
|
|
2222
|
+
const targetBrowserKey =
|
|
2223
|
+
account?.key ??
|
|
2224
|
+
(body?.single === true
|
|
2225
|
+
? signInKey(provider, "new", liveAccountKeys(), { single: true })
|
|
2226
|
+
: null);
|
|
2227
|
+
if (targetBrowserKey && accountBrowser.isOpen?.(provider, targetBrowserKey)) {
|
|
2228
|
+
throw new HttpError(
|
|
2229
|
+
409,
|
|
2230
|
+
"browser_open",
|
|
2231
|
+
"Close this account's fingerprint browser before signing in or rotating its fingerprint.",
|
|
2232
|
+
);
|
|
2233
|
+
}
|
|
2084
2234
|
// Whitelist the chosen exit for the ownership ledger. A stored proxy
|
|
2085
2235
|
// carries credentials that must never be written to account.json, so
|
|
2086
2236
|
// only these four display fields are handed to the flow. A direct
|
|
@@ -2365,6 +2515,7 @@ export async function startServer({
|
|
|
2365
2515
|
store,
|
|
2366
2516
|
assetStore,
|
|
2367
2517
|
renewHosting,
|
|
2518
|
+
accountBrowser,
|
|
2368
2519
|
// Exposed so the package's own tests can wait for the startup sweep
|
|
2369
2520
|
// instead of racing it.
|
|
2370
2521
|
prune: () => runPrune(),
|
|
@@ -2380,6 +2531,7 @@ export async function startServer({
|
|
|
2380
2531
|
// host) on shutdown instead of leaving an orphan browser.
|
|
2381
2532
|
await proxyScore.close?.();
|
|
2382
2533
|
await identityCheck.close?.();
|
|
2534
|
+
await accountBrowser.closeAll?.();
|
|
2383
2535
|
// Stop the watcher and close any renewal browser it had open, instead of
|
|
2384
2536
|
// leaving a headless profile running after the service is gone.
|
|
2385
2537
|
renewHosting.stop?.();
|
package/lib/signin-flow.mjs
CHANGED
|
@@ -1231,6 +1231,17 @@ export function createSignInFlow(options = {}) {
|
|
|
1231
1231
|
return publicFlow(flow);
|
|
1232
1232
|
}
|
|
1233
1233
|
|
|
1234
|
+
// Read-only view of the account key currently occupied by a non-terminal
|
|
1235
|
+
// sign-in or rotation flow (if any), so sibling features (such as opening
|
|
1236
|
+
// the account's fingerprint browser) never collide on the same profile dir.
|
|
1237
|
+
function activeAccount() {
|
|
1238
|
+
const current = flow;
|
|
1239
|
+
if (!current || current.state === "idle" || TERMINAL.has(current.state)) return null;
|
|
1240
|
+
const key = current.accountKey || current.claimKey || current.key || null;
|
|
1241
|
+
if (!current.provider || !key) return null;
|
|
1242
|
+
return { provider: current.provider, key };
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1234
1245
|
async function cancel() {
|
|
1235
1246
|
const current = flow;
|
|
1236
1247
|
if (!current || TERMINAL.has(current.state)) {
|
|
@@ -1260,6 +1271,7 @@ export function createSignInFlow(options = {}) {
|
|
|
1260
1271
|
startSignIn,
|
|
1261
1272
|
submitCode,
|
|
1262
1273
|
status,
|
|
1274
|
+
activeAccount,
|
|
1263
1275
|
cancel,
|
|
1264
1276
|
// Waits for the background callback to reach a terminal state. Used by
|
|
1265
1277
|
// tests; in production a GET reads `status()` instead.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gruu6y/page-clean-login",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.12",
|
|
4
4
|
"description": "干净登录:通过领先的指纹浏览器,在插件页面接管magpie的凭据登录与续期,缓解封号焦虑",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"playwright-core": "1.62.0"
|
|
48
48
|
},
|
|
49
49
|
"magpie": {
|
|
50
|
-
"icon": "https://raw.githubusercontent.com/Gruu6y/magpie-clean-login/main/assets/icon.svg",
|
|
50
|
+
"icon": "https://raw.githubusercontent.com/Gruu6y/magpie-clean-login/main/assets/icon.svg?v=eb8489f5",
|
|
51
51
|
"page": {
|
|
52
52
|
"title": "Clean Login",
|
|
53
53
|
"titles": {
|
package/ui/account-view.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// and formatting check summaries and exits. Keeping these helpers free of DOM
|
|
3
3
|
// dependencies allows comprehensive headless testing.
|
|
4
4
|
|
|
5
|
-
import { accountLabel } from "./account-state.js";
|
|
5
|
+
import { accountIdentityPath, accountLabel } from "./account-state.js";
|
|
6
6
|
import { ALL_PROVIDERS, UNKNOWN_PROVIDER } from "./account-provider-filter.js";
|
|
7
7
|
|
|
8
8
|
export function accountView(account) {
|
|
@@ -38,6 +38,7 @@ export function accountView(account) {
|
|
|
38
38
|
const methodLabel = typeof value.methodLabel === "string" ? value.methodLabel.trim() : "";
|
|
39
39
|
const unverified = Boolean(value.unverified);
|
|
40
40
|
const hosting = normalizeHosting(value.hosting);
|
|
41
|
+
const browser = normalizeAccountBrowser(value.browser);
|
|
41
42
|
const rawExpiry = value.expiresAt ?? hosting.expiresAt;
|
|
42
43
|
const expiresAt = (() => {
|
|
43
44
|
if (typeof rawExpiry === "string" && rawExpiry.trim()) return rawExpiry.trim();
|
|
@@ -67,6 +68,7 @@ export function accountView(account) {
|
|
|
67
68
|
note,
|
|
68
69
|
unverified,
|
|
69
70
|
hosting,
|
|
71
|
+
browser,
|
|
70
72
|
};
|
|
71
73
|
}
|
|
72
74
|
|
|
@@ -145,6 +147,143 @@ export function hostingTone(hosting) {
|
|
|
145
147
|
return "none";
|
|
146
148
|
}
|
|
147
149
|
|
|
150
|
+
// Per-account fingerprint browser endpoint. URL-encodes both segments via
|
|
151
|
+
// accountIdentityPath so `#` and `/` in keys stay safe.
|
|
152
|
+
export function accountBrowserPath(provider, key) {
|
|
153
|
+
return `${accountIdentityPath(provider, key)}/browser`;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Credential-free normalization of the account browser state returned by
|
|
157
|
+
// GET /api/accounts, GET/POST/DELETE /api/accounts/:provider/:key/browser,
|
|
158
|
+
// and PATCH account/exit responses. Unknown fields (like raw URLs or auth)
|
|
159
|
+
// are never forwarded.
|
|
160
|
+
export function normalizeAccountBrowser(value) {
|
|
161
|
+
const raw = value && typeof value === "object" ? value : {};
|
|
162
|
+
const open = raw.open === true;
|
|
163
|
+
if (!open) {
|
|
164
|
+
return {
|
|
165
|
+
open: false,
|
|
166
|
+
openedAt: null,
|
|
167
|
+
exit: null,
|
|
168
|
+
exitChanged: false,
|
|
169
|
+
warning: null,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
const openedAt = (() => {
|
|
173
|
+
if (typeof raw.openedAt === "string" && raw.openedAt.trim()) return raw.openedAt.trim();
|
|
174
|
+
if (typeof raw.openedAt === "number" && Number.isFinite(raw.openedAt) && raw.openedAt > 0) {
|
|
175
|
+
const date = new Date(raw.openedAt);
|
|
176
|
+
return Number.isNaN(date.getTime()) ? null : date.toISOString();
|
|
177
|
+
}
|
|
178
|
+
return null;
|
|
179
|
+
})();
|
|
180
|
+
const rawExit = raw.exit && typeof raw.exit === "object" ? raw.exit : null;
|
|
181
|
+
const exit = rawExit
|
|
182
|
+
? {
|
|
183
|
+
ok: rawExit.ok !== false,
|
|
184
|
+
mode: typeof rawExit.mode === "string" ? rawExit.mode : null,
|
|
185
|
+
effectiveMode: typeof rawExit.effectiveMode === "string" ? rawExit.effectiveMode : null,
|
|
186
|
+
proxyId: typeof rawExit.proxyId === "string" && rawExit.proxyId ? rawExit.proxyId : null,
|
|
187
|
+
proxyName: typeof rawExit.proxyName === "string" ? rawExit.proxyName : "",
|
|
188
|
+
region: typeof rawExit.region === "string" ? rawExit.region : "",
|
|
189
|
+
type: typeof rawExit.type === "string" && rawExit.type ? rawExit.type : null,
|
|
190
|
+
displayUrl: typeof rawExit.displayUrl === "string" && rawExit.displayUrl.trim()
|
|
191
|
+
? rawExit.displayUrl.trim()
|
|
192
|
+
: null,
|
|
193
|
+
providerProxy: typeof rawExit.providerProxy === "string" && rawExit.providerProxy.trim()
|
|
194
|
+
? rawExit.providerProxy.trim()
|
|
195
|
+
: null,
|
|
196
|
+
matchedStore: Boolean(rawExit.matchedStore),
|
|
197
|
+
}
|
|
198
|
+
: null;
|
|
199
|
+
const rawWarning = raw.warning && typeof raw.warning === "object" ? raw.warning : null;
|
|
200
|
+
const warning = rawWarning && typeof rawWarning.code === "string" && rawWarning.code.trim()
|
|
201
|
+
? {
|
|
202
|
+
code: rawWarning.code.trim(),
|
|
203
|
+
status: Number.isFinite(rawWarning.status) ? rawWarning.status : 0,
|
|
204
|
+
message: typeof rawWarning.message === "string" ? rawWarning.message : "",
|
|
205
|
+
}
|
|
206
|
+
: null;
|
|
207
|
+
return {
|
|
208
|
+
open: true,
|
|
209
|
+
openedAt,
|
|
210
|
+
exit,
|
|
211
|
+
exitChanged: raw.exitChanged === true,
|
|
212
|
+
warning,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Formats the publicAccountExit projection attached to an open browser.
|
|
217
|
+
// External custom URLs (effectiveMode === "proxy" without a stored proxyId)
|
|
218
|
+
// never fall back to "Direct".
|
|
219
|
+
export function formatBrowserExit(exit, directText = "直连", labels = {}) {
|
|
220
|
+
if (!exit || typeof exit !== "object") return directText;
|
|
221
|
+
if (exit.effectiveMode === "direct") {
|
|
222
|
+
if (exit.mode === "inherit") {
|
|
223
|
+
return formatExit({ mode: "inherit", proxyId: null, displayUrl: null, custom: false }, directText, labels);
|
|
224
|
+
}
|
|
225
|
+
return directText;
|
|
226
|
+
}
|
|
227
|
+
const displayUrl =
|
|
228
|
+
(typeof exit.displayUrl === "string" && exit.displayUrl.trim()) ||
|
|
229
|
+
(exit.mode === "inherit" && typeof exit.providerProxy === "string" && exit.providerProxy.trim()) ||
|
|
230
|
+
null;
|
|
231
|
+
const isExternalCustom =
|
|
232
|
+
(exit.effectiveMode === "proxy" && !exit.proxyId) ||
|
|
233
|
+
(exit.mode === "custom" && !exit.proxyId && exit.matchedStore !== true);
|
|
234
|
+
return formatExit(
|
|
235
|
+
{
|
|
236
|
+
...exit,
|
|
237
|
+
displayUrl,
|
|
238
|
+
custom: Boolean(exit.custom || isExternalCustom),
|
|
239
|
+
},
|
|
240
|
+
directText,
|
|
241
|
+
labels,
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Pure display summary for an account's fingerprint browser state. Used by
|
|
246
|
+
// both the table row and the account detail dialog so status wording, opening
|
|
247
|
+
// exit label, exit-changed warning, and visual tone stay identical.
|
|
248
|
+
export function browserStateSummary(browser, labels = {}) {
|
|
249
|
+
const state = normalizeAccountBrowser(browser);
|
|
250
|
+
if (!state.open) {
|
|
251
|
+
return {
|
|
252
|
+
open: false,
|
|
253
|
+
exitChanged: false,
|
|
254
|
+
exitLabel: "",
|
|
255
|
+
statusText: typeof labels?.closed === "string" ? labels.closed : "",
|
|
256
|
+
noticeText: "",
|
|
257
|
+
tone: "none",
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
const directText = typeof labels?.direct === "string" && labels.direct ? labels.direct : "直连";
|
|
261
|
+
const exitLabel = formatBrowserExit(state.exit, directText, {
|
|
262
|
+
custom: labels?.custom,
|
|
263
|
+
inherit: labels?.inherit,
|
|
264
|
+
});
|
|
265
|
+
const openTemplate =
|
|
266
|
+
typeof labels?.openWithExit === "string" && labels.openWithExit.trim()
|
|
267
|
+
? labels.openWithExit.trim()
|
|
268
|
+
: "浏览器已打开 · {exit}";
|
|
269
|
+
const statusText = openTemplate.includes("{exit}")
|
|
270
|
+
? openTemplate.replace("{exit}", exitLabel)
|
|
271
|
+
: `${openTemplate} · ${exitLabel}`;
|
|
272
|
+
const noticeText = state.exitChanged
|
|
273
|
+
? (typeof labels?.exitChanged === "string" && labels.exitChanged.trim()
|
|
274
|
+
? labels.exitChanged.trim()
|
|
275
|
+
: "出口已变更,关闭并重新打开浏览器后生效。")
|
|
276
|
+
: "";
|
|
277
|
+
return {
|
|
278
|
+
open: true,
|
|
279
|
+
exitChanged: state.exitChanged,
|
|
280
|
+
exitLabel,
|
|
281
|
+
statusText,
|
|
282
|
+
noticeText,
|
|
283
|
+
tone: state.exitChanged ? "warn" : "good",
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
148
287
|
function formatNamedExit(login, directText) {
|
|
149
288
|
const region = typeof login.region === "string" ? login.region.trim() : "";
|
|
150
289
|
const name = typeof login.proxyName === "string"
|