Skip to content

Record a sky camera around the clock, keep the footage on your own disk, and find what flew past.

Relay: percent-encode the camera login when building the RTSP URL (#8)

Hunter Deiblerdcdbfd69f14e
diff --git a/docs/capture/relay.md b/docs/capture/relay.md
index bee3109..1488b91 100644
--- a/docs/capture/relay.md
+++ b/docs/capture/relay.md
@@ -32,7 +32,7 @@ make deploy HOST=<relay-host>
| `.env` value | Default | Used for |
|---|---|---|
| `CAMERA_IP_ADDRESS` | | Camera host in the RTSP URL |
-| `CAMERA_USERNAME`, `CAMERA_PASSWORD` | | Camera login in the RTSP URL. They are inserted as written, so percent-encode `@`, `:` and `/`. |
+| `CAMERA_USERNAME`, `CAMERA_PASSWORD` | | Camera login in the RTSP URL. Write them as they are; `deploy.sh` percent-encodes them. |
| `CAMERA_RTSP_PORT` | `554` | RTSP port |
| `CAMERA_RTSP_PATH` | `/cam/realmonitor?channel=1&subtype=0` | Stream path. The default is a Dahua main stream; Hikvision uses `/Streaming/Channels/101`, TP-Link Tapo `/stream1`. Quote it in `.env` because it contains `&`. |
| `CAM_ID` | `sky01` | Camera ID in file names and object keys. It must be in the Workers' `CAMERAS` list. |
diff --git a/docs/configuration.md b/docs/configuration.md
index ad106db..745e845 100644
--- a/docs/configuration.md
+++ b/docs/configuration.md
@@ -23,7 +23,7 @@ Copy `.env.example` to `.env`. Three programs read it: bash (`make deploy`, `mak
| Key | Default | Used by | Meaning |
|---|---|---|---|
| `CAMERA_IP_ADDRESS` | none | relay, camera MCP | The camera's address |
-| `CAMERA_USERNAME`, `CAMERA_PASSWORD` | none | relay, camera MCP | Camera credentials. They go into the RTSP URL unescaped, so they must not contain `@`, `:` or `/` |
+| `CAMERA_USERNAME`, `CAMERA_PASSWORD` | none | relay, camera MCP | Camera credentials, written as they are. The relay percent-encodes them in the RTSP URL |
| `CAMERA_RTSP_PORT` | `554` | relay | RTSP port |
| `CAMERA_RTSP_PATH` | `/cam/realmonitor?channel=1&subtype=0` | relay | Stream path after the port. The default is the Dahua main stream |
| `CAMERA_SCHEME` | `http` | camera MCP | `http` or `https`, for the camera's web API |
@@ -135,7 +135,7 @@ Fixed in code ([`package/worker/src/utils/configStorageUtil.ts`](https://github.
| Key in `relay.env` | Value |
|---|---|
| `CAM_ID` | From `.env`, default `sky01` |
-| `CAMERA_RTSP_URL` | `rtsp://<user>:<password>@<address>:<port><path>` |
+| `CAMERA_RTSP_URL` | `rtsp://<user>:<password>@<address>:<port><path>`, with the user and password percent-encoded |
| `LIVE_DIR` | `/dev/shm/skyvision` (RAM) |
| `SPOOL_DIR` | `~/skyvision/spool` (disk), with `pending/` and `quarantine/` |
| `INGEST_URL` | From `.env` |
diff --git a/external/relay/deploy.sh b/external/relay/deploy.sh
index bf452ed..4e742ff 100755
--- a/external/relay/deploy.sh
+++ b/external/relay/deploy.sh
@@ -13,8 +13,10 @@ set -a
# shellcheck source=/dev/null
. ../../.env
set +a
-: "${INGEST_URL:?set INGEST_URL in .env}"
+: "${INGEST_URL:?set INGEST_URL in .env}" "${CAMERA_IP_ADDRESS:?set CAMERA_IP_ADDRESS in .env}" "${CAMERA_USERNAME?set CAMERA_USERNAME in .env}" "${CAMERA_PASSWORD?set CAMERA_PASSWORD in .env}"
esc() { printf '%s' "$1" | sed 's/[\\"]/\\&/g'; }
+# The URL the setup guide's stream check opened: the same function percent-encodes the login.
+CAMERA_RTSP_URL=$(node --input-type=module -e "import { rtspUrl } from '../../setup/lib.mjs'; process.stdout.write(rtspUrl(process.env));")
REMOTE_HOME=$(ssh "$HOST" 'mkdir -p ~/skyvision ~/.config/skyvision ~/.config/systemd/user && echo "$HOME"')
scp -q record.sh upload.sh "$HOST":skyvision/
@@ -23,7 +25,7 @@ scp -q skyvision-record.service skyvision-upload.service "$HOST":.config/systemd
# shellcheck disable=SC2087
ssh "$HOST" 'chmod 755 ~/skyvision/*.sh; umask 077; cat > ~/.config/skyvision/relay.env' <<EOF
CAM_ID=${CAM_ID:-sky01}
-CAMERA_RTSP_URL="$(esc "rtsp://$CAMERA_USERNAME:$CAMERA_PASSWORD@$CAMERA_IP_ADDRESS:${CAMERA_RTSP_PORT:-554}${CAMERA_RTSP_PATH:-/cam/realmonitor?channel=1&subtype=0}")"
+CAMERA_RTSP_URL="$(esc "$CAMERA_RTSP_URL")"
LIVE_DIR=/dev/shm/skyvision
SPOOL_DIR=$REMOTE_HOME/skyvision/spool
INGEST_URL=$INGEST_URL
diff --git a/setup/lib.check.mjs b/setup/lib.check.mjs
index f1a160f..d12598c 100644
--- a/setup/lib.check.mjs
+++ b/setup/lib.check.mjs
@@ -64,6 +64,15 @@ test('team domains are normalized, and bad input is reported', () => {
assert.ok(lib.problems.folder('footage'));
});
+test('the camera stream URL percent-encodes the login that .env holds as written', () => {
+ const env = { CAMERA_IP_ADDRESS: '192.168.1.20', CAMERA_USERNAME: 'ad:min', CAMERA_PASSWORD: 'p@ss/w?rd%41' };
+ assert.equal(lib.rtspUrl(env), 'rtsp://ad%3Amin:p%40ss%2Fw%3Frd%[email protected]:554/cam/realmonitor?channel=1&subtype=0');
+ assert.equal(
+ lib.rtspUrl({ ...env, CAMERA_RTSP_PORT: '8554', CAMERA_RTSP_PATH: '/stream1' }),
+ 'rtsp://ad%3Amin:p%40ss%2Fw%3Frd%[email protected]:8554/stream1',
+ );
+});
+
test('camera stream URLs and their passwords are removed from error text', () => {
const url = 'rtsp://admin:s3cret%[email protected]:554/stream1';
assert.equal(lib.redactUrl(`${url}: Connection refused`, url), '<camera stream>: Connection refused');
diff --git a/setup/lib.mjs b/setup/lib.mjs
index 13d673e..07d1431 100644
--- a/setup/lib.mjs
+++ b/setup/lib.mjs
@@ -99,6 +99,15 @@ export const problems = {
address: v => (/^[A-Za-z0-9.:-]+$/.test(v) ? undefined : 'Enter the camera address, such as 192.168.1.20.'),
};
+/**
+ * The camera stream URL, for the setup guide's stream check and external/relay/deploy.sh alike.
+ * The login is percent-encoded here, so .env holds it as the camera expects it (camera_mcp.py sends it as is).
+ */
+export function rtspUrl(env) {
+ const login = `${encodeURIComponent(env.CAMERA_USERNAME)}:${encodeURIComponent(env.CAMERA_PASSWORD)}`;
+ return `rtsp://${login}@${env.CAMERA_IP_ADDRESS}:${env.CAMERA_RTSP_PORT || 554}${env.CAMERA_RTSP_PATH || '/cam/realmonitor?channel=1&subtype=0'}`;
+}
+
/** Text from a tool that was given `url`, without the URL or any user:password@ in it. */
export function redactUrl(text, url) {
return text.replaceAll(url, '<camera stream>').replace(/\/\/[^/@\s]*@/g, '//');
diff --git a/setup/setup.mjs b/setup/setup.mjs
index f958686..3dc1a5b 100644
--- a/setup/setup.mjs
+++ b/setup/setup.mjs
@@ -497,7 +497,7 @@ const steps = [
const streamPath = await ask('RTSP path', { initial: env.CAMERA_RTSP_PATH || '/cam/realmonitor?channel=1&subtype=0', problem: lib.problems.path, key: 'CAMERA_RTSP_PATH' });
save({ CAMERA_IP_ADDRESS: ip, CAMERA_USERNAME: user, CAMERA_PASSWORD: password, CAMERA_RTSP_PORT: port, CAMERA_RTSP_PATH: streamPath });
say(`Checking the stream from ${host}…`);
- const url = `rtsp://${encodeURIComponent(user)}:${encodeURIComponent(password)}@${ip}:${port}${streamPath}`;
+ const url = lib.rtspUrl(env);
const probe = ssh(host, 'read -r url; timeout 20 ffprobe -v error -rtsp_transport tcp -select_streams v:0 -show_entries stream=codec_name,width,height -of csv=p=0 "$url"', url);
const [codec, width, height] = probe.out.trim().split('\n').pop().split(',');
if (probe.ok && ['hevc', 'h264'].includes(codec)) {