# ======================================================== # Stage: Builder # ======================================================== FROM golang:1.23-alpine AS builder WORKDIR /app ARG TARGETARCH ARG ANTIZAPRET RUN apk --no-cache --update add \ build-base \ gcc \ wget \ unzip COPY . . ENV CGO_ENABLED=1 ENV CGO_CFLAGS="-D_LARGEFILE64_SOURCE" RUN go build -o build/x-ui main.go RUN ./DockerInit.sh "$TARGETARCH" "$ANTIZAPRET" # ======================================================== # Stage: Final Image of 3x-ui # ======================================================== FROM alpine ENV TZ=Europe/Moscow WORKDIR /app RUN apk add --no-cache --update \ ca-certificates \ tzdata \ fail2ban \ ip6tables \ iptables \ bash \ nano \ unzip COPY --from=builder /app/build/ /app/ COPY --from=builder /app/DockerEntrypoint.sh /app/ COPY --from=builder /app/x-ui.sh /usr/bin/x-ui # Configure fail2ban RUN rm -f /etc/fail2ban/jail.d/alpine-ssh.conf \ && cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local \ && sed -i "s/^\[ssh\]$/&\nenabled = false/" /etc/fail2ban/jail.local \ && sed -i "s/^\[sshd\]$/&\nenabled = false/" /etc/fail2ban/jail.local \ && sed -i "s/#allowipv6 = auto/allowipv6 = auto/g" /etc/fail2ban/fail2ban.conf RUN < /etc/fail2ban/jail.d/3x-ipl.conf [3x-ipl] enabled=true backend=auto filter=3x-ipl action=3x-ipl logpath=${iplimit_log_path} maxretry=2 findtime=32 bantime=${bantime}m EOF cat << EOF > /etc/fail2ban/filter.d/3x-ipl.conf [Definition] datepattern = ^%%Y/%%m/%%d %%H:%%M:%%S failregex = \[LIMIT_IP\]\s*Email\s*=\s*.+\s*\|\|\s*SRC\s*=\s* ignoreregex = EOF cat << EOF > /etc/fail2ban/action.d/3x-ipl.conf [INCLUDES] before = iptables-common.conf [Definition] actionstart = -N f2b- -A f2b- -j -I -p -j f2b- actionstop = -D -p -j f2b- -X f2b- actioncheck = -n -L | grep -q 'f2b-[ \t]' actionban = -I f2b- 1 -s -j echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") BAN [Email] = [IP] = banned for seconds." >> ${iplimit_banned_log_path} actionunban = -D f2b- -s -j echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") UNBAN [Email] = [IP] = unbanned." >> ${iplimit_banned_log_path} [Init] name = default protocol = tcp chain = INPUT EOF cat << EOF > /etc/fail2ban/action.d/iptables-common.conf [INCLUDES] after = iptables-blocktype.local iptables-common.local [Definition] actionflush = -F f2b- [Init] chain = INPUT name = default port = ssh protocol = tcp blocktype = REJECT --reject-with icmp-port-unreachable returntype = RETURN lockingopt = -w iptables = iptables [Init?family=inet6] blocktype = REJECT --reject-with icmp6-port-unreachable iptables = ip6tables EOF sed -i "1s/^/[INCLUDES]\n\nbefore = iptables-common.conf\n\n/" /etc/fail2ban/action.d/iptables.conf EOT RUN mkdir -p /etc/x-ui && touch /etc/x-ui/announce.txt RUN chmod +x \ /app/DockerEntrypoint.sh \ /app/x-ui \ /usr/bin/x-ui VOLUME [ "/etc/x-ui" ] CMD [ "./x-ui" ] ENTRYPOINT [ "/app/DockerEntrypoint.sh" ]