diff --git a/CHANGELOG.md b/CHANGELOG.md index 1babd01..7b202c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ The version lives in `CERBERUS_VERSION` in `src/cerberus.c` (`MAJOR.MINOR.PATCH` published only as the [`latest`](https://github.com/OpenIPC/cerberus/releases/latest) release (see the README); this file is the change history, newest first. +## 0.3.4 — 2026-11-13 + +- The coordinator's `NightModeOn` / `NightModeOff` commands switch majestic into night or day mode + through its own `/night/on` and `/night/off`, on the same address the snapshot comes from — so a + majestic with a password is reached with the one already in `cerberus_snapshot_url`, and the log + names the path, never that address. Like every command there is no answer to the cloud; if + majestic's automatic mode is on, its light sensor may switch back. + ## 0.3.3 — 2026-11-12 - The coordinator's `EnableMotion` command switches majestic's own motion detector on diff --git a/src/cerberus.c b/src/cerberus.c index 97a8c62..141d06c 100644 --- a/src/cerberus.c +++ b/src/cerberus.c @@ -70,7 +70,7 @@ /* The version of every build, release builds included (#32); `make VERSION=...` overrides it for a test build. */ #ifndef CERBERUS_VERSION -#define CERBERUS_VERSION "0.3.3" +#define CERBERUS_VERSION "0.3.4" #endif /* Where the errors are decoded. Printed after every fatal one: the reader is on a camera @@ -901,13 +901,15 @@ static int conn_send(struct conn *c, const char *buf, size_t len) return 0; } +#define HTTP_TIMEOUT_MS 10000 + static int conn_recv(struct conn *c, unsigned char *buf, size_t len) { int n; do { n = c->tls ? mbedtls_ssl_read(&c->ssl, buf, len) - : mbedtls_net_recv(&c->net, buf, len); + : mbedtls_net_recv_timeout(&c->net, buf, len, HTTP_TIMEOUT_MS); } while (n == MBEDTLS_ERR_SSL_WANT_READ || n == MBEDTLS_ERR_SSL_WANT_WRITE); return n; } @@ -1082,9 +1084,10 @@ static long http_do(const struct http_req *rq, unsigned char *resp, size_t resp_ } mbedtls_ssl_conf_rng(&c.conf, mbedtls_ctr_drbg_random, rng); + mbedtls_ssl_conf_read_timeout(&c.conf, HTTP_TIMEOUT_MS); if (mbedtls_ssl_setup(&c.ssl, &c.conf) != 0) die("ssl setup failed"); if (mbedtls_ssl_set_hostname(&c.ssl, host) != 0) die("ssl SNI failed"); - mbedtls_ssl_set_bio(&c.ssl, &c.net, mbedtls_net_send, mbedtls_net_recv, NULL); + mbedtls_ssl_set_bio(&c.ssl, &c.net, mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout); while ((rc = mbedtls_ssl_handshake(&c.ssl)) != 0) { if (rc == MBEDTLS_ERR_SSL_WANT_READ || rc == MBEDTLS_ERR_SSL_WANT_WRITE) continue; @@ -1126,7 +1129,15 @@ static long http_do(const struct http_req *rq, unsigned char *resp, size_t resp_ for (;;) { n = conn_recv(&c, buf, sizeof buf); if (n == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY || n <= 0) break; - if (total + (size_t)n + 1 > resp_sz) { overflow = 1; break; } + if (total + (size_t)n + 1 > resp_sz) { + size_t room = resp_sz > total + 1 ? resp_sz - 1 - total : 0; + if (room) { + memcpy(resp + total, buf, room); + total += room; + } + overflow = 1; + break; + } memcpy(resp + total, buf, (size_t)n); total += (size_t)n; } @@ -2135,6 +2146,50 @@ static void enable_motion(void) g_cfg.events ? "" : " (motion events are off in this agent, so nothing will reach the cloud)"); } +/* + * NightModeOn / NightModeOff: majestic's own /night/on and /night/off — the switch its web interface + * flips. Sent to the same origin the snapshot comes from (cerberus_snapshot_url), so a majestic with + * a password is reached with the password already given for frames, and no second address has to be + * configured. There is no answer to the cloud either way — the directive channel has none — so what + * happened goes to the log; and if majestic's automatic mode is on, its light sensor may switch back. + */ +static void night_mode(int on) +{ + const char *what = on ? "NightModeOn" : "NightModeOff"; + const char *s = g_cfg.snapshot_url, *scheme = s ? strstr(s, "://") : NULL, *path; + struct http_req rq = { "GET", NULL, NULL, NULL, 0, NULL }; + unsigned char resp[512]; + char url[512]; + size_t origin; + int status = 0; + long n; + + if (!url_ok(s) || !scheme) { + warn_("%s: CERBERUS_SNAPSHOT_URL is not an http(s) URL, so there is no majestic to ask", what); + return; + } + path = strchr(scheme + 3, '/'); + origin = path ? (size_t)(path - s) : strlen(s); + if (origin + sizeof "/night/off" > sizeof url) { + warn_("%s: CERBERUS_SNAPSHOT_URL is too long", what); + return; + } + memcpy(url, s, origin); + strcpy(url + origin, on ? "/night/on" : "/night/off"); + rq.url = url; + + n = http_do(&rq, resp, sizeof resp, &status); + /* The address may carry majestic's password: say the path, never the URL. */ + if (n == -1) + warn_("%s: majestic did not answer %s", what, on ? "/night/on" : "/night/off"); + else if (status == 401 || status == 403) + warn_("%s: majestic wants a password — put it in cerberus_snapshot_url, as for frames", what); + else if (status != 200) + warn_("%s: majestic answered HTTP %d to %s", what, status, on ? "/night/on" : "/night/off"); + else + note("%s mode switched on in majestic", on ? "night" : "day"); +} + /* * Применяет NodeDirective из ответа на heartbeat. * @@ -2196,6 +2251,10 @@ static void apply_directive(const char *json) self_restart(); else if (!strcmp(command, "EnableMotion")) enable_motion(); + else if (!strcmp(command, "NightModeOn")) + night_mode(1); + else if (!strcmp(command, "NightModeOff")) + night_mode(0); else if (strcmp(command, "None") != 0) warn_("unknown command '%s' from the coordinator — ignoring", command); } diff --git a/tests/fake_coordinator.py b/tests/fake_coordinator.py index 46a74a4..f6e12ae 100755 --- a/tests/fake_coordinator.py +++ b/tests/fake_coordinator.py @@ -230,7 +230,7 @@ def heartbeat(self, node): class Majestic(BaseHTTPRequestHandler): - """majestic's /image.jpg: Basic auth root:12345, a JPEG-looking body, sent chunked.""" + """majestic's /image.jpg (a JPEG-looking body, sent chunked) and /night/on|off, behind Basic auth root:12345.""" protocol_version = "HTTP/1.1" FRAME = b"\xff\xd8\xff\xe0" + bytes(range(256)) * 40 + b"\xff\xd9" @@ -242,6 +242,22 @@ def do_GET(self): with LOCK: STATE["requests"].append({"path": "majestic:" + self.path, "authorized": self.headers.get("Authorization") == want}) + if self.path in ("/night/on", "/night/off"): + if self.headers.get("Authorization") != want: + self.send_response(401) + self.send_header("WWW-Authenticate", 'Basic realm="majestic"') + self.send_header("Content-Length", "0") + self.send_header("Connection", "close") + self.end_headers() + return + out = b"ok" if self.path == "/night/on" else b"ok\n" + b"x" * 600 + self.send_response(200) + self.send_header("Content-Type", "text/plain") + self.send_header("Content-Length", str(len(out))) + self.send_header("Connection", "close") + self.end_headers() + self.wfile.write(out) + return if not self.path.startswith("/image.jpg"): self.send_response(404) self.send_header("Content-Length", "0") diff --git a/tests/run.sh b/tests/run.sh index 5994f6d..6ed2b12 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -24,4 +24,5 @@ MSYS_NO_PATHCONV=1 exec docker run --rm --privileged \ --add-host coordinator.test:127.0.0.1 \ -v "$(host_path .):/src:ro" \ -e REQUIRE_WG="${REQUIRE_WG:-}" \ + -e GITHUB_ACTIONS="${GITHUB_ACTIONS:-}" \ "$IMAGE" sh /src/tests/suite.sh diff --git a/tests/suite.sh b/tests/suite.sh index c090341..fbb9afc 100755 --- a/tests/suite.sh +++ b/tests/suite.sh @@ -20,6 +20,9 @@ check() { # check