zerochat 10.4.0__tar.gz → 10.5.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of zerochat might be problematic. Click here for more details.
- {zerochat-10.4.0/zerochat.egg-info → zerochat-10.5.0}/PKG-INFO +1 -1
- {zerochat-10.4.0 → zerochat-10.5.0}/pyproject.toml +1 -1
- {zerochat-10.4.0 → zerochat-10.5.0/zerochat.egg-info}/PKG-INFO +1 -1
- {zerochat-10.4.0 → zerochat-10.5.0}/zerochat.py +71 -21
- {zerochat-10.4.0 → zerochat-10.5.0}/LICENSE +0 -0
- {zerochat-10.4.0 → zerochat-10.5.0}/README.md +0 -0
- {zerochat-10.4.0 → zerochat-10.5.0}/setup.cfg +0 -0
- {zerochat-10.4.0 → zerochat-10.5.0}/zerochat.egg-info/SOURCES.txt +0 -0
- {zerochat-10.4.0 → zerochat-10.5.0}/zerochat.egg-info/dependency_links.txt +0 -0
- {zerochat-10.4.0 → zerochat-10.5.0}/zerochat.egg-info/entry_points.txt +0 -0
- {zerochat-10.4.0 → zerochat-10.5.0}/zerochat.egg-info/top_level.txt +0 -0
|
@@ -30,7 +30,6 @@ import json
|
|
|
30
30
|
import math
|
|
31
31
|
import os
|
|
32
32
|
import queue
|
|
33
|
-
import random
|
|
34
33
|
import re
|
|
35
34
|
import secrets
|
|
36
35
|
import shutil
|
|
@@ -54,7 +53,7 @@ import zlib
|
|
|
54
53
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
55
54
|
from pathlib import Path
|
|
56
55
|
|
|
57
|
-
SOURCE_BACKEND_VERSION = "10.
|
|
56
|
+
SOURCE_BACKEND_VERSION = "10.5.0"
|
|
58
57
|
|
|
59
58
|
def _read_source_version(filename: str) -> str | None:
|
|
60
59
|
"""Lee la versión de un archivo del repositorio cuando se ejecuta desde fuentes."""
|
|
@@ -2299,7 +2298,7 @@ def chromium_install() -> tuple[str, Path] | None:
|
|
|
2299
2298
|
return (executable, data_dir) if executable and os.path.isfile(executable) else None
|
|
2300
2299
|
|
|
2301
2300
|
|
|
2302
|
-
def playwright_launch_args(config: dict) -> list[str]:
|
|
2301
|
+
def playwright_launch_args(config: dict, user_agent: str | None = None) -> list[str]:
|
|
2303
2302
|
"""Argumentos de Playwright MCP para la configuración del navegador."""
|
|
2304
2303
|
if config["mode"] == "user":
|
|
2305
2304
|
# La extensión maneja el navegador abierto del usuario: siempre con ventana.
|
|
@@ -2321,6 +2320,9 @@ def playwright_launch_args(config: dict) -> list[str]:
|
|
|
2321
2320
|
args.append(f"--user-data-dir={profile_dir}")
|
|
2322
2321
|
else:
|
|
2323
2322
|
args.append("--isolated")
|
|
2323
|
+
# El motor propio usa el User-Agent de ZeroChat; en modo user lo pone el navegador del usuario.
|
|
2324
|
+
if user_agent:
|
|
2325
|
+
args.append(f"--user-agent={user_agent}")
|
|
2324
2326
|
if config["headless"]:
|
|
2325
2327
|
args.append("--headless")
|
|
2326
2328
|
return args
|
|
@@ -2365,17 +2367,27 @@ def browser_choice(config: dict) -> dict:
|
|
|
2365
2367
|
return {"mode": "user", "userBrowser": config["userBrowser"]}
|
|
2366
2368
|
|
|
2367
2369
|
|
|
2368
|
-
def _playwright_launch(browser: dict | None) -> tuple[list[str], dict[str, str], dict]:
|
|
2370
|
+
def _playwright_launch(browser: dict | None, user_agent: str | None = None) -> tuple[list[str], dict[str, str], dict]:
|
|
2369
2371
|
"""La configuración de Ajustes › Navegador con el modo y el navegador que exija el perfil activo."""
|
|
2370
2372
|
config = load_browser_config()
|
|
2371
2373
|
if browser is not None:
|
|
2372
2374
|
config = {**config, **validate_profile_browser(browser)}
|
|
2373
|
-
return playwright_launch_args(config), playwright_launch_env(config), browser_choice(config)
|
|
2375
|
+
return playwright_launch_args(config, user_agent), playwright_launch_env(config), browser_choice(config)
|
|
2376
|
+
|
|
2377
|
+
|
|
2378
|
+
# User-Agent que usan los motores propios de Playwright; lo fija el servidor con el User-Agent
|
|
2379
|
+
# activo de las salidas web (ff-server) para que ambos compartan el mismo fingerprint.
|
|
2380
|
+
SERVICE_USER_AGENT: str | None = None
|
|
2381
|
+
|
|
2382
|
+
|
|
2383
|
+
def set_service_user_agent(user_agent: str | None) -> None:
|
|
2384
|
+
global SERVICE_USER_AGENT
|
|
2385
|
+
SERVICE_USER_AGENT = user_agent
|
|
2374
2386
|
|
|
2375
2387
|
|
|
2376
2388
|
# Argumentos, entorno y navegador elegido que añade ZeroChat al arrancar un servicio gestionado,
|
|
2377
2389
|
# calculados en cada arranque.
|
|
2378
|
-
SERVICE_LAUNCH = {"playwright": _playwright_launch}
|
|
2390
|
+
SERVICE_LAUNCH = {"playwright": lambda browser: _playwright_launch(browser, SERVICE_USER_AGENT)}
|
|
2379
2391
|
|
|
2380
2392
|
|
|
2381
2393
|
# Motores propios de Playwright: cada uno se compone de las entradas de la caché que instala
|
|
@@ -4077,19 +4089,39 @@ METADATA_HOSTS = frozenset({
|
|
|
4077
4089
|
"instance-data",
|
|
4078
4090
|
"100.100.100.200",
|
|
4079
4091
|
})
|
|
4080
|
-
DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/
|
|
4092
|
+
DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
|
|
4093
|
+
# El User-Agent activo se sustituye por el de la primera petición del frontend (variabilidad de
|
|
4094
|
+
# fingerprint); sin frontend (--prompt/--conversation) se queda el valor por defecto.
|
|
4095
|
+
ACTIVE_USER_AGENT = DEFAULT_USER_AGENT
|
|
4096
|
+
|
|
4097
|
+
|
|
4098
|
+
def capture_frontend_user_agent(user_agent: str) -> None:
|
|
4099
|
+
"""Adopta el User-Agent de la primera petición del frontend como User-Agent de las salidas web."""
|
|
4100
|
+
global ACTIVE_USER_AGENT
|
|
4101
|
+
if ACTIVE_USER_AGENT is DEFAULT_USER_AGENT and isinstance(user_agent, str) and user_agent.strip():
|
|
4102
|
+
ACTIVE_USER_AGENT = user_agent.strip()
|
|
4103
|
+
set_service_user_agent(ACTIVE_USER_AGENT)
|
|
4104
|
+
|
|
4105
|
+
|
|
4106
|
+
# Los motores propios de Playwright arrancan con el User-Agent por defecto hasta que el
|
|
4107
|
+
# frontend fija el suyo en la primera petición.
|
|
4108
|
+
set_service_user_agent(DEFAULT_USER_AGENT)
|
|
4081
4109
|
WEB_FETCH_MAX_BYTES = 10 * 1024 * 1024
|
|
4082
4110
|
WEB_FETCH_MAX_REDIRECTS = 5
|
|
4083
4111
|
WEB_FETCH_READ_CHUNK = 64 * 1024
|
|
4084
4112
|
TEXT_CONTENT_TYPE_RE = re.compile(
|
|
4085
4113
|
r"^(text/|application/(json|xml|xhtml\+xml|javascript|x-javascript|ecmascript)$|[a-z]+/[^;]*\+(json|xml)$)"
|
|
4086
4114
|
)
|
|
4087
|
-
# Control de cadencia
|
|
4115
|
+
# Control de cadencia frente a retos anti-bot: mínimo WEB_FETCH_MIN_GAP_S desde la RESPUESTA
|
|
4116
|
+
# anterior al mismo host; las peticiones en paralelo se escalonan de WEB_FETCH_PARALLEL_STAGGER_S
|
|
4117
|
+
# en WEB_FETCH_PARALLEL_STAGGER_S para que no coincidan. El timestamp se inicializa al arrancar
|
|
4118
|
+
# para que la primera petición espere aunque no se haya hecho ninguna antes.
|
|
4088
4119
|
_WEB_FETCH_LOCK = threading.Lock()
|
|
4089
|
-
_LAST_WEB_FETCH_AT: dict[str, float] = {}
|
|
4120
|
+
_LAST_WEB_FETCH_AT: dict[str, float] = {"duckduckgo.com": time.monotonic()}
|
|
4121
|
+
_WEB_FETCH_PENDING: dict[str, int] = {}
|
|
4090
4122
|
WEB_FETCH_HOST_PACING_DOMAINS = ("duckduckgo.com",)
|
|
4091
|
-
|
|
4092
|
-
|
|
4123
|
+
WEB_FETCH_MIN_GAP_S = 2.0
|
|
4124
|
+
WEB_FETCH_PARALLEL_STAGGER_S = 5.0
|
|
4093
4125
|
|
|
4094
4126
|
|
|
4095
4127
|
|
|
@@ -4174,27 +4206,44 @@ def fetch_web_resource(params: dict) -> dict:
|
|
|
4174
4206
|
parsed = urllib.parse.urlsplit(url)
|
|
4175
4207
|
hostname = (parsed.hostname or "").lower()
|
|
4176
4208
|
|
|
4177
|
-
#
|
|
4178
|
-
|
|
4179
|
-
|
|
4180
|
-
|
|
4181
|
-
|
|
4182
|
-
|
|
4183
|
-
|
|
4184
|
-
|
|
4209
|
+
# Hosts sensibles (ej. DuckDuckGo): esperar al menos WEB_FETCH_MIN_GAP_S desde la respuesta
|
|
4210
|
+
# anterior y escalar las peticiones en paralelo de a WEB_FETCH_PARALLEL_STAGGER_S.
|
|
4211
|
+
pacing_domain = next(
|
|
4212
|
+
(domain for domain in WEB_FETCH_HOST_PACING_DOMAINS if hostname == domain or hostname.endswith("." + domain)),
|
|
4213
|
+
None,
|
|
4214
|
+
)
|
|
4215
|
+
if pacing_domain is None:
|
|
4216
|
+
return _fetch_web_resource_unpaced(url, params, hostname, parsed)
|
|
4217
|
+
|
|
4218
|
+
with _WEB_FETCH_LOCK:
|
|
4219
|
+
pending = _WEB_FETCH_PENDING.get(pacing_domain, 0)
|
|
4220
|
+
gap = time.monotonic() - _LAST_WEB_FETCH_AT.get(pacing_domain, 0.0)
|
|
4221
|
+
sleep_needed = max(0.0, WEB_FETCH_MIN_GAP_S - gap) + WEB_FETCH_PARALLEL_STAGGER_S * pending
|
|
4222
|
+
_WEB_FETCH_PENDING[pacing_domain] = pending + 1
|
|
4223
|
+
try:
|
|
4185
4224
|
if sleep_needed > 0:
|
|
4186
4225
|
time.sleep(sleep_needed)
|
|
4226
|
+
return _fetch_web_resource_unpaced(url, params, hostname, parsed)
|
|
4227
|
+
finally:
|
|
4228
|
+
# El timestamp marca la respuesta (o el intento) terminado: la próxima petición
|
|
4229
|
+
# cuenta los 2 s desde aquí.
|
|
4230
|
+
with _WEB_FETCH_LOCK:
|
|
4231
|
+
_LAST_WEB_FETCH_AT[pacing_domain] = time.monotonic()
|
|
4232
|
+
_WEB_FETCH_PENDING[pacing_domain] = max(0, _WEB_FETCH_PENDING[pacing_domain] - 1)
|
|
4233
|
+
|
|
4187
4234
|
|
|
4235
|
+
def _fetch_web_resource_unpaced(url: str, params: dict, hostname: str, parsed) -> dict:
|
|
4236
|
+
"""Cuerpo de fetch_web_resource sin cadencia: cabeceras, descarga y respuesta."""
|
|
4188
4237
|
timeout_s = max(0.5, min(float(params.get("timeout_s", 10.0)), 30.0))
|
|
4189
4238
|
max_bytes = max(1024, min(int(params.get("max_bytes", 2 * 1024 * 1024)), WEB_FETCH_MAX_BYTES))
|
|
4190
4239
|
as_binary = bool(params.get("binary", False))
|
|
4191
4240
|
accept = params.get("accept")
|
|
4192
4241
|
req_headers = {
|
|
4193
|
-
"User-Agent":
|
|
4242
|
+
"User-Agent": ACTIVE_USER_AGENT,
|
|
4194
4243
|
"Accept": accept if isinstance(accept, str) and accept else "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
|
4195
4244
|
"Accept-Language": "es-ES,es;q=0.9,en;q=0.8",
|
|
4196
4245
|
"Accept-Encoding": "gzip, deflate",
|
|
4197
|
-
"Sec-Ch-Ua": '"Not_A Brand";v="8", "Chromium";v="
|
|
4246
|
+
"Sec-Ch-Ua": '"Not_A Brand";v="8", "Chromium";v="131", "Google Chrome";v="131"',
|
|
4198
4247
|
"Sec-Ch-Ua-Mobile": "?0",
|
|
4199
4248
|
"Sec-Ch-Ua-Platform": '"Windows"',
|
|
4200
4249
|
"Sec-Fetch-Dest": "document",
|
|
@@ -4890,6 +4939,7 @@ class ZeroChatServerHandler(BaseHTTPRequestHandler):
|
|
|
4890
4939
|
self._log_res(403, safe_path, (time.monotonic() - t0) * 1000, f"Origen no permitido: '{origin}'")
|
|
4891
4940
|
return
|
|
4892
4941
|
|
|
4942
|
+
capture_frontend_user_agent(self.headers.get("User-Agent", ""))
|
|
4893
4943
|
mark_browser_active()
|
|
4894
4944
|
if not self.verify_token():
|
|
4895
4945
|
# Sin token válido solo cabe identificarse para obtenerlo.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|