feat(remote)!: make remote serve WebSocket-only and refuse older clients with CLIENT_OUTDATED

This commit is contained in:
imSp4rky
2026-12-05 09:47:22 -07:00
parent 641623590d
commit 00ad15527c
62 changed files with 657 additions and 2098 deletions
+35
View File
@@ -0,0 +1,35 @@
"""Open a remote session as the dl --remote client does: a create with its own session_id, then the channel."""
from __future__ import annotations
import asyncio
import threading
import uuid
from typing import Any
import aiohttp
import requests
from unshackle.core.remote_protocol import CHANNEL_ROUTE, SESSION_CREATE_ROUTE
def open_session(http_session: requests.Session, server_url: str, payload: dict[str, Any]) -> requests.Response:
"""POST create and hold the remote session channel open on a daemon thread; the create reply."""
session_id = str(uuid.uuid4())
body = {**payload, "session_id": session_id}
reply = http_session.post(server_url + SESSION_CREATE_ROUTE, json=body, timeout=120)
if reply.ok:
attached = threading.Event()
headers = {k: str(v) for k, v in http_session.headers.items() if k.startswith("X-")}
url = server_url + CHANNEL_ROUTE.format(session_id=session_id)
threading.Thread(target=asyncio.run, args=(hold_channel(url, headers, attached),), daemon=True).start()
assert attached.wait(30), f"the remote session channel of {session_id} did not open"
return reply
async def hold_channel(url: str, headers: dict[str, str], attached: threading.Event) -> None:
"""Read the channel until the server closes it, as the remote session ends."""
async with aiohttp.ClientSession(headers=headers) as http, http.ws_connect(url) as ws:
attached.set()
async for _ in ws:
pass
+7 -5
View File
@@ -17,6 +17,8 @@ import time
import pytest
from tests.remote.e2e.channel import open_session
pytestmark = [pytest.mark.live, pytest.mark.slow]
@@ -31,7 +33,7 @@ def wait_titles(http_session, server_url: str, sid: str, timeout: float = 120.0)
body = r.json()
except Exception:
body = {}
if (body.get("details") or {}).get("auth_status") in ("authenticating", "pending_input"):
if (body.get("details") or {}).get("auth_status") == "authenticating":
time.sleep(2.0)
continue
return None
@@ -60,9 +62,10 @@ def test_download_manifest_fetch(http_session, server_url: str, service_case) ->
if not title_input:
pytest.skip(f"{service}: no title/series in fixture")
r = http_session.post(
f"{server_url}/api/session/create",
json={
r = open_session(
http_session,
server_url,
{
"service": service,
"title_id": title_input,
"range_": ["SDR"],
@@ -70,7 +73,6 @@ def test_download_manifest_fetch(http_session, server_url: str, service_case) ->
"quality": [target_height],
"best_available": True,
},
timeout=120,
)
if r.status_code >= 400:
pytest.skip(f"{service}: session create failed {r.status_code}: {r.text[:200]}")
@@ -1,46 +0,0 @@
"""E2E: interactive auth prompt round-trip.
For services that don't naturally prompt during ``authenticate()``, the
test still verifies the GET prompt endpoint returns the documented
shape (empty when no prompt pending).
"""
from __future__ import annotations
import pytest
pytestmark = [pytest.mark.live, pytest.mark.slow]
def test_prompt_endpoint_shape_without_pending(http_session, server_url: str, service_case) -> None:
service, conf = service_case
r = http_session.post(
f"{server_url}/api/session/create",
json={"service": service, "title_id": conf["title_url"]},
timeout=120,
)
if r.status_code >= 400:
pytest.skip(f"{service}: session creation failed: {r.status_code}")
sid = r.json()["session_id"]
try:
pr = http_session.get(f"{server_url}/api/session/{sid}/prompt", timeout=10)
assert pr.status_code in (200, 404), pr.text
if pr.status_code == 200:
body = pr.json()
assert "prompt" in body or "status" in body
finally:
http_session.delete(f"{server_url}/api/session/{sid}", timeout=30)
def test_prompt_post_unknown_session_returns_404(http_session, server_url: str) -> None:
r = http_session.post(
f"{server_url}/api/session/bogus-session-id/prompt",
json={"response": "irrelevant"},
timeout=10,
)
assert r.status_code == 404
def test_prompt_get_unknown_session_returns_404(http_session, server_url: str) -> None:
r = http_session.get(f"{server_url}/api/session/bogus-session-id/prompt", timeout=10)
assert r.status_code == 404
+7 -5
View File
@@ -17,6 +17,8 @@ import time
import pytest
from tests.remote.e2e.channel import open_session
pytestmark = [pytest.mark.live, pytest.mark.slow]
@@ -31,7 +33,7 @@ def wait_titles(http_session, server_url: str, sid: str, timeout: float = 120.0)
body = r.json()
except Exception:
body = {}
if (body.get("details") or {}).get("auth_status") in ("authenticating", "pending_input"):
if (body.get("details") or {}).get("auth_status") == "authenticating":
time.sleep(2.0)
continue
return None
@@ -64,16 +66,16 @@ def test_license_server_cdm(http_session, server_url: str, service_case) -> None
if not title_input:
pytest.skip(f"{service}: no title/series in fixture")
r = http_session.post(
f"{server_url}/api/session/create",
json={
r = open_session(
http_session,
server_url,
{
"service": service,
"title_id": title_input,
"range_": ["SDR"],
"vcodec": ["AVC"],
"best_available": True,
},
timeout=120,
)
if r.status_code >= 400:
pytest.skip(f"{service}: session create failed {r.status_code}: {r.text[:200]}")
+4 -2
View File
@@ -15,6 +15,8 @@ from typing import Any
import pytest
from tests.remote.e2e.channel import open_session
pytestmark = [pytest.mark.live, pytest.mark.slow]
@@ -27,7 +29,7 @@ def create_session(http_session, server_url: str, service: str, title_id: str) -
"vcodec": ["AVC", "HEVC"],
"best_available": True,
}
r = http_session.post(f"{server_url}/api/session/create", json=payload, timeout=120)
r = open_session(http_session, server_url, payload)
if r.status_code >= 400:
pytest.skip(f"{service}: session create failed {r.status_code}: {r.text[:200]}")
return r.json()["session_id"]
@@ -44,7 +46,7 @@ def wait_for_titles(http_session, server_url: str, sid: str, timeout: float = 12
body = r.json()
except Exception:
body = {}
if (body.get("details") or {}).get("auth_status") in ("authenticating", "pending_input"):
if (body.get("details") or {}).get("auth_status") == "authenticating":
time.sleep(2.0)
continue
return r.status_code, r.json() if r.text else {}
@@ -6,12 +6,14 @@ import time
import pytest
from tests.remote.e2e.channel import open_session
pytestmark = [pytest.mark.live, pytest.mark.slow]
def create_session(http_session, server_url: str, service: str, conf: dict) -> str:
payload = {"service": service, "title_id": conf["title_url"]}
r = http_session.post(f"{server_url}/api/session/create", json=payload, timeout=120)
r = open_session(http_session, server_url, payload)
if r.status_code >= 400:
pytest.skip(f"Auth/setup not available for {service}: {r.status_code} {r.text[:200]}")
body = r.json()
@@ -37,7 +39,7 @@ def wait_for_titles(http_session, server_url: str, sid: str, timeout: float = 12
except Exception:
body = {}
auth_status = (body.get("details") or {}).get("auth_status")
if auth_status in ("authenticating", "pending_input"):
if auth_status == "authenticating":
time.sleep(2.0)
continue
return r.status_code, r.json() if r.text else {}