feat(browser): unify site credentials with browser profiles (BR-2)
- Inject the site account Cookie (manual + auto-login session, session wins)
into the camofox profile before the first tab opens; password accounts
without a session run refresh-login first; API Key sites are skipped.
- Cookie expired after injection -> credential.status=expired; cookie
accounts go to waiting/login_required ("log in once, applies site-wide").
- Write back site-domain cookies from the browser on handover, session close,
every 10 min of activity, and from refresh-login when the stored cookie is
invalid (verified before saving). user_info.cookieOrigin records
source=browser; source shown as manual/login/browser in the origin view.
- New camofox plugin umt-cookies (GET/POST /umt/sessions/:userId/cookies
under the access key); profile dir 0700/files 0600 in the desktop image.
- Single entry: sources-identity/service/credentials.ts.
- Real check: HHClub cookie injected into a fresh profile -> home page
logged in (logout link present), 5 cookies read back.
Requires rebuilding umt-browser-desktop (docker compose build camofox &&
docker compose up -d camofox) to take effect.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
901b01d1a1
commit
28974d1e1a
58 files changed
+2843
-1112
No files matched your search
@@ -34,8 +34,16 @@ export interface MockCamofoxSite {
|
||||
/** 下载内容;缺省返回固定字节 */
|
||||
torrent?: (id: string) => Uint8Array;
|
||||
passkey?: string;
|
||||
/**
|
||||
* 登录态 Cookie(BR-2):注入的 Cookie 里 name 的值在 valid 中 → 该 profile 视为已登录;值不在其中 → 视为未登录(过期)。
|
||||
* 表单登录成功后把 name=fresh 写进该 profile 的 Cookie(读回时可见)。
|
||||
*/
|
||||
sessionCookie?: { name: string; valid: string[]; fresh: string };
|
||||
}
|
||||
|
||||
/** mock 的 Cookie 接口形态:plugin = umt-cookies 插件(读写);upstream = 只有上游导入端点(要求 CAMOFOX_API_KEY);none = 都没有 */
|
||||
export type MockCookieApi = "plugin" | "upstream" | "none";
|
||||
|
||||
export const MOCK_VIEWPORT = { width: 1280, height: 800 } as const;
|
||||
export const MOCK_ROW_Y0 = 20;
|
||||
export const MOCK_ROW_H = 40;
|
||||
@@ -52,6 +60,21 @@ export interface MockCamofoxOptions {
|
||||
/** 视口(CSS 像素)与设备像素比:截图尺寸 = 视口 × DPR;元素行坐标按 CSS 像素。缺省 MOCK_VIEWPORT、DPR 1 */
|
||||
viewport?: { width: number; height: number };
|
||||
devicePixelRatio?: number;
|
||||
/** 缺省 plugin */
|
||||
cookieApi?: MockCookieApi;
|
||||
/** upstream 模式下上游导入端点认的口令(模拟 CAMOFOX_API_KEY);缺省不设 → 403 */
|
||||
cookieImportKey?: string;
|
||||
}
|
||||
|
||||
export interface MockCamofoxCookie {
|
||||
name: string;
|
||||
value: string;
|
||||
domain: string;
|
||||
path: string;
|
||||
expires: number;
|
||||
httpOnly: boolean;
|
||||
secure: boolean;
|
||||
sameSite?: string;
|
||||
}
|
||||
|
||||
export interface MockCamofoxTab {
|
||||
@@ -90,6 +113,8 @@ export type MockCamofox = MockHttp & {
|
||||
actions: MockCamofoxAction[];
|
||||
/** 已登录的 userId(profile) */
|
||||
loggedIn: Set<string>;
|
||||
/** 各 profile 的 Cookie(注入的 + 表单登录写入的) */
|
||||
cookieJars: Map<string, MockCamofoxCookie[]>;
|
||||
/** 模拟站点登录过期(全部 profile 或指定 profile) */
|
||||
expireLogin(userId?: string): void;
|
||||
/** 模拟上游回收标签页(空闲超时 / 重启) */
|
||||
@@ -298,7 +323,40 @@ export async function createMockCamofox(opts: MockCamofoxOptions = {}): Promise<
|
||||
const tabs = new Map<string, MockCamofoxTab>();
|
||||
const actions: MockCamofoxAction[] = [];
|
||||
const loggedIn = new Set<string>();
|
||||
const cookieJars = new Map<string, MockCamofoxCookie[]>();
|
||||
const cookieApi = opts.cookieApi ?? "plugin";
|
||||
const site = opts.site;
|
||||
const putCookie = (userId: string, c: MockCamofoxCookie) => {
|
||||
const jar = (cookieJars.get(userId) ?? []).filter(
|
||||
(x) => !(x.name === c.name && x.domain === c.domain && x.path === c.path),
|
||||
);
|
||||
jar.push(c);
|
||||
cookieJars.set(userId, jar);
|
||||
const sc = site?.sessionCookie;
|
||||
if (sc && c.name === sc.name && new URL(site.origin).hostname.endsWith(c.domain.replace(/^\./, ""))) {
|
||||
if (sc.valid.includes(c.value)) loggedIn.add(userId);
|
||||
else loggedIn.delete(userId);
|
||||
}
|
||||
};
|
||||
const importCookies = (userId: string, b: Body): MockReply => {
|
||||
const list = Array.isArray(b.cookies) ? (b.cookies as Record<string, unknown>[]) : null;
|
||||
if (!list) return err(400, "cookies must be an array");
|
||||
for (const c of list) {
|
||||
if (typeof c.name !== "string" || typeof c.value !== "string" || typeof c.domain !== "string")
|
||||
return err(400, "invalid cookie objects");
|
||||
putCookie(userId, {
|
||||
name: c.name,
|
||||
value: c.value,
|
||||
domain: c.domain,
|
||||
path: typeof c.path === "string" ? c.path : "/",
|
||||
expires: typeof c.expires === "number" ? c.expires : -1,
|
||||
httpOnly: c.httpOnly === true,
|
||||
secure: c.secure === true,
|
||||
...(typeof c.sameSite === "string" ? { sameSite: c.sameSite } : {}),
|
||||
});
|
||||
}
|
||||
return { json: { ok: true, userId, count: list.length } };
|
||||
};
|
||||
const viewport = opts.viewport ?? MOCK_VIEWPORT;
|
||||
const dpr = opts.devicePixelRatio ?? 1;
|
||||
const loginPath = site?.loginPath ?? "/login.php";
|
||||
@@ -351,6 +409,19 @@ export async function createMockCamofox(opts: MockCamofoxOptions = {}): Promise<
|
||||
return;
|
||||
}
|
||||
loggedIn.add(t.userId);
|
||||
if (site.sessionCookie) {
|
||||
if (!site.sessionCookie.valid.includes(site.sessionCookie.fresh))
|
||||
site.sessionCookie.valid.push(site.sessionCookie.fresh);
|
||||
putCookie(t.userId, {
|
||||
name: site.sessionCookie.name,
|
||||
value: site.sessionCookie.fresh,
|
||||
domain: new URL(site.origin).hostname,
|
||||
path: "/",
|
||||
expires: Math.floor(Date.now() / 1000) + 86400,
|
||||
httpOnly: true,
|
||||
secure: site.origin.startsWith("https:"),
|
||||
});
|
||||
}
|
||||
go(t, `${site.origin}${back}`);
|
||||
};
|
||||
|
||||
@@ -713,6 +784,32 @@ export async function createMockCamofox(opts: MockCamofoxOptions = {}): Promise<
|
||||
return { json: { downloads: list } };
|
||||
},
|
||||
],
|
||||
[
|
||||
/^\/umt\/sessions\/([^/]+)\/cookies$/,
|
||||
(m, q, req, b) => {
|
||||
if (cookieApi !== "plugin") return err(404, "no route");
|
||||
const userId = decodeURIComponent(m[1]!);
|
||||
if (req.method === "POST") return importCookies(userId, b);
|
||||
const hosts = (q.urls ?? "")
|
||||
.split(",")
|
||||
.filter(Boolean)
|
||||
.map((u) => new URL(u).hostname);
|
||||
const cookies = (cookieJars.get(userId) ?? []).filter((c) => {
|
||||
const d = c.domain.replace(/^\./, "");
|
||||
return hosts.some((h) => h === d || h.endsWith(`.${d}`));
|
||||
});
|
||||
return { json: { source: "live", cookies } };
|
||||
},
|
||||
],
|
||||
[
|
||||
/^\/sessions\/([^/]+)\/cookies$/,
|
||||
(m, _q, req, b) => {
|
||||
if (cookieApi === "none") return err(404, "no route");
|
||||
if (!opts.cookieImportKey || req.headers.authorization !== `Bearer ${opts.cookieImportKey}`)
|
||||
return err(403, "Forbidden");
|
||||
return importCookies(decodeURIComponent(m[1]!), b);
|
||||
},
|
||||
],
|
||||
[
|
||||
/^\/sessions\/([^/]+)$/,
|
||||
(m) => {
|
||||
@@ -731,7 +828,8 @@ export async function createMockCamofox(opts: MockCamofoxOptions = {}): Promise<
|
||||
port: opts.port,
|
||||
guard: (req) => {
|
||||
const path = new URL(req.url ?? "/", "http://mock").pathname;
|
||||
if (!opts.accessKey || path === "/health") return null;
|
||||
// 与上游一致:导入端点有自己的口令(CAMOFOX_API_KEY),不走访问凭据
|
||||
if (!opts.accessKey || path === "/health" || /^\/sessions\/[^/]+\/cookies$/.test(path)) return null;
|
||||
return req.headers.authorization === `Bearer ${opts.accessKey}` ? null : err(401, "Unauthorized");
|
||||
},
|
||||
},
|
||||
@@ -740,6 +838,7 @@ export async function createMockCamofox(opts: MockCamofoxOptions = {}): Promise<
|
||||
tabs,
|
||||
actions,
|
||||
loggedIn,
|
||||
cookieJars,
|
||||
expireLogin(userId?: string) {
|
||||
if (userId) loggedIn.delete(userId);
|
||||
else loggedIn.clear();
|
||||
|
||||
@@ -45,38 +45,3 @@ export const HHANCLUB_DETAIL_PAGE = `<!DOCTYPE html><html><head><title>HHCLUB ::
|
||||
<div><a href="download.php?id=90002">下载种子</a></div>
|
||||
<div id="kdescr"><a href="https://www.imdb.com/title/tt15239678/">IMDb</a> <a href="https://movie.douban.com/subject/35575567/">豆瓣</a> 简介……</div>
|
||||
</div></body></html>`;
|
||||
|
||||
/**
|
||||
* 2026-09-29 真实账号核对后补的最小脱敏片段(名称、ID、数值均已替换;不含 passkey 与用户信息)。
|
||||
* 核对到的真实行为:列表标题链接文本末尾带新种标记「[新]」(详情页标题没有);短剧分类 412;
|
||||
* 新种完成数为 0;无促销时没有 promotion-tag 元素;上传时间在 span[title] 里,文本是相对时间。
|
||||
* 下载链接带 passkey(这里用占位值)。
|
||||
*/
|
||||
export const HHANCLUB_LIST_PAGE_2026 = `<!DOCTYPE html><html><head><title>HHCLUB :: 种子</title></head><body>
|
||||
<div id="user-info-panel"><a href="userdetails.php?id=7" class="VIP_Name">bob</a> <a href="logout.php">退出</a></div>
|
||||
<div class="torrent-table">
|
||||
<div class="torrent-table-sub-info">
|
||||
<div class="torrent-cat"><a href="?cat[]=412"><img src="pic/cattrans.gif" alt="短剧"/></a></div>
|
||||
<div class="torrent-title"><a class="torrent-info-text-name" href="details.php?id=91001&hit=1">示例短剧 1080p WEB-DL H265 AAC-HHWEB[新]</a></div>
|
||||
<div class="torrent-info-text-small_name">示例短剧 | 全30集 | 1080p</div>
|
||||
<div class="torrent-manage"><a href="download.php?id=91001&passkey=0000000000000000">下载</a></div>
|
||||
<div class="torrent-info-text-added"><span title="2026-09-29 14:07:03">5分钟</span></div>
|
||||
<div class="torrent-info-text-size">185.38 MB</div>
|
||||
<div class="torrent-info-text-seeders">1</div>
|
||||
<div class="torrent-info-text-leechers">58</div>
|
||||
<div class="torrent-info-text-finished">0</div>
|
||||
</div>
|
||||
<div class="torrent-table-sub-info">
|
||||
<div class="torrent-cat"><a href="?cat[]=402"><img src="pic/cattrans.gif" alt="电视剧"/></a></div>
|
||||
<div class="torrent-title"><a class="torrent-info-text-name" href="details.php?id=91002&hit=1">Example Show S01 2026 2160p WEB-DL H.264 AAC-HHWEB</a></div>
|
||||
<div class="torrent-info-text-small_name">示例剧集 | 第12集 | 4K | 类型: 奇幻 [内嵌中字]</div>
|
||||
<div class="torrent-manage"><a href="download.php?id=91002&passkey=0000000000000000">下载</a></div>
|
||||
<div class="torrent-info-text-added"><span title="2026-09-29 12:03:28">2时8分钟</span></div>
|
||||
<div class="torrent-info-text-size">1.46 GB</div>
|
||||
<div class="torrent-info-text-seeders">131</div>
|
||||
<div class="torrent-info-text-leechers">5</div>
|
||||
<div class="torrent-info-text-finished">187</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="pagination"><a href="?page=0">1-100</a><a href="?page=1">101-200</a></div>
|
||||
</body></html>`;
|
||||
Reference in new issue
Block a user