deploy: the gateway always runs with the page, a LAN mode, docker-compose v1

- deploy/nginx starts the GPRS gateway without the `gprs` profile.
- docker-compose.lan.yml + lan-tls.sh: the page and the gateway without the
  front, at https://<host>:8443 with a self-signed certificate kept in a
  volume (browsers need a secure context for cross-origin isolation).
- The gateway's source is the deploy/gprs-gw/bts-emulator submodule (branch
  gprs-gw, 55c911a) inside its build context, replacing the named build
  context and gprs-gw.sha256.
- Everything works with legacy docker-compose v1 and the classic builder:
  no additional_contexts, !reset, nested ${A:-${B}}, ADD --checksum or
  COPY --chmod. GPRS_GW_ORIGINS falls back to https://$VIRTUAL_HOST in the
  gateway's shell command instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Xda8d7rfJjkFSmKVQtcBGh
This commit is contained in:
Konstantin Pereiaslov 2026-11-15 18:03:43 -06:00
parent 6a9d12d8b9
commit 1db4b50730
11 changed files with 205 additions and 63 deletions

4
.gitmodules vendored
View file

@ -10,3 +10,7 @@
path = bsp
url = https://github.com/Azq2/pmb887x-dev
branch = master
[submodule "deploy/gprs-gw/bts-emulator"]
path = deploy/gprs-gw/bts-emulator
url = https://github.com/siemens-mobile-hacks/bts-emulator
branch = gprs-gw

View file

@ -1,22 +1,16 @@
# syntax=docker/dockerfile:1
#
# bts-emulator's GPRS gateway (gprs-gw/server.mjs + the slirp-pipe helper)
# on its own, for the emulator page's /gprs WebSocket. See README.md.
#
# The gateway's two source files come from the named build context `bts`, a
# bts-emulator checkout of branch gprs-gw, and must match gprs-gw.sha256
# (commit 55c911aba7be4d00a7e6ada36e73a55e21a2e931):
# The gateway's two source files come from the bts-emulator submodule next to
# this file, pinned to a commit of its branch gprs-gw:
#
# docker build --build-context bts=../bts-emulator -t pmb887x-gprs-gw deploy/gprs-gw
# git submodule update --init deploy/gprs-gw/bts-emulator
# docker build -t pmb887x-gprs-gw deploy/gprs-gw
#
# deploy/nginx/docker-compose.yml passes it as additional_contexts.
# No BuildKit-only syntax, so docker-compose v1's classic builder builds it.
FROM node:24.22.1-trixie-slim@sha256:89c5244bc22a9bc3287d89ebc7e49b9cb5bb84cd40e6175ac3421d5a9f68b95a AS base
# Overridden by --build-context bts=...; without one, the COPY below fails
# instead of pulling some image called "bts".
FROM scratch AS bts
# slirp-pipe needs libslirp >= 4.9 (slirp_pollfds_fill_socket); Debian 13
# has 4.8.0, so it is linked statically against an upstream release.
FROM base AS build
@ -25,19 +19,18 @@ RUN apt-get update \
gcc=4:14.2.0-1 libc6-dev=2.41-12+deb13u4 pkg-config=1.8.1-4 \
meson=1.7.0-1 ninja-build=1.12.1-1 libglib2.0-dev=2.84.4-3~deb13u5 xz-utils=5.8.1-1+deb13u2 \
&& rm -rf /var/lib/apt/lists/*
ADD --checksum=sha256:d76df61220a55c429f8637a550168d8d4cc66e4606d64c178ff5929c004cbece \
https://gitlab.freedesktop.org/-/project/2767/uploads/128da958c5bca62a4b7eae695abff645/libslirp-4.9.7.tar.xz /src/
RUN tar -xJf /src/libslirp-4.9.7.tar.xz -C /src \
ADD https://gitlab.freedesktop.org/-/project/2767/uploads/128da958c5bca62a4b7eae695abff645/libslirp-4.9.7.tar.xz /src/
RUN echo "d76df61220a55c429f8637a550168d8d4cc66e4606d64c178ff5929c004cbece /src/libslirp-4.9.7.tar.xz" | sha256sum -c \
&& tar -xJf /src/libslirp-4.9.7.tar.xz -C /src \
&& meson setup /src/build /src/libslirp-4.9.7 --buildtype=release -Ddefault_library=static \
--prefix=/usr/local --libdir=lib \
&& ninja -C /src/build install
WORKDIR /build
COPY gprs-gw.sha256 package.json package-lock.json ./
COPY --from=bts gprs-gw/server.mjs gprs-gw/slirp-pipe.c ./
COPY package.json package-lock.json ./
COPY bts-emulator/gprs-gw/server.mjs bts-emulator/gprs-gw/slirp-pipe.c ./
# pkg-config's flags are meant to split into words
# hadolint ignore=SC2046
RUN sha256sum -c gprs-gw.sha256 \
&& gcc -O2 -Wall -o slirp-pipe slirp-pipe.c $(pkg-config --cflags --libs slirp) \
RUN gcc -O2 -Wall -o slirp-pipe slirp-pipe.c $(pkg-config --cflags --libs slirp) \
&& npm ci --omit=dev --ignore-scripts --no-audit --no-fund
FROM base

View file

@ -14,29 +14,36 @@ stack, next to the page's nginx (`deploy/nginx`), which passes `/gprs` to it.
| file | what it is |
|---|---|
| `Dockerfile` | `node:24.22.1-trixie-slim` (pinned by digest), libslirp 4.9.7 built from its release tarball (sha256-checked), `slirp-pipe` linked against it, `ws` from `package-lock.json`; runs as `node` (uid 1000) |
| `gprs-gw.sha256` | the gateway's two source files at bts-emulator commit `55c911aba7be4d00a7e6ada36e73a55e21a2e931`; the build fails on any other content |
| `bts-emulator/` | submodule, branch `gprs-gw`; the image uses only `gprs-gw/server.mjs` and `gprs-gw/slirp-pipe.c` |
| `package.json`, `package-lock.json` | `ws` 8.21.3, the version the repository's own gateway checks run with |
| `healthcheck.mjs` | `HEALTHCHECK`: a plain GET, which the gateway answers with 426 (not logged, not counted) |
| `../nginx/docker-compose.yml` | the `pmb887x-gprs-gw` service (profile `gprs`) |
| `../nginx/docker-compose.yml` | the `pmb887x-gprs-gw` service |
| `../nginx/site.conf` | `location = /gprs`, the WebSocket proxy |
## Where the source comes from
The gateway lives on branch `gprs-gw` of a bts-emulator checkout
(`55c911a`, three commits on top of `1a124e7`). That branch is not on
GitHub yet: upstream `main`, and the `pmb887x-emu/bts` submodule that pins
it (`01955ac`), have no `gprs-gw/`. So the image takes the two files from a
named build context, `bts`, and checks them against `gprs-gw.sha256`. Nothing
else from the checkout is used, so its other files and its checked-out
branch do not matter as long as `gprs-gw/` matches.
The gateway lives on branch `gprs-gw` of bts-emulator (`55c911a`, three
commits on top of `1a124e7`); upstream `main`, and the `pmb887x-emu/bts`
submodule that pins it (`01955ac`), have no `gprs-gw/`. `bts-emulator/` here
is a submodule pinned to a commit of that branch, inside the image's build
context, so a plain build (compose v1 or v2, or `docker build`) finds the
two files without a named build context. Nothing else from the submodule is
used.
- Compose: `BTS_EMULATOR` (in `deploy/nginx/.env`), default
`../../../bts-emulator`, a checkout next to this repository.
- Once the branch is pushed, a pinned git URL works the same way:
`BTS_EMULATOR=https://github.com/siemens-mobile-hacks/bts-emulator.git#55c911aba7be4d00a7e6ada36e73a55e21a2e931`.
- When the gateway changes, update `gprs-gw.sha256` (and the commit named
in it and in the `Dockerfile`) from the new commit:
`git -C ../bts-emulator show <commit>:gprs-gw/server.mjs | sha256sum`.
```sh
git submodule update --init deploy/gprs-gw/bts-emulator
```
Without it the build stops at `COPY bts-emulator/gprs-gw/...`.
When the gateway changes, move the submodule to the new commit and commit
that:
```sh
git -C deploy/gprs-gw/bts-emulator fetch origin gprs-gw
git -C deploy/gprs-gw/bts-emulator checkout <commit>
git add deploy/gprs-gw/bts-emulator
```
`slirp-pipe.c` needs libslirp 4.9 or newer (`slirp_pollfds_fill_socket`,
`slirp_os_socket`). Debian 13 ships 4.8.0, which fails to compile it, so the
@ -59,11 +66,10 @@ in `deploy/nginx/.env`:
VIRTUAL_HOST=emu.example.org
LETSENCRYPT_HOST=emu.example.org
LETSENCRYPT_EMAIL=you@example.org
COMPOSE_PROFILES=gprs
# BTS_EMULATOR=/path/to/bts-emulator # if not next to this repository
```
```sh
git submodule update --init deploy/gprs-gw/bts-emulator
cd deploy/nginx
docker compose up -d --build
docker compose ps # pmb887x-gprs-gw should become "healthy"
@ -72,16 +78,17 @@ docker compose logs -f pmb887x-gprs-gw
The gateway joins only the stack's own `gprs` network, shared with
`pmb887x-web`; it publishes no port and is not on the `nginx-proxy`
network. Without the `gprs` profile, the stack is the static page as before,
and `/gprs` answers 502: the phone has no Internet, nothing else changes.
nginx looks the gateway's name up per request, so the order containers
start in does not matter.
network. The stack always builds and starts it, so it needs the
submodule. nginx looks the gateway's name up per request, so the
order containers start in does not matter, and while the gateway is down
`/gprs` answers 502: the phone has no Internet, the page still works.
On its own, for `serve.mjs` on the same host (it passes `/gprs` to
`GPRS_GW`, default `127.0.0.1:5751`, adding one `X-Forwarded-For` hop):
```sh
docker build --build-context bts=../bts-emulator -t pmb887x-gprs-gw deploy/gprs-gw
git submodule update --init deploy/gprs-gw/bts-emulator
docker build -t pmb887x-gprs-gw deploy/gprs-gw
docker run -d --name pmb887x-gprs-gw --init --read-only --cap-drop ALL \
--security-opt no-new-privileges -p 127.0.0.1:5751:5751 \
-v pmb887x-gprs-gw-state:/var/lib/gprs-gw \
@ -97,7 +104,7 @@ All settings are environment variables. In compose they come from
| variable | default | meaning |
|---|---|---|
| `GPRS_GW_LISTEN` | `0.0.0.0:5751` | listen address in the container (nginx expects port 5751) |
| `GPRS_GW_ORIGINS` | compose: `https://$VIRTUAL_HOST`; gateway: empty = any | page origins allowed to connect, comma separated; others are closed with 4009, which the page takes as final. Set it explicitly when `VIRTUAL_HOST` lists several names |
| `GPRS_GW_ORIGINS` | compose: `https://$VIRTUAL_HOST` (`GPRS_GW_PAGE_ORIGIN`), LAN mode: empty; gateway: empty = any | page origins allowed to connect, comma separated; others are closed with 4009, which the page takes as final. Set it explicitly when `VIRTUAL_HOST` lists several names |
| `GPRS_GW_TRUSTED_PROXIES` | compose: `2`; gateway: `0` | proxies in front that each append to `X-Forwarded-For` (below) |
| `GPRS_GW_PER_IP` | `2` | connections at once per client address (IPv6: per /64) |
| `GPRS_GW_MAX_SESSIONS` | `100` | connections at once in total (raise `pids_limit` and `mem_limit` with it) |

@ -0,0 +1 @@
Subproject commit 55c911aba7be4d00a7e6ada36e73a55e21a2e931

View file

@ -1,2 +0,0 @@
6d826a99b74e92abb7f111e020da1a3d45c4cedb2bcf69daad1528b4bc7a12ab server.mjs
0a26d262cb25f93c1cb9f5b22fc56c97f17b3c78586fa80333714df48c2eca64 slirp-pipe.c

View file

@ -10,6 +10,11 @@
# to the daemon.
FROM nginx:stable-alpine
# openssl and lan-tls.sh: the self-signed https of docker-compose.lan.yml
COPY deploy/nginx/lan-tls.sh /docker-entrypoint.d/40-lan-tls.sh
RUN apk add --no-cache openssl \
&& chmod 755 /docker-entrypoint.d/40-lan-tls.sh \
&& mkdir -p /etc/nginx/lan-tls
COPY deploy/nginx/site.conf /etc/nginx/conf.d/default.conf
COPY dist/ /usr/share/nginx/html/

View file

@ -1,12 +1,13 @@
# Deploying the emulator page behind perk11/nginx-proxy
Three moving parts:
The moving parts:
| piece | what it is |
|---|---|
| `scripts/bundle-dist.sh` | assembles the self-contained bundle in `<repo>/dist` (gitignored) |
| `Dockerfile` + `site.conf` | `nginx:stable-alpine` image serving that bundle with the same headers/contract as `serve.mjs` |
| `docker-compose.yml` | runs it on the external `nginx-proxy` network with `VIRTUAL_HOST` env, so the front (jwilder/nginx-proxy + letsencrypt companion) picks it up; with profile `gprs`, also the GPRS gateway (`../gprs-gw`) |
| `docker-compose.yml` | runs it on the external `nginx-proxy` network with `VIRTUAL_HOST` env, so the front (jwilder/nginx-proxy + letsencrypt companion) picks it up, and the GPRS gateway (`../gprs-gw`) |
| `docker-compose.lan.yml` + `lan-tls.sh` | LAN mode instead of the front: https with a self-signed certificate on a published port (section 4) |
## 1. Bundle
@ -46,9 +47,11 @@ docker run -d --name nginx-letsencrypt --restart=always \
--volumes-from nginx-proxy jrcs/letsencrypt-nginx-proxy-companion
```
Then, from `deploy/nginx/` here:
Then check out the GPRS gateway's submodule once (`../gprs-gw/README.md`,
"Where the source comes from") and, from `deploy/nginx/` here:
```bash
git submodule update --init ../gprs-gw/bts-emulator
VIRTUAL_HOST=emu.example.org \
LETSENCRYPT_HOST=emu.example.org \
LETSENCRYPT_EMAIL=you@example.org \
@ -59,6 +62,11 @@ LETSENCRYPT_EMAIL=you@example.org \
terminated at the front; http→https redirects appear automatically once
the cert is issued.
Both compose files and both Dockerfiles also work with the legacy
`docker-compose` v1 (1.27 or newer, for files without `version:`) and its
classic builder: no named build contexts, no `!reset`, no nested
`${A:-${B}}`, no BuildKit-only Dockerfile flags. Keep it that way.
### What the front is
perk11/nginx-proxy is not a fork of nginx-proxy's code. It is a compose
@ -124,16 +132,16 @@ template differs only where noted.
The page relays the phone's GPRS to `wss://<host>/gprs`, which `site.conf`
passes to bts-emulator's GPRS gateway in the `pmb887x-gprs-gw` container
(`deploy/gprs-gw`). It is the compose profile `gprs`: add
`COMPOSE_PROFILES=gprs` to `.env`, with a bts-emulator checkout of branch
`gprs-gw` next to this repository (or at `BTS_EMULATOR`), and
`docker compose up -d --build`. `deploy/gprs-gw/README.md` has its settings
(origins, trusted proxies, limits), networking and security notes.
(`deploy/gprs-gw`), which `docker compose up -d --build` builds and starts
with the page, from the `deploy/gprs-gw/bts-emulator` submodule (branch
`gprs-gw`). `deploy/gprs-gw/README.md`
has its settings (origins, trusted proxies, limits), networking and security
notes. While the gateway is down the emulator runs as before, only without
Internet on the phone (`/gprs` answers 502).
The alternative is bts-emulator's full stack with its `compose.web.yaml` and
the same host name: the front then routes `/gprs` there (`VIRTUAL_PATH`),
past this container. Use one or the other. Without either the emulator runs
as before, only without Internet on the phone (`/gprs` answers 502).
Do not also run bts-emulator's full stack with its `compose.web.yaml` for the
same host name: the front would then route `/gprs` there (`VIRTUAL_PATH`),
past this container.
## 3. Standalone (no proxy mesh)
@ -146,7 +154,52 @@ docker run --rm -p 8080:80 pmb887x-web # http://localhost:8080
Same caveat as `serve.mjs`: browsers ignore COOP/COEP on plain http for
anything but localhost, so cross-origin isolation (and therefore the
emulator) only works via `http://localhost…` or via the front's TLS —
not over `http://<lan-ip>:8080`.
not over `http://<lan-ip>:8080`. For other machines use LAN mode (section 4).
## 4. LAN (no front, self-signed https)
The page and the GPRS gateway for the machines on your network, without
the front, a domain or Let's Encrypt:
```bash
scripts/bundle-dist.sh
git submodule update --init deploy/gprs-gw/bts-emulator
cd deploy/nginx
docker compose -f docker-compose.yml -f docker-compose.lan.yml up -d --build
# open https://<lan-ip>:8443 and accept the certificate warning once
```
To make it the default for this directory, put
`COMPOSE_FILE=docker-compose.yml:docker-compose.lan.yml` in `.env`; then a
plain `docker compose up -d --build` uses it.
`docker-compose.lan.yml` on top of the normal stack:
- publishes `pmb887x-web`'s port 443 on `LAN_ADDRESS:LAN_HTTPS_PORT`
(default `0.0.0.0:8443`), and nothing on plain http;
- sets `LAN_TLS=1`, so `lan-tls.sh` (run by the image's entrypoint) adds
`listen 443 ssl` to `site.conf` with a self-signed certificate. The
certificate is kept in the `lan-cert` volume and regenerated only when
`LAN_HOSTS` changes, so browsers keep their exception across rebuilds.
`LAN_HOSTS` (comma separated names and addresses, e.g.
`192.168.1.10,emu.lan`) only adds them to the certificate; self-signed, it
needs the browser's exception either way;
- replaces the external `nginx-proxy` network with an ordinary one
(`pmb887x-web-lan`), and
empties `VIRTUAL_HOST` and `LETSENCRYPT_HOST`, so a front running on the
same Docker host ignores the container;
- runs the gateway with `GPRS_GW_TRUSTED_PROXIES=1` (only `pmb887x-web` in
front) and `GPRS_GW_ORIGINS` empty (any origin, since the page's origin
is whatever LAN address it is opened at). Set `GPRS_GW_ORIGINS` in `.env`
to `https://<lan-ip>:8443` to restrict it.
Without `LAN_TLS`, `lan-tls.sh` does nothing and the image is the
front's plain port-80 server as before.
Anyone who can reach the port gets the page and, through the gateway, a
NATed way out to the Internet from this host (`../gprs-gw/README.md`).
Set `LAN_ADDRESS` to one interface's address, or firewall the port, if the
host is on more than the LAN.
## What `site.conf` sends, and why

View file

@ -0,0 +1,37 @@
# LAN mode: the page and the GPRS gateway without the nginx-proxy front, at
# https://<this host>:8443 with a self-signed certificate (README.md, "4. LAN").
#
# docker compose -f docker-compose.yml -f docker-compose.lan.yml up -d --build
#
# or COMPOSE_FILE=docker-compose.yml:docker-compose.lan.yml in .env.
services:
pmb887x-web:
ports:
- ${LAN_ADDRESS:-0.0.0.0}:${LAN_HTTPS_PORT:-8443}:443
environment:
LAN_TLS: "1"
# names and addresses the certificate covers, comma separated
LAN_HOSTS: ${LAN_HOSTS:-}
# keep a running nginx-proxy and letsencrypt companion away from it
VIRTUAL_HOST: ""
LETSENCRYPT_HOST: ""
volumes:
- lan-cert:/etc/nginx/lan-cert
pmb887x-gprs-gw:
environment:
# the page's origin is https://<lan address>:<port>; empty allows any
GPRS_GW_ORIGINS: ${GPRS_GW_ORIGINS:-}
GPRS_GW_PAGE_ORIGIN: ""
# browsers reach the gateway through pmb887x-web only
GPRS_GW_TRUSTED_PROXIES: ${GPRS_GW_TRUSTED_PROXIES:-1}
networks:
# an ordinary network instead of the external nginx-proxy one; named, since
# Compose v2 merges this with the base definition and would keep its name
default:
name: pmb887x-web-lan
external: false
volumes:
lan-cert:

View file

@ -16,8 +16,8 @@
# `docker compose up -d --build`.
#
# The phone's Internet (GPRS gateway at /gprs, ../gprs-gw/README.md) is the
# `gprs` profile: COMPOSE_PROFILES=gprs, plus a bts-emulator checkout of
# branch gprs-gw at BTS_EMULATOR (default ../../../bts-emulator). The front
# pmb887x-gprs-gw service, built from the ../gprs-gw/bts-emulator submodule
# (git submodule update --init deploy/gprs-gw/bts-emulator). The front
# must not close an idle /gprs after nginx's default 60 s: the proxy-timeout
# label below (README.md, "What the front is").
services:
@ -54,11 +54,8 @@ services:
- gprs
pmb887x-gprs-gw:
profiles: [gprs]
build:
context: ../gprs-gw
additional_contexts:
bts: ${BTS_EMULATOR:-../../../bts-emulator}
image: pmb887x-gprs-gw
container_name: pmb887x-gprs-gw
restart: unless-stopped
@ -72,9 +69,13 @@ services:
mem_limit: 1g
expose:
- "5751"
# GPRS_GW_ORIGINS falls back to GPRS_GW_PAGE_ORIGIN here, in the shell:
# docker-compose v1 has no nested ${A:-${B}}.
command: ["sh", "-c", "export GPRS_GW_ORIGINS=\"$${GPRS_GW_ORIGINS:-$$GPRS_GW_PAGE_ORIGIN}\"; exec node /opt/gprs-gw/server.mjs"]
# An empty value leaves the gateway's default (../gprs-gw/README.md).
environment:
GPRS_GW_ORIGINS: ${GPRS_GW_ORIGINS:-https://${VIRTUAL_HOST:-emu.example.org}}
GPRS_GW_ORIGINS: ${GPRS_GW_ORIGINS:-}
GPRS_GW_PAGE_ORIGIN: https://${VIRTUAL_HOST:-emu.example.org}
# nginx-proxy front + pmb887x-web, each appending to X-Forwarded-For
GPRS_GW_TRUSTED_PROXIES: ${GPRS_GW_TRUSTED_PROXIES:-2}
GPRS_GW_PER_IP: ${GPRS_GW_PER_IP:-}

41
deploy/nginx/lan-tls.sh Executable file
View file

@ -0,0 +1,41 @@
#!/bin/sh
# LAN mode (docker-compose.lan.yml sets LAN_TLS=1): https on port 443 with a
# self-signed certificate, so browsers on other machines get a secure context
# and with it cross-origin isolation. The certificate lives on a volume and
# is only regenerated when LAN_HOSTS changes, so browsers keep their exception.
# Run by the nginx image's entrypoint from /docker-entrypoint.d.
set -eu
conf=/etc/nginx/lan-tls/listen.conf
dir=/etc/nginx/lan-cert
if [ "${LAN_TLS:-0}" != 1 ]; then
rm -f "$conf"
exit 0
fi
san="DNS:localhost,IP:127.0.0.1"
for h in $(echo "${LAN_HOSTS:-}" | tr ',' ' '); do
case $h in
*:*) san="$san,IP:$h" ;;
*[!0-9.]*) san="$san,DNS:$h" ;;
*) san="$san,IP:$h" ;;
esac
done
mkdir -p "$dir"
if [ ! -s "$dir/cert.pem" ] || [ ! -s "$dir/key.pem" ] || [ "$(cat "$dir/san" 2>/dev/null)" != "$san" ]; then
echo "$0: generating a self-signed certificate for $san"
openssl req -x509 -newkey rsa:2048 -nodes -days 825 -subj /CN=pmb887x-web \
-addext "subjectAltName=$san" -keyout "$dir/key.pem" -out "$dir/cert.pem" 2>/dev/null
echo "$san" > "$dir/san"
fi
cat > "$conf" <<EOF
listen 443 ssl;
ssl_certificate $dir/cert.pem;
ssl_certificate_key $dir/key.pem;
# the published port differs from 443, so redirects must not name one
absolute_redirect off;
EOF
echo "$0: https on 443, certificate $(openssl x509 -in "$dir/cert.pem" -noout -fingerprint -sha256)"

View file

@ -30,6 +30,8 @@ map $http_upgrade $gprs_connection {
server {
listen 80 default_server;
server_name _; # vhost routing happens at the nginx-proxy
# LAN mode's https listener (lan-tls.sh); no file, no https
include /etc/nginx/lan-tls/*.conf;
root /usr/share/nginx/html;
index index.html;