feat(stealth): 默认开启并收敛 chrome 指纹特征
This commit is contained in:
Generated
+1
-1
@@ -3,7 +3,7 @@
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "agent-browser"
|
||||
name = "agent-browser-stealth"
|
||||
version = "0.14.0"
|
||||
dependencies = [
|
||||
"base64",
|
||||
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
[package]
|
||||
name = "agent-browser"
|
||||
name = "agent-browser-stealth"
|
||||
version = "0.14.0"
|
||||
edition = "2021"
|
||||
description = "Fast browser automation CLI for AI agents"
|
||||
description = "Stealth browser automation CLI for AI agents with anti-bot evasions"
|
||||
license = "Apache-2.0"
|
||||
|
||||
[dependencies]
|
||||
|
||||
@@ -1923,10 +1923,8 @@ mod tests {
|
||||
cli_proxy_bypass: false,
|
||||
cli_allow_file_access: false,
|
||||
cli_annotate: false,
|
||||
cli_stealth: false,
|
||||
annotate: false,
|
||||
color_scheme: None,
|
||||
stealth: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -220,7 +220,6 @@ pub fn ensure_daemon(
|
||||
provider: Option<&str>,
|
||||
device: Option<&str>,
|
||||
session_name: Option<&str>,
|
||||
stealth: bool,
|
||||
debug: bool,
|
||||
) -> Result<DaemonResult, String> {
|
||||
// Check if daemon is running AND responsive
|
||||
@@ -366,7 +365,7 @@ pub fn ensure_daemon(
|
||||
cmd.env("AGENT_BROWSER_SESSION_NAME", sn);
|
||||
}
|
||||
|
||||
cmd.env("AGENT_BROWSER_STEALTH", if stealth { "1" } else { "0" });
|
||||
cmd.env("AGENT_BROWSER_STEALTH", "1");
|
||||
if debug {
|
||||
cmd.env("AGENT_BROWSER_DEBUG", "1");
|
||||
}
|
||||
@@ -454,7 +453,7 @@ pub fn ensure_daemon(
|
||||
cmd.env("AGENT_BROWSER_SESSION_NAME", sn);
|
||||
}
|
||||
|
||||
cmd.env("AGENT_BROWSER_STEALTH", if stealth { "1" } else { "0" });
|
||||
cmd.env("AGENT_BROWSER_STEALTH", "1");
|
||||
if debug {
|
||||
cmd.env("AGENT_BROWSER_DEBUG", "1");
|
||||
}
|
||||
|
||||
@@ -34,7 +34,6 @@ pub struct Config {
|
||||
pub headers: Option<String>,
|
||||
pub annotate: Option<bool>,
|
||||
pub color_scheme: Option<String>,
|
||||
pub stealth: Option<bool>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
@@ -69,7 +68,6 @@ impl Config {
|
||||
headers: other.headers.or(self.headers),
|
||||
annotate: other.annotate.or(self.annotate),
|
||||
color_scheme: other.color_scheme.or(self.color_scheme),
|
||||
stealth: other.stealth.or(self.stealth),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -204,7 +202,6 @@ pub struct Flags {
|
||||
pub session_name: Option<String>,
|
||||
pub annotate: bool,
|
||||
pub color_scheme: Option<String>,
|
||||
pub stealth: bool,
|
||||
|
||||
// Track which launch-time options were explicitly passed via CLI
|
||||
// (as opposed to being set only via environment variables)
|
||||
@@ -218,7 +215,6 @@ pub struct Flags {
|
||||
pub cli_proxy_bypass: bool,
|
||||
pub cli_allow_file_access: bool,
|
||||
pub cli_annotate: bool,
|
||||
pub cli_stealth: bool,
|
||||
}
|
||||
|
||||
pub fn parse_flags(args: &[String]) -> Flags {
|
||||
@@ -283,10 +279,6 @@ pub fn parse_flags(args: &[String]) -> Flags {
|
||||
color_scheme: env::var("AGENT_BROWSER_COLOR_SCHEME")
|
||||
.ok()
|
||||
.or(config.color_scheme),
|
||||
stealth: match env::var("AGENT_BROWSER_STEALTH") {
|
||||
Ok(val) => !matches!(val.to_lowercase().as_str(), "0" | "false" | "no" | ""),
|
||||
Err(_) => config.stealth.unwrap_or(true),
|
||||
},
|
||||
cli_executable_path: false,
|
||||
cli_extensions: false,
|
||||
cli_profile: false,
|
||||
@@ -297,7 +289,6 @@ pub fn parse_flags(args: &[String]) -> Flags {
|
||||
cli_proxy_bypass: false,
|
||||
cli_allow_file_access: false,
|
||||
cli_annotate: false,
|
||||
cli_stealth: false,
|
||||
};
|
||||
|
||||
let mut i = 0;
|
||||
@@ -453,14 +444,6 @@ pub fn parse_flags(args: &[String]) -> Flags {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
"--stealth" => {
|
||||
let (val, consumed) = parse_bool_arg(args, i);
|
||||
flags.stealth = val;
|
||||
flags.cli_stealth = true;
|
||||
if consumed {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
"--color-scheme" => {
|
||||
if let Some(s) = args.get(i + 1) {
|
||||
flags.color_scheme = Some(s.clone());
|
||||
@@ -492,7 +475,6 @@ pub fn clean_args(args: &[String]) -> Vec<String> {
|
||||
"--allow-file-access",
|
||||
"--auto-connect",
|
||||
"--annotate",
|
||||
"--stealth",
|
||||
];
|
||||
// Global flags that always take a value (need to skip the next arg too)
|
||||
const GLOBAL_FLAGS_WITH_VALUE: &[&str] = &[
|
||||
|
||||
+3
-22
@@ -52,14 +52,6 @@ fn parse_proxy(proxy_str: &str) -> serde_json::Value {
|
||||
})
|
||||
}
|
||||
|
||||
fn print_stealth_debug(resp: &connection::Response) {
|
||||
if let Some(data) = &resp.data {
|
||||
if let Some(stealth) = data.get("stealth") {
|
||||
eprintln!("[DEBUG] stealth: {}", stealth);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run_session(args: &[String], session: &str, json_mode: bool) {
|
||||
let subcommand = args.get(1).map(|s| s.as_str());
|
||||
|
||||
@@ -234,7 +226,6 @@ fn main() {
|
||||
flags.provider.as_deref(),
|
||||
flags.device.as_deref(),
|
||||
flags.session_name.as_deref(),
|
||||
flags.stealth,
|
||||
flags.debug,
|
||||
) {
|
||||
Ok(result) => result,
|
||||
@@ -291,7 +282,6 @@ fn main() {
|
||||
},
|
||||
flags.ignore_https_errors.then_some("--ignore-https-errors"),
|
||||
flags.cli_allow_file_access.then_some("--allow-file-access"),
|
||||
flags.cli_stealth.then_some("--stealth"),
|
||||
]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
@@ -472,9 +462,7 @@ fn main() {
|
||||
}
|
||||
exit(1);
|
||||
}
|
||||
if flags.debug {
|
||||
print_stealth_debug(&resp);
|
||||
}
|
||||
|
||||
}
|
||||
Err(e) => {
|
||||
if flags.json {
|
||||
@@ -512,9 +500,7 @@ fn main() {
|
||||
}
|
||||
exit(1);
|
||||
}
|
||||
if flags.debug {
|
||||
print_stealth_debug(&resp);
|
||||
}
|
||||
|
||||
}
|
||||
Err(e) => {
|
||||
if flags.json {
|
||||
@@ -537,7 +523,6 @@ fn main() {
|
||||
|| flags.user_agent.is_some()
|
||||
|| flags.ignore_https_errors
|
||||
|| flags.allow_file_access
|
||||
|| flags.cli_stealth
|
||||
|| flags.debug
|
||||
|| flags.color_scheme.is_some())
|
||||
&& flags.cdp.is_none()
|
||||
@@ -601,8 +586,6 @@ fn main() {
|
||||
launch_cmd["allowFileAccess"] = json!(true);
|
||||
}
|
||||
|
||||
launch_cmd["stealth"] = json!(flags.stealth);
|
||||
|
||||
if let Some(ref cs) = flags.color_scheme {
|
||||
launch_cmd["colorScheme"] = json!(cs);
|
||||
}
|
||||
@@ -621,9 +604,7 @@ fn main() {
|
||||
}
|
||||
exit(1);
|
||||
}
|
||||
if flags.debug {
|
||||
print_stealth_debug(&resp);
|
||||
}
|
||||
|
||||
}
|
||||
Err(e) => {
|
||||
if flags.json {
|
||||
|
||||
+1
-2
@@ -2114,7 +2114,6 @@ Options:
|
||||
e.g., --proxy-bypass "localhost,*.internal.com"
|
||||
--ignore-https-errors Ignore HTTPS certificate errors
|
||||
--allow-file-access Allow file:// URLs to access local files (Chromium only)
|
||||
--stealth Stealth mode (default: on): local=launch args+init scripts, CDP/provider=init scripts
|
||||
-p, --provider <name> Browser provider: ios, browserbase, kernel, browseruse
|
||||
--device <name> iOS device name (e.g., "iPhone 15 Pro")
|
||||
--json JSON output
|
||||
@@ -2126,7 +2125,7 @@ Options:
|
||||
--color-scheme <scheme> Color scheme: dark, light, no-preference (or AGENT_BROWSER_COLOR_SCHEME)
|
||||
--session-name <name> Auto-save/restore session state (cookies, localStorage)
|
||||
--config <path> Use a custom config file (or AGENT_BROWSER_CONFIG env)
|
||||
--debug Debug output (includes stealth connection type + capabilities)
|
||||
--debug Debug output
|
||||
--version, -V Show version
|
||||
|
||||
Configuration:
|
||||
|
||||
+5
-18
@@ -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
@@ -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(),
|
||||
});
|
||||
|
||||
@@ -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
@@ -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,
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user