# dockerfile for "interactive" minimal 3proxy execution, no configuration mounting is required, configuration
# is accepted from stdin. Use "end" command to indicate the end of configuration. Use "log" for stdout logging.
#
# Examples are for podman. For docker change 'podman' to 'docker'.
#
# This is a scratch-based docker with statically linked 3proxy executable
# built against musl and wolfSSL.
#
# Limitations for minimal version:
# no support for plugins and system resolver.
# 'nserver' or 'fakeresolve' are mandatory in configuration.
#
# Build:
#
# podman build -f Dockerfile.minimal -t 3proxy.minimal .
#
# Run example:
#
# podman run --read-only -i -p 3129:3129 --name 3proxy 3proxy.minimal
#or
# podman start -ai 3proxy
#<nserver 8.8.8.8
#<nscache 65535
#<log
#<proxy -p3129
#<end
#

FROM docker.io/alpine:latest AS wolfssl
RUN apk add --no-cache build-base curl autoconf automake libtool linux-headers
RUN TAG=$(curl -s https://api.github.com/repos/wolfSSL/wolfssl/releases/latest \
          | sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p') && \
    echo "wolfssl tag: $TAG" && \
    curl -sL https://api.github.com/repos/wolfSSL/wolfssl/tarball/$TAG -o /tmp/w.tar.gz && \
    mkdir /src && tar -xzf /tmp/w.tar.gz -C /src --strip-components=1 && \
    cd /src && \
    autoreconf -i && \
    ./configure --enable-opensslextra --enable-certgen \
                --enable-tls13 --enable-sni --enable-session-ticket \
                --enable-static --disable-shared --prefix=/usr/local && \
    make -j$(nproc) && make install

FROM docker.io/alpine:latest AS buildenv
RUN apk add --no-cache gcc make musl-dev
COPY --from=wolfssl /usr/local /usr/local
COPY . 3proxy
RUN cd 3proxy && mkdir -p bin &&\
 make -f Makefile.Linux STATIC=true WOLFSSL_CHECK=true \
      EXTRA_LDFLAGS="-L/usr/local/lib" &&\
 strip bin/3proxy

FROM scratch
COPY --from=buildenv 3proxy/bin/3proxy /bin/3proxy
CMD ["/bin/3proxy"]
