# Tor client for the last-resort search door (src/dethrottled/tor.py).
#
# C tor from Alpine's package: about 28 MB resident and 8 MB of state
# (measured 25 Nov 2026), in an image of about 53 MB (Debian: 126 MB). Arti, the Rust client,
# has no distribution package or prebuilt binary to pin. Multi-arch: alpine
# and its tor package exist for amd64 and arm64.
FROM alpine:3.22@sha256:49340b346798875b5592e5d420241f2f9cfba5f47e62ebb1446a1b34734e898f
RUN apk add --no-cache tor netcat-openbsd \
    # Group 10001 is the API's: it may read and write the control socket.
    && addgroup -g 10001 dethrottled \
    && adduser -D -H -u 10002 -G dethrottled -s /sbin/nologin dtor \
    && install -d -o dtor -g dethrottled -m 0700 /var/lib/dethrottled-tor \
    && install -d -o dtor -g dethrottled -m 0750 /run/tor-control
COPY docker/tor/torrc /etc/tor/dethrottled.torrc
COPY docker/tor/entrypoint.sh /usr/local/bin/dethrottled-tor
USER dtor
# Bootstrapped (or switched off): the health check never gates other services.
HEALTHCHECK --interval=30s --timeout=5s --start-period=300s --retries=3 \
    CMD [ "$DETHROTTLED_TOR" = "0" ] || printf 'AUTHENTICATE\r\nGETINFO status/bootstrap-phase\r\nQUIT\r\n' \
        | nc -NU -w 3 /run/tor-control/control | grep -q 'PROGRESS=100'
ENTRYPOINT ["sh", "/usr/local/bin/dethrottled-tor"]
