mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-08-22 00:32:50 +08:00
revert(login): remove anonymous wallpaper proxy (#6201)
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
from datetime import timedelta
|
||||
from typing import Annotated, Any, List
|
||||
from urllib.parse import quote, urlparse, urlunparse
|
||||
from typing import Any, List, Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, Form, Header, HTTPException, Request, Response
|
||||
from fastapi import APIRouter, Depends, Form, HTTPException, Request, Response
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
@@ -12,19 +11,11 @@ from app.core import security
|
||||
from app.core.config import settings
|
||||
from app.db.systemconfig_oper import SystemConfigOper
|
||||
from app.helper.sites import SitesHelper # noqa
|
||||
from app.helper.image import ImageHelper, WallpaperHelper
|
||||
from app.helper.image import WallpaperHelper
|
||||
from app.schemas.types import SystemConfigKey
|
||||
from app.utils.crypto import HashUtils
|
||||
from app.utils.http import RequestUtils
|
||||
from app.utils.security import SecurityUtils
|
||||
from app.utils.url import UrlUtils
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
_LOGIN_WALLPAPER_MAX_BYTES = 32 * 1024 * 1024
|
||||
_LOGIN_WALLPAPER_PUBLIC_PURPOSE = "login-wallpaper-public"
|
||||
_LOGIN_WALLPAPER_MEDIA_PURPOSE = "login-wallpaper-media"
|
||||
|
||||
|
||||
@router.post("/access-token", summary="获取token", response_model=schemas.Token)
|
||||
def login_access_token(
|
||||
@@ -104,134 +95,8 @@ def wallpaper() -> Any:
|
||||
|
||||
|
||||
@router.get("/wallpapers", summary="登录页面电影海报列表", response_model=List[str])
|
||||
def wallpapers(same_origin: bool = False) -> Any:
|
||||
def wallpapers() -> Any:
|
||||
"""
|
||||
获取登录页面电影海报。
|
||||
|
||||
默认保持外链列表合同;同源模式只对绝对 HTTP(S) 地址做一对一签名转换,不改变
|
||||
来源数量、顺序、重复项或相对地址。
|
||||
获取登录页面电影海报
|
||||
"""
|
||||
wallpaper_urls = WallpaperHelper().get_wallpapers()
|
||||
if not same_origin:
|
||||
return wallpaper_urls
|
||||
|
||||
purpose = (
|
||||
_LOGIN_WALLPAPER_MEDIA_PURPOSE
|
||||
if settings.WALLPAPER == "mediaserver"
|
||||
else _LOGIN_WALLPAPER_PUBLIC_PURPOSE
|
||||
)
|
||||
return [_login_wallpaper_proxy_url(url, purpose) for url in wallpaper_urls]
|
||||
|
||||
|
||||
def _login_wallpaper_proxy_url(url: str, purpose: str) -> str:
|
||||
"""
|
||||
将可代理的绝对壁纸地址转换为登录页专用同源签名地址。
|
||||
|
||||
`//cdn.example/one.jpg` 这类网络路径引用会被浏览器解析成跨源地址,因此按
|
||||
当前页面协议补全后同样走代理;只有不带 netloc 的相对地址才原样返回。
|
||||
"""
|
||||
parsed = urlparse(url)
|
||||
if not parsed.netloc:
|
||||
return url
|
||||
if not parsed.scheme:
|
||||
url = urlunparse(parsed._replace(scheme="https"))
|
||||
elif parsed.scheme not in {"http", "https"}:
|
||||
return url
|
||||
signed_url = SecurityUtils.sign_url(url, purpose=purpose)
|
||||
return (
|
||||
f"{settings.API_V1_STR}/login/wallpapers/image"
|
||||
f"?url={quote(signed_url, safe='')}"
|
||||
)
|
||||
|
||||
|
||||
def _url_origin(url: str) -> tuple[str, str, int | None] | None:
|
||||
"""返回 HTTP(S) 地址的规范 origin,拒绝缺少主机或携带用户信息的地址。"""
|
||||
try:
|
||||
parsed = urlparse(url)
|
||||
port = parsed.port
|
||||
except ValueError:
|
||||
return None
|
||||
if (
|
||||
parsed.scheme not in {"http", "https"}
|
||||
or not parsed.hostname
|
||||
or parsed.username
|
||||
or parsed.password
|
||||
):
|
||||
return None
|
||||
if port is None:
|
||||
port = 443 if parsed.scheme == "https" else 80
|
||||
return parsed.scheme, parsed.hostname.lower(), port
|
||||
|
||||
|
||||
@router.get("/wallpapers/image", summary="登录页面同源壁纸")
|
||||
async def wallpaper_image(
|
||||
url: str,
|
||||
if_none_match: Annotated[str | None, Header()] = None,
|
||||
) -> Response:
|
||||
"""
|
||||
读取后端壁纸来源签发的图片;客户端无法修改目标后继续复用签名。
|
||||
|
||||
响应带内容 ETag,缓存过期后条件请求命中时只返回 304。
|
||||
"""
|
||||
source_url = SecurityUtils.verify_signed_url(
|
||||
url, purpose=_LOGIN_WALLPAPER_PUBLIC_PURPOSE
|
||||
)
|
||||
media_source = False
|
||||
if not source_url:
|
||||
source_url = SecurityUtils.verify_signed_url(
|
||||
url, purpose=_LOGIN_WALLPAPER_MEDIA_PURPOSE
|
||||
)
|
||||
media_source = bool(source_url)
|
||||
source_origin = _url_origin(source_url or "")
|
||||
if not source_url or not source_origin:
|
||||
raise HTTPException(status_code=404, detail="Wallpaper not found")
|
||||
|
||||
async def is_safe_public_target(target_url: str) -> bool:
|
||||
"""自定义公共来源可跨域跳转,但每个目标都必须通过 DNS/私网校验。"""
|
||||
target_origin = _url_origin(target_url)
|
||||
if not target_origin:
|
||||
return False
|
||||
return await SecurityUtils.is_safe_image_url_async(
|
||||
target_url,
|
||||
{target_origin[1]},
|
||||
allowed_private_ranges=settings.IMAGE_PROXY_ALLOWED_PRIVATE_RANGES,
|
||||
)
|
||||
|
||||
async def is_safe_redirect(target_url: str) -> bool:
|
||||
"""媒体服务器签名只授权原 origin;其它跳转按公共图片目标重新校验。"""
|
||||
target_origin = _url_origin(target_url)
|
||||
if media_source and target_origin == source_origin:
|
||||
return True
|
||||
return await is_safe_public_target(target_url)
|
||||
|
||||
if not media_source and not await is_safe_public_target(source_url):
|
||||
raise HTTPException(status_code=404, detail="Wallpaper not found")
|
||||
|
||||
# 媒体服务器来源额外继承原 origin 的私网授权,与公共来源的重定向授权范围不同,
|
||||
# 因此按来源类型声明策略标识,只让同策略的并发请求共享一次抓取。
|
||||
redirect_policy = (
|
||||
f"{_LOGIN_WALLPAPER_MEDIA_PURPOSE}:{source_origin[0]}://"
|
||||
f"{source_origin[1]}:{source_origin[2]}"
|
||||
if media_source
|
||||
else _LOGIN_WALLPAPER_PUBLIC_PURPOSE
|
||||
)
|
||||
content = await ImageHelper().async_fetch_image_guarded(
|
||||
url=source_url,
|
||||
redirect_validator=is_safe_redirect,
|
||||
redirect_policy=redirect_policy,
|
||||
max_bytes=_LOGIN_WALLPAPER_MAX_BYTES,
|
||||
use_cache=True,
|
||||
)
|
||||
if not content:
|
||||
raise HTTPException(status_code=502, detail="Wallpaper unavailable")
|
||||
|
||||
etag = f'"{HashUtils.md5(content)}"'
|
||||
headers = RequestUtils.generate_cache_headers(etag, max_age=86400)
|
||||
if RequestUtils.if_none_match_matches(if_none_match, etag):
|
||||
return Response(status_code=304, headers=headers)
|
||||
|
||||
return Response(
|
||||
content=content,
|
||||
media_type=UrlUtils.get_mime_type(source_url, "image/jpeg"),
|
||||
headers=headers,
|
||||
)
|
||||
return WallpaperHelper().get_wallpapers()
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import asyncio
|
||||
import io
|
||||
import threading
|
||||
from pathlib import Path
|
||||
from typing import Awaitable, Callable, Optional, List
|
||||
from urllib.parse import urljoin
|
||||
from typing import Optional, List
|
||||
|
||||
from PIL import Image
|
||||
|
||||
@@ -180,8 +177,6 @@ class ImageHelper(metaclass=Singleton):
|
||||
_ttl = settings.GLOBAL_IMAGE_CACHE_DAYS * 24 * 3600
|
||||
self.file_cache = FileCache(base=_base_path, ttl=_ttl)
|
||||
self.async_file_cache = AsyncFileCache(base=_base_path, ttl=_ttl)
|
||||
self._guarded_fetch_tasks: dict[tuple, asyncio.Task[Optional[bytes]]] = {}
|
||||
self._guarded_fetch_tasks_lock = threading.Lock()
|
||||
|
||||
@staticmethod
|
||||
def _prepare_cache_path(url: str) -> str:
|
||||
@@ -291,147 +286,3 @@ class ImageHelper(metaclass=Singleton):
|
||||
# 保存缓存
|
||||
await self.async_file_cache.set(cache_path, content, region="images")
|
||||
return content
|
||||
|
||||
async def async_fetch_image_guarded(
|
||||
self,
|
||||
url: str,
|
||||
*,
|
||||
redirect_validator: Callable[[str], Awaitable[bool]],
|
||||
redirect_policy: str,
|
||||
max_bytes: int,
|
||||
proxy: Optional[bool] = None,
|
||||
use_cache: bool = True,
|
||||
max_redirects: int = 3,
|
||||
) -> Optional[bytes]:
|
||||
"""
|
||||
以有界流式请求抓取需要逐跳校验的图片。
|
||||
|
||||
每个重定向目标必须重新通过调用方的安全校验,字节上限和图片有效性检查在
|
||||
写入共享缓存前生效。只有抓取策略完全等价的并发调用才共享一次远端抓取:
|
||||
合并键包含缓存键、`redirect_policy`、字节上限、代理与重定向上限,避免某
|
||||
次调用收到超出自身上限的图片,或沿用他人的重定向授权。
|
||||
|
||||
:param redirect_validator: 逐跳校验重定向目标的协程
|
||||
:param redirect_policy: 描述该校验授权范围的稳定标识;`redirect_validator`
|
||||
通常是每次请求新建的闭包,无法按对象身份判断等价,由调用方显式声明
|
||||
"""
|
||||
if not url or max_bytes <= 0:
|
||||
return None
|
||||
|
||||
cache_path = self._prepare_cache_path(url)
|
||||
if use_cache:
|
||||
content = await self.async_file_cache.get(cache_path, region="images")
|
||||
if content:
|
||||
if len(content) <= max_bytes and self._validate_image(content):
|
||||
return content
|
||||
await self.async_file_cache.delete(cache_path, region="images")
|
||||
|
||||
task_key = (
|
||||
cache_path,
|
||||
redirect_policy,
|
||||
max_bytes,
|
||||
proxy,
|
||||
use_cache,
|
||||
max_redirects,
|
||||
)
|
||||
loop = asyncio.get_running_loop()
|
||||
with self._guarded_fetch_tasks_lock:
|
||||
task = self._guarded_fetch_tasks.get(task_key)
|
||||
if task is None or task.get_loop() is not loop:
|
||||
task = loop.create_task(
|
||||
self._download_guarded_image(
|
||||
url=url,
|
||||
cache_path=cache_path,
|
||||
redirect_validator=redirect_validator,
|
||||
max_bytes=max_bytes,
|
||||
proxy=proxy,
|
||||
use_cache=use_cache,
|
||||
max_redirects=max_redirects,
|
||||
)
|
||||
)
|
||||
self._guarded_fetch_tasks[task_key] = task
|
||||
task.add_done_callback(
|
||||
lambda completed, key=task_key: self._forget_guarded_fetch_task(
|
||||
key, completed
|
||||
)
|
||||
)
|
||||
|
||||
return await asyncio.shield(task)
|
||||
|
||||
def _forget_guarded_fetch_task(
|
||||
self, task_key: tuple, task: asyncio.Task[Optional[bytes]]
|
||||
) -> None:
|
||||
"""抓取完成后只移除仍指向该任务的合并键,避免旧任务清除后继任务。"""
|
||||
with self._guarded_fetch_tasks_lock:
|
||||
if self._guarded_fetch_tasks.get(task_key) is task:
|
||||
self._guarded_fetch_tasks.pop(task_key, None)
|
||||
|
||||
async def _download_guarded_image(
|
||||
self,
|
||||
*,
|
||||
url: str,
|
||||
cache_path: str,
|
||||
redirect_validator: Callable[[str], Awaitable[bool]],
|
||||
max_bytes: int,
|
||||
proxy: Optional[bool],
|
||||
use_cache: bool,
|
||||
max_redirects: int,
|
||||
) -> Optional[bytes]:
|
||||
"""执行一次受保护图片抓取,并在任务内复查并写入共享缓存。"""
|
||||
if use_cache:
|
||||
content = await self.async_file_cache.get(cache_path, region="images")
|
||||
if content:
|
||||
if len(content) <= max_bytes and self._validate_image(content):
|
||||
return content
|
||||
await self.async_file_cache.delete(cache_path, region="images")
|
||||
|
||||
current_url = url
|
||||
redirects = 0
|
||||
|
||||
while True:
|
||||
params = self._get_request_params(current_url, proxy, cookies=None)
|
||||
request = AsyncRequestUtils(**params, follow_redirects=False)
|
||||
|
||||
async with request.get_stream(current_url) as response:
|
||||
if response is None:
|
||||
return None
|
||||
|
||||
if response.status_code in {301, 302, 303, 307, 308}:
|
||||
location = response.headers.get("location")
|
||||
if not location or redirects >= max_redirects:
|
||||
return None
|
||||
next_url = urljoin(current_url, location)
|
||||
if not await redirect_validator(next_url):
|
||||
return None
|
||||
current_url = next_url
|
||||
redirects += 1
|
||||
continue
|
||||
|
||||
if response.status_code != 200:
|
||||
logger.warning(
|
||||
"登录壁纸抓取失败,状态码: %s",
|
||||
response.status_code,
|
||||
)
|
||||
return None
|
||||
|
||||
content_length = response.headers.get("content-length")
|
||||
if content_length:
|
||||
try:
|
||||
if int(content_length) > max_bytes:
|
||||
return None
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
payload = bytearray()
|
||||
async for chunk in response.aiter_bytes():
|
||||
payload.extend(chunk)
|
||||
if len(payload) > max_bytes:
|
||||
return None
|
||||
break
|
||||
|
||||
content = bytes(payload)
|
||||
if not self._validate_image(content):
|
||||
return None
|
||||
if use_cache:
|
||||
await self.async_file_cache.set(cache_path, content, region="images")
|
||||
return content
|
||||
|
||||
@@ -720,39 +720,6 @@ class RequestUtils:
|
||||
|
||||
return cache_headers
|
||||
|
||||
@staticmethod
|
||||
def if_none_match_matches(if_none_match: Optional[str], etag: Optional[str]) -> bool:
|
||||
"""
|
||||
按 entity-tag 语义判断 If-None-Match 是否命中当前响应的 ETag
|
||||
|
||||
条件缓存的客户端会发送带引号的标签、弱标签、逗号分隔的标签列表或 `*`,
|
||||
直接做裸摘要字符串全等比较会让 304 永远无法命中。这里按 RFC 9110
|
||||
的弱比较规则解析请求头:`*` 命中任何已存在的 ETag,标签列表逐项比较,
|
||||
比较前统一剥离 `W/` 前缀和外层引号。
|
||||
|
||||
:param if_none_match: 请求的 If-None-Match 头部,可为 None
|
||||
:param etag: 本次响应生成的 ETag,可为裸摘要或带引号的标签
|
||||
:return: 命中(应返回 304)时为 True
|
||||
"""
|
||||
if not if_none_match or not etag:
|
||||
return False
|
||||
|
||||
def normalize(tag: str) -> str:
|
||||
tag = tag.strip()
|
||||
if tag[:2].upper() == "W/":
|
||||
tag = tag[2:].strip()
|
||||
if len(tag) >= 2 and tag.startswith('"') and tag.endswith('"'):
|
||||
tag = tag[1:-1]
|
||||
return tag
|
||||
|
||||
if if_none_match.strip() == "*":
|
||||
return True
|
||||
|
||||
current = normalize(etag)
|
||||
if not current:
|
||||
return False
|
||||
return any(normalize(candidate) == current for candidate in if_none_match.split(","))
|
||||
|
||||
@staticmethod
|
||||
def detect_encoding_from_html_response(
|
||||
response: Response,
|
||||
|
||||
Reference in New Issue
Block a user