mirror of
https://github.com/daijro/camoufox.git
synced 2026-11-19 00:07:28 +00:00
* Let a launch leave Web Audio output unmodified The launchers seed Web Audio noise per identity so a returning identity reproduces its audio hash. A caller who wanted stock audio output had to pass audio:seed in config, and NewContext contexts still drew their own seed. audio_noise (default True, as before) set to False sends seed 0, which audio-fingerprint-manager.patch reads as no noise, and NewBrowser records it so NewContext gives its contexts seed 0 as well. An audio:seed the caller sets wins either way. Python and TypeScript take the option the same way, the launch goldens gain an audio_noise_false scenario, and both package READMEs list it. On v158.0-beta.40 an OfflineAudioContext probe renders the same with audio_noise=False across launches, in the page and in a NewContext, and matches a launch with audio:seed 0; default launches differ. Split out of #890, without its gpu_passthrough option. Co-Authored-By: coffeegrind123 <103264516+coffeegrind123@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Let a context turn its Web Audio noise off A context without an audio seed of its own takes the launch's audio:seed, so a per-context seed of 0 means "the launch's seed", not "no noise". On a browser launched with noise, no context could render stock audio. setAudioFingerprintNoise(false) stores that choice for the calling context, and AudioFingerprintManager::GetSeed returns 0 for it whatever the launch seed. The setter has its own gate and is sealed with the others; the build tester and the setter-seal guard list it. The context-audio-off guard renders an OfflineAudioContext with the noise off at launch as the reference, then on a noised launch requires a context that called the setter to match it, also after a navigation, while a plain context and a NewContext context stay noised. It fails on v158.0-beta.40, which has no such setter. Both patches were regenerated from a tree of the files the series touches, and the whole series applies to vanilla 158 without fuzz. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Replace audio_noise with fingerprint_options The launchers take one option for the noise Camoufox adds on top of an identity, in Python and TypeScript alike: fingerprint_options={"audio": {"noise": False}} # stock Web Audio fingerprint_options={"audio": {"seed": 1000433}} # fixed audio seed fingerprint_options={"canvas": {"randomize": True}} Without an audio entry the seed still follows the identity; without a canvas entry randomization is still on for another OS or GPU. Any other surface or key raises InvalidFingerprintOptions, naming the tribal rules that keep canvas pixels and glyph spacing unnoised, as do a seed outside 1 to 2**32-1 and noise off together with a seed. fingerprint= stays the fpgen identity. canvas_noise= and config={"audio:seed": N} keep working with a FutureWarning, as a deprecated GeoIP source does; either one together with the matching fingerprint_options entry raises. NewContext takes the audio entry per context. Without one a context follows the browser, read from the audio:seed the launch sent, so launch_options() and from_options launches behave the same: no noise if the launch sent 0, otherwise a fresh seed of its own. A context never takes the browser's seed, fixed or not, since two devices sharing an audio hash are linked; a per-context seed reproduces a context instead. A context with its noise off calls setAudioFingerprintNoise(false). On a noised browser that needs a build with the setter: NewContext asks the browser once, from an init script in a throwaway context, and raises on an older build rather than leave the context noised. canvas in a context raises: randomization belongs to the browser. audio_noise, context_audio(browser) and _camoufox_audio_noise are gone. The launch goldens replace audio_noise_false with audio and canvas scenarios, the init-script golden gains a seed-0 case, and the READMEs, AGENTS.md and docs/per-context-patches.md describe the option; repo callers of canvas_noise use the new form. On v158.0-beta.40, an OfflineAudioContext probe renders the same with {"audio": {"noise": False}} in the page, in a NewContext and with config audio:seed 0; default launches differ; seed 1000433 and a context seed reproduce across launches; a context turning its noise off on a noised browser raises. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Audio renders as stock: remove the noise, the seed and fingerprint_options Stock Firefox has one audio fingerprint per CPU architecture. 103 runs of Firefox 152-158 on 12 CPUs gave 35.749972093850374 on every x86-64 machine and 35.74996626004577 on every ARM64 one, whatever the OS, sound device, sample rate or version. The per-identity noise put every Camoufox identity on a value no real machine renders. Browser: audio-fingerprint-manager.patch is gone, with setAudioFingerprintSeed and setAudioFingerprintNoise; font-list-spoofing, webrtc-ip-spoofing and window-setter-seal are regenerated without its context (the series applies cleanly to vanilla 158). audio:seed is marked removed in properties.json. Library (py and ts): no seed is derived or drawn, and config or config_overrides carrying audio:seed raise InvalidPropertyType pointing at the new tribal rule. fingerprint_options, never released, is reverted; canvas_noise stays as released. Tests: tribal rule audio-is-not-noised with its test; the audio-matches-stock guard replaces context-audio-off and audio-frequency-noise, holding the fpjs buffer, AnalyserNode, biquad and sine renders to the recorded stock values in several contexts and two launches while sending the seed released libraries send. build-tester counts the audio hash as must-match and fails its noise trap. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * A removed audio:seed warns and is dropped; the rule lists each machine Passing audio:seed in config (or a NewContext config_overrides) no longer raises. Both launchers drop the key and emit one FutureWarning from warnings.yml (audio_seed_removed): the option was removed and audio is rendered exactly as stock Firefox renders it. The caller's dict is left as it was. Tests on both sides check the single warning and a normal launch; the launch golden records the warning in order. audio-is-not-noised now lists the evidence per machine and architecture, including the Raspberry Pi 4 runs (106 runs: 45 ARM64, 61 x86-64), the device swap, fpgen's share and what the noise produced. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: coffeegrind123 <103264516+coffeegrind123@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
266 lines
10 KiB
Python
266 lines
10 KiB
Python
import asyncio
|
|
from functools import partial
|
|
from typing import Any, Dict, Optional, Tuple, Union, overload
|
|
|
|
from playwright.async_api import (
|
|
Browser,
|
|
BrowserContext,
|
|
Playwright,
|
|
PlaywrightContextManager,
|
|
)
|
|
from typing_extensions import Literal
|
|
|
|
from camoufox.virtdisplay import VirtualDisplay
|
|
|
|
from .fingerprints import generate_context_fingerprint
|
|
from .ip import Proxy, proxy_exit_geo
|
|
from .utils import (
|
|
async_attach_vd,
|
|
attach_context_defaults,
|
|
context_identity,
|
|
host_identity,
|
|
launch_options,
|
|
new_context_options,
|
|
persistent_context_options,
|
|
spoofs_window_dimensions,
|
|
start_virtual_display,
|
|
)
|
|
|
|
|
|
class AsyncCamoufox(PlaywrightContextManager):
|
|
"""
|
|
Wrapper around playwright.async_api.PlaywrightContextManager that automatically
|
|
launches a browser and closes it when the context manager is exited.
|
|
"""
|
|
|
|
def __init__(self, **launch_options):
|
|
super().__init__()
|
|
self.launch_options = launch_options
|
|
self.browser: Optional[Union[Browser, BrowserContext]] = None
|
|
|
|
async def __aenter__(self) -> Union[Browser, BrowserContext]:
|
|
_playwright = await super().__aenter__()
|
|
try:
|
|
self.browser = await AsyncNewBrowser(_playwright, **self.launch_options)
|
|
except BaseException as e:
|
|
# Any launch failure (InvalidProxy, missing browser, bad options, ...)
|
|
# must tear down the playwright session started above so the driver
|
|
# process/connection is not leaked.
|
|
await super().__aexit__(type(e), e, e.__traceback__)
|
|
raise
|
|
return self.browser
|
|
|
|
async def __aexit__(self, *args: Any):
|
|
# Run the base teardown even if browser.close() raises (e.g. the browser
|
|
# process already crashed), so the playwright driver/connection is not leaked.
|
|
try:
|
|
if self.browser:
|
|
await self.browser.close()
|
|
finally:
|
|
await super().__aexit__(*args)
|
|
|
|
|
|
@overload
|
|
async def AsyncNewBrowser(
|
|
playwright: Playwright,
|
|
*,
|
|
from_options: Optional[Dict[str, Any]] = None,
|
|
persistent_context: Literal[False] = False,
|
|
**kwargs,
|
|
) -> Browser: ...
|
|
|
|
|
|
@overload
|
|
async def AsyncNewBrowser(
|
|
playwright: Playwright,
|
|
*,
|
|
from_options: Optional[Dict[str, Any]] = None,
|
|
persistent_context: Literal[True],
|
|
**kwargs,
|
|
) -> BrowserContext: ...
|
|
|
|
|
|
async def AsyncNewBrowser(
|
|
playwright: Playwright,
|
|
*,
|
|
headless: Optional[Union[bool, Literal['virtual', 'virtual-gpu']]] = None,
|
|
from_options: Optional[Dict[str, Any]] = None,
|
|
persistent_context: bool = False,
|
|
debug: Optional[bool] = None,
|
|
**kwargs,
|
|
) -> Union[Browser, BrowserContext]:
|
|
"""
|
|
Launches a new browser instance for Camoufox given a set of launch options.
|
|
|
|
Parameters:
|
|
headless (Optional[Union[bool, Literal['virtual', 'virtual-gpu']]]):
|
|
True for Firefox headless. On Linux, 'virtual' runs a headed browser on
|
|
an Xvfb display, and 'virtual-gpu' on a headless Xorg server bound to
|
|
the GPU `gpu` (or CAMOUFOX_GPU) names; `require_gpu=False` warns instead
|
|
of failing when the browser does not render on it.
|
|
from_options (Dict[str, Any]):
|
|
A set of launch options generated by `launch_options()` to use
|
|
persistent_context (bool):
|
|
Whether to use a persistent context.
|
|
**kwargs:
|
|
All other keyword arguments passed to `launch_options()`.
|
|
"""
|
|
virtual_display, headless = await asyncio.get_running_loop().run_in_executor(
|
|
None, start_virtual_display, headless, debug, kwargs
|
|
)
|
|
# Everything after the display exists can fail (bad proxy, missing binary,
|
|
# unsatisfiable constraints) before sync/async_attach_vd wires kill() into
|
|
# close, which used to strand one Xvfb and its /tmp/.X<n> lock per failed
|
|
# launch.
|
|
try:
|
|
return await _new_browser(
|
|
playwright, virtual_display, headless, from_options, persistent_context, debug, kwargs
|
|
)
|
|
except BaseException:
|
|
if virtual_display:
|
|
virtual_display.kill()
|
|
raise
|
|
|
|
|
|
async def _new_browser(
|
|
playwright: Playwright,
|
|
virtual_display: Optional[VirtualDisplay],
|
|
headless: Any,
|
|
from_options: Optional[Dict[str, Any]],
|
|
persistent_context: bool,
|
|
debug: Optional[bool],
|
|
kwargs: Dict[str, Any],
|
|
) -> Union[Browser, BrowserContext]:
|
|
# Options the caller built leave its contexts to the caller (host_identity).
|
|
canvas_noise: Optional[bool] = False
|
|
if not from_options:
|
|
canvas_noise = kwargs.get('canvas_noise')
|
|
# Opt-in; see the note in sync_api.NewBrowser.
|
|
kwargs.setdefault('pin_cpu_cores', False)
|
|
from_options = await asyncio.get_event_loop().run_in_executor(
|
|
None,
|
|
partial(launch_options, headless=headless, debug=debug, **kwargs),
|
|
)
|
|
|
|
# Playwright's default viewport deadlocks Juggler when the window is spoofed
|
|
# to a different size (daijro/camoufox#666), so default to no_viewport.
|
|
no_viewport_default = spoofs_window_dimensions(from_options)
|
|
|
|
# Pin the driver (and so the browser it is about to spawn) to as many
|
|
# cores as the identity reports, so measurable parallelism matches
|
|
# navigator.hardwareConcurrency; the driver gets its cores back afterwards.
|
|
from . import cpu_affinity
|
|
from .utils import driver_pid, pinned_core_count
|
|
|
|
pin_to = pinned_core_count(from_options)
|
|
pid = driver_pid(playwright) if pin_to else None
|
|
if not pid:
|
|
return await _launch(
|
|
playwright, from_options, persistent_context, no_viewport_default, virtual_display, canvas_noise
|
|
)
|
|
# The browser inherits the driver's mask at spawn, so two concurrent launches
|
|
# on one driver must not interleave pin/restore: the second pin would land on
|
|
# the first browser, and the first restore would leave the driver pinned.
|
|
async with _pin_lock(pid):
|
|
previous = cpu_affinity.pin(pid, pin_to)
|
|
try:
|
|
return await _launch(
|
|
playwright, from_options, persistent_context, no_viewport_default, virtual_display, canvas_noise
|
|
)
|
|
finally:
|
|
cpu_affinity.restore(pid, previous)
|
|
|
|
|
|
_PIN_LOCKS: Dict[int, asyncio.Lock] = {}
|
|
|
|
|
|
def _pin_lock(pid: int) -> asyncio.Lock:
|
|
# One lock per driver: a driver belongs to one event loop.
|
|
lock = _PIN_LOCKS.get(pid)
|
|
if lock is None:
|
|
lock = _PIN_LOCKS[pid] = asyncio.Lock()
|
|
return lock
|
|
|
|
|
|
async def _launch(
|
|
playwright: Playwright,
|
|
from_options: Dict[str, Any],
|
|
persistent_context: bool,
|
|
no_viewport_default: bool,
|
|
virtual_display: Optional[VirtualDisplay],
|
|
canvas_noise: Optional[bool],
|
|
) -> Union[Browser, BrowserContext]:
|
|
if persistent_context:
|
|
context = await playwright.firefox.launch_persistent_context(
|
|
**persistent_context_options(from_options, no_viewport_default)
|
|
)
|
|
return await async_attach_vd(context, virtual_display)
|
|
|
|
identity = await asyncio.get_running_loop().run_in_executor(None, host_identity, from_options, canvas_noise)
|
|
browser = await playwright.firefox.launch(**from_options)
|
|
attach_context_defaults(browser, no_viewport_default)
|
|
browser._camoufox_host_identity = identity
|
|
return await async_attach_vd(browser, virtual_display)
|
|
|
|
|
|
async def _resolve_proxy_geo(proxy: Dict[str, str]) -> Tuple[str, str]:
|
|
"""The proxy's exit IP and timezone, looked up off the event loop."""
|
|
# run_in_executor rather than asyncio.to_thread, which needs Python 3.9.
|
|
loop = asyncio.get_running_loop()
|
|
return await loop.run_in_executor(None, proxy_exit_geo, Proxy(**proxy).as_string())
|
|
|
|
|
|
async def AsyncNewContext(
|
|
browser: Browser,
|
|
*,
|
|
preset: Optional[Dict[str, Any]] = None,
|
|
os: Optional[str] = None,
|
|
ff_version: Optional[str] = None,
|
|
webrtc_ip: Optional[str] = None,
|
|
proxy: Optional[Dict[str, str]] = None,
|
|
geolocation: Optional[Dict[str, float]] = None,
|
|
**context_kwargs: Any,
|
|
) -> BrowserContext:
|
|
"""
|
|
Creates a new browser context with a unique fingerprint identity.
|
|
|
|
Each context gets its own identity (navigator, screen, WebGL, fonts, voices),
|
|
drawn by fpgen unless a preset is given. All values are applied
|
|
via addInitScript so they self-destruct before page scripts can detect them.
|
|
|
|
Parameters:
|
|
browser: A Browser instance from AsyncNewBrowser or AsyncCamoufox.
|
|
preset: A fingerprint preset dict to use. If None, fpgen draws a new identity.
|
|
os: Target OS for the drawn identity ("windows", "macos", "linux"). On a browser launched
|
|
without canvas noise, contexts claim the host's OS and GPU, and another os or preset
|
|
raises ValueError.
|
|
ff_version: Firefox major version to claim in the UA. Defaults to the browser's own.
|
|
webrtc_ip: IPv4 or IPv6 address to spoof for WebRTC ICE candidates.
|
|
proxy: Per-context proxy (Playwright format: {"server": "...", "username": "...", "password": "..."}).
|
|
Unless webrtc_ip and timezone_id are both given, they are looked up from the
|
|
proxy's exit IP; InvalidIP is raised if that lookup fails.
|
|
geolocation: Per-context geolocation ({"latitude": float, "longitude": float}).
|
|
**context_kwargs: Additional Playwright new_context() options.
|
|
"""
|
|
# The drawn UA carries fpgen's Firefox version, which must not disagree with
|
|
# the browser the page is actually talking to.
|
|
ff_version = ff_version or browser.version.split('.', 1)[0]
|
|
|
|
# Auto-derive WebRTC IP and timezone from proxy's exit IP when not explicitly provided
|
|
if proxy and (not webrtc_ip or "timezone_id" not in context_kwargs):
|
|
exit_ip, timezone = await _resolve_proxy_geo(proxy)
|
|
webrtc_ip = webrtc_ip or exit_ip
|
|
context_kwargs.setdefault("timezone_id", timezone)
|
|
|
|
os, webgl_config = context_identity(browser, os, preset)
|
|
fp = await asyncio.get_event_loop().run_in_executor(
|
|
None,
|
|
lambda: generate_context_fingerprint(
|
|
preset=preset, os=os, ff_version=ff_version, webrtc_ip=webrtc_ip, webgl_config=webgl_config
|
|
),
|
|
)
|
|
|
|
context = await browser.new_context(**new_context_options(fp, context_kwargs, proxy, geolocation))
|
|
await context.add_init_script(fp['init_script'])
|
|
return context
|