mirror of
https://github.com/daijro/camoufox.git
synced 2026-11-19 00:07:28 +00:00
Found in review of the previous commits: - identity_seed() hashed only the UA, platform, screen size and core count. Those take a handful of values per OS, so over 500 launches the seed took 12-30 distinct values and every install drew its fonts, voices, GPU, media devices and canvas/audio noise seeds from that same short list. The seed now mixes in identity_salt(): derived from what the caller pinned the identity with (a Fingerprint, a preset dict, a config naming the UA) so relaunching that identity reproduces every draw, and random otherwise. A pinned preset now reproduces its noise seeds too; seeds the caller sets are kept. - Concurrent AsyncNewBrowser launches on one driver interleaved pin/restore: one browser inherited the other's mask and the driver could stay pinned. pin -> launch -> restore is serialized per driver. - Every pinned browser landed on cores 0..N-1; pins now take N adjacent cores from a random start. - A pinnable host with 1-3 cores reported 1, 2 or 3 (2 is the resistFingerprinting value); the table floor of 4 applies as on other hosts. - launch_options() callers that launch the browser themselves (launch_server, direct use) kept the drawn core count although nothing pins the browser; only Camoufox/AsyncCamoufox pass pin_cpu_cores=True now, everyone else reports the host's snapped count. - PLAUSIBLE_CORE_COUNTS gains 18, 22, 28 and 32, all recorded in the -v150 corpus. - The Windows voice list was drawn before the locale was resolved, so an fr-FR identity got en-US voices; it is drawn after locale/geoip now. - macOS "Alex" gets its com.apple.speech.synthesis.voice identifier. - CAMOU_PREFS env chunks are ASCII-only JSON (Windows getenv goes through the ANSI code page). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
155 lines
5.0 KiB
Python
155 lines
5.0 KiB
Python
"""Pin the browser to as many CPU cores as the identity reports.
|
|
|
|
navigator.hardwareConcurrency is spoofed by the browser, but the number of
|
|
cores a page can *measure* (timing N parallel workers) is the number the OS
|
|
lets the browser run on. Reporting the fingerprint's value and pinning the
|
|
browser's CPU affinity to that many cores makes the two agree, so the drawn
|
|
value survives instead of being replaced by the host count.
|
|
|
|
The pin is applied to the Playwright driver process right before the browser
|
|
is launched -- child processes inherit the affinity mask on Linux and Windows,
|
|
so the browser and every content/GPU process it spawns run on the pinned set
|
|
-- and lifted from the driver again afterwards. macOS has no process affinity
|
|
API, so nothing can be pinned there and the launcher falls back to reporting
|
|
the host's (snapped) count.
|
|
"""
|
|
|
|
import os
|
|
import platform
|
|
import random
|
|
from typing import Iterable, List, Optional, Sequence
|
|
|
|
|
|
def supported() -> bool:
|
|
"""Whether this host can constrain a process to a subset of its cores."""
|
|
system = platform.system()
|
|
if system == 'Linux':
|
|
return hasattr(os, 'sched_setaffinity')
|
|
return system == 'Windows'
|
|
|
|
|
|
def host_cores() -> Optional[List[int]]:
|
|
"""The cores this process may run on, in order."""
|
|
try:
|
|
if hasattr(os, 'sched_getaffinity'):
|
|
return sorted(os.sched_getaffinity(0)) # type: ignore[attr-defined]
|
|
except OSError:
|
|
pass
|
|
if platform.system() == 'Windows':
|
|
mask = _win_get_mask(os.getpid())
|
|
if mask:
|
|
return _mask_to_cores(mask)
|
|
n = os.cpu_count()
|
|
return list(range(n)) if n else None
|
|
|
|
|
|
def _pick(cores: Sequence[int], count: int) -> List[int]:
|
|
"""`count` adjacent cores from a random starting point (wrapping). Always
|
|
taking the first `count` stacked every browser on one host onto cores
|
|
0..count-1, so concurrent browsers measured far less parallelism than they
|
|
report; adjacent cores keep the SMT topology a real machine of that size
|
|
would have."""
|
|
start = random.randrange(len(cores))
|
|
return sorted((list(cores[start:]) + list(cores[:start]))[:count])
|
|
|
|
|
|
def pin(pid: int, count: int) -> Optional[Sequence[int]]:
|
|
"""Restrict `pid` to `count` of its cores. Returns the previous set so it
|
|
can be handed back to `restore()`, or None if nothing was changed.
|
|
|
|
The caller must not pin the same process for two launches at once: the
|
|
browser inherits whatever mask the driver has when it is spawned."""
|
|
if count < 1 or not supported():
|
|
return None
|
|
system = platform.system()
|
|
if system == 'Linux':
|
|
try:
|
|
before = sorted(os.sched_getaffinity(pid)) # type: ignore[attr-defined]
|
|
if count >= len(before):
|
|
return None
|
|
os.sched_setaffinity(pid, set(_pick(before, count))) # type: ignore[attr-defined]
|
|
return before
|
|
except OSError:
|
|
return None
|
|
if system == 'Windows':
|
|
before_mask = _win_get_mask(pid)
|
|
if not before_mask:
|
|
return None
|
|
before = _mask_to_cores(before_mask)
|
|
if count >= len(before):
|
|
return None
|
|
return before if _win_set_mask(pid, _cores_to_mask(_pick(before, count))) else None
|
|
return None
|
|
|
|
|
|
def restore(pid: int, previous: Optional[Sequence[int]]) -> None:
|
|
"""Give `pid` back the cores it had before `pin()`."""
|
|
if not previous:
|
|
return
|
|
system = platform.system()
|
|
try:
|
|
if system == 'Linux':
|
|
os.sched_setaffinity(pid, set(previous)) # type: ignore[attr-defined]
|
|
elif system == 'Windows':
|
|
_win_set_mask(pid, _cores_to_mask(previous))
|
|
except OSError:
|
|
pass
|
|
|
|
|
|
# -- Windows ---------------------------------------------------------------
|
|
|
|
_PROCESS_QUERY_INFORMATION = 0x0400
|
|
_PROCESS_SET_INFORMATION = 0x0200
|
|
|
|
|
|
def _mask_to_cores(mask: int) -> List[int]:
|
|
return [i for i in range(mask.bit_length()) if mask >> i & 1]
|
|
|
|
|
|
def _cores_to_mask(cores: Iterable[int]) -> int:
|
|
mask = 0
|
|
for c in cores:
|
|
mask |= 1 << c
|
|
return mask
|
|
|
|
|
|
def _win_handle(pid: int):
|
|
import ctypes
|
|
|
|
k32 = ctypes.windll.kernel32 # type: ignore[attr-defined]
|
|
return k32, k32.OpenProcess(_PROCESS_QUERY_INFORMATION | _PROCESS_SET_INFORMATION, False, pid)
|
|
|
|
|
|
def _win_get_mask(pid: int) -> int:
|
|
try:
|
|
import ctypes
|
|
|
|
k32, h = _win_handle(pid)
|
|
if not h:
|
|
return 0
|
|
try:
|
|
proc_mask = ctypes.c_size_t()
|
|
sys_mask = ctypes.c_size_t()
|
|
if not k32.GetProcessAffinityMask(h, ctypes.byref(proc_mask), ctypes.byref(sys_mask)):
|
|
return 0
|
|
return int(proc_mask.value)
|
|
finally:
|
|
k32.CloseHandle(h)
|
|
except Exception:
|
|
return 0
|
|
|
|
|
|
def _win_set_mask(pid: int, mask: int) -> bool:
|
|
try:
|
|
import ctypes
|
|
|
|
k32, h = _win_handle(pid)
|
|
if not h:
|
|
return False
|
|
try:
|
|
return bool(k32.SetProcessAffinityMask(h, ctypes.c_size_t(mask)))
|
|
finally:
|
|
k32.CloseHandle(h)
|
|
except Exception:
|
|
return False
|