Spaces:
Running

Bot / Dockerfile
ddosvn's picture
Isolate zone terminal to zone directory (per-zone UID/GID, bash jail, env filter)
8c88f47 verified
Raw History Blame Contribute Delete
6.69 kB
FROM python:3.11-slim
# Cài system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
curl wget git build-essential procps htop nano vim sudo openssl ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# Cài Node.js 20
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
&& apt-get install -y nodejs \
&& rm -rf /var/lib/apt/lists/* \
&& mkdir -p /opt/fb-bypass/node && cd /opt/fb-bypass/node \
&& npm init -y >/dev/null 2>&1 && npm install undici@6 --omit=dev >/dev/null 2>&1 || true
# Cài FFmpeg, Chromium và các thư viện/font Linux cho Puppeteer
RUN apt-get update && apt-get install -y --no-install-recommends \
ffmpeg chromium \
fontconfig fonts-liberation fonts-dejavu-core fonts-freefont-ttf fonts-noto-color-emoji \
libgbm1 libdrm2 libnss3 libnspr4 libxkbcommon0 \
libxcomposite1 libxdamage1 libxfixes3 libxrandr2 libxshmfence1 libxss1 libxtst6 \
xdg-utils dbus dumb-init \
&& rm -rf /var/lib/apt/lists/*
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \
PUPPETEER_SKIP_DOWNLOAD=true \
PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium \
CHROME_BIN=/usr/bin/chromium \
CHROMIUM_PATH=/usr/bin/chromium \
FFMPEG_PATH=/usr/bin/ffmpeg \
FFPROBE_PATH=/usr/bin/ffprobe \
NODE_PATH=/usr/lib/node_modules
RUN npm install -g puppeteer puppeteer-core && npm cache clean --force
# Cài Go 1.22
RUN wget -q https://go.dev/dl/go1.22.5.linux-amd64.tar.gz \
&& tar -C /usr/local -xzf go1.22.5.linux-amd64.tar.gz \
&& rm go1.22.5.linux-amd64.tar.gz
ENV PATH="/usr/local/go/bin:${PATH}"
ENV GOPATH="/data/go"
# Tạo user (HF Spaces yêu cầu user 1000)
RUN useradd -m -u 1000 user
ENV HOME=/home/user
ENV PATH="/home/user/.local/bin:${PATH}"
WORKDIR /app
# Cài Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy source code
COPY . .
# Phân quyền chạy fb-bypass-system.sh và cấu hình sudo -E không cần mật khẩu cho user
RUN chmod +x /app/fb-bypass-system.sh && \
printf 'user ALL=(root) NOPASSWD: SETENV: /app/fb-bypass-system.sh, /usr/local/bin/fb-bypass-proxy\n' > /etc/sudoers.d/panelthtuanit-bypass && \
chmod 0440 /etc/sudoers.d/panelthtuanit-bypass
# Tự động đồng bộ biến môi trường fb-bypass cho mọi interactive bash shell (cả non-login và shell mở ngoài panel)
RUN printf '\n# >>> fb-bypass-system >>>\n_fb_bypass_sync_env() {\n local _pid=""\n if [ -r /run/fb-bypass-proxy.pid ]; then\n read -r _pid < /run/fb-bypass-proxy.pid 2>/dev/null || _pid=""\n fi\n if [ -n "$_pid" ] && [ -d "/proc/$_pid" ] && [ -f /etc/profile.d/fb-bypass-proxy.sh ]; then\n if [ -z "${HTTPS_PROXY:-}" ] || { [ -f /etc/profile.d/fb-bypass-node.sh ] && [ -z "${NODE_OPTIONS:-}" ]; }; then\n . /etc/profile.d/fb-bypass-proxy.sh\n [ -f /etc/profile.d/fb-bypass-node.sh ] && . /etc/profile.d/fb-bypass-node.sh\n fi\n elif [ "${HTTPS_PROXY:-}" = "https://127.0.0.1:1443" ]; then\n unset HTTPS_PROXY https_proxy NO_PROXY no_proxy SSL_CERT_FILE REQUESTS_CA_BUNDLE CURL_CA_BUNDLE NODE_EXTRA_CA_CERTS NODE_OPTIONS\n fi\n [ -n "${PANELTHTUANIT_PS1:-}" ] && PS1="$PANELTHTUANIT_PS1"\n}\n_fb_bypass_sync_env\ncase ";${PROMPT_COMMAND:-};" in\n *";_fb_bypass_sync_env;"*) ;;\n *) PROMPT_COMMAND="_fb_bypass_sync_env${PROMPT_COMMAND:+; $PROMPT_COMMAND}" ;;\nesac\n[ -n "${PANELTHTUANIT_PS1:-}" ] && PS1="$PANELTHTUANIT_PS1"\n# <<< fb-bypass-system <<<\n' >> /etc/bash.bashrc && \
printf '[ -f /etc/bash.bashrc ] && . /etc/bash.bashrc\n[ -f "$HOME/.bashrc" ] && . "$HOME/.bashrc"\nif [ -n "${PANELTHTUANIT_PS1:-}" ]; then\n PS1="$PANELTHTUANIT_PS1"\nfi\n' > /etc/panelthtuanit-bashrc && \
chmod 0644 /etc/panelthtuanit-bashrc && \
printf '[ -f /etc/bash.bashrc ] && . /etc/bash.bashrc\n[ -f "$HOME/.bashrc" ] && . "$HOME/.bashrc"\nif [ -n "${PANELTHTUANIT_PS1:-}" ]; then\n PS1="$PANELTHTUANIT_PS1"\nfi\nif [ -n "${PANEL_ZONE_ROOT:-}" ]; then\n readonly PANEL_ZONE_ROOT HOME\n _pt_zone_check_dir() {\n local _root _cand\n _root="$(builtin cd -P -- "$PANEL_ZONE_ROOT" 2>/dev/null && pwd -P)" || return 1\n if [ -z "${1:-}" ]; then\n printf "%%s" "$_root"\n return 0\n fi\n _cand="$(builtin cd -P -- "$1" >/dev/null 2>&1 && pwd -P)" || return 1\n case "$_cand" in\n "$_root"|"$_root"/*) printf "%%s" "$_cand"; return 0 ;;\n *) return 1 ;;\n esac\n }\n cd() {\n local _target="" _resolved=""\n while [ $# -gt 0 ]; do\n case "$1" in\n --) shift; break ;;\n -L|-P|-e|-@) shift ;;\n -) _target="${OLDPWD:-$PANEL_ZONE_ROOT}"; shift; break ;;\n *) break ;;\n esac\n done\n if [ -z "$_target" ]; then\n _target="${1:-$PANEL_ZONE_ROOT}"\n fi\n if _resolved="$(_pt_zone_check_dir "$_target")"; then\n builtin cd -- "$_resolved"\n return $?\n fi\n printf "Truy cap ngoai thu muc zone khong duoc phep\n" >&2\n return 1\n }\n pushd() {\n local _target="${1:-$PANEL_ZONE_ROOT}" _resolved=""\n if [ "$1" = "-n" ] && [ -n "${2:-}" ]; then\n _target="$2"\n fi\n if _resolved="$(_pt_zone_check_dir "$_target")"; then\n builtin pushd -- "$_resolved" >/dev/null && pwd\n return $?\n fi\n printf "Truy cap ngoai thu muc zone khong duoc phep\n" >&2\n return 1\n }\n popd() {\n local _next=""\n _next="$(builtin dirs -l +1 2>/dev/null || true)"\n if [ -n "$_next" ] && _pt_zone_check_dir "$_next" >/dev/null; then\n builtin popd >/dev/null && pwd\n return $?\n fi\n builtin cd -- "$PANEL_ZONE_ROOT"\n }\n readonly -f _pt_zone_check_dir cd pushd popd\n builtin cd -- "$PANEL_ZONE_ROOT" 2>/dev/null || true\nfi\n' > /etc/panelthtuanit-zone-bashrc && \
chmod 0644 /etc/panelthtuanit-zone-bashrc
# Build Next.js frontend (bỏ qua npm install nếu đã có sẵn bản build frontend/out)
RUN if [ ! -f "frontend/out/index.html" ] && [ -f "frontend/package.json" ]; then cd frontend && npm install && npm run build; fi
# Build Go backend binary
RUN cd /app/backend && go build -ldflags="-s -w" -o /app/panelthtuanit-server .
# Tạo thư mục data
RUN mkdir -p /data/zones && chown -R user:user /data
# Mở port 7860 (mặc định HF Spaces)
EXPOSE 7860
# Tạo startup script: tạo thư mục /data rồi chạy Go server trực tiếp (fb-bypass-system.sh chạy trong Terminal Admin khi server mở lên)
RUN printf '#!/bin/sh\n\
mkdir -p /data/zones 2>/dev/null || true\n\
cd /app && exec /app/panelthtuanit-server\n' > /app/start.sh && chmod +x /app/start.sh
CMD ["/app/start.sh"]