feat(stealth): 默认开启并收敛 chrome 指纹特征

This commit is contained in:
leeguooooo
2026-02-24 14:54:30 +09:00
parent c5b2292caa
commit 0443e4ed7a
12 changed files with 29 additions and 76 deletions
+5 -18
View File
@@ -152,7 +152,7 @@ export class BrowserManager {
private lastSnapshot: string = '';
private scopedHeaderRoutes: Map<string, (route: Route) => Promise<void>> = new Map();
private colorScheme: 'light' | 'dark' | 'no-preference' | null = null;
private stealthEnabled: boolean = false;
private stealthEnabled: boolean = true;
private stealthConnectionKind: StealthConnectionKind = 'local';
private contextLocale: string | undefined = undefined;
private contextTimezoneId: string | undefined = undefined;
@@ -172,17 +172,6 @@ export class BrowserManager {
* Local Chromium gets args + init scripts; CDP/providers get init scripts only.
*/
private getStealthPolicy(browserType: BrowserType = 'chromium'): StealthPolicy {
if (!this.stealthEnabled) {
return {
enabled: false,
connectionKind: this.stealthConnectionKind,
applyChromiumArgs: false,
applyInitScripts: false,
providerManaged: false,
capabilities: [],
};
}
const applyChromiumArgs = this.stealthConnectionKind === 'local' && browserType === 'chromium';
const applyInitScripts = true;
const providerManaged = this.stealthConnectionKind === 'provider-kernel';
@@ -1414,11 +1403,9 @@ export class BrowserManager {
if (options.colorScheme) {
this.colorScheme = options.colorScheme;
}
this.stealthEnabled = options.stealth ?? false;
this.contextLocale = this.stealthEnabled
? this.resolveStealthLocale(options.headers)
: undefined;
this.contextTimezoneId = this.stealthEnabled ? this.resolveStealthTimezoneId() : undefined;
this.stealthEnabled = true;
this.contextLocale = this.resolveStealthLocale(options.headers);
this.contextTimezoneId = this.resolveStealthTimezoneId();
this.contextHeaders = undefined;
this.contextUserAgent = options.userAgent;
// -p flag takes precedence over AGENT_BROWSER_PROVIDER.
@@ -2749,7 +2736,7 @@ export class BrowserManager {
this.isPersistentContext = false;
this.activePageIndex = 0;
this.colorScheme = null;
this.stealthEnabled = false;
this.stealthEnabled = true;
this.stealthConnectionKind = 'local';
this.contextLocale = undefined;
this.contextTimezoneId = undefined;
+2 -2
View File
@@ -450,7 +450,7 @@ export async function startDaemon(options?: {
const ignoreHTTPSErrors = process.env.AGENT_BROWSER_IGNORE_HTTPS_ERRORS === '1';
const allowFileAccess = process.env.AGENT_BROWSER_ALLOW_FILE_ACCESS === '1';
const stealth = process.env.AGENT_BROWSER_STEALTH !== '0';
// Stealth is always enabled in agent-browser-stealth
const colorSchemeEnv = process.env.AGENT_BROWSER_COLOR_SCHEME;
const colorScheme =
colorSchemeEnv === 'dark' ||
@@ -471,7 +471,7 @@ export async function startDaemon(options?: {
proxy,
ignoreHTTPSErrors: ignoreHTTPSErrors,
allowFileAccess: allowFileAccess,
stealth,
colorScheme,
autoStateFilePath: getSessionAutoStatePath(),
});
-2
View File
@@ -49,8 +49,6 @@ const launchSchema = baseCommandSchema.extend({
provider: z.string().optional(),
ignoreHTTPSErrors: z.boolean().optional(),
allowFileAccess: z.boolean().optional(),
// Stealth toggle is part of launch semantics for local/CDP/provider modes.
stealth: z.boolean().optional(),
colorScheme: z.enum(['light', 'dark', 'no-preference']).optional(),
profile: z.string().optional(),
storageState: z.string().optional(),
+13 -3
View File
@@ -244,8 +244,18 @@ function patchNavigatorWebdriver(): string {
*/
function patchChromeRuntime(): string {
return `(function(){
if (!window.chrome) { window.chrome = {}; }
if (!window.chrome.runtime) {
const chromeObject = ('chrome' in window && window.chrome) ? window.chrome : {};
if (!('chrome' in window)) {
try {
Object.defineProperty(Window.prototype, 'chrome', {
get: () => chromeObject,
configurable: true,
});
} catch {
try { Object.defineProperty(window, 'chrome', { value: chromeObject, configurable: true }); } catch {}
}
}
if (!chromeObject.runtime) {
const makeEvent = () => ({
addListener: () => {},
removeListener: () => {},
@@ -268,7 +278,7 @@ function patchChromeRuntime(): string {
onConnect: makeEvent(),
onMessage: makeEvent(),
};
Object.defineProperty(window.chrome, 'runtime', {
Object.defineProperty(chromeObject, 'runtime', {
value: runtime,
configurable: true,
});
-1
View File
@@ -32,7 +32,6 @@ export interface LaunchCommand extends BaseCommand {
ignoreHTTPSErrors?: boolean;
allowFileAccess?: boolean; // Enable file:// URL access and cross-origin file requests
colorScheme?: 'light' | 'dark' | 'no-preference'; // Persistent color scheme override
stealth?: boolean; // Enable stealth mode to avoid automation detection
// Auto-load state file for session persistence
autoStateFilePath?: string;
}