zerochat 10.3.0__tar.gz → 10.4.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.3.0/zerochat.egg-info → zerochat-10.4.0}/PKG-INFO +1 -1
- {zerochat-10.3.0 → zerochat-10.4.0}/pyproject.toml +1 -1
- {zerochat-10.3.0 → zerochat-10.4.0/zerochat.egg-info}/PKG-INFO +1 -1
- {zerochat-10.3.0 → zerochat-10.4.0}/zerochat.py +43 -2
- {zerochat-10.3.0 → zerochat-10.4.0}/LICENSE +0 -0
- {zerochat-10.3.0 → zerochat-10.4.0}/README.md +0 -0
- {zerochat-10.3.0 → zerochat-10.4.0}/setup.cfg +0 -0
- {zerochat-10.3.0 → zerochat-10.4.0}/zerochat.egg-info/SOURCES.txt +0 -0
- {zerochat-10.3.0 → zerochat-10.4.0}/zerochat.egg-info/dependency_links.txt +0 -0
- {zerochat-10.3.0 → zerochat-10.4.0}/zerochat.egg-info/entry_points.txt +0 -0
- {zerochat-10.3.0 → zerochat-10.4.0}/zerochat.egg-info/top_level.txt +0 -0
|
@@ -30,6 +30,7 @@ import json
|
|
|
30
30
|
import math
|
|
31
31
|
import os
|
|
32
32
|
import queue
|
|
33
|
+
import random
|
|
33
34
|
import re
|
|
34
35
|
import secrets
|
|
35
36
|
import shutil
|
|
@@ -53,7 +54,7 @@ import zlib
|
|
|
53
54
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
54
55
|
from pathlib import Path
|
|
55
56
|
|
|
56
|
-
SOURCE_BACKEND_VERSION = "10.
|
|
57
|
+
SOURCE_BACKEND_VERSION = "10.4.0"
|
|
57
58
|
|
|
58
59
|
def _read_source_version(filename: str) -> str | None:
|
|
59
60
|
"""Lee la versión de un archivo del repositorio cuando se ejecuta desde fuentes."""
|
|
@@ -4083,6 +4084,13 @@ WEB_FETCH_READ_CHUNK = 64 * 1024
|
|
|
4083
4084
|
TEXT_CONTENT_TYPE_RE = re.compile(
|
|
4084
4085
|
r"^(text/|application/(json|xml|xhtml\+xml|javascript|x-javascript|ecmascript)$|[a-z]+/[^;]*\+(json|xml)$)"
|
|
4085
4086
|
)
|
|
4087
|
+
# Control de cadencia entre peticiones consecutivas al mismo host (mitigación de retos anti-bot).
|
|
4088
|
+
_WEB_FETCH_LOCK = threading.Lock()
|
|
4089
|
+
_LAST_WEB_FETCH_AT: dict[str, float] = {}
|
|
4090
|
+
WEB_FETCH_HOST_PACING_DOMAINS = ("duckduckgo.com",)
|
|
4091
|
+
WEB_FETCH_MIN_DELAY_S = 0.6
|
|
4092
|
+
WEB_FETCH_MAX_DELAY_S = 1.2
|
|
4093
|
+
|
|
4086
4094
|
|
|
4087
4095
|
|
|
4088
4096
|
def _is_public_ip(address: str) -> bool:
|
|
@@ -4163,15 +4171,40 @@ def fetch_web_resource(params: dict) -> dict:
|
|
|
4163
4171
|
raise ValueError("URL no válida o ausente.")
|
|
4164
4172
|
url = _validate_web_url(url.strip())
|
|
4165
4173
|
|
|
4174
|
+
parsed = urllib.parse.urlsplit(url)
|
|
4175
|
+
hostname = (parsed.hostname or "").lower()
|
|
4176
|
+
|
|
4177
|
+
# Si se repite una consulta a un host sensible (ej. DuckDuckGo), aplicar un retardo mínimo aleatorio.
|
|
4178
|
+
if any(hostname == domain or hostname.endswith("." + domain) for domain in WEB_FETCH_HOST_PACING_DOMAINS):
|
|
4179
|
+
with _WEB_FETCH_LOCK:
|
|
4180
|
+
now = time.monotonic()
|
|
4181
|
+
last_call = _LAST_WEB_FETCH_AT.get(hostname, 0.0)
|
|
4182
|
+
required_gap = random.uniform(WEB_FETCH_MIN_DELAY_S, WEB_FETCH_MAX_DELAY_S)
|
|
4183
|
+
sleep_needed = max(0.0, (last_call + required_gap) - now)
|
|
4184
|
+
_LAST_WEB_FETCH_AT[hostname] = now + sleep_needed
|
|
4185
|
+
if sleep_needed > 0:
|
|
4186
|
+
time.sleep(sleep_needed)
|
|
4187
|
+
|
|
4166
4188
|
timeout_s = max(0.5, min(float(params.get("timeout_s", 10.0)), 30.0))
|
|
4167
4189
|
max_bytes = max(1024, min(int(params.get("max_bytes", 2 * 1024 * 1024)), WEB_FETCH_MAX_BYTES))
|
|
4168
4190
|
as_binary = bool(params.get("binary", False))
|
|
4169
4191
|
accept = params.get("accept")
|
|
4170
4192
|
req_headers = {
|
|
4171
4193
|
"User-Agent": DEFAULT_USER_AGENT,
|
|
4172
|
-
"Accept": accept if isinstance(accept, str) and accept else "
|
|
4194
|
+
"Accept": accept if isinstance(accept, str) and accept else "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
|
4195
|
+
"Accept-Language": "es-ES,es;q=0.9,en;q=0.8",
|
|
4173
4196
|
"Accept-Encoding": "gzip, deflate",
|
|
4197
|
+
"Sec-Ch-Ua": '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
|
|
4198
|
+
"Sec-Ch-Ua-Mobile": "?0",
|
|
4199
|
+
"Sec-Ch-Ua-Platform": '"Windows"',
|
|
4200
|
+
"Sec-Fetch-Dest": "document",
|
|
4201
|
+
"Sec-Fetch-Mode": "navigate",
|
|
4202
|
+
"Sec-Fetch-Site": "none",
|
|
4203
|
+
"Sec-Fetch-User": "?1",
|
|
4204
|
+
"Upgrade-Insecure-Requests": "1",
|
|
4174
4205
|
}
|
|
4206
|
+
if hostname.endswith("duckduckgo.com"):
|
|
4207
|
+
req_headers["Referer"] = f"{parsed.scheme}://{parsed.netloc}/"
|
|
4175
4208
|
|
|
4176
4209
|
req = urllib.request.Request(url, headers=req_headers)
|
|
4177
4210
|
try:
|
|
@@ -4180,6 +4213,14 @@ def fetch_web_resource(params: dict) -> dict:
|
|
|
4180
4213
|
raw_data, truncated = _read_limited_body(resp, resp.headers.get("Content-Encoding", ""), max_bytes)
|
|
4181
4214
|
# Las cabeceras de transporte ya no describen el cuerpo descomprimido.
|
|
4182
4215
|
resp_headers = {k: v for k, v in resp.getheaders() if k.lower() not in ("content-encoding", "content-length")}
|
|
4216
|
+
# Respuestas 202 u otros retos anti-bot se marcan como error no exitoso.
|
|
4217
|
+
if resp.status == 202:
|
|
4218
|
+
return {
|
|
4219
|
+
"ok": False,
|
|
4220
|
+
"status": resp.status,
|
|
4221
|
+
"error": "HTTP 202: Reto anti-bot o verificación de anomalía detectada por el servidor remoto.",
|
|
4222
|
+
"text": raw_data.decode("utf-8", errors="replace")
|
|
4223
|
+
}
|
|
4183
4224
|
result = {
|
|
4184
4225
|
"ok": True,
|
|
4185
4226
|
"status": resp.status,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|