ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
fix(connect): learn revocation from the relay's close-reason frame
Juan Denis60f8bd8c60cb
diff --git a/backend/app/services/connect_client.py b/backend/app/services/connect_client.pyindex 53bd45a7..0dde9505 100644--- a/backend/app/services/connect_client.py+++ b/backend/app/services/connect_client.py@@ -870,6 +870,8 @@ class RelayClient:return 'stopped'except Exception as exc:from websockets.exceptions import ConnectionClosed+ if isinstance(exc, RelayRevoked):+ return 'revoked'if isinstance(exc, ConnectionClosed):code = exc.rcvd.code if exc.rcvd else Noneif code == 4009:@@ -910,6 +912,13 @@ class RelayClient:get_panel_version())ws.send(json.dumps(hello))frame = json.loads(ws.recv(timeout=HELLO_TIMEOUT_S))+ if frame.get('t') == 'close':+ # Edge proxies strip the close code, so the relay refuses a+ # hello with the reason in a frame. Revocation is terminal.+ reason = str(frame.get('reason') or 'refused')+ if reason == 'revoked':+ raise RelayRevoked()+ raise _HandshakeRefused(reason)if frame.get('t') != 'ready':raise _HandshakeRefused('relay_unreachable')self.relay_instance = frame.get('instance')@@ -981,9 +990,16 @@ class RelayClient:except ValueError:returnif frame.get('t') == 'close':+ stream_id = frame.get('s')+ if stream_id is None:+ # Session-level close. Edge proxies strip the numeric close+ # code, so the relay sends the reason in a frame first —+ # without it a revoked panel would keep retrying forever.+ if frame.get('reason') == 'revoked':+ raise RelayRevoked()+ return# The relay closes an ingest stream with ServerKit Cloud's own answer, which# carries the interval Cloud wants us to send at.- stream_id = frame.get('s')if stream_id in self._metrics_inflight:payload = frame.get('p') or {}if payload.get('ok'):diff --git a/backend/tests/BASELINE_COUNT b/backend/tests/BASELINE_COUNTindex 008e44a8..8d3ce569 100644--- a/backend/tests/BASELINE_COUNT+++ b/backend/tests/BASELINE_COUNT@@ -1 +1 @@-5275+5278diff --git a/backend/tests/test_connect_relay.py b/backend/tests/test_connect_relay.pyindex 873741aa..d653c332 100644--- a/backend/tests/test_connect_relay.py+++ b/backend/tests/test_connect_relay.py@@ -366,3 +366,76 @@ def test_run_command_refreshes_a_stale_jwks_and_acks(config_dir, monkeypatch):ws = types.SimpleNamespace(send=lambda raw: sent.append(json.loads(raw)))client._run_command(ws, {'p': {'jwt': sign()}})assert sent and sent[0]['p']['state'] == 'running'+++# ---------- close-reason frames (the edge strips numeric close codes) ----------+++def test_a_session_close_frame_with_revoked_reason_raises():+ """Production edge proxies answer 1005 for the relay's 4009, so revocation+ arrives as a frame. Stream closes still route by stream id."""+ client = connect_client.RelayClient()+ with pytest.raises(connect_client.RelayRevoked):+ client._handle_frame(types.SimpleNamespace(),+ json.dumps({'t': 'close', 'reason': 'revoked'}))+ # Other session-level reasons and ordinary stream closes do not raise.+ client._handle_frame(types.SimpleNamespace(),+ json.dumps({'t': 'close', 'reason': 'version_unsupported'}))+ client._handle_frame(types.SimpleNamespace(),+ json.dumps({'s': 'met1', 't': 'close', 'p': {'ok': True}}))+++def test_a_close_frame_during_hello_is_the_refusal_reason(monkeypatch):+ from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey++ class FakeWs:+ def __init__(self, frame):+ self.frame = frame++ def send(self, raw):+ pass++ def recv(self, timeout=None):+ return json.dumps(self.frame)++ def close(self):+ pass++ cfg = {'private_key': Ed25519PrivateKey.generate(), 'device_id': 'dev_abc123',+ 'relay_url': 'wss://relay.test/v1/device'}+ import websockets.sync.client as ws_client+ monkeypatch.setattr(ws_client, 'connect', lambda *a, **k: FakeWs(+ {'t': 'close', 'reason': 'revoked'}))+ with pytest.raises(connect_client.RelayRevoked):+ connect_client.RelayClient()._ws_connect(cfg)+ monkeypatch.setattr(ws_client, 'connect', lambda *a, **k: FakeWs(+ {'t': 'close', 'reason': 'version_unsupported'}))+ with pytest.raises(connect_client._HandshakeRefused) as exc:+ connect_client.RelayClient()._ws_connect(cfg)+ assert 'version_unsupported' in str(exc.value)+++def test_a_revoked_frame_mid_session_ends_it_as_revoked(monkeypatch):+ calls = []++ class FakeWs:+ def recv(self, timeout=None):+ if not calls:+ calls.append(1)+ return json.dumps({'t': 'close', 'reason': 'revoked'})+ raise TimeoutError()++ def send(self, raw):+ pass++ def close(self):+ pass++ client = connect_client.RelayClient()+ client.running = True+ monkeypatch.setattr(client, '_ws_connect', lambda cfg: FakeWs())+ monkeypatch.setattr(client, '_set_state', lambda *args, **kwargs: None)+ for name in ('_check_for_update', '_load_jwks', '_publish_storage',+ '_publish_policy', '_publish_inventory'):+ monkeypatch.setattr(client, name, lambda *args: None)+ assert client._ws_session({}) == 'revoked'