feat: reverse SSH tunnel so FCP (.96) reaches recounter via 127.0.0.1:9050

.96 cannot initiate connections to .93 (firewall drops NEW inbound,
100% ping loss .96->.93), but .93->.96 outbound works. start_tunnel.sh
opens ssh -R from .93 to .96, publishing recounter as .96's own
loopback :9050 - FCP sets RECOUNTER_URL=http://127.0.0.1:9050 and
counting stays on .93 hardware.

- start_tunnel.sh / stop_tunnel.sh (idempotent, keepalive, pidfile/log)
- start_dashboard.sh auto-starts tunnel (SKIP_TUNNEL=1 to opt out)
- stop_dashboard.sh stops tunnel alongside the web UI
- handoff doc section 1b: network solution + RECOUNTER_URL=127.0.0.1:9050
- E2E from .96: 401 no key / 202 with key / job COMPLETED on .93
This commit is contained in:
jetson committed 2026-10-20 15:22:59 +07:00
1 parent 43d1dee8b4
commit e3a905e170
6 files changed
+94 -1

No files matched your search

+7
View File
@@ -7,6 +7,13 @@ version tags are cut in this repo — `main` is the release line).
## [Unreleased]
### Added
- **Reverse SSH tunnel** `start_tunnel.sh` / `stop_tunnel.sh`
(`192.168.192.96` ↔ `.93`): .93 dials out to .96 and publishes recounter
as `127.0.0.1:9050` **on .96**, because .96→.93 inbound is firewalled
(100% loss). FCP now calls `RECOUNTER_URL=http://127.0.0.1:9050` —
recounter "at .96's IP", counting on .93 hardware. Auto-started by
`start_dashboard.sh` (skip: `SKIP_TUNNEL=1`). E2E verified: analyze
POST from .96 → job COMPLETED on .93.
- **`POST /api/analyze` machine endpoint** (`540e1b5`): multipart video +
`fcp_mode` letter (A–D) or explicit `model_stems`; returns
**202** `{"job_id","status_url"}`; **409 busy** guard when any job is
+26 -1
View File
@@ -84,6 +84,31 @@ Everything needed is already in FCP (researched, file:line verified):
different data attribute (e.g. `data-recount`) and its own
`apply*State` fn — do not reuse `data-batch`.
## 1b. NETWORK — how FCP reaches recounter across the firewall (SOLVED)
**Problem:** FCP host `192.168.192.96` cannot initiate connections to
recounter host `192.168.192.93` — .93's firewall (ufw active) drops NEW
inbound connections (100% ping loss .96→.93, while .93→.96 works fine).
**Solution — reverse SSH tunnel (working, E2E-tested 2026-10-20):**
- `.93` dials OUT to `.96` (`ssh -R 127.0.0.1:9050:127.0.0.1:9050`) —
outbound from .93 is allowed.
- `.96` then serves recounter on **its own loopback** → FCP calls
`RECOUNTER_URL=http://127.0.0.1:9050` — literally "recounter at .96's
IP", counting on .93 hardware.
- Scripts: `start_tunnel.sh` / `stop_tunnel.sh` (auto-started by
`start_dashboard.sh`, stopped by `stop_dashboard.sh`; `SKIP_TUNNEL=1`
to disable). PID/log: `/tmp/recounter_tunnel.pid` / `.log`.
- SSH key: .93's `~/.ssh/id_ed25519` is already installed in
`.96:~/.ssh/authorized_keys` (password auth to .96 works too —
`jetson/jetson`, same default FCP's `deploy_to_jetson.py` uses).
- Health check from FCP side:
`curl -m 5 -o /dev/null -w '%{http_code}' http://127.0.0.1:9050/api/models`
→ expect `200`.
- **If tunnel is down**, recounter API is only reachable from .93 itself.
FCP will get connection-refused → show "recounter tidak terhubung" and
retry, not an OCR-style error.
## 2. Environment / URLs (given by operator)
```
@@ -94,7 +119,7 @@ MOTIONEYE_CAMERA_ID=2
Suggested new FCP env (`.env`, read like the other motioneye vars):
```
RECOUNTER_URL=http://localhost:9050 # http://192.168.192.96:9050 from other hosts
RECOUNTER_URL=http://127.0.0.1:9050 # via reverse tunnel (see §1b) — NOT .93
RECOUNTER_API_KEY= # must equal recounter's RECOUNTER_API_KEY
```
+6
View File
@@ -13,3 +13,9 @@ nohup python app.py > "$LOG" 2>&1 &
sleep 1
echo "Started. PID $(pgrep -f 'python app.py'). Log: $LOG"
echo "Open http://localhost:${WEB_PORT:-9050}"
# Best-effort reverse tunnel so FCP (.96) can reach us at 127.0.0.1:9050
# (see start_tunnel.sh header for the why). Dashboard is up either way.
if [ -x ./start_tunnel.sh ] && [ "${SKIP_TUNNEL:-0}" != "1" ]; then
./start_tunnel.sh || echo "WARN: tunnel failed (FCP can't reach recounter yet) — see /tmp/recounter_tunnel.log" >&2
fi
+41
View File
@@ -0,0 +1,41 @@
#!/usr/bin/env bash
# Reverse SSH tunnel: publish THIS host's recounter :9050 as 127.0.0.1:9050 on .96.
#
# Why: .96 (FCP) cannot initiate connections to .93 (firewall drops inbound),
# but .93 → .96 outbound works. So .93 dials OUT to .96 and asks sshd there
# to listen on 9050, forwarding back through the tunnel to this host.
# FCP then calls http://127.0.0.1:9050/api/analyze — same IP as .96 — while
# counting runs on .93's hardware.
#
# FCP .env: RECOUNTER_URL=http://127.0.0.1:9050
set -euo pipefail
REMOTE_HOST="${RECOUNTER_TUNNEL_HOST:-192.168.192.96}"
REMOTE_USER="${RECOUNTER_TUNNEL_USER:-jetson}"
REMOTE_PORT="${RECOUNTER_TUNNEL_PORT:-9050}" # listen port on .96
LOCAL_PORT="${WEB_PORT:-9050}" # recounter on this host
PIDFILE=/tmp/recounter_tunnel.pid
pgrep -f "ssh.*-R ${REMOTE_PORT}:127.0.0.1:${LOCAL_PORT}" > /dev/null && {
echo "Tunnel already running (pid $(pgrep -f "ssh.*-R ${REMOTE_PORT}:127.0.0.1:${LOCAL_PORT}" | tr '\n' ' '))"
exit 0
}
nohup ssh -N \
-o ServerAliveInterval=30 \
-o ServerAliveCountMax=3 \
-o ExitOnForwardFailure=yes \
-o StrictHostKeyChecking=accept-new \
-R "127.0.0.1:${REMOTE_PORT}:127.0.0.1:${LOCAL_PORT}" \
"${REMOTE_USER}@${REMOTE_HOST}" \
> /tmp/recounter_tunnel.log 2>&1 &
echo $! > "$PIDFILE"
sleep 1
if kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then
echo "Tunnel up: ${REMOTE_HOST}:${REMOTE_PORT} -> 127.0.0.1:${LOCAL_PORT} (pid $(cat "$PIDFILE"))"
else
echo "Tunnel FAILED — see /tmp/recounter_tunnel.log" >&2
cat /tmp/recounter_tunnel.log >&2
exit 1
fi
+4
View File
@@ -16,3 +16,7 @@ if pgrep -f "python app.py" > /dev/null; then
else
echo "Stopped."
fi
# Tunnel is useless without the web UI behind it (best-effort).
[ -x "$(dirname "$0")/stop_tunnel.sh" ] && "$(dirname "$0")/stop_tunnel.sh" || true
exit 0
Executable
+10
View File
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
# Stop the reverse SSH tunnel started by start_tunnel.sh.
set -euo pipefail
PIDS=$(pgrep -f "ssh.*-R .*127.0.0.1:9050" || true)
if [ -z "$PIDS" ]; then
echo "No tunnel running."
exit 0
fi
kill $PIDS
echo "Stopped tunnel (pid $PIDS)."