Add public status endpoint for availability monitors
This commit is contained in:
1 parent
fe7ccf6aac
commit
7e42e72a67
5 files changed
+73
-4
No files matched your search
@@ -1,5 +1,9 @@
|
||||
# Changelog
|
||||
|
||||
## 0.53.5 - 2026-10-02
|
||||
|
||||
- Added an unauthenticated `GET` and `HEAD /status` endpoint for availability monitors. It returns `200 OK` with a plain `OK` body and keeps the existing `KAIZOKU_ALLOW_REMOTE` gate for non-local clients.
|
||||
|
||||
## 0.53.4 - 2026-09-30
|
||||
|
||||
- Switched Jellyfin API-key requests from the removed `X-Emby-Token` header to the current `Authorization: MediaBrowser` scheme, fixing library refreshes that returned `HTTP 401` on Jellyfin 12.
|
||||
|
||||
@@ -50,6 +50,8 @@ Then open:
|
||||
http://127.0.0.1:8421/
|
||||
```
|
||||
|
||||
For Uptime Kuma or another HTTP availability monitor, use `http://IP:PORT/status`. `GET` returns HTTP 200 with `OK`; `HEAD` also returns HTTP 200. The endpoint needs no authentication and reports whether Kaizoku's HTTP server is responding. It does not check provider or download health. For a monitor on another machine, bind Kaizoku to a reachable address and set `KAIZOKU_ALLOW_REMOTE=1`; the normal app and API routes still require configured credentials for remote clients.
|
||||
|
||||
Run the local regression tests:
|
||||
|
||||
```sh
|
||||
|
||||
+29
-3
@@ -250,7 +250,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||
server_version = "Kaizoku/0.1"
|
||||
remote_session_cookie_name = "kaizoku_session"
|
||||
remote_session_cookie_max_age_seconds = 30 * 24 * 60 * 60
|
||||
quiet_poll_paths = {"/api/queue", "/api/watchlist/refresh-status", "/api/config/jellyfin/sync-status"}
|
||||
quiet_poll_paths = {"/status", "/api/queue", "/api/watchlist/refresh-status", "/api/config/jellyfin/sync-status"}
|
||||
|
||||
def _context(self):
|
||||
context = getattr(self, "handler_context", None) or getattr(type(self), "handler_context", None)
|
||||
@@ -570,6 +570,18 @@ class Handler(BaseHTTPRequestHandler):
|
||||
if not Handler._remote_credentials_valid(self):
|
||||
raise PermissionError("Remote username or password missing or invalid.")
|
||||
|
||||
def serve_status(self):
|
||||
client_host = Handler._effective_client_host(self)
|
||||
if not client_address_is_local(client_host) and not remote_access_allowed():
|
||||
raise PermissionError(
|
||||
"Remote access is disabled. Set KAIZOKU_ALLOW_REMOTE=1 to allow non-local clients."
|
||||
)
|
||||
self.write_response_bytes(
|
||||
b"OK\n",
|
||||
HTTPStatus.OK,
|
||||
{"Content-Type": "text/plain; charset=utf-8", "Cache-Control": "no-store"},
|
||||
)
|
||||
|
||||
def enforce_same_origin(self):
|
||||
origin = Handler._origin(self)
|
||||
if not origin:
|
||||
@@ -595,6 +607,9 @@ class Handler(BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
parsed = urlparse(self.path)
|
||||
try:
|
||||
if parsed.path == "/status":
|
||||
Handler.serve_status(self)
|
||||
return
|
||||
if parsed.path == "/auth/login":
|
||||
if Handler._session_cookie(self) and remote_access_session_matches(Handler._session_cookie(self)):
|
||||
params = parse_qs(parsed.query, keep_blank_values=True)
|
||||
@@ -728,6 +743,16 @@ class Handler(BaseHTTPRequestHandler):
|
||||
except Exception as exc:
|
||||
self.exception(exc)
|
||||
|
||||
def do_HEAD(self):
|
||||
try:
|
||||
if urlparse(self.path).path == "/status":
|
||||
Handler.serve_status(self)
|
||||
else:
|
||||
self.ensure_client_access()
|
||||
self.error(HTTPStatus.NOT_FOUND, "Not found")
|
||||
except Exception as exc:
|
||||
self.exception(exc)
|
||||
|
||||
def do_POST(self):
|
||||
parsed = urlparse(self.path)
|
||||
try:
|
||||
@@ -1015,7 +1040,8 @@ class Handler(BaseHTTPRequestHandler):
|
||||
self.send_header(key, value)
|
||||
self.send_header("Content-Length", str(len(payload)))
|
||||
self.end_headers()
|
||||
self.wfile.write(payload)
|
||||
if getattr(self, "command", "") != "HEAD":
|
||||
self.wfile.write(payload)
|
||||
except CLIENT_DISCONNECT_ERRORS:
|
||||
return
|
||||
|
||||
@@ -1062,7 +1088,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||
|
||||
def log_message(self, fmt, *args):
|
||||
parsed = urlparse(getattr(self, "path", "") or "")
|
||||
if self.command == "GET" and parsed.path in Handler.quiet_poll_paths:
|
||||
if self.command in {"GET", "HEAD"} and parsed.path in Handler.quiet_poll_paths:
|
||||
return
|
||||
print(f"{self.address_string()} - {fmt % args}")
|
||||
|
||||
|
||||
+37
@@ -111,6 +111,43 @@ class DummyHandler:
|
||||
|
||||
|
||||
class NetworkGuardTests(unittest.TestCase):
|
||||
def test_status_allows_remote_monitor_without_credentials(self):
|
||||
handler = DummyHandler("/status?source=uptime-kuma")
|
||||
handler.client_address = ("192.168.1.25", 8421)
|
||||
handler.command = "GET"
|
||||
|
||||
with mock.patch.dict(
|
||||
os.environ,
|
||||
{"KAIZOKU_ALLOW_REMOTE": "1", "KAIZOKU_AUTH_USERNAME": "", "KAIZOKU_AUTH_PASSWORD": ""},
|
||||
):
|
||||
APP.Handler.do_GET(handler)
|
||||
|
||||
self.assertEqual(handler.response_status, HTTPStatus.OK)
|
||||
self.assertEqual(handler.wfile.getvalue(), b"OK\n")
|
||||
self.assertEqual(dict(handler.response_headers)["Cache-Control"], "no-store")
|
||||
|
||||
def test_status_head_has_no_body(self):
|
||||
handler = DummyHandler("/status")
|
||||
handler.client_address = ("192.168.1.25", 8421)
|
||||
handler.command = "HEAD"
|
||||
|
||||
with mock.patch.dict(os.environ, {"KAIZOKU_ALLOW_REMOTE": "1"}):
|
||||
APP.Handler.do_HEAD(handler)
|
||||
|
||||
self.assertEqual(handler.response_status, HTTPStatus.OK)
|
||||
self.assertEqual(dict(handler.response_headers)["Content-Length"], "3")
|
||||
self.assertEqual(handler.wfile.getvalue(), b"")
|
||||
|
||||
def test_status_respects_remote_access_setting(self):
|
||||
handler = DummyHandler("/status")
|
||||
handler.client_address = ("192.168.1.25", 8421)
|
||||
handler.command = "GET"
|
||||
|
||||
with mock.patch.dict(os.environ, {"KAIZOKU_ALLOW_REMOTE": "0"}):
|
||||
APP.Handler.do_GET(handler)
|
||||
|
||||
self.assertEqual(handler.error_status, HTTPStatus.FORBIDDEN)
|
||||
|
||||
def test_runtime_bootstrap_is_deferred_until_initialize(self):
|
||||
self.assertTrue(IMPORT_RUNTIME_WAS_DEFERRED)
|
||||
|
||||
|
||||
Reference in new issue
Block a user