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
+12 -45
View File
@@ -33,9 +33,10 @@ from tests.remote.unit.test_tunnel import wait_until
from unshackle.core import process_exit, remote_channel, remote_lifetime, remote_service
from unshackle.core.api import handlers, routes, session_store
from unshackle.core.api.download_manager import busy_services
from unshackle.core.api.errors import APIError
from unshackle.core.api.session_store import SessionEntry
from unshackle.core.config import config
from unshackle.core.remote_channel import PollingChannel, ServerMessages, WebSocketChannel
from unshackle.core.remote_channel import ServerMessages, WebSocketChannel
from unshackle.core.remote_lifetime import Presses, RemoteSessionLifetime
from unshackle.core.remote_protocol import SESSION_CREATE_ROUTE, SESSION_ROUTE, APIErrorCode, ClientErrorKind
from unshackle.core.remote_service import RemoteError, RemoteService
@@ -558,7 +559,9 @@ def test_an_id_that_is_not_a_canonical_uuid4_is_refused(server: SimpleNamespace,
def test_the_server_takes_the_id_that_the_client_names() -> None:
named = RemoteSessionLifetime(SimpleNamespace(), lambda cache: None).name() # type: ignore[arg-type]
assert handlers.new_session_id({"session_id": named}) == named
assert uuid.UUID(handlers.new_session_id({})).version == 4
with pytest.raises(APIError) as e:
handlers.new_session_id({})
assert e.value.error_code == APIErrorCode.CLIENT_OUTDATED
def test_a_create_that_times_out_is_deleted(build: SimpleNamespace) -> None:
@@ -577,13 +580,9 @@ def test_a_create_that_times_out_is_deleted(build: SimpleNamespace) -> None:
assert count(build.server, "DELETE", SESSION_ROUTE) == 1
@pytest.mark.parametrize("older", [False, True], ids=["server", "older-server"])
def test_a_create_that_answers_after_the_end_leaves_no_session(
build: SimpleNamespace, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, older: bool
build: SimpleNamespace, caplog: pytest.LogCaptureFixture
) -> None:
"""An older server picks the id itself, so the DELETE of the named id finds nothing and the reply is deleted."""
if older:
monkeypatch.setattr(handlers, "new_session_id", lambda data: str(uuid.uuid4()))
service = make_client(build.server.url)
opened = in_thread(service.authenticate)
assert build.started.wait(DEADLINE)
@@ -599,22 +598,6 @@ def test_a_create_that_answers_after_the_end_leaves_no_session(
assert not [r for r in caplog.records if r.levelname == "ERROR"]
def test_the_client_takes_the_id_that_an_older_server_picks(
server: SimpleNamespace, # noqa: F811
logins: list[Any],
monkeypatch: pytest.MonkeyPatch,
) -> None:
picked = str(uuid.uuid4())
monkeypatch.setattr(handlers, "new_session_id", lambda data: picked)
service = make_client(server.url)
service.authenticate()
assert service._session_id == picked
service.close()
assert sessions() == []
assert count(server, "DELETE", SESSION_ROUTE) == 1
def test_an_ended_owner_never_opens_again(server: SimpleNamespace, logins: list[Any]) -> None: # noqa: F811
service = make_client(server.url)
service.authenticate()
@@ -779,12 +762,10 @@ def test_a_search_interrupted_in_its_tunnel_stops_it(monkeypatch: pytest.MonkeyP
assert len(stopped) == 1
@pytest.mark.parametrize("channel", ["socket", "polling"])
def test_an_end_during_the_login_is_silent(
server: SimpleNamespace, # noqa: F811
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
channel: str,
) -> None:
asked, release = Event(), Event()
@@ -793,13 +774,11 @@ def test_an_end_during_the_login_is_silent(
release.wait(DEADLINE)
monkeypatch.setattr(StubService, "authenticate", login)
if channel == "polling":
monkeypatch.setattr(WebSocketChannel, "open", lambda self, timeout=0: False)
service = make_client(server.url)
opened = in_thread(service.authenticate)
try:
assert asked.wait(DEADLINE)
assert wait_until(lambda: isinstance(service.channel, WebSocketChannel | PollingChannel), DEADLINE)
assert wait_until(lambda: isinstance(service.channel, WebSocketChannel), DEADLINE)
service.close()
assert isinstance(opened(), RemoteError)
finally:
@@ -807,7 +786,6 @@ def test_an_end_during_the_login_is_silent(
assert sessions() == []
assert not [seen for seen in server.seen if "/None" in seen[1]]
assert not [r for r in caplog.records if r.levelname == "ERROR" and r.name.startswith("remote_")]
assert channel == "polling" or not service.client.no_channel
def test_an_end_during_the_channel_open_is_silent(
@@ -819,7 +797,7 @@ def test_an_end_during_the_channel_open_is_silent(
service = make_client(server.url)
connect = WebSocketChannel.open
def end_then_open(self: WebSocketChannel, timeout: float = 10) -> bool:
def end_then_open(self: WebSocketChannel, timeout: float = 10) -> None:
assert in_thread(service.close)() is None
self.stopped = False
return connect(self, timeout)
@@ -829,7 +807,6 @@ def test_an_end_during_the_channel_open_is_silent(
service.authenticate()
assert not [seen for seen in server.seen if "/None" in seen[1]]
assert not [r for r in caplog.records if r.levelname == "ERROR"]
assert not service.client.no_channel
def test_an_exit_in_the_channel_open_closes_the_channel(
@@ -1002,9 +979,8 @@ def test_an_end_of_another_thread_stops_the_opening(
elif at == "create":
monkeypatch.setattr(remote_service, "launch_keys", end)
else:
drain = service.drain_server_logs
monkeypatch.setattr(service, "drain_server_logs", lambda: (drain(), end()))
monkeypatch.setattr(WebSocketChannel, "keep_alive", lambda self: None)
opened = remote_channel.open_channel
monkeypatch.setattr(remote_channel, "open_channel", lambda service: (opened(service), end())[0])
with pytest.raises(RemoteError):
service.authenticate()
@@ -1034,7 +1010,7 @@ def test_a_stop_before_the_thread_runs_keeps_it_from_connecting(monkeypatch: pyt
else:
service = make_service()
service.ctx = SimpleNamespace(obj=None) # type: ignore[assignment]
channel = WebSocketChannel(ServerMessages(service), PollingChannel(ServerMessages(service)))
channel = WebSocketChannel(ServerMessages(service))
monkeypatch.setattr(WebSocketChannel, "connect", connect)
channel.close()
channel.thread.run()
@@ -1081,16 +1057,7 @@ def test_a_press_after_the_end_does_not_leave_its_handler(monkeypatch: pytest.Mo
def test_a_channel_of_an_ended_owner_stops_by_itself() -> None:
service = make_service()
service.ctx = SimpleNamespace(obj=None) # type: ignore[assignment]
channel = WebSocketChannel(ServerMessages(service), PollingChannel(ServerMessages(service)))
channel = WebSocketChannel(ServerMessages(service))
service.lifetime.ended = True
channel.thread.run()
assert channel.attempted.is_set() and not channel.connected.is_set()
def test_a_create_of_a_reopen_passes_through_the_old_channel() -> None:
"""The client's in_flight hook stays the one of the closed channel until the new channel is open."""
service = make_service()
channel = PollingChannel(ServerMessages(service))
service.lifetime.session_id = None
with channel.in_flight(SESSION_CREATE_ROUTE):
assert channel.cdm_requests == 0