forked from kenzuya/unshackle
refactor(remote): keep the session protocol in core/remote_protocol.py
This commit is contained in:
@@ -365,8 +365,9 @@ def perform_download(
|
||||
import click
|
||||
|
||||
from unshackle.commands.dl import dl
|
||||
from unshackle.core.api.errors import APIError, APIErrorCode
|
||||
from unshackle.core.api.errors import APIError
|
||||
from unshackle.core.config import config, load_yaml
|
||||
from unshackle.core.remote_protocol import APIErrorCode
|
||||
from unshackle.core.services import Services
|
||||
from unshackle.core.utils.click_types import ContextData
|
||||
from unshackle.core.utils.collections import merge_dict
|
||||
|
||||
@@ -10,47 +10,14 @@ from __future__ import annotations
|
||||
import secrets
|
||||
import traceback
|
||||
from datetime import datetime, timezone
|
||||
from enum import Enum
|
||||
from typing import Any
|
||||
|
||||
from aiohttp import web
|
||||
|
||||
from unshackle.core.remote_protocol import APIErrorCode
|
||||
from unshackle.core.utils.redact import redact_secrets
|
||||
|
||||
|
||||
class APIErrorCode(str, Enum):
|
||||
"""Standard API error codes for programmatic error handling."""
|
||||
|
||||
INVALID_INPUT = "INVALID_INPUT"
|
||||
INVALID_SERVICE = "INVALID_SERVICE"
|
||||
INVALID_PROXY = "INVALID_PROXY"
|
||||
INVALID_PARAMETERS = "INVALID_PARAMETERS"
|
||||
|
||||
AUTH_FAILED = "AUTH_FAILED"
|
||||
FORBIDDEN = "FORBIDDEN"
|
||||
SERVER_CDM_CAPPED = "SERVER_CDM_CAPPED"
|
||||
GEOFENCE = "GEOFENCE"
|
||||
|
||||
NOT_FOUND = "NOT_FOUND"
|
||||
NO_CONTENT = "NO_CONTENT"
|
||||
JOB_NOT_FOUND = "JOB_NOT_FOUND"
|
||||
SESSION_NOT_FOUND = "SESSION_NOT_FOUND"
|
||||
TRACK_NOT_FOUND = "TRACK_NOT_FOUND"
|
||||
|
||||
CONFLICT = "CONFLICT"
|
||||
|
||||
RATE_LIMITED = "RATE_LIMITED"
|
||||
|
||||
INTERNAL_ERROR = "INTERNAL_ERROR"
|
||||
SERVICE_ERROR = "SERVICE_ERROR"
|
||||
NETWORK_ERROR = "NETWORK_ERROR"
|
||||
DRM_ERROR = "DRM_ERROR"
|
||||
DOWNLOAD_ERROR = "DOWNLOAD_ERROR"
|
||||
SERVICE_UNAVAILABLE = "SERVICE_UNAVAILABLE"
|
||||
SERVER_DRAINING = "SERVER_DRAINING"
|
||||
WORKER_ERROR = "WORKER_ERROR"
|
||||
|
||||
|
||||
class APIError(Exception):
|
||||
"""
|
||||
Structured API error with error code, message, and details.
|
||||
|
||||
@@ -24,8 +24,8 @@ import click
|
||||
from aiohttp import web
|
||||
|
||||
from unshackle.core.api.compression import safe_inflate
|
||||
from unshackle.core.api.errors import APIError, APIErrorCode, categorize_exception, handle_api_exception
|
||||
from unshackle.core.api.input_bridge import AuthStatus, InputBridge, NoUserBridge
|
||||
from unshackle.core.api.errors import APIError, categorize_exception, handle_api_exception
|
||||
from unshackle.core.api.input_bridge import InputBridge, NoUserBridge
|
||||
from unshackle.core.api.progress import track_log_label
|
||||
from unshackle.core.api.sanitize import MAX_SESSION_CACHE_KEYS, safe_cache_key, safe_cache_payload, sanitize_log
|
||||
from unshackle.core.api.session_log import SessionLogBuffer, SessionLogMirror, capture_service_logs
|
||||
@@ -45,6 +45,26 @@ from unshackle.core.drm import BlowfishStripe, cbcs, is_null_key
|
||||
from unshackle.core.manifests.snapshot import capture
|
||||
from unshackle.core.providers.anilist import parse_anilist_ref
|
||||
from unshackle.core.proxies.resolve import initialize_proxy_providers, is_loopback, resolve_proxy
|
||||
from unshackle.core.remote_protocol import (
|
||||
HEALTH_ROUTE,
|
||||
APIErrorCode,
|
||||
AuthStatus,
|
||||
BadKeyReport,
|
||||
LicenseReply,
|
||||
LicenseRequest,
|
||||
LogsReply,
|
||||
PromptAnswer,
|
||||
PromptReply,
|
||||
SegmentFilterReply,
|
||||
SegmentFilterRequest,
|
||||
SessionClosed,
|
||||
SessionCreate,
|
||||
SessionCreated,
|
||||
SessionInfo,
|
||||
TitlesReply,
|
||||
TracksReply,
|
||||
TracksRequest,
|
||||
)
|
||||
from unshackle.core.services import Services
|
||||
from unshackle.core.session import RnetSession, utf8_text
|
||||
from unshackle.core.titles import Episode, Song
|
||||
@@ -1502,7 +1522,7 @@ async def api_key_authentication(request: web.Request, handler: Any) -> web.Stre
|
||||
# recovered byte-by-byte from response timing.
|
||||
if not any(hmac.compare_digest(secret_key, k) for k in request.app["config"]["users"]):
|
||||
return web.json_response({"status": 401, "message": "Secret Key is Invalid."}, status=401)
|
||||
if request.path == "/api/health":
|
||||
if request.path == HEALTH_ROUTE:
|
||||
return await handler(request)
|
||||
return rate_limit_response(secret_key) or add_rate_limit_headers(await handler(request), secret_key)
|
||||
|
||||
@@ -3296,36 +3316,8 @@ async def env_check_handler(request: Optional[web.Request] = None) -> web.Respon
|
||||
return handle_api_exception(e, context={"operation": "env_check"}, debug_mode=debug_mode)
|
||||
|
||||
|
||||
SESSION_TRANSPORT_KEYS = {
|
||||
"service",
|
||||
"title_id",
|
||||
"profile",
|
||||
"service_params",
|
||||
"season",
|
||||
"episode",
|
||||
"part",
|
||||
"wanted",
|
||||
"proxy",
|
||||
"no_proxy",
|
||||
"credentials",
|
||||
"cookies",
|
||||
"cache",
|
||||
"client_region",
|
||||
"proxy_region",
|
||||
"server_cdm",
|
||||
"cdm_type",
|
||||
"cdm_security_level",
|
||||
"cdm_relay",
|
||||
"cdm_system_id",
|
||||
"cdm_device_type",
|
||||
"service_config",
|
||||
"range_",
|
||||
"vcodec",
|
||||
"quality",
|
||||
"best_available",
|
||||
"generate_dv",
|
||||
"dl_params",
|
||||
}
|
||||
# "client" stays out: a flat "client" value with no service_params is a service option.
|
||||
SESSION_TRANSPORT_KEYS = (SessionCreate.__required_keys__ | SessionCreate.__optional_keys__) - {"client"}
|
||||
|
||||
|
||||
# A client's service config carries a device identity (an ESN, a few keys), never a bulk payload.
|
||||
@@ -3656,16 +3648,16 @@ async def session_create_handler(data: Dict[str, Any], request: Optional[web.Req
|
||||
auth_task.add_done_callback(_auth_tasks.discard)
|
||||
|
||||
return web.json_response(
|
||||
{
|
||||
"session_id": session.session_id,
|
||||
"service": normalized_service,
|
||||
"status": "authenticating",
|
||||
"server_account": server_account,
|
||||
"server_cdm": use_server_cdm,
|
||||
"server_cdm_max_height": max_height,
|
||||
"server_vault": session.server_vault,
|
||||
"server_device": session.server_device,
|
||||
}
|
||||
SessionCreated(
|
||||
session_id=session.session_id,
|
||||
service=normalized_service,
|
||||
status=AuthStatus.AUTHENTICATING,
|
||||
server_account=server_account,
|
||||
server_cdm=use_server_cdm,
|
||||
server_cdm_max_height=max_height,
|
||||
server_vault=session.server_vault,
|
||||
server_device=session.server_device,
|
||||
)
|
||||
)
|
||||
|
||||
except APIError:
|
||||
@@ -3708,12 +3700,7 @@ async def session_titles_handler(session_id: str, request: Optional[web.Request]
|
||||
serialized_titles.append(stamp_service_flags(serialize_title(t), service_instance))
|
||||
SessionStore.publish_update(session)
|
||||
|
||||
return web.json_response(
|
||||
{
|
||||
"session_id": session_id,
|
||||
"titles": serialized_titles,
|
||||
}
|
||||
)
|
||||
return web.json_response(TitlesReply(session_id=session_id, titles=serialized_titles))
|
||||
|
||||
except (Exception, SystemExit) as e:
|
||||
log.exception("Error getting titles")
|
||||
@@ -3798,7 +3785,7 @@ def scrub_track_data(data: Optional[Dict[str, Any]], session: Any) -> Optional[D
|
||||
|
||||
|
||||
async def session_tracks_handler(
|
||||
data: Dict[str, Any], session_id: str, request: Optional[web.Request] = None
|
||||
data: TracksRequest, session_id: str, request: Optional[web.Request] = None
|
||||
) -> web.Response:
|
||||
"""Get tracks and chapters for a specific title in the remote session.
|
||||
|
||||
@@ -3896,6 +3883,17 @@ async def session_tracks_handler(
|
||||
d["data"] = scrub_track_data(d.get("data"), session)
|
||||
return {**lists, "attachments": [a for a in map(serialize_attachment, tracks.attachments) if a]}
|
||||
|
||||
session_fields = TracksReply(
|
||||
session_headers=session_headers,
|
||||
session_cookies=cookie_values(session_cookies),
|
||||
session_cookie_jar=session_cookies,
|
||||
server_cdm=session.server_cdm,
|
||||
server_cdm_type=server_cdm_type,
|
||||
sample_decrypt_tracks=[
|
||||
str(t.id) for t in [*tracks.videos, *tracks.audio] if service_instance.decrypts_samples(t)
|
||||
],
|
||||
)
|
||||
|
||||
return web.json_response(
|
||||
{
|
||||
"title": stamp_service_flags(serialize_title(title), service_instance),
|
||||
@@ -3905,14 +3903,7 @@ async def session_tracks_handler(
|
||||
for ch in session.chapters_by_title.get(str(title_id), [])
|
||||
],
|
||||
**snapshot,
|
||||
"session_headers": session_headers,
|
||||
"session_cookies": cookie_values(session_cookies),
|
||||
"session_cookie_jar": session_cookies,
|
||||
"server_cdm": session.server_cdm,
|
||||
"server_cdm_type": server_cdm_type,
|
||||
"sample_decrypt_tracks": [
|
||||
str(t.id) for t in [*tracks.videos, *tracks.audio] if service_instance.decrypts_samples(t)
|
||||
],
|
||||
**session_fields,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -4121,25 +4112,25 @@ async def session_prompt_get_handler(session_id: str, request: Optional[web.Requ
|
||||
bridge = session.input_bridge
|
||||
if bridge and (cdm_call := bridge.get_pending_cdm_call()):
|
||||
return web.json_response(
|
||||
{"status": "pending_input", "prompt": bridge.get_pending_prompt(), "cdm_call": cdm_call}
|
||||
PromptReply(status=AuthStatus.PENDING_INPUT, prompt=bridge.get_pending_prompt(), cdm_call=cdm_call)
|
||||
)
|
||||
|
||||
if session.auth_status == AuthStatus.AUTHENTICATED:
|
||||
return web.json_response({"status": "authenticated"})
|
||||
return web.json_response(PromptReply(status=AuthStatus.AUTHENTICATED))
|
||||
|
||||
if session.auth_status == AuthStatus.FAILED:
|
||||
return web.json_response({"status": "failed", "error": session.auth_error or "unknown error"})
|
||||
return web.json_response(PromptReply(status=AuthStatus.FAILED, error=session.auth_error or "unknown error"))
|
||||
|
||||
if bridge:
|
||||
prompt = bridge.get_pending_prompt()
|
||||
if prompt:
|
||||
return web.json_response({"status": "pending_input", "prompt": prompt})
|
||||
return web.json_response(PromptReply(status=AuthStatus.PENDING_INPUT, prompt=prompt))
|
||||
|
||||
return web.json_response({"status": "authenticating"})
|
||||
return web.json_response(PromptReply(status=AuthStatus.AUTHENTICATING))
|
||||
|
||||
|
||||
async def session_prompt_post_handler(
|
||||
data: Dict[str, Any], session_id: str, request: Optional[web.Request] = None
|
||||
data: PromptAnswer, session_id: str, request: Optional[web.Request] = None
|
||||
) -> web.Response:
|
||||
"""Submit a response to a pending interactive prompt.
|
||||
|
||||
@@ -4172,13 +4163,7 @@ async def session_logs_handler(session_id: str, since: int = 0, request: Optiona
|
||||
session = await get_validated_session(session_id, request)
|
||||
buffer = session.log_buffer
|
||||
logs = buffer.since(since) if buffer else []
|
||||
return web.json_response(
|
||||
{
|
||||
"session_id": session_id,
|
||||
"logs": logs,
|
||||
"last_seq": logs[-1]["seq"] if logs else since,
|
||||
}
|
||||
)
|
||||
return web.json_response(LogsReply(session_id=session_id, logs=logs, last_seq=logs[-1]["seq"] if logs else since))
|
||||
|
||||
|
||||
async def get_validated_session(session_id: str, request: Optional[web.Request]) -> Any:
|
||||
@@ -4930,16 +4915,16 @@ def handle_proxy_license(
|
||||
raise APIError(APIErrorCode.SERVICE_ERROR, f"Licence request failed: {exc}") from None
|
||||
|
||||
if license_blob is None:
|
||||
return web.json_response({"license": None})
|
||||
return web.json_response(LicenseReply(license=None))
|
||||
if isinstance(license_blob, str):
|
||||
# a Widevine service may answer in Base64 text, and the client hands its CDM the raw bytes
|
||||
license_blob = base64.b64decode(license_blob) if drm_type == "widevine" else license_blob.encode("utf-8")
|
||||
|
||||
return web.json_response({"license": base64.b64encode(license_blob).decode("ascii")})
|
||||
return web.json_response(LicenseReply(license=base64.b64encode(license_blob).decode("ascii")))
|
||||
|
||||
|
||||
async def session_segment_filter_handler(
|
||||
data: Dict[str, Any], session_id: str, request: Optional[web.Request] = None
|
||||
data: SegmentFilterRequest, session_id: str, request: Optional[web.Request] = None
|
||||
) -> web.Response:
|
||||
"""Run the service's HLS ``OnSegmentFilter`` for one track and return the unwanted segment URIs.
|
||||
|
||||
@@ -4963,7 +4948,7 @@ async def session_segment_filter_handler(
|
||||
|
||||
segment_filter = getattr(track, "OnSegmentFilter", None)
|
||||
if not callable(segment_filter) or not getattr(track, "url", None):
|
||||
return web.json_response({"unwanted": None})
|
||||
return web.json_response(SegmentFilterReply(unwanted=None))
|
||||
|
||||
def unwanted_segments() -> list[str]:
|
||||
import m3u8
|
||||
@@ -4982,7 +4967,7 @@ async def session_segment_filter_handler(
|
||||
log.exception(f"Error running the segment filter for track {sanitize_log(str(track_id))}")
|
||||
raise APIError(APIErrorCode.SERVICE_ERROR, "Could not run the segment filter for this track") from None
|
||||
|
||||
return web.json_response({"unwanted": unwanted})
|
||||
return web.json_response(SegmentFilterReply(unwanted=unwanted))
|
||||
|
||||
|
||||
async def session_samples_handler(
|
||||
@@ -5039,7 +5024,7 @@ async def session_samples_handler(
|
||||
|
||||
|
||||
async def session_license_handler(
|
||||
data: Dict[str, Any], session_id: str, request: Optional[web.Request] = None
|
||||
data: LicenseRequest, session_id: str, request: Optional[web.Request] = None
|
||||
) -> web.Response:
|
||||
"""Do the DRM licensing in proxy or server_cdm mode.
|
||||
|
||||
@@ -5059,7 +5044,7 @@ async def session_license_handler(
|
||||
|
||||
|
||||
def license_response(
|
||||
data: Dict[str, Any], session: Any, session_id: str, request: Optional[web.Request]
|
||||
data: LicenseRequest, session: Any, session_id: str, request: Optional[web.Request]
|
||||
) -> web.Response:
|
||||
"""The licence response for :func:`session_license_handler`, made on a worker thread."""
|
||||
track_id = data.get("track_id")
|
||||
@@ -5229,7 +5214,7 @@ def license_response(
|
||||
drm_types[tid] = track_drm_type
|
||||
actual_drm_type = track_drm_type
|
||||
|
||||
response: Dict[str, Any] = {"keys": all_keys}
|
||||
response = LicenseReply(keys=all_keys)
|
||||
if clear_tracks:
|
||||
response["clear_tracks"] = clear_tracks
|
||||
if vault_keys:
|
||||
@@ -5304,7 +5289,7 @@ def license_response(
|
||||
)
|
||||
log.info(f"Server CDM resolved {len(keys)} key(s) for track {track_log_label(track)}")
|
||||
note_served_keys(session, keys, key_sources)
|
||||
return web.json_response({"keys": keys, "vault_keys": list(key_sources), "drm_type": server_drm_type})
|
||||
return web.json_response(LicenseReply(keys=keys, vault_keys=list(key_sources), drm_type=server_drm_type))
|
||||
|
||||
if pssh_b64:
|
||||
require_track_pssh(track, pssh_b64, drm_type)
|
||||
@@ -5348,7 +5333,7 @@ def note_served_keys(session: Any, keys: Dict[str, str], sources: Dict[str, str]
|
||||
|
||||
|
||||
async def session_bad_key_handler(
|
||||
data: Dict[str, Any], session_id: str, request: Optional[web.Request] = None
|
||||
data: BadKeyReport, session_id: str, request: Optional[web.Request] = None
|
||||
) -> web.Response:
|
||||
"""Flag a server-vault content key the client proved wrong, so the next licence reaches the CDM.
|
||||
|
||||
@@ -5400,15 +5385,15 @@ async def session_info_handler(session_id: str, request: Optional[web.Request] =
|
||||
from unshackle.core.api.session_store import get_session_store
|
||||
|
||||
return web.json_response(
|
||||
{
|
||||
"session_id": session.session_id,
|
||||
"service": session.service_tag,
|
||||
"valid": True,
|
||||
"expires_in": get_session_store().ttl,
|
||||
"draining": get_session_store().draining,
|
||||
"track_count": len(session.tracks),
|
||||
"title_count": len(session.title_map),
|
||||
}
|
||||
SessionInfo(
|
||||
session_id=session.session_id,
|
||||
service=session.service_tag,
|
||||
valid=True,
|
||||
expires_in=get_session_store().ttl,
|
||||
draining=get_session_store().draining,
|
||||
track_count=len(session.tracks),
|
||||
title_count=len(session.title_map),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -5427,7 +5412,7 @@ async def session_delete_handler(session_id: str, request: Optional[web.Request]
|
||||
|
||||
await store.delete(session_id)
|
||||
|
||||
response: Dict[str, Any] = {"status": "ok"}
|
||||
response = SessionClosed(status="ok")
|
||||
if cache_data:
|
||||
response["cache"] = cache_data
|
||||
return web.json_response(response)
|
||||
|
||||
@@ -12,18 +12,9 @@ from __future__ import annotations
|
||||
import json
|
||||
import threading
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import Any, Optional
|
||||
|
||||
|
||||
class AuthStatus(Enum):
|
||||
"""Authentication lifecycle states for a remote session."""
|
||||
|
||||
AUTHENTICATING = "authenticating"
|
||||
PENDING_INPUT = "pending_input"
|
||||
AUTHENTICATED = "authenticated"
|
||||
FAILED = "failed"
|
||||
|
||||
from unshackle.core.remote_protocol import AuthStatus
|
||||
|
||||
# Seconds the server waits for a remote client to answer an input prompt before
|
||||
# giving up; also the TTL SessionStore grants AUTHENTICATING/PENDING_INPUT sessions.
|
||||
|
||||
@@ -13,13 +13,7 @@ from aiohttp_swagger3 import SwaggerDocs, SwaggerInfo, SwaggerUiSettings
|
||||
|
||||
from unshackle.core import __code_hash__, __version__
|
||||
from unshackle.core import services as services_module
|
||||
from unshackle.core.api.errors import (
|
||||
APIError,
|
||||
APIErrorCode,
|
||||
build_error_response,
|
||||
categorize_exception,
|
||||
handle_api_exception,
|
||||
)
|
||||
from unshackle.core.api.errors import APIError, build_error_response, categorize_exception, handle_api_exception
|
||||
from unshackle.core.api.handlers import (
|
||||
CORS_HEADERS,
|
||||
DASHBOARD_EVENTS_ROUTE,
|
||||
@@ -82,6 +76,23 @@ from unshackle.core.api.handlers import (
|
||||
)
|
||||
from unshackle.core.api.sanitize import sanitize_log
|
||||
from unshackle.core.drm import cbcs
|
||||
from unshackle.core.remote_protocol import (
|
||||
BAD_KEY_ROUTE,
|
||||
HEALTH_ROUTE,
|
||||
LICENSE_ROUTE,
|
||||
LOGS_ROUTE,
|
||||
PROMPT_ROUTE,
|
||||
SAMPLES_ROUTE,
|
||||
SEARCH_ROUTE,
|
||||
SEGMENT_FILTER_ROUTE,
|
||||
SEGMENTS_ROUTE,
|
||||
SERVICES_ROUTE,
|
||||
SESSION_CREATE_ROUTE,
|
||||
SESSION_ROUTE,
|
||||
TITLES_ROUTE,
|
||||
TRACKS_ROUTE,
|
||||
APIErrorCode,
|
||||
)
|
||||
from unshackle.core.services import Services
|
||||
from unshackle.core.tunnel import CHECK_ROUTE as TUNNEL_CHECK_ROUTE
|
||||
from unshackle.core.tunnel import ROUTE as TUNNEL_ROUTE
|
||||
@@ -3399,9 +3410,9 @@ async def remote_login_input(request: web.Request) -> web.Response:
|
||||
# --remote-only server mode; full mode registers every route. setup_routes registers the
|
||||
# routes and their docs from this table, so the live routes and the docs cannot drift.
|
||||
ROUTES: list[tuple[str, str, Handler, bool]] = [
|
||||
("GET", "/api/health", health, True),
|
||||
("GET", "/api/services", services, True),
|
||||
("POST", "/api/search", search, True),
|
||||
("GET", HEALTH_ROUTE, health, True),
|
||||
("GET", SERVICES_ROUTE, services, True),
|
||||
("POST", SEARCH_ROUTE, search, True),
|
||||
("POST", "/api/list-titles", list_titles, False),
|
||||
("POST", "/api/list-tracks", list_tracks, False),
|
||||
("POST", "/api/download", download, False),
|
||||
@@ -3423,21 +3434,21 @@ ROUTES: list[tuple[str, str, Handler, bool]] = [
|
||||
("GET", "/api/env/check", env_check, False),
|
||||
("GET", "/api/remote/logins", remote_logins, False),
|
||||
("POST", "/api/remote/logins/{login_id}/input", remote_login_input, False),
|
||||
("POST", "/api/session/create", session_create, True),
|
||||
("POST", SESSION_CREATE_ROUTE, session_create, True),
|
||||
("GET", TUNNEL_ROUTE, tunnel, True),
|
||||
("GET", TUNNEL_CHECK_ROUTE, tunnel_check, True),
|
||||
("GET", "/api/session/{session_id}/titles", session_titles, True),
|
||||
("POST", "/api/session/{session_id}/tracks", session_tracks, True),
|
||||
("POST", "/api/session/{session_id}/segments", session_segments, True),
|
||||
("POST", "/api/session/{session_id}/segment_filter", session_segment_filter, True),
|
||||
("POST", "/api/session/{session_id}/samples", session_samples, True),
|
||||
("POST", "/api/session/{session_id}/license", session_license, True),
|
||||
("POST", "/api/session/{session_id}/keys/bad", session_bad_key, True),
|
||||
("GET", "/api/session/{session_id}/logs", session_logs, True),
|
||||
("GET", "/api/session/{session_id}/prompt", session_prompt_get, True),
|
||||
("POST", "/api/session/{session_id}/prompt", session_prompt_submit, True),
|
||||
("GET", "/api/session/{session_id}", session_info, True),
|
||||
("DELETE", "/api/session/{session_id}", session_delete, True),
|
||||
("GET", TITLES_ROUTE, session_titles, True),
|
||||
("POST", TRACKS_ROUTE, session_tracks, True),
|
||||
("POST", SEGMENTS_ROUTE, session_segments, True),
|
||||
("POST", SEGMENT_FILTER_ROUTE, session_segment_filter, True),
|
||||
("POST", SAMPLES_ROUTE, session_samples, True),
|
||||
("POST", LICENSE_ROUTE, session_license, True),
|
||||
("POST", BAD_KEY_ROUTE, session_bad_key, True),
|
||||
("GET", LOGS_ROUTE, session_logs, True),
|
||||
("GET", PROMPT_ROUTE, session_prompt_get, True),
|
||||
("POST", PROMPT_ROUTE, session_prompt_submit, True),
|
||||
("GET", SESSION_ROUTE, session_info, True),
|
||||
("DELETE", SESSION_ROUTE, session_delete, True),
|
||||
]
|
||||
|
||||
DASHBOARD_ROUTES: list[tuple[str, str, Handler, bool]] = [
|
||||
@@ -3473,7 +3484,7 @@ def setup_routes(
|
||||
"POST": router.add_post,
|
||||
"DELETE": router.add_delete,
|
||||
}
|
||||
api_routes = [r for r in ROUTES if r[1] == "/api/health"] if cdm_only else ROUTES
|
||||
api_routes = [r for r in ROUTES if r[1] == HEALTH_ROUTE] if cdm_only else ROUTES
|
||||
for method, path, handler, remote in api_routes + (DASHBOARD_ROUTES if dashboard else []):
|
||||
if remote_only and not remote:
|
||||
continue
|
||||
|
||||
@@ -18,9 +18,10 @@ import threading
|
||||
import time
|
||||
import traceback
|
||||
from collections import deque
|
||||
from typing import Any, Deque, Dict, List, MutableMapping, Optional, Tuple
|
||||
from typing import Any, Deque, List, MutableMapping, Optional, Tuple
|
||||
|
||||
from unshackle.core.api.sanitize import sanitize_log
|
||||
from unshackle.core.remote_protocol import LogRecord
|
||||
|
||||
MAX_RECORDS = 500
|
||||
MAX_MESSAGE_LEN = 2000
|
||||
@@ -30,7 +31,7 @@ class SessionLogBuffer:
|
||||
"""Thread-safe bounded buffer of log records with a monotonic sequence."""
|
||||
|
||||
def __init__(self, maxlen: int = MAX_RECORDS) -> None:
|
||||
self._records: Deque[Dict[str, Any]] = deque(maxlen=maxlen)
|
||||
self._records: Deque[LogRecord] = deque(maxlen=maxlen)
|
||||
self._seq = 0
|
||||
self._lock = threading.Lock()
|
||||
|
||||
@@ -38,18 +39,18 @@ class SessionLogBuffer:
|
||||
with self._lock:
|
||||
self._seq += 1
|
||||
self._records.append(
|
||||
{
|
||||
"seq": self._seq,
|
||||
"level": logging.getLevelName(level),
|
||||
"message": sanitize_log(message)[:MAX_MESSAGE_LEN],
|
||||
"ts": time.time(),
|
||||
}
|
||||
LogRecord(
|
||||
seq=self._seq,
|
||||
level=logging.getLevelName(level),
|
||||
message=sanitize_log(message)[:MAX_MESSAGE_LEN],
|
||||
ts=time.time(),
|
||||
)
|
||||
)
|
||||
|
||||
def since(self, seq: int) -> List[Dict[str, Any]]:
|
||||
def since(self, seq: int) -> List[LogRecord]:
|
||||
"""Records with a sequence number greater than *seq*, oldest first."""
|
||||
with self._lock:
|
||||
return [dict(r) for r in self._records if r["seq"] > seq]
|
||||
return [r.copy() for r in self._records if r["seq"] > seq]
|
||||
|
||||
@property
|
||||
def last_seq(self) -> int:
|
||||
|
||||
@@ -17,9 +17,10 @@ from datetime import datetime, timezone
|
||||
from typing import Any, Deque, Dict, List, Optional
|
||||
|
||||
from unshackle.core.api.events import bus
|
||||
from unshackle.core.api.input_bridge import AUTH_INPUT_TIMEOUT, AuthStatus, InputBridge
|
||||
from unshackle.core.api.input_bridge import AUTH_INPUT_TIMEOUT, InputBridge
|
||||
from unshackle.core.api.sanitize import sanitize_log
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.remote_protocol import AuthStatus
|
||||
from unshackle.core.tracks import Track
|
||||
|
||||
log = logging.getLogger("api.session")
|
||||
@@ -369,7 +370,8 @@ session_store: Optional[SessionStore] = None
|
||||
|
||||
def refuse_while_draining() -> None:
|
||||
"""Raise SERVER_DRAINING for a new session or job once the operator has started a drain."""
|
||||
from unshackle.core.api.errors import APIError, APIErrorCode
|
||||
from unshackle.core.api.errors import APIError
|
||||
from unshackle.core.remote_protocol import APIErrorCode
|
||||
|
||||
if get_session_store().draining:
|
||||
raise APIError(
|
||||
|
||||
Reference in New Issue
Block a user