forked from kenzuya/unshackle
feat(remote)!: make remote serve WebSocket-only and refuse older clients with CLIENT_OUTDATED
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user