diff --git a/README.md b/README.md index 12a0e2d..eefd8fc 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,27 @@ HOST=root@ ./deploy.sh # or put HOST=… in .deploy.env (gitignored) On the server: `/opt/tools/{compose.yml,app/,data/}`, container `tools`, listening on `127.0.0.1:8090` only, so put a reverse proxy or tunnel connector on the same host in front of it. -It trusts `X-Forwarded-For` from loopback only, which is what makes the per-IP paste limit work. +It trusts `X-Forwarded-For` only from `FORWARDED_ALLOW_IPS` (default loopback), which is what makes +the per-IP paste limit work. + +For a proxy that runs in Docker instead (reaching the app over a Docker network), add a +`compose.override.yml` next to `compose.yml` on the server; `deploy.sh` leaves it alone: + +```yaml +services: + tools: + network_mode: !reset null + networks: [proxy] + environment: + HOST: 0.0.0.0 # inside the container's own network namespace + FORWARDED_ALLOW_IPS: 172.16.0.0/12 # the proxy's Docker network +networks: + proxy: + external: true + name: +``` + +Each server keeps its own pastes; instances don't share data. ## Pastebin limits diff --git a/app/Dockerfile b/app/Dockerfile index 7d4d3f8..5dd5c25 100644 --- a/app/Dockerfile +++ b/app/Dockerfile @@ -8,5 +8,6 @@ COPY static ./static RUN useradd --system --uid 10001 tools && mkdir -p /data && chown tools /data USER tools ENV DATA_DIR=/data PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 -# HOST/PORT come from compose; uvicorn trusts X-Forwarded-* only from loopback (the local proxy/tunnel). -CMD ["sh", "-c", "exec uvicorn main:app --host ${HOST:-127.0.0.1} --port ${PORT:-8090} --proxy-headers --forwarded-allow-ips 127.0.0.1 --no-server-header"] +# HOST/PORT come from compose. uvicorn trusts X-Forwarded-For only from FORWARDED_ALLOW_IPS +# (IPs or CIDRs; default loopback), i.e. only from the reverse proxy in front of it. +CMD ["sh", "-c", "exec uvicorn main:app --host ${HOST:-127.0.0.1} --port ${PORT:-8090} --proxy-headers --forwarded-allow-ips \"${FORWARDED_ALLOW_IPS:-127.0.0.1}\" --no-server-header"] diff --git a/app/tools/paste.py b/app/tools/paste.py index 1e9bb6b..9dd7b0b 100644 --- a/app/tools/paste.py +++ b/app/tools/paste.py @@ -90,8 +90,8 @@ async def shutdown(): def client_ip(request: Request) -> str: - # uvicorn runs with --proxy-headers --forwarded-allow-ips 127.0.0.1, so behind a - # reverse proxy that connects over loopback this is already the visitor's address. + # uvicorn runs with --proxy-headers and trusts X-Forwarded-For only from the proxy + # (FORWARDED_ALLOW_IPS), so this is already the visitor's address. return request.client.host if request.client else "" diff --git a/deploy.sh b/deploy.sh index 94aba83..ac2a9ab 100755 --- a/deploy.sh +++ b/deploy.sh @@ -1,13 +1,21 @@ #!/usr/bin/env bash # Deploy this checkout (including uncommitted changes) over SSH: # HOST=root@ ./deploy.sh -# or put HOST=root@ in .deploy.env (gitignored; keep server details out of the repo). +# ./deploy.sh # HOST from .deploy.env +# ./deploy.sh # HOST_ from .deploy.env (several servers) +# .deploy.env is gitignored: keep server details out of the repo. +# A compose.override.yml on the server (e.g. different networking) is left alone. # Copies app/ and compose.yml to /opt/tools, rebuilds and restarts the container, # then waits for it to report healthy. Paste data in /opt/tools/data is kept. set -euo pipefail cd "$(dirname "$0")" -[ -z "${HOST:-}" ] && [ -f .deploy.env ] && . ./.deploy.env -[ -n "${HOST:-}" ] || { echo "usage: HOST=root@ $0 (or set HOST in .deploy.env)" >&2; exit 1; } +[ -f .deploy.env ] && { CLI_HOST=${HOST:-}; . ./.deploy.env; HOST=${CLI_HOST:-${HOST:-}}; } +if [ -n "${1:-}" ]; then + var="HOST_$(printf %s "$1" | tr '[:lower:]-' '[:upper:]_')" + HOST=${!var:-} + [ -n "$HOST" ] || { echo "no $var in .deploy.env" >&2; exit 1; } +fi +[ -n "${HOST:-}" ] || { echo "usage: HOST=root@ $0 (or set HOST / HOST_ in .deploy.env)" >&2; exit 1; } node --test tests/ >/dev/null || { echo "tests failed; not deploying" >&2; exit 1; } tar -czf - --exclude=__pycache__ --exclude=data app compose.yml | ssh "$HOST" 'set -e; mkdir -p /opt/tools && rm -rf /opt/tools/app && tar --no-same-owner -xzf - -C /opt/tools' @@ -18,7 +26,7 @@ mkdir -p data && chown 10001:10001 data && chmod 700 data docker compose up -d --build --remove-orphans for i in $(seq 1 30); do s=$(docker inspect -f '{{.State.Health.Status}}' tools 2>/dev/null || echo none) - [ "$s" = healthy ] && { echo "tools is healthy on 127.0.0.1:8090"; docker image prune -f >/dev/null; exit 0; } + [ "$s" = healthy ] && { echo "tools is healthy"; docker image prune -f >/dev/null; exit 0; } sleep 2 done echo "tools did not become healthy:" >&2; docker logs --tail 50 tools >&2; exit 1