feat(browser): fixed Windows identity and per-site egress in umt-cookies plugin (BR-4)

- browser:launching hook generates a Windows fingerprint once (camoufox-js,
  os=windows), persists it to /root/.camofox/umt-identity.json and reuses it
  on every launch; Firefox/Camoufox upgrades only bump UA/version fields
- Windows fontconfig with absolute font dir (upstream path was broken)
- GET /umt/identity (public UA/OS/version) and PUT /umt/sessions/:id/proxy
  (per-context proxy; Firefox no longer uses container HTTP_PROXY env)
- adapter identity()/setEgress(), splitBrowserOnlyCookies for cf_clearance/__cf_bm

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
rootandClaude Opus 5.5 committed 2026-09-30 03:17:06 +08:00
1 parent 6663afce7e
commit ebd07c9230
8 files changed
+746 -5

No files matched your search

@@ -0,0 +1,178 @@
import { createMockCamofox, MOCK_CAMOFOX_IDENTITY, type MockCamofox } from "@umt/testkit";
import { afterEach, describe, expect, it } from "vitest";
import { createCamofoxBrowserProvider, splitBrowserOnlyCookies, staticBrowserSecrets } from "./index.ts";
// BR-4:umt-cookies 插件的固定身份纯函数(docker/browser-desktop/.../umt-cookies/identity.js)、
// 适配器的 identity() / setEgress()、仅浏览器 Cookie 的剔除。
// 插件是容器里的纯 JS(不进 tsc 模块图):按路径动态导入
const PLUGIN = new URL(
"../../../../docker/browser-desktop/root/opt/camofox/plugins/umt-cookies/identity.js",
import.meta.url,
).href;
// biome-ignore lint/suspicious/noExplicitAny: 插件是无类型 JS
const plugin = async (): Promise<any> => import(PLUGIN);
const UA152 = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:152.0) Gecko/20100101 Firefox/152.0";
describe("umt-cookies 插件:固定身份", () => {
it("config ⇄ CAMOU_CONFIG_n 分片往返;旧分片先清掉", async () => {
const p = await plugin();
const big = { "navigator.userAgent": UA152, pad: "x".repeat(70_000) };
const env: Record<string, string> = { CAMOU_CONFIG_1: "stale", CAMOU_CONFIG_9: "stale", PATH: "/bin" };
p.encodeConfig(env, big);
expect(Object.keys(env).filter((k) => k.startsWith("CAMOU_CONFIG_"))).toEqual([
"CAMOU_CONFIG_1",
"CAMOU_CONFIG_2",
"CAMOU_CONFIG_3",
]);
expect(env.PATH).toBe("/bin");
expect(p.decodeConfig(env)).toEqual(big);
expect(p.decodeConfig({})).toBeNull();
});
it("启动 config:运行期字段取上游,指纹字段一律取身份", async () => {
const p = await plugin();
const upstream = {
"navigator.userAgent": "Mozilla/5.0 (X11; Linux x86_64; rv:152.0) Gecko/20100101 Firefox/152.0",
"screen.width": 1920,
humanize: true,
"humanize:maxTime": 2,
"locale:language": "zh",
"geolocation:latitude": 1,
timezone: "Asia/Shanghai",
addons: ["/a"],
"webrtc:ipv4": "1.2.3.4",
};
const identity = { "navigator.userAgent": UA152, "navigator.platform": "Win32", "screen.width": 2560 };
expect(p.mergeLaunchConfig(upstream, identity)).toEqual({
humanize: true,
"humanize:maxTime": 2,
"locale:language": "zh",
"geolocation:latitude": 1,
timezone: "Asia/Shanghai",
addons: ["/a"],
"webrtc:ipv4": "1.2.3.4",
"navigator.userAgent": UA152,
"navigator.platform": "Win32",
"screen.width": 2560,
});
expect(p.fingerprintPart(upstream)).toEqual({
"navigator.userAgent": upstream["navigator.userAgent"],
"screen.width": 1920,
});
});
it("Firefox 升级:只改 navigator / headers 里的版本号,其余保留;新字段从样本补齐;版本相同不改", async () => {
const p = await plugin();
const id = {
schema: 1,
os: "windows",
firefoxVersion: "140",
camoufoxVersion: "140.0-a",
fingerprintId: "fp1",
revision: 1,
createdAt: "2026-01-01T00:00:00Z",
updatedAt: "2026-01-01T00:00:00Z",
config: {
"navigator.userAgent": UA152.replaceAll("152.0", "140.0"),
"navigator.appVersion": "5.0 (Windows)",
"headers.User-Agent": UA152.replaceAll("152.0", "140.0"),
"screen.width": 2560,
"webGl:renderer": "ANGLE 140.0 build",
"canvas:seed": 42,
},
};
const sample = { "navigator.userAgent": "other", "audio:seed": 7, "screen.width": 1 };
const r = p.upgradeIdentity(id, { firefoxVersion: "152", sample, camoufoxVersion: "152.0.4", now: "N" });
expect(r.changed).toBe(true);
expect(r.identity.config).toEqual({
"navigator.userAgent": UA152,
"navigator.appVersion": "5.0 (Windows)",
"headers.User-Agent": UA152,
"screen.width": 2560,
"webGl:renderer": "ANGLE 140.0 build",
"canvas:seed": 42,
"audio:seed": 7,
});
expect(r.identity).toMatchObject({ firefoxVersion: "152", revision: 2, fingerprintId: "fp1", updatedAt: "N" });
expect(p.publicIdentity(r.identity)).toEqual({
userAgent: UA152,
os: "windows",
platform: null,
firefoxVersion: "152",
camoufoxVersion: "152.0.4",
fingerprintId: "fp1",
revision: 2,
schema: 1,
createdAt: "2026-01-01T00:00:00Z",
updatedAt: "N",
});
const same = p.upgradeIdentity(r.identity, { firefoxVersion: "152", sample, camoufoxVersion: "152.0.4", now: "M" });
expect(same.changed).toBe(false);
expect(p.validIdentity(r.identity)).toBe(true);
expect(p.validIdentity({ schema: 1, config: {} })).toBe(false);
expect(p.firefoxMajorOf(UA152)).toBe("152");
});
it("出口代理规范化:口令从 URL 拆出;非法协议拒绝;null = 直连", async () => {
const p = await plugin();
expect(p.normalizeProxy(null)).toBeNull();
expect(p.normalizeProxy({ server: "http://u%40x:p%[email protected]:7891/" })).toEqual({
server: "http://172.17.0.1:7891",
username: "u@x",
password: "p:w",
});
expect(p.normalizeProxy({ server: "socks5://h:1080", username: "a", password: "b" })).toEqual({
server: "socks5://h:1080",
username: "a",
password: "b",
});
expect(p.normalizeProxy({ server: "ftp://h" })).toBeUndefined();
expect(p.normalizeProxy({})).toBeUndefined();
expect(p.sameProxy(null, null)).toBe(true);
expect(p.sameProxy({ server: "http://a:1" }, null)).toBe(false);
});
});
describe("仅浏览器 Cookie", () => {
it("剔除 cf_clearance / __cf_bm / cf_chl_*,只返回名字", () => {
expect(splitBrowserOnlyCookies("a=1; cf_clearance=S; __cf_bm=T; cf_chl_rc_m=1")).toEqual({
header: "a=1",
browserOnly: ["__cf_bm", "cf_chl_rc_m", "cf_clearance"],
});
expect(splitBrowserOnlyCookies("cf_clearance=S")).toEqual({ header: null, browserOnly: ["cf_clearance"] });
expect(splitBrowserOnlyCookies(null)).toEqual({ header: null, browserOnly: [] });
});
});
describe("camofox 适配器:身份与出口", () => {
let mock: MockCamofox | null = null;
afterEach(async () => {
await mock?.close();
mock = null;
});
const provider = (m: MockCamofox) =>
createCamofoxBrowserProvider({
providerId: "11111111-1111-4111-8111-111111111111",
baseUrl: m.url,
secrets: staticBrowserSecrets({ "provider:11111111-1111-4111-8111-111111111111": "k" }),
});
it("identity() 读插件身份;setEgress() 下发代理 / 直连", async () => {
mock = await createMockCamofox({ accessKey: "k" });
const p = provider(mock);
expect(await p.identity!()).toMatchObject({ userAgent: MOCK_CAMOFOX_IDENTITY.userAgent, os: "windows" });
expect(await p.setEgress!("site-a", { server: "http://127.0.0.1:7891" })).toEqual({ restarted: false });
expect(await p.setEgress!("site-b", null)).toEqual({ restarted: false });
expect(mock.egress.get("site-a")).toEqual({ server: "http://127.0.0.1:7891" });
expect(mock.egress.get("site-b")).toBeNull();
});
it("旧插件(没有接口):identity() 为 null,setEgress() 报 not_implemented", async () => {
mock = await createMockCamofox({ accessKey: "k", cookieApi: "none" });
const p = provider(mock);
expect(await p.identity!()).toBeNull();
await expect(p.setEgress!("x", null)).rejects.toMatchObject({ kind: "not_implemented" });
});
});