Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(api): 媒体处理与缩略图档位只接受本机请求

longmeidaof84034cf6804
diff --git a/docs/OPERATIONS.md b/docs/OPERATIONS.md
index 9eb0903e2..ac8d375f6 100644
--- a/docs/OPERATIONS.md
+++ b/docs/OPERATIONS.md
@@ -467,7 +467,7 @@ untrunc 按相同顺序查找,对应目录为 `tools/untrunc`。独立程序
- 同时最多进行 2 个转码或探测,单次最长 1 小时,关掉播放页会取消对应的任务。
- 115 和 PikPak 的视频通过 CloudDrive2 挂载的 `B:`、`A:` 播放。能不能播,以在 Peach 里实际打开一个已知视频为准。
-Peach 配置「媒体库与入库 → 视频预览」中的「抽帧解码方式」与「应用与维护 → 视频播放」中的「转码方式」分别保存为运行 Peach 那台电脑的偏好,两者互不影响,均可选择「自动(硬件优先)」或软件。抽帧设置控制封面、九宫格与时间轴;扫描、读 NFO 和时长探测不依赖 GPU。播放设置只控制 Peach 服务端的整片与 HLS 转码,浏览器直接播放时自行选择解码方式。保存无需重启,对新任务生效;已生成的有效缓存继续使用,运行中的 FFmpeg 不被强行中断。macOS 抽帧在同一超时预算内尝试 VideoToolbox,Windows 抽帧对 H.264、HEVC、VP9、AV1 尝试 D3D11VA,失败后都回退软件;视频编码格式探测按文件大小和修改时间缓存。Windows 播放的自动档依次探测 NVENC、AMF、QSV 编码,三者都不可用时用 D3D11VA 解码再交给 libx264;AMF 只接受 AMD 显卡上的 D3D11 设备,Peach 按显卡序号逐个探测。偏好通过 `/api/media-processing` 的 GET/POST 读写,`preview_acceleration` 和 `playback_acceleration` 分别接受 `auto/software`;省略的字段保持现值。`PEACH_TRANSCODE_BACKEND` 可用于管理员诊断,支持 `auto/software/nvenc/amf/qsv/vaapi/videotoolbox`,显式 `software` 会保持禁用硬件。
+Peach 配置「媒体库与入库 → 视频预览」中的「抽帧解码方式」与「应用与维护 → 视频播放」中的「转码方式」分别保存为运行 Peach 那台电脑的偏好,两者互不影响,均可选择「自动(硬件优先)」或软件。抽帧设置控制封面、九宫格与时间轴;扫描、读 NFO 和时长探测不依赖 GPU。播放设置只控制 Peach 服务端的整片与 HLS 转码,浏览器直接播放时自行选择解码方式。保存无需重启,对新任务生效;已生成的有效缓存继续使用,运行中的 FFmpeg 不被强行中断。macOS 抽帧在同一超时预算内尝试 VideoToolbox,Windows 抽帧对 H.264、HEVC、VP9、AV1 尝试 D3D11VA,失败后都回退软件;视频编码格式探测按文件大小和修改时间缓存。Windows 播放的自动档依次探测 NVENC、AMF、QSV 编码,三者都不可用时用 D3D11VA 解码再交给 libx264;AMF 只接受 AMD 显卡上的 D3D11 设备,Peach 按显卡序号逐个探测。偏好通过 `/api/media-processing` 的 GET/POST 读写,`preview_acceleration` 和 `playback_acceleration` 分别接受 `auto/software`;省略的字段保持现值。这个接口与时间轴缩略图的 `/api/thumbnail-jobs` 和 `/api/configuration/*` 一样只接受运行 Peach 的电脑发来的请求,其他设备即使已登录或处于开放访问也返回 403。`PEACH_TRANSCODE_BACKEND` 可用于管理员诊断,支持 `auto/software/nvenc/amf/qsv/vaapi/videotoolbox`,显式 `software` 会保持禁用硬件。
卡片悬停预览请求的 `/stream` 不带流会话,取消不了,也看不到浏览器断开。片源需要整片转码且没有现成缓存时,这条请求直接回 415,不启动 FFmpeg;详情与沉浸模式带会话播放,关闭时按会话结束转码。
diff --git a/src/peach/routes_api.py b/src/peach/routes_api.py
index f4d3ab7eb..858758eed 100644
--- a/src/peach/routes_api.py
+++ b/src/peach/routes_api.py
@@ -34,6 +34,7 @@ from .update_tasks import UpdateBusy
from .platform import is_unmapped, translate_ledger_path
from .providers import ProviderUnavailable
from .routes_auth import require_auth
+from .routes_configuration import local_only
from .task_runs import task_label
from .taste_history import analyze_history, import_history_exports, write_manifest
@@ -310,8 +311,15 @@ def _conflict(error: TaskRunConflict) -> JSONResponse:
status_code=409)
+#: 走通用契约入口、却是运行 Peach 那台电脑的配置:生成预览与编解码方式、时间轴缩略图档位。
+#: 只有配置页读写它们,读写都按 `/api/configuration/*` 的本机判据放行,非本机一律 403。
+LOCAL_ONLY_ROUTES = frozenset({"/api/media-processing", "/api/thumbnail-jobs"})
+
+
@router.get("/api/{route:path}")
def api_get(request: Request, route: str, args: dict[str, str] = Depends(require_auth)):
+ if f"/api/{route}" in LOCAL_ONLY_ROUTES:
+ local_only(request)
state = request.app.state
sync = state.sync
try:
@@ -344,6 +352,8 @@ def api_post(
):
sync = request.app.state.sync
route_path = f"/api/{route}"
+ if route_path in LOCAL_ONLY_ROUTES:
+ local_only(request)
if (sync is not None and sync.read_only
and route_path not in web_contract.READ_ONLY_POST_ROUTES):
# 非写入端或冲突状态都只读;继续写只会产生无法自动合并的分叉。
diff --git a/tests/test_strm.py b/tests/test_strm.py
index 15d2c6f62..4443d8395 100644
--- a/tests/test_strm.py
+++ b/tests/test_strm.py
@@ -7,13 +7,14 @@ import unittest
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from dataclasses import replace
from pathlib import Path
-from contextlib import closing
+from contextlib import closing, contextmanager
from types import SimpleNamespace
from unittest.mock import patch
import httpx
+from fastapi.testclient import TestClient
-from peach import media_processing_policy as policy, settings_file, strm
+from peach import access, media_processing_policy as policy, settings_file, strm
from peach.api import create_app
from peach.config import PeachSettings
from peach.media_probe import probe_unmeasured
@@ -23,6 +24,16 @@ from support.ledger import fresh_ledger
from support.web import route_client
+@contextmanager
+def addressed_client(app, address):
+ """按客户端地址发请求。本机配置接口按 IP 判来源,默认客户端名 `testclient` 不是 IP,判为非本机。"""
+ client = TestClient(app, client=(address, 12345))
+ try:
+ yield client
+ finally:
+ client.close()
+
+
class StrmTextTests(unittest.TestCase):
def setUp(self):
temporary = tempfile.TemporaryDirectory()
@@ -271,7 +282,7 @@ class StrmPlaybackTests(unittest.TestCase):
self.assertEqual(self.requests, [])
def test_policy_endpoint_stops_timeline_without_touching_ledger(self):
- with route_client(self.app) as client:
+ with addressed_client(self.app, '127.0.0.1') as client:
response = client.post('/api/media-processing', json={'metadata_only': True})
self.assertEqual(response.status_code, 200)
self.assertTrue(client.get('/api/media-processing').json()['metadata_only'])
@@ -280,8 +291,30 @@ class StrmPlaybackTests(unittest.TestCase):
self.assertFalse(client.post('/api/media-processing', json={'metadata_only': False}).json()['metadata_only'])
self.assertEqual(json.loads((self.root / 'state' / 'timeline-thumbnails.json').read_text())['mode'], 'off')
+ def test_machine_media_settings_only_answer_the_machine_running_peach(self):
+ refused = '请在运行 Peach 的电脑上打开配置'
+ for path in ('/api/media-processing', '/api/thumbnail-jobs'):
+ with self.subTest(path=path), addressed_client(self.app, '192.0.2.50') as client:
+ for response in (client.get(path), client.post(path, json={'playback_acceleration': 'software', 'mode': 'off'})):
+ self.assertEqual((response.status_code, response.json()['error']), (403, refused))
+ self.assertEqual(policy.acceleration(self.root / 'state'), 'auto')
+ with addressed_client(self.app, '127.0.0.1') as client:
+ self.assertEqual(client.get('/api/thumbnail-jobs').status_code, 200)
+ self.assertEqual(client.post('/api/media-processing', json={'playback_acceleration': 'software'}).status_code, 200)
+ self.assertEqual(policy.acceleration(self.root / 'state'), 'software')
+
+ def test_open_access_still_keeps_machine_media_settings_local(self):
+ self.app.state.settings = replace(self.app.state.settings, token='password', access_path=self.root / 'access.json')
+ access.save(self.root / 'access.json', '')
+ with addressed_client(self.app, '192.0.2.50') as client:
+ # 开放访问下这台远程设备已经算登录了:同一个客户端读别的接口照常 200。
+ self.assertEqual(client.get('/api/settings').status_code, 200)
+ response = client.post('/api/media-processing', json={'playback_acceleration': 'software'})
+ self.assertEqual((response.status_code, response.json()['error']), (403, '请在运行 Peach 的电脑上打开配置'))
+ self.assertEqual(policy.acceleration(self.root / 'state'), 'auto')
+
def test_media_acceleration_is_a_live_device_preference_and_preserves_preview_policy(self):
- with route_client(self.app) as client:
+ with addressed_client(self.app, '127.0.0.1') as client:
self.assertEqual(client.get('/api/media-processing').json()['playback_acceleration'], 'auto')
self.assertEqual(client.post('/api/media-processing', json={'playback_acceleration': 'software'}).status_code, 200)
self.assertEqual(client.get('/api/media-processing').json()['playback_acceleration'], 'software')