diff --git a/app/chain/download/failure.py b/app/chain/download/failure.py index 7c17216b5..aadbd4dce 100644 --- a/app/chain/download/failure.py +++ b/app/chain/download/failure.py @@ -146,12 +146,17 @@ class DownloadFailureOwner(_DownloadOwnerBase): @staticmethod def _download_failure_ttl(error_msg: Optional[str]) -> int: """ - 限流按站点窗口冷却;泛化的空内容不能证明资源失效,使用瞬时冷却。 + 限流按站点窗口冷却;换票、临时地址和泛化空内容失败使用瞬时冷却。 + + :param error_msg: 准备或下载器提交阶段保留的具体失败原因 + :return: 冷却秒数;换票诊断中的 404 等关键字不能升级为资源失效 """ rate_limit_ttl = torrent_rules.rate_limit_cooldown(error_msg) if rate_limit_ttl is not None: return rate_limit_ttl error_text = str(error_msg or "").lower() + if error_text.startswith(("换票失败:", "临时下载地址失效:")): + return DOWNLOAD_FAILURE_TRANSIENT_TTL_SECONDS if any(keyword in error_text for keyword in DOWNLOAD_FAILURE_RESOURCE_ERROR_KEYWORDS): return DOWNLOAD_FAILURE_RESOURCE_TTL_SECONDS return DOWNLOAD_FAILURE_TRANSIENT_TTL_SECONDS diff --git a/app/chain/download/submission.py b/app/chain/download/submission.py index 3514d2100..2d35e8c91 100644 --- a/app/chain/download/submission.py +++ b/app/chain/download/submission.py @@ -40,6 +40,7 @@ from app.domain.context import ( TorrentInfo, ) from app.domain.meta.metabase import MetaBase +from app.foundation.url import UrlUtils, url_origin_for_log from app.runtime.cache import FileCache from app.runtime.events import eventmanager from app.runtime.log import logger @@ -142,7 +143,7 @@ class _DownloadResourceOwner(_DownloadOwnerBase): ua: Optional[str] = None, cookie: Optional[str] = None, site_id: Optional[int] = None, - ) -> Optional[str]: + ) -> str: """ 解析索引器编码的换票请求,并返回可信来源的临时下载地址。 @@ -150,18 +151,25 @@ class _DownloadResourceOwner(_DownloadOwnerBase): :param ua: 本次换票使用的用户代理 :param cookie: 换票协议允许时附带的站点 Cookie :param site_id: 原始资源的站点 ID,用于共享流控额度 - :return: 临时下载地址;换票失败时返回 None + :return: 临时下载地址;失败原因通过异常交回种子准备流程 :raises RateLimitExceededException: 站点限流,保留原始提示供通知和冷却记录使用 - :raises ValueError: 适配器拒绝换票响应或下载地址无效 + :raises ValueError: 配方、换票响应或下载地址无效,错误使用“换票失败”分类 """ match = re.search(r"\[(.*)](.*)", url) if not match: - return None + raise ValueError("换票失败:请求编码无效") encoded_params, request_url = match.groups() if not encoded_params: return request_url - request_text = base64.b64decode(encoded_params.encode("utf-8")).decode("utf-8") - request_params = cast(Dict[str, Any], json.loads(request_text)) + try: + request_text = base64.b64decode(encoded_params.encode("utf-8")).decode("utf-8") + request_params = json.loads(request_text) + except (ValueError, UnicodeError) as err: + # 编码内容包含认证信息,不能把原始配方放进错误消息。 + raise ValueError("换票失败:无法解析请求编码") from err + if not isinstance(request_params, dict): + raise ValueError("换票失败:请求参数格式无效") + original_cookie = cookie if not request_params.get("cookie"): cookie = None headers_value = request_params.get("header") @@ -206,21 +214,22 @@ class _DownloadResourceOwner(_DownloadOwnerBase): **post_options, ) try: - return self._indirect_download_response(response, request_params, site) + return self._indirect_download_response(response, request_params, site, original_cookie) finally: if response is not None: _close_download_response(response) def _indirect_download_response(self, response: Optional[DownloadResponsePort], request_params: Dict[str, Any], - site: dict[str, object]) -> Optional[str]: + site: dict[str, object], cookie: Optional[str] = None) -> str: """ 在响应关闭前统一交给 Module 解释新旧配方;限流由共享冷却负责。 :param response: 尚未关闭的原始换票响应,None 表示连接失败 :param request_params: 索引器编码的换票协议及结果字段 :param site: 原始站点的不含凭据流控配置 - :return: 适配器校验的下载地址;未声明专用解析器的配方沿用空结果语义 - :raises ValueError: 适配器拒绝该响应或响应解析能力不可用 + :param cookie: 换票协议筛选前的 Cookie,仅用于错误脱敏和是否发送的诊断 + :return: 适配器解释并通过格式校验的下载地址 + :raises ValueError: 适配器拒绝响应、能力不可用或没有有效地址,保留脱敏后的响应摘要 :raises RateLimitExceededException: 任一协议返回明确限流 """ try: @@ -239,11 +248,77 @@ class _DownloadResourceOwner(_DownloadOwnerBase): if torrent_rules.rate_limit_cooldown(str(err)) is not None: defer_site_request(site, str(err)) raise RateLimitExceededException(str(err)) from err - raise + raise ValueError(self._indirect_download_failure_reason( + response, payload, request_params, cookie, str(err), + )) from err if error := torrent_rate_limit_error(response): defer_site_request(site, error) raise RateLimitExceededException(error) - return url if isinstance(url, str) else None + normalized_url = UrlUtils.normalize_http_url(url) + if isinstance(url, str) and url.startswith("magnet:"): + return url + if normalized_url is None: + raise ValueError(self._indirect_download_failure_reason( + response, payload, request_params, cookie, "接口未返回有效下载地址", + )) + return normalized_url + + @staticmethod + def _redact_indirect_download_error(text: str, request_params: Dict[str, Any], cookie: Optional[str]) -> str: + """ + 移除换票错误中的凭据回显及完整签名地址,再限制通知和日志长度。 + + :param text: 适配器原因或响应的有限错误字段,不接受整份业务载荷 + :param request_params: 当前配方,请求头中的认证值仅用于脱敏 + :param cookie: 调用方 Cookie,协议禁止发送时同样不能出现在诊断中 + :return: 不含已知凭据和 URL 路径、查询串的单行错误摘要 + """ + headers = request_params.get("header") + secrets = list(headers.values()) if isinstance(headers, dict) else [] + if cookie: + secrets.extend([cookie, *(part.partition("=")[2].strip() for part in cookie.split(";"))]) + for secret in secrets: + if isinstance(secret, str) and secret: + text = text.replace(secret, "[已隐藏]") + text = text.replace(secret.removeprefix("Bearer "), "[已隐藏]") + text = re.sub(r"https?://[^\s<>\"',;()]+", lambda match: url_origin_for_log(match.group()), text) + text = re.sub(r"(?i)\bBearer\s+[^\s,;]+", "Bearer [已隐藏]", text) + text = re.sub( + r"(?i)\b(token|passkey|api[-_]?key|cookie|password|secret|sign)\s*[:=]\s*[^\s,;]+", + r"\1=[已隐藏]", text, + ) + return " ".join(text.split())[:500] + + @classmethod + def _indirect_download_failure_reason( + cls, response: Optional[DownloadResponsePort], payload: object, + request_params: Dict[str, Any], cookie: Optional[str], reason: str, + ) -> str: + """ + 为旧配方的空结果及适配器拒绝补齐换票阶段和 HTTP 诊断,不解释专站业务码。 + + 只摘取错误字段,不回显 data、完整页面或下载票据;明确限流在调用方优先处理。 + + :param response: 尚未关闭的换票响应;None 表示连接失败或超时 + :param payload: 索引模块使用的 JSON 载荷,非 JSON 时为 None + :param request_params: 当前请求配方,用于凭据脱敏 + :param cookie: 协议筛选前的 Cookie,用于脱敏及发送状态判断 + :param reason: 适配器的拒绝原因或通用的地址缺失说明 + :return: 可传到日志、通知和瞬时冷却台账的失败说明 + """ + headers = request_params.get("header") + header_cookie = isinstance(headers, dict) and any(str(key).lower() == "cookie" and value + for key, value in headers.items()) + cookie_sent = bool((cookie and request_params.get("cookie")) or header_cookie) + details = ["未收到响应(连接失败或超时)"] if response is None else [f"HTTP {response.status_code}"] + if response is not None: + fields = [f"{key}={payload[key]}" for key in ("code", "message", "msg", "error") + if isinstance(payload, dict) and isinstance(payload.get(key), (str, int))] + summary = ";".join(fields) or ("空响应" if not response.content else "响应未包含有效下载地址") + details.append(f"响应摘要:{summary}") + details.append(f"携带 Cookie:{'是' if cookie_sent else '否'}") + error = cls._redact_indirect_download_error(f"{reason}({';'.join(details)})", request_params, cookie) + return f"换票失败:{error}" def download_torrent(self, torrent: TorrentInfo, channel: Optional[NotificationChannel] = None, @@ -258,12 +333,13 @@ class _DownloadResourceOwner(_DownloadOwnerBase): :param on_failure: 将本次具体失败原因交还准备流程,保持返回元组与并发调用隔离 :return: 种子内容,种子目录名,种子文件清单 """ - if not torrent.enclosure: + enclosure = torrent.enclosure + if not enclosure: return None, "", [] - if torrent.enclosure.startswith("magnet:"): - return torrent.enclosure, "", [] + if enclosure.startswith("magnet:"): + return enclosure, "", [] helper: TorrentHelper = cast(Any, TorrentHelper)() - if helper.is_invalid(torrent.enclosure): + if helper.is_invalid(enclosure): if on_failure: on_failure("种子已加入下载失败黑名单(24小时)") logger.info(f"{torrent.title} 命中下载失败黑名单,跳过下载") @@ -271,7 +347,8 @@ class _DownloadResourceOwner(_DownloadOwnerBase): torrent = self._with_site_credentials(torrent) result = self._get_torrent_content(torrent) if result is None: - return None, "", [] + reason = "换票失败:未获取到下载地址" if enclosure.startswith("[") else "无法获取下载地址" + result = None, "", [], reason content, download_folder, files, error_msg = result if isinstance(content, str): @@ -280,9 +357,9 @@ class _DownloadResourceOwner(_DownloadOwnerBase): if not content: if on_failure: on_failure(error_msg or "下载种子内容为空") - if error_msg and "种子数据有误" in error_msg: - helper.add_invalid(torrent.enclosure or "") - logger.error(f"下载种子文件失败:{torrent.title}") + if not enclosure.startswith("[") and error_msg and "种子数据有误" in error_msg: + helper.add_invalid(enclosure) + logger.error(f"下载种子文件失败:{torrent.title},{error_msg}") self.post_message(Message( channel=channel, source=source if channel else None, @@ -310,6 +387,9 @@ class _DownloadResourceOwner(_DownloadOwnerBase): ]]: """ 先读取已验证的换票种子内容;缓存损坏时重新换票,失败与磁力链不缓存。 + + :param torrent: 含原始资源地址和站点认证上下文的候选 + :return: 内容、目录、文件列表及失败原因;换票失败不作为资源损坏处理 """ if not torrent.enclosure: return None @@ -333,18 +413,25 @@ class _DownloadResourceOwner(_DownloadOwnerBase): cookie=site_cookie, site_id=torrent.site, ) - except (RateLimitExceededException, ValueError) as err: + except RateLimitExceededException as err: return None, "", [], str(err) + except ValueError as err: + reason = str(err) + return None, "", [], reason if reason.startswith("换票失败:") else f"换票失败:{reason}" # 涉及解析地址的不使用Cookie下载种子,否则MT会出错 site_cookie = None else: torrent_url = torrent.enclosure if not torrent_url: - logger.error(f"{torrent.title} 无法获取下载地址!") - return None + reason = "换票失败:未获取到下载地址" if indirect_download else "无法获取下载地址" + return None, "", [], reason _, content, download_folder, files, error_msg = self._download_torrent_content( torrent, torrent_url, site_cookie, indirect_download ) + if indirect_download and not content and torrent_rules.rate_limit_cooldown(error_msg) is None: + # 临时票据的错误页或失效不证明原种子损坏,下一次调用仍应重新换票。 + reason = "返回内容不是有效种子,请重新换票" if "种子数据有误" in (error_msg or "") else (error_msg or "") + error_msg = f"临时下载地址失效:{reason or '未下载到种子数据'}" if cache is not None and cache_key is not None and isinstance(content, bytes) and files: cache.set(cache_key, content, region="torrents") diff --git a/docs/architecture-overview.md b/docs/architecture-overview.md index 8514b2a14..21db3c50d 100644 --- a/docs/architecture-overview.md +++ b/docs/architecture-overview.md @@ -837,7 +837,7 @@ flowchart LR | 指标 | 当前值 | |---|---:| | Python 模块 | 1164 | -| 内部导入边 | 10,115 | +| 内部导入边 | 10,117 | | 非平凡 SCC | 1(29 个模块的 TMDB vendor 精确 containment) | | Application / Chain 具体 Adapter 直连 | 0 / 0 | | Direct egress | 51(精确 policy 准入,Application / Chain 均为 0) | diff --git a/docs/testing.md b/docs/testing.md index ebde70598..53b3dd8d0 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -269,9 +269,16 @@ HTML `Request frequency limit, please try in N seconds` 按 N 秒冷却整个站 此策略不追溯修改已经写入的失败记录。 种子内容耗尽重试仍无效或首次下载确认仍返回无效内容时,加入现有 24 小时下载失败黑名单; 下载链再次遇到该地址直接跳过请求和通知。黑名单保存原始资源地址,不使用重定向目标; -短期签名或换票资源在下载链中屏蔽原始 enclosure,不将临时请求地址写入无效缓存。 +短期签名资源在下载链中屏蔽原始 enclosure,不将临时请求地址写入无效缓存。 订阅持久化保留具体失败原因,无效种子使用 24 小时资源冷却,避免重启后每小时重复通知。 网络异常、空响应、403/5xx 和限流仍沿用瞬时重试与流控规则,不因本次修改加入内容无效黑名单。 +换票失败与换票所得临时地址的无效响应不证明原种子损坏,均不写入无效地址缓存。 +`tests/test_indirect_download_failure_reason.py` 覆盖旧配方无响应、HTTP 错误、结果缺失、 +成功标志失败及临时地址的非种子内容:日志、通知与准备/完整包/拆包冷却保留 +“换票失败”或“临时下载地址失效”分类,并使用现有 1 小时瞬时冷却。 +换票诊断包含 HTTP 状态、有限错误字段摘要及是否携带 Cookie,不输出凭据或完整签名 URL。 +再次调用可重新换票,但不增加自动换票重试次数;明确限流仍优先沿用站点窗口, +直接链接真实损坏种子的 24 小时黑名单与已经写入的旧冷却记录保持不变。 下载器报告同一任务已存在且整包全部媒体文件明细已记录时,按资源指纹登记 7 天提交退避, 使订阅不再重投同一个已持有的整包;退避只压制该资源,候选仍继续尝试同媒体的其他种子, 订阅不会被判定完成或失败,且仅订阅来源写台账(手动补投不留冷却记录)。冷却跳过日志转述台账 diff --git a/tests/fixtures/architecture/dependency-baseline.json b/tests/fixtures/architecture/dependency-baseline.json index 9e2b9316e..efc454391 100644 --- a/tests/fixtures/architecture/dependency-baseline.json +++ b/tests/fixtures/architecture/dependency-baseline.json @@ -962,8 +962,8 @@ "runtime_only": true } }, - "edge_count": 10115, - "edge_sha256": "6530f15d7ea0a0beac162389c68c0264aac83c47589e2c3af0446ce3423c9d47", + "edge_count": 10117, + "edge_sha256": "220d35d404b1eb6a5b2900840a44af7d560a9d48e3250e0dbbee1e8714f79ebd", "edges": [ "app -> app.foundation", "app -> app.foundation.environment", @@ -4895,6 +4895,8 @@ "app.chain.download.submission -> app.domain.meta", "app.chain.download.submission -> app.domain.meta.metabase", "app.chain.download.submission -> app.domain.torrent", + "app.chain.download.submission -> app.foundation", + "app.chain.download.submission -> app.foundation.url", "app.chain.download.submission -> app.runtime", "app.chain.download.submission -> app.runtime.cache", "app.chain.download.submission -> app.runtime.events", diff --git a/tests/test_hdarea_download.py b/tests/test_hdarea_download.py index 0e0bda8ea..532437183 100644 --- a/tests/test_hdarea_download.py +++ b/tests/test_hdarea_download.py @@ -158,7 +158,7 @@ def test_missing_ticket_response_preserves_failure_reason(boundary): boundary.ticket.post.return_value = None failures = [] boundary.chain.download_torrent(boundary.torrent, on_failure=failures.append) - assert failures == ["无法连接 HDArea API"] + assert len(failures) == 1 and failures[0].startswith("换票失败:") and "无法连接 HDArea API" in failures[0] boundary.port.request.assert_not_called() diff --git a/tests/test_indirect_download_failure_reason.py b/tests/test_indirect_download_failure_reason.py new file mode 100644 index 000000000..e08bbbaa5 --- /dev/null +++ b/tests/test_indirect_download_failure_reason.py @@ -0,0 +1,296 @@ +""" +换票与临时下载地址失败的原因传播、凭据脱敏和冷却隔离回归。 +""" + +import base64 +import json +from datetime import datetime +from pathlib import Path +from types import SimpleNamespace +from typing import Any, Optional +from unittest.mock import Mock + +import pytest +from requests import Response + +import app.application.site.throttle as throttle +import app.application.torrent.download as torrent_download +import app.chain.download.submission as submission +from app.adapters.cache.backends import FileBackend +from app.chain.download import DownloadChain +from app.chain.download.batch import _BatchDownloadRun +from app.chain.download.failure import DownloadFailureOwner +from app.domain.context import Context, MediaInfo, TorrentInfo +from app.domain.metainfo import MetaInfo +from app.modules.indexer import IndexerModule +from app.runtime.rate import RequestRateLimiter +from app.schemas.types import MediaType + +# 合成的换票接口、短效下载地址和单文件种子,不使用真实站点或账号。 +TICKET_URL = "https://api.tracker.example/token" +DOWNLOAD_URL = "https://api.tracker.example/download?tid=39&t=1&sign=synthetic-ticket" +CONTENT = b"d4:infod4:name10:Series.mkv6:lengthi1eee" + + +def _response(payload: object, status: int = 200) -> Response: + """ + 构造真实 HTTP 响应,保留错误状态为假的布尔语义。 + + :param payload: JSON 数据或原始响应字节 + :param status: 回放的 HTTP 状态码 + :return: 带可核验关闭回执的离线响应 + """ + response = Response() + response.status_code = status + response._content = payload if isinstance(payload, bytes) else json.dumps(payload, ensure_ascii=False).encode() + response.encoding = "utf-8" + response.headers["Content-Type"] = "text/html" if isinstance(payload, bytes) else "application/json" + response.close = Mock() + return response + + +def _ticket(**options: Any) -> str: + """ + 生成未声明专站协议的旧换票配方,覆盖 RSS 缓存的兼容路径。 + + :param options: 本次覆盖的请求方法、结果字段及 Cookie 开关 + :return: 编码的原始资源下载地址 + """ + recipe = {"method": "post", "params": {"id": "39"}, "result": "data", "cookie": False, **options} + encoded = base64.b64encode(json.dumps(recipe).encode()).decode() + return f"[{encoded}]{TICKET_URL}" + + +@pytest.fixture +def boundary(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> SimpleNamespace: + """ + 保留真实配方解析、种子解析及冷却写入,仅隔离 HTTP、缓存和外部媒体补全。 + + :param monkeypatch: 用例结束时恢复模块和实例替身的夹具 + :param tmp_path: 本用例私有种子缓存目录 + :return: 下载链、原始候选、HTTP 回放端口和黑名单观察点 + """ + cache = FileBackend(tmp_path) + monkeypatch.setattr(submission, "FileCache", lambda **_kwargs: cache) + monkeypatch.setattr(torrent_download, "FileCache", lambda **_kwargs: cache) + monkeypatch.setattr(torrent_download, "TTLCache", Mock(return_value={})) + monkeypatch.setattr(torrent_download.time, "sleep", Mock()) + monkeypatch.setattr(throttle, "_site_requests", RequestRateLimiter()) + query = Mock(get_sync=Mock(return_value=None), get_by_domain_sync=Mock(return_value=None)) + monkeypatch.setattr(throttle, "get_configured_site_query_service", lambda: query) + helper = torrent_download.TorrentHelper() + helper.add_invalid = Mock(wraps=helper.add_invalid) + monkeypatch.setattr(submission, "TorrentHelper", lambda: helper) + ticket_response = _response({"code": "0", "data": DOWNLOAD_URL}) + ticket = Mock(post=Mock(return_value=ticket_response), get=Mock(return_value=ticket_response)) + monkeypatch.setattr(submission, "_download_ports_snapshot", lambda: (ticket, Mock())) + port = Mock(request=Mock(return_value=_response(CONTENT))) + monkeypatch.setattr(torrent_download, "_require_torrent_port", lambda: port) + chain = object.__new__(DownloadChain) + chain.post_message = Mock() + chain.download_failure_repository = Mock() + chain._apply_resource_download_event = Mock(return_value=(None, None)) + module = object.__new__(IndexerModule) + + def dispatch(method: str, **kwargs: Any) -> Any: + """ + 保留真实索引模块解释旧配方,不通过模块管理器访问外部站点。 + """ + kwargs.pop("raise_exception", None) + return getattr(module, method)(**kwargs) + + chain.run_module = Mock(side_effect=dispatch) + torrent = TorrentInfo(site=7372, site_name="测试站", title="Series S01E39", enclosure=_ticket(), + page_url="https://tracker.example/detail/39", site_cookie="session=synthetic-cookie") + context = Context(torrent_info=torrent, media_info=MediaInfo(title="Series", type=MediaType.TV), + meta_info=MetaInfo("Series S01E39")) + media = Mock(supplement_tmdb_info=Mock(return_value=context.media_info)) + monkeypatch.setattr(submission, "MediaChain", lambda: media) + log = Mock() + monkeypatch.setattr(submission, "logger", log) + return SimpleNamespace(chain=chain, torrent=torrent, context=context, ticket=ticket, + ticket_response=ticket_response, port=port, helper=helper, cache=cache, log=log) + + +@pytest.mark.parametrize("method", ["get", "post"]) +@pytest.mark.parametrize("status,payload,options,detail", [ + (None, None, {}, "未收到响应"), + (500, {"code": "1", "message": "upstream unavailable"}, {}, "upstream unavailable"), + (200, {"code": "1", "msg": "authentication failed"}, {}, "authentication failed"), + (200, {"data": ""}, {}, "下载地址"), + (200, {"data": DOWNLOAD_URL, "success": False, "message": "permission denied"}, + {"success": "success"}, "permission denied"), + (200, b"temporary upstream error", {}, "下载地址"), + (200, b"", {}, "下载地址"), + (200, ["unexpected JSON"], {}, "下载地址"), + (200, {"data": 42}, {}, "下载地址"), + (200, {"data": "not a download URL"}, {}, "下载地址"), + (200, b'{"message":"authentication failed"}', {"result": None}, "下载地址"), +]) +def test_legacy_ticket_failure_reports_reason_without_blacklisting( + boundary: SimpleNamespace, method: str, status: Optional[int], payload: object, + options: dict[str, Any], detail: str, +) -> None: + """ + 旧换票的无响应、错误 JSON 和 HTTP 失败均上报原因,不请求种子或污染黑名单。 + """ + response = None if status is None else _response(payload, status) + getattr(boundary.ticket, method).return_value = response + boundary.torrent.enclosure = _ticket(method=method, **options) + errors: list[str] = [] + + assert boundary.chain.download_torrent(boundary.torrent, on_failure=errors.append) == (None, "", []) + + assert len(errors) == 1 and errors[0].startswith("换票失败:") and detail in errors[0] + if status is not None: + assert f"HTTP {status}" in errors[0] + response.close.assert_called_once() + assert DownloadFailureOwner._download_failure_ttl(errors[0]) == 3600 + assert errors[0] in boundary.chain.post_message.call_args.args[0].text + assert errors[0] in str(boundary.log.error.call_args_list) + getattr(boundary.ticket, method).assert_called_once() + boundary.port.request.assert_not_called() + boundary.helper.add_invalid.assert_not_called() + + +@pytest.mark.parametrize("status,payload", [ + (404, b"expired ticket"), (401, b"unauthorized"), (403, b"forbidden"), (500, b"upstream error"), + (200, {"message": "torrent not found"}), (200, b"login required"), + (200, b"d4:info"), (200, b""), +]) +def test_temporary_download_failure_does_not_mark_resource_corrupt( + boundary: SimpleNamespace, status: int, payload: object, +) -> None: + """ + 临时地址的过期、鉴权、上游错误和无效内容使用短冷却,不冒充原资源损坏。 + """ + boundary.port.request.return_value = _response(payload, status) + errors: list[str] = [] + + assert boundary.chain.download_torrent(boundary.torrent, on_failure=errors.append) == (None, "", []) + + assert len(errors) == 1 and errors[0].startswith("临时下载地址失效:") + assert "种子数据有误" not in errors[0] + if status != 200: + assert str(status) in errors[0] + assert DownloadFailureOwner._download_failure_ttl(errors[0]) == 3600 + assert errors[0] in boundary.chain.post_message.call_args.args[0].text + boundary.ticket.post.assert_called_once() + boundary.port.request.assert_called_once() + boundary.helper.add_invalid.assert_not_called() + assert not boundary.helper.is_invalid(boundary.torrent.enclosure) + assert not boundary.helper.is_invalid(DOWNLOAD_URL) + + +def test_retry_fetches_new_ticket_after_temporary_content_failure(boundary: SimpleNamespace) -> None: + """ + 临时响应无效后再次调用必须重新换票,成功内容仍按原合同解析和缓存。 + """ + new_url = DOWNLOAD_URL.replace("synthetic-ticket", "new-ticket") + boundary.ticket.post.side_effect = [boundary.ticket_response, _response({"data": new_url})] + boundary.port.request.side_effect = [_response({"message": "expired"}), _response(CONTENT)] + + assert boundary.chain.download_torrent(boundary.torrent) == (None, "", []) + boundary.ticket.post.assert_called_once() + assert boundary.chain.download_torrent(boundary.torrent) == (CONTENT, "", ["Series.mkv"]) + assert [call.kwargs["url"] for call in boundary.port.request.call_args_list] == [DOWNLOAD_URL, new_url] + assert boundary.ticket.post.call_count == 2 + boundary.helper.add_invalid.assert_not_called() + boundary.chain.post_message.assert_called_once() + + +@pytest.mark.parametrize("partial", [None, False, True]) +def test_ticket_failure_reaches_preparation_and_batch_cooldown(boundary: SimpleNamespace, partial: Optional[bool]) -> None: + """ + 单资源准备、完整包和拆包读取都持久化真实换票原因,并只写一次一小时冷却。 + """ + boundary.ticket.post.return_value = _response({"message": "torrent not found"}, 404) + if partial is None: + prepared, error = boundary.chain._prepare_download_single( + context=boundary.context, torrent_file=None, torrent_content=None, episodes=None, + channel=None, source="Subscribe|7372", downloader=None, save_path=None, userid=None, username=None, + ) + assert prepared is None + else: + options = dict(save_path=None, channel=None, source="Subscribe|7372", userid=None, + username=None, downloader=None, custom_words=None, governance=None) + batch = _BatchDownloadRun(boundary.chain, [], {}, options, None) + assert batch._read_torrent(boundary.context, partial=partial) is None + error = boundary.chain.post_message.call_args.args[0].text.split("错误信息:", 1)[1].split("\n站点:", 1)[0] + + boundary.chain.download_failure_repository.record_failure.assert_called_once() + write = boundary.chain.download_failure_repository.record_failure.call_args.args[0] + assert write.error_message == error and error.startswith("换票失败:") + assert (datetime.fromisoformat(write.next_retry_at) - datetime.fromisoformat(write.failed_at)).total_seconds() == 3600 + assert write.source == "Subscribe|7372" + + +def test_missing_content_result_still_reports_failure(boundary: SimpleNamespace) -> None: + """ + 旧调用或扩展返回空准备结果时也触发一次失败回调和通知,不静默退出。 + """ + boundary.chain._get_torrent_content = Mock(return_value=None) + errors: list[str] = [] + assert boundary.chain.download_torrent(boundary.torrent, on_failure=errors.append) == (None, "", []) + assert len(errors) == 1 and errors[0].startswith("换票失败:") + boundary.chain.post_message.assert_called_once() + boundary.helper.add_invalid.assert_not_called() + + +@pytest.mark.parametrize("cookie_allowed", [False, True]) +def test_ticket_diagnostics_hide_credentials_and_report_cookie_usage(boundary: SimpleNamespace, cookie_allowed: bool) -> None: + """ + 日志只记录 Cookie 是否发送,错误摘要不泄露请求密钥、Cookie 或签名路径。 + """ + boundary.torrent.enclosure = _ticket(cookie=cookie_allowed, header={"x-api-key": "synthetic-api-key"}) + message = ("authentication failed synthetic-api-key session=synthetic-cookie " + "https://tracker.example/dl/private-path-ticket?sign=private-query-ticket") + boundary.ticket.post.return_value = _response({"code": "1", "message": message, "token": "private-body-token"}) + errors: list[str] = [] + boundary.chain.download_torrent(boundary.torrent, on_failure=errors.append) + diagnostic = errors[0] + str(boundary.log.error.call_args_list) + boundary.chain.post_message.call_args.args[0].text + assert "authentication failed" in diagnostic and "HTTP 200" in diagnostic + for secret in ("synthetic-api-key", "synthetic-cookie", "private-path-ticket", "private-query-ticket", "private-body-token"): + assert secret not in diagnostic + assert f"携带 Cookie:{'是' if cookie_allowed else '否'}" in diagnostic + assert boundary.ticket.post.call_args.kwargs["cookies"] == (boundary.torrent.site_cookie if cookie_allowed else None) + + +@pytest.mark.parametrize("stage", ["ticket", "content"]) +@pytest.mark.parametrize("message,ttl", [ + ("相同種子當天最多下載10次", 86400), + ("Request frequency limit, please try in 600 seconds.", 600), +]) +def test_rate_limit_keeps_existing_site_cooldown(boundary: SimpleNamespace, stage: str, message: str, ttl: int) -> None: + """ + 换票和临时 GET 的明确限流仍优先于瞬时分类,并阻止冷却期内再次请求。 + """ + response = _response({"message": message}) + if stage == "ticket": + boundary.ticket.post.return_value = response + else: + boundary.port.request.return_value = response + errors: list[str] = [] + for _ in range(2): + assert boundary.chain.download_torrent(boundary.torrent, on_failure=errors.append) == (None, "", []) + assert errors == [f"站点限流:{message}"] * 2 + assert DownloadFailureOwner._download_failure_ttl(errors[0]) == ttl + boundary.ticket.post.assert_called_once() + assert boundary.port.request.call_count == (1 if stage == "content" else 0) + boundary.helper.add_invalid.assert_not_called() + + +def test_direct_corrupt_torrent_keeps_resource_blacklist(boundary: SimpleNamespace) -> None: + """ + 直接链接耗尽重试仍无法解析时,保留原资源损坏原因和二十四小时黑名单。 + """ + url = "https://tracker.example/download.php?id=39" + boundary.torrent.enclosure = url + boundary.port.request.return_value = _response(b"d4:info") + errors: list[str] = [] + assert boundary.chain.download_torrent(boundary.torrent, on_failure=errors.append) == (None, "", []) + assert errors == ["种子数据有误,请确认链接是否正确"] + assert DownloadFailureOwner._download_failure_ttl(errors[0]) == 86400 + assert boundary.helper.is_invalid(url) + assert boundary.port.request.call_count == 3 + boundary.ticket.post.assert_not_called() diff --git a/tests/test_piggo_download.py b/tests/test_piggo_download.py index 79fd91a85..f65b96817 100644 --- a/tests/test_piggo_download.py +++ b/tests/test_piggo_download.py @@ -191,7 +191,7 @@ def test_missing_ticket_response_reports_failure(boundary): boundary.ticket.post.return_value = None errors = [] boundary.chain.download_torrent(boundary.torrent, on_failure=errors.append) - assert errors == ["无法连接猪猪实验室 API"] + assert len(errors) == 1 and errors[0].startswith("换票失败:") and "无法连接猪猪实验室 API" in errors[0] boundary.port.request.assert_not_called()