forge
Explore
Log in

Don't you dare go Hollow

πŸ”Œ host networking so the proxy reaches the backend on loopback

Browse files at this commit

f643e6af0a1b2b2993e7b35970a6082f32f369f3Ahmet Kaan GümüşParent: 8970f61
  • fix
    • πŸ”Œ host networking so the proxy reaches the backend on loopback
Show full message
fix: πŸ”Œ host networking so the proxy reaches the backend on loopback
- run the frontend with network_mode: host: a bridge network resolves
  127.0.0.1 inside the container, so BACKEND_ORIGIN=http://127.0.0.1:7656
  (the backend is published on the host loopback) refused every connection
- drop the service ports: mapping and the environment: block β€” under host
  networking the app binds the host interface directly, and every knob now
  arrives through env_file with nothing shadowing it
- server.ts binds HOST (default 0.0.0.0): a server sets HOST=127.0.0.1 so the
  app stays on loopback and the reverse proxy is the only door
- docs: env example now ships BACKEND_ORIGIN/HOST/PORT for a co-located stack
README.mdmodified+2 -2
@@ -63,9 +63,9 @@ podman compose logs -f frontend
6363 podman compose down # stop
6464 ```
6565
66-The `Containerfile` is a two-stage build: `bun install --frozen-lockfile` plus `bun run build` produce `dist/`, and `oven/bun:1-slim` serves it with `server.ts` β€” which also reverse-proxies `/api/*` (plain HTTP and the exam-room WebSocket) to `BACKEND_ORIGIN`. No nginx, no node_modules at runtime, no state: one container and no volumes. The port is published on loopback only β€” `127.0.0.1:<PORT>:<PORT>`, 5173 by default β€” so a server deployment terminates TLS in a reverse proxy in front of it.
66+The `Containerfile` is a two-stage build: `bun install --frozen-lockfile` plus `bun run build` produce `dist/`, and `oven/bun:1-slim` serves it with `server.ts` β€” which also reverse-proxies `/api/*` (plain HTTP and the exam-room WebSocket) to `BACKEND_ORIGIN`. No nginx, no node_modules at runtime, no state: one container and no volumes. It runs with `network_mode: host`, so it shares the host's loopback and reaches a backend published on `127.0.0.1:7656` exactly as a host process would (a bridge network would resolve `127.0.0.1` to the container itself). Host networking means no port mapping: the app binds the host's interface directly, so set `HOST=127.0.0.1` and let the reverse proxy on the same loopback be the only door.
6767
68-**Knobs live in two places and are never mixed.** Locally the `${VAR:-default}` interpolations in `compose.yaml` are enough: the `up` above works with zero extra files. On a server the operator copies `deploy/hezarfen_frontend.env.example` to `$HOME/hezarfen_frontend/hezarfen_frontend.env`, `chmod 0600` it and edits it β€” nothing automated creates, overwrites or uploads that file. The service declares it as `env_file:`, so **every key it carries reaches the container with no extra flag**; `BACKEND_ORIGIN` (the proxy target) is one of them, and any further key is passed through untouched. Only `PORT` is also interpolated by compose β€” it has to be, since the published port and the container listener come from one value β€” so give it `--env-file` if you change it (`podman compose --env-file hezarfen_frontend.env up -d --build`); the systemd unit and the deploy always do.
68+**Knobs live in two places and are never mixed.** On a laptop nothing is required: with no env file the app falls back to its own defaults (`BACKEND_ORIGIN=https://hezarfen.dizey.sh`, `HOST=0.0.0.0`, `PORT=5173`), so the `up` above works with zero extra files. On a server the operator copies `deploy/hezarfen_frontend.env.example` to `$HOME/hezarfen_frontend/hezarfen_frontend.env`, `chmod 0600` it and edits it β€” nothing automated creates, overwrites or uploads that file. The service declares it as `env_file:`, so **every key it carries reaches the container with no extra flag**; nothing else is interpolated from it, and the only compose-level variable is `HEZARFEN_TAG`, which the deploy owns in `stack.env`.
6969
7070 Environment values are read when the container **starts**: after editing the file, recreate (`podman compose up -d`, or `systemctl --user restart hezarfen_frontend_compose`). `podman restart` keeps the old environment.
7171
compose.yamlmodified+11 -12
@@ -30,22 +30,21 @@ services:
3030 # latest local build on a laptop.
3131 image: localhost/hezarfen_frontend:${HEZARFEN_TAG:-latest}
3232 container_name: hezarfen_frontend
33- ports:
34- # Loopback only: terminate TLS on a reverse proxy in front of this. Host
35- # and container port both follow PORT, so the publish can never drift
36- # from what the server listens on.
37- - "127.0.0.1:${PORT:-5173}:${PORT:-5173}"
33+ # Host networking, on purpose: the process then shares the host's loopback,
34+ # so BACKEND_ORIGIN=http://127.0.0.1:7656 (the backend's published loopback
35+ # port) is reachable from inside the container exactly as it would be from
36+ # a host process β€” a bridge network would resolve 127.0.0.1 to the
37+ # container itself and refuse the connection.
38+ #
39+ # There is no `ports:` mapping under host networking: the app binds the
40+ # host's interface directly. Keep it off the LAN with HOST=127.0.0.1 in
41+ # hezarfen_frontend.env (server.ts binds what HOST says, 0.0.0.0 by
42+ # default), leaving the reverse proxy on the same loopback as the only door.
43+ network_mode: host
3844 env_file:
3945 # Written once by the operator on the server (0600); absent on a laptop.
4046 - path: hezarfen_frontend.env
4147 required: false
42- environment:
43- # Listen port, read from hezarfen_frontend.env (passed as --env-file by
44- # the systemd unit and the deploy). server.ts reads PORT, so this one
45- # value moves the container, the publish above and the healthcheck
46- # together β€” and keeping it here is what stops the publish and the
47- # listener from drifting when no --env-file is given.
48- PORT: "${PORT:-5173}"
4948 restart: unless-stopped
5049 healthcheck:
5150 # Bun-based: the slim runtime image ships no curl. Reads the container's
deploy/hezarfen_frontend.env.examplemodified+25 -26
@@ -8,41 +8,40 @@
88 #
99 # Nothing automated creates, overwrites or uploads this file β€” the operator
1010 # owns it; compose only reads it (`podman compose --env-file`, plus `env_file`
11-# into the frontend container). Plain KEY=value lines: no quotes, no `export`.
11+# into the frontend container). Plain KEY=value lines: no quotes, no `export`,
12+# no trailing comment.
1213 #
13-# One file, two paths into the stack:
14-# * `env_file:` passes this file into the container as-is β€” keys the compose
15-# file does not list under `environment:` (BACKEND_ORIGIN among them) apply
16-# with or without `--env-file`, and are read when the container STARTS.
17-# Change a value, then recreate: `podman compose up -d` (or
18-# `systemctl --user restart hezarfen_frontend_compose`). `podman restart`
19-# keeps the old environment.
20-# * `--env-file` additionally feeds compose's own ${VAR:-default}
21-# interpolation. Only PORT needs that (the publish and the container
22-# listener are derived from one value); compose.yaml passes no other knob
23-# through it, so nothing here is shadowed by a compose default.
14+# The service declares this file as `env_file:`, so every key below reaches the
15+# container with no extra flag and nothing is shadowed by a compose default.
16+# Values are read when the container STARTS: after editing, recreate
17+# (`podman compose up -d`, or `systemctl --user restart
18+# hezarfen_frontend_compose`). `podman restart` keeps the old environment.
2419 #
25-# The app reads exactly two keys today (server.ts): BACKEND_ORIGIN and PORT.
26-# Any other key here is carried into the container untouched, unread.
20+# The app reads three keys today (server.ts): BACKEND_ORIGIN, PORT, HOST.
2721
2822 # ── Backend origin ───────────────────────────────────────────────────────────
2923 # Where the container proxies /api/* β€” plain HTTP and the exam-room WebSocket
3024 # alike. The browser only ever talks to this frontend, so session cookies stay
31-# same-origin. Must be reachable FROM INSIDE the container.
25+# same-origin.
3226 #
33-# The default below is the public origin and always works. A
34-# container-to-container origin such as http://hezarfen_backend:7656 is NOT
35-# wired today: neither compose stack declares a shared podman network, and the
36-# backend publishes on host loopback only (127.0.0.1:7656).
37-BACKEND_ORIGIN=https://hezarfen.dizey.sh
27+# The frontend runs with host networking (`network_mode: host` in compose.yaml),
28+# so it shares the host's loopback and this is simply where the backend
29+# listens on this machine: the backend's compose publishes 127.0.0.1:7656.
30+# A bridge network would resolve 127.0.0.1 to the container itself and refuse
31+# the connection.
32+BACKEND_ORIGIN=http://127.0.0.1:7656
33+
34+# ── Listen address ───────────────────────────────────────────────────────────
35+# HOST=127.0.0.1 keeps the app on loopback: with host networking there is no
36+# port mapping in front of it, so 0.0.0.0 would put it on every interface and
37+# bypass the reverse proxy. The proxy (Ferron et al.) proxies to
38+# http://127.0.0.1:<PORT> from this same machine.
39+HOST=127.0.0.1
3840
3941 # ── Listen port ──────────────────────────────────────────────────────────────
40-# One value moves three things: what server.ts listens on, the host-loopback
41-# publish (127.0.0.1:<PORT>:<PORT> in compose.yaml) and the deploy's health
42-# gate, which reads this line back out of this file. Unlike BACKEND_ORIGIN this
43-# one is interpolated by compose, so it needs `--env-file` to take effect (the
44-# systemd unit and the deploy both pass it). Leave it at 5173 unless something
45-# else already owns that port on the server.
42+# The port the app binds (and the port the reverse proxy must target). The
43+# deploy's health gate reads this line back out of this file, so the two cannot
44+# drift.
4645 PORT=5173
4746
4847 # ── Image tag ────────────────────────────────────────────────────────────────
deploy/hezarfen_frontend_compose.servicemodified+7 -5
@@ -12,11 +12,13 @@
1212 #
1313 # Keys read from hezarfen_frontend.env (full docs in
1414 # deploy/hezarfen_frontend.env.example):
15-# BACKEND_ORIGIN the origin /api/* is proxied to; reaches
16-# the container through env_file, so it needs
17-# no flag β€” but only on (re)start
18-# PORT listen port + published port (interpolated,
19-# hence the --env-file below), default 5173
15+# BACKEND_ORIGIN the origin /api/* is proxied to; on this
16+# host that is http://127.0.0.1:7656, which
17+# the service can reach because compose runs
18+# it with host networking
19+# HOST bind address; 127.0.0.1 keeps the app on
20+# loopback behind the reverse proxy
21+# PORT listen port, default 5173
2022 #
2123 # Env-only restart (re-runs `compose up -d`, recreating the container when its
2224 # environment changed):
server.tsmodified+6 -2
@@ -7,6 +7,10 @@
77 // container's runtime after `vite build`.
88
99 const PORT = Number(Bun.env.PORT ?? 5173);
10+// Interface to bind. 0.0.0.0 is what a published container port needs; under
11+// host networking the process sits directly on the host, so a server sets
12+// HOST=127.0.0.1 and only the reverse proxy beside it can reach the app.
13+const HOST = Bun.env.HOST ?? "0.0.0.0";
1014 // Where the backend lives on the compose network. Override in compose.
1115 const BACKEND_ORIGIN = Bun.env.BACKEND_ORIGIN ?? "https://hezarfen.dizey.sh";
1216 const BACKEND_HTTP = BACKEND_ORIGIN.replace(/\/+$/, "");
@@ -70,7 +74,7 @@ async function proxyHttp(req: Request, url: URL): Promise<Response> {
7074
7175 const server = Bun.serve<Bridge, {}>({
7276 port: PORT,
73- hostname: "0.0.0.0",
77+ hostname: HOST,
7478 // Long-running exam sockets must not be reaped by an idle timeout; the
7579 // backend sends its own periodic state ticks but a quiet client can still
7680 // sit for minutes between frames.
@@ -135,4 +139,4 @@ const server = Bun.serve<Bridge, {}>({
135139 },
136140 });
137141
138-console.log(`serving dist/ on :${server.port}, proxying /api -> ${BACKEND_HTTP}`);
142+console.log(`serving dist/ on ${HOST}:${server.port}, proxying /api -> ${BACKEND_HTTP}`);