mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-12-01 16:36:28 +08:00
fix(mediaserver): 兼容含冒号媒体源外挂字幕代理与已配置媒体服自签证书回退
This commit is contained in:
@@ -15,6 +15,7 @@ from urllib.parse import quote, urljoin, urlsplit, urlunsplit
|
||||
from cryptography.fernet import Fernet, InvalidToken
|
||||
from fastapi import HTTPException, Request, status
|
||||
from fastapi.responses import StreamingResponse
|
||||
import requests
|
||||
|
||||
from app.adapters.network.http import RequestUtils
|
||||
from app.runtime.settings import get_runtime_setting
|
||||
@@ -804,6 +805,7 @@ def _open_upstream_stream(
|
||||
:raises HTTPException: 上游不可用、目标不安全或重定向次数超限时
|
||||
"""
|
||||
client = RequestUtils(timeout=30, verify=True)
|
||||
insecure_client: Optional[RequestUtils] = None
|
||||
current_url = url
|
||||
initial_origin = _stream_origin(url)
|
||||
try:
|
||||
@@ -814,15 +816,33 @@ def _open_upstream_stream(
|
||||
headers.update(extra_headers)
|
||||
if range_header:
|
||||
headers["Range"] = range_header
|
||||
context = client.get_stream(
|
||||
active_client = client
|
||||
context = active_client.get_stream(
|
||||
current_url,
|
||||
headers=headers,
|
||||
allow_redirects=False,
|
||||
timeout=(10, 60),
|
||||
timeout=(10, 90),
|
||||
raise_exception=True,
|
||||
)
|
||||
try:
|
||||
response = context.__enter__()
|
||||
except requests.exceptions.SSLError:
|
||||
if _stream_origin(current_url)[1] not in configured_hosts:
|
||||
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="STRM 上游连接失败") from None
|
||||
if insecure_client is None:
|
||||
insecure_client = RequestUtils(timeout=30, verify=False)
|
||||
active_client = insecure_client
|
||||
context = active_client.get_stream(
|
||||
current_url,
|
||||
headers=headers,
|
||||
allow_redirects=False,
|
||||
timeout=(10, 90),
|
||||
raise_exception=True,
|
||||
)
|
||||
try:
|
||||
response = context.__enter__()
|
||||
except Exception:
|
||||
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="STRM 上游连接失败") from None
|
||||
except Exception:
|
||||
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="STRM 上游连接失败") from None
|
||||
if response is None:
|
||||
@@ -835,10 +855,17 @@ def _open_upstream_stream(
|
||||
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="STRM 上游重定向无效")
|
||||
current_url = urljoin(current_url, location)
|
||||
continue
|
||||
if active_client is insecure_client:
|
||||
client.close()
|
||||
return response, context, insecure_client
|
||||
if insecure_client is not None:
|
||||
insecure_client.close()
|
||||
return response, context, client
|
||||
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="STRM 上游重定向次数过多")
|
||||
except Exception:
|
||||
client.close()
|
||||
if insecure_client is not None:
|
||||
insecure_client.close()
|
||||
raise
|
||||
|
||||
|
||||
|
||||
@@ -53,8 +53,11 @@ _SUBTITLE_EXT_MAP = {
|
||||
"smi": "smi",
|
||||
"sami": "smi",
|
||||
"sub": "sub",
|
||||
"pgssub": "sup",
|
||||
"pgs": "sup",
|
||||
"hdmv_pgs_subtitle": "sup",
|
||||
}
|
||||
_SUBTITLE_ID_RE = re.compile(r"^ms:([^:]+):(\d+)\.([a-z0-9]{2,6})$", re.IGNORECASE)
|
||||
_SUBTITLE_ID_RE = re.compile(r"^ms:(.+):(\d+)\.([a-z0-9]{2,6})$", re.IGNORECASE)
|
||||
_ITEM_TYPE_MAP = {
|
||||
"movie": "Movie",
|
||||
"电影": "Movie",
|
||||
|
||||
@@ -943,3 +943,68 @@ def test_smb_open_file_stream_branches(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
types.SimpleNamespace(stat=_boom, open_file=lambda _p, mode="rb": None),
|
||||
)
|
||||
assert storage.open_file_stream(FileItem(storage="smb", type="file", path="/movies/a.mp4", name="a.mp4")) is None
|
||||
|
||||
|
||||
def test_emby_subtitle_url_supports_colon_in_media_source_id() -> None:
|
||||
"""
|
||||
验证代理字幕标识中媒体源 ID 含有冒号时仍能准确解析上游字幕流地址。
|
||||
"""
|
||||
from app.modules._base.embybrowse import EmbyBrowseConfig, emby_subtitle_url
|
||||
|
||||
cfg = EmbyBrowseConfig(
|
||||
host="https://emby.example:8920/",
|
||||
api_key="emby-token",
|
||||
user_id="u1",
|
||||
server_type="emby",
|
||||
)
|
||||
url = emby_subtitle_url(cfg, "item-42", "ms:strm:source:99:3.ass")
|
||||
assert url == (
|
||||
"https://emby.example:8920/emby/Videos/item-42/"
|
||||
"strm%3Asource%3A99/Subtitles/3/Stream.ass?api_key=emby-token"
|
||||
)
|
||||
|
||||
|
||||
def test_open_upstream_stream_retries_self_signed_tls_only_for_configured_hosts(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
"""
|
||||
验证管理员配置的媒体服务器遇到自签名证书 SSLError 时允许单跳回退,外部未配置主机仍严格拒绝。
|
||||
"""
|
||||
import requests
|
||||
|
||||
ssl_fail_ctx = Mock()
|
||||
ssl_fail_ctx.__enter__ = Mock(side_effect=requests.exceptions.SSLError("self-signed cert"))
|
||||
ssl_fail_ctx.__exit__ = Mock(return_value=False)
|
||||
|
||||
ok_upstream = Mock(status_code=200, headers={"Content-Type": "text/x-ssa"})
|
||||
ok_ctx = nullcontext(ok_upstream)
|
||||
|
||||
strict_client = Mock(get_stream=Mock(return_value=ssl_fail_ctx))
|
||||
insecure_client = Mock(get_stream=Mock(return_value=ok_ctx))
|
||||
|
||||
def fake_request_utils(**kwargs):
|
||||
return strict_client if kwargs.get("verify", True) else insecure_client
|
||||
|
||||
monkeypatch.setattr(mediastream, "RequestUtils", fake_request_utils)
|
||||
monkeypatch.setattr(mediastream, "_validate_stream_url", lambda url, **_kwargs: url)
|
||||
|
||||
resp, _ctx, returned_client = mediastream._open_upstream_stream(
|
||||
"https://192.168.1.10:8920/emby/Subtitles/Stream.ass",
|
||||
None,
|
||||
{"192.168.1.10"},
|
||||
)
|
||||
assert resp is ok_upstream
|
||||
assert returned_client is insecure_client
|
||||
strict_client.close.assert_called_once()
|
||||
|
||||
strict_client.reset_mock()
|
||||
insecure_client.reset_mock()
|
||||
with pytest.raises(HTTPException) as external_err:
|
||||
mediastream._open_upstream_stream(
|
||||
"https://cdn.external.example/sub.ass",
|
||||
None,
|
||||
{"192.168.1.10"},
|
||||
)
|
||||
assert external_err.value.status_code == 502
|
||||
insecure_client.get_stream.assert_not_called()
|
||||
strict_client.close.assert_called_once()
|
||||
|
||||
Reference in New Issue
Block a user