chore!: прикручиваю персистентность для отпечатков
This commit is contained in:
1 parent
4d0d3d4b09
commit
638c7a9618
1 file changed
+12
-16
+12
-16
@@ -34,13 +34,17 @@ def parse_proxy(proxy_url: str) -> ProxySettings:
|
|||||||
return settings
|
return settings
|
||||||
|
|
||||||
|
|
||||||
def load_or_create_identity(user_data_dir, os=None, ff_version=None, locale=None, time_zone=None) -> dict:
|
def load_or_create_identity(user_data_dir, os=None, ff_version=None, locale=None, time_zone=None, user_agent=None) -> dict:
|
||||||
identity_file = user_data_dir / IDENTITY_FILE
|
identity_file = user_data_dir / IDENTITY_FILE
|
||||||
|
|
||||||
if identity_file.exists():
|
if identity_file.exists():
|
||||||
return json.loads(identity_file.read_text())
|
return json.loads(identity_file.read_text())
|
||||||
|
|
||||||
# один вызов -> config (для браузера) + context_options/init_script (для контекста)
|
kwargs = {}
|
||||||
|
if user_agent:
|
||||||
|
os, ff_version = parse_os_and_ff(user_agent)
|
||||||
|
kwargs["config_overrides"] = {"navigator.UserAgent": user_agent}
|
||||||
|
|
||||||
fp = generate_context_fingerprint(os=os, ff_version=ff_version, locale=locale, timezone=time_zone)
|
fp = generate_context_fingerprint(os=os, ff_version=ff_version, locale=locale, timezone=time_zone)
|
||||||
identity_file.write_text(json.dumps(fp))
|
identity_file.write_text(json.dumps(fp))
|
||||||
return fp
|
return fp
|
||||||
@@ -57,12 +61,12 @@ async def root():
|
|||||||
_OS_MAP = {'win': 'windows', 'mac': 'macos', 'lin': 'linux'}
|
_OS_MAP = {'win': 'windows', 'mac': 'macos', 'lin': 'linux'}
|
||||||
|
|
||||||
|
|
||||||
def parse_os_and_ff(user_agent):
|
def parse_os_and_ff(user_agent) -> tuple[str, str]:
|
||||||
os_name = _OS_MAP[determine_ua_os(user_agent)]
|
os_name = _OS_MAP[determine_ua_os(user_agent)]
|
||||||
parsed = user_agent_parser.ParseUserAgent(user_agent)
|
parsed = user_agent_parser.ParseUserAgent(user_agent)
|
||||||
ff_version = int(parsed["major"]) if (
|
ff_version = int(parsed["major"]) if (
|
||||||
parsed.get("family") == "Firefox" and parsed.get("major")) else None
|
parsed.get("family") == "Firefox" and parsed.get("major")) else 150
|
||||||
return os_name, ff_version
|
return os_name, str(ff_version)
|
||||||
|
|
||||||
|
|
||||||
@app.post("/v1/solve")
|
@app.post("/v1/solve")
|
||||||
@@ -98,27 +102,19 @@ async def solve(
|
|||||||
if not user_data_dir.exists():
|
if not user_data_dir.exists():
|
||||||
user_data_dir.mkdir(parents=True)
|
user_data_dir.mkdir(parents=True)
|
||||||
|
|
||||||
browser_options = {
|
ctx_opts = {
|
||||||
# "locale": payload.locale,
|
# "locale": payload.locale,
|
||||||
# "timezone": payload.time_zone
|
# "timezone": payload.time_zone
|
||||||
}
|
}
|
||||||
ctx_opts = {}
|
|
||||||
if payload.user_agent:
|
|
||||||
os, ff_version = parse_os_and_ff(payload.user_agent)
|
|
||||||
ctx_opts["config"] = {'navigator.UserAgent': payload.user_agent}
|
|
||||||
else:
|
|
||||||
os = ff_version = None
|
|
||||||
|
|
||||||
config = load_or_create_identity(user_data_dir, os=os, ff_version=ff_version, **browser_options)
|
config = load_or_create_identity(user_data_dir, payload.user_agent, **ctx_opts)
|
||||||
|
|
||||||
ctx_opts = {**config["context_options"], **browser_options}
|
ctx_opts = {**config["context_options"], "config": config["config"]}
|
||||||
async with AsyncCamoufox(
|
async with AsyncCamoufox(
|
||||||
persistent_context=True,
|
persistent_context=True,
|
||||||
user_data_dir=user_data_dir,
|
user_data_dir=user_data_dir,
|
||||||
proxy=proxy,
|
proxy=proxy,
|
||||||
window=scr_size,
|
window=scr_size,
|
||||||
os=os,
|
|
||||||
ff_version=ff_version,
|
|
||||||
**ctx_opts,
|
**ctx_opts,
|
||||||
) as camoufo:
|
) as camoufo:
|
||||||
await camoufo.add_init_script(config["init_script"])
|
await camoufo.add_init_script(config["init_script"])
|
||||||
|
|||||||
Reference in new issue
Block a user