Spaces:
Sleeping
Sleeping
Update Dockerfile
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124FROM ymlisoft/kkfileviewUSER rootRUN apt-get update && apt-get install -y xvfb python3 curl && rm -rf /var/lib/apt/lists/*ENV SERVER_PORT=8012ENV KKFILEVIEW_SECURITY_TRUST_HOST=defaultENV ACCESS_TOKEN=your-secret-token-here# 创建 Token 验证脚本RUN printf '#!/usr/bin/env python3\n\import http.server\n\import socketserver\n\import urllib.request\n\import os\n\import sys\n\from urllib.parse import urlparse, parse_qs\n\\n\sys.stdout = os.fdopen(sys.stdout.fileno(), "w", buffering=1)\n\sys.stderr = os.fdopen(sys.stderr.fileno(), "w", buffering=1)\n\\n\PORT = 7860\n\BACKEND = "http://127.0.0.1:8012"\n\TOKEN = os.getenv("ACCESS_TOKEN", "changeme")\n\\n\print(f"[TOKEN_PROXY] Starting on port {PORT}", flush=True)\n\print(f"[TOKEN_PROXY] Backend: {BACKEND}", flush=True)\n\print(f"[TOKEN_PROXY] Token: {TOKEN[:5]}***", flush=True)\n\\n\class TokenHandler(http.server.SimpleHTTPRequestHandler):\n\def log_message(self, format, *args):\n\print(f"[ACCESS] {format % args}", flush=True)\n\\n\def do_GET(self):\n\parsed = urlparse(self.path)\n\params = parse_qs(parsed.query)\n\provided_token = params.get("token", [""])[0]\n\\n\if provided_token != TOKEN:\n\print(f"[DENIED] Invalid token from {self.client_address[0]}", flush=True)\n\self.send_response(403)\n\self.send_header("Content-type", "text/html; charset=utf-8")\n\self.end_headers()\n\html = f"""<!DOCTYPE html>\n\<html><head><meta charset="utf-8"><title>Access Denied</title></head>\n\<body style="font-family: Arial; padding: 50px; text-align: center;">\n\<h1>�� Access Denied</h1>\n\<p>Please provide valid token in URL</p>\n\<p><code>?token=YOUR_TOKEN</code></p>\n\<p style="color: #666; margin-top: 30px;">Current token: <strong>{provided_token or "(empty)"}</strong></p>\n\</body></html>"""\n\self.wfile.write(html.encode())\n\return\n\\n\print(f"[ALLOWED] Valid token - forwarding to backend", flush=True)\n\url = BACKEND + self.path\n\try:\n\req = urllib.request.Request(url)\n\for header in ["User-Agent", "Accept", "Accept-Language"]:\n\if header in self.headers:\n\req.add_header(header, self.headers[header])\n\\n\response = urllib.request.urlopen(req, timeout=60)\n\self.send_response(response.getcode())\n\for header, value in response.headers.items():\n\if header.lower() not in ["connection", "transfer-encoding"]:\n\self.send_header(header, value)\n\self.end_headers()\n\self.wfile.write(response.read())\n\except Exception as e:\n\print(f"[ERROR] Backend error: {str(e)}", flush=True)\n\self.send_error(502, f"Backend error: {str(e)}")\n\\n\do_POST = do_GET\n\\n\try:\n\with socketserver.TCPServer(("0.0.0.0", PORT), TokenHandler) as httpd:\n\print(f"[TOKEN_PROXY] ✅ Server ready on port {PORT}!", flush=True)\n\print(f"[TOKEN_PROXY] Access: https://your-space.hf.space/?token={TOKEN}", flush=True)\n\httpd.serve_forever()\n\except Exception as e:\n\print(f"[FATAL] {str(e)}", flush=True)\n\import traceback\n\traceback.print_exc()\n\sys.exit(1)\n' > /token_check.py && \chmod +x /token_check.py# 启动脚本RUN printf '#!/bin/bash\n\set -e\n\echo "=== kkFileView with Token Auth ==="\n\echo ""\n\echo "[1/4] Starting Xvfb..."\n\Xvfb :99 -screen 0 1024x768x24 >/dev/null 2>&1 &\n\export DISPLAY=:99\n\sleep 1\n\echo "✓ Xvfb started"\n\\n\echo "[2/4] Starting kkFileView backend..."\n\/opt/kkFileView/bin/kkFileView --server.port=8012 >/tmp/kkfileview.log 2>&1 &\n\KKFILEVIEW_PID=$!\n\echo "✓ kkFileView PID: $KKFILEVIEW_PID"\n\\n\echo "[3/4] Waiting for backend (max 60s)..."\n\for i in {1..60}; do\n\if python3 -c "import urllib.request; urllib.request.urlopen('"'"'http://127.0.0.1:8012'"'"', timeout=1)" 2>/dev/null; then\n\echo "✓ Backend ready after ${i}s"\n\break\n\fi\n\if [ $i -eq 60 ]; then\n\echo "✗ Backend timeout! Showing logs:"\n\tail -50 /tmp/kkfileview.log\n\exit 1\n\fi\n\sleep 1\n\done\n\\n\echo "[4/4] Starting Token Proxy..."\n\echo ""\n\exec python3 -u /token_check.py\n' > /start.sh && \chmod +x /start.shEXPOSE 786012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576FROM ymlisoft/kkfileview:4.4.0USER root# 安装 Nginx、xvfb 和密码工具RUN apt-get update && apt-get install -y \nginx \apache2-utils \xvfb \&& rm -rf /var/lib/apt/lists/*# 环境变量ENV SERVER_PORT=8012ENV KKFILEVIEW_SECURITY_TRUST_HOST=defaultENV AUTH_USERNAME=adminENV AUTH_PASSWORD=123456# 配置 NginxRUN rm -f /etc/nginx/sites-enabled/default && \printf 'server {\n\listen 7860;\n\server_name _;\n\\n\# 密码保护\n\auth_basic "kkFileView Access";\n\auth_basic_user_file /etc/nginx/.htpasswd;\n\\n\location / {\n\proxy_pass http://127.0.0.1:8012;\n\proxy_set_header Host $host;\n\proxy_set_header X-Real-IP $remote_addr;\n\proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n\proxy_set_header X-Forwarded-Proto $scheme;\n\proxy_buffering off;\n\}\n\}\n' > /etc/nginx/sites-available/kkfileview && \ln -s /etc/nginx/sites-available/kkfileview /etc/nginx/sites-enabled/# 启动脚本RUN printf '#!/bin/bash\n\set -e\n\\n\echo "=== kkFileView with Password Protection ==="\n\echo ""\n\\n\# 创建密码文件(使用环境变量)\n\echo "Creating password file..."\n\htpasswd -cb /etc/nginx/.htpasswd "$AUTH_USERNAME" "$AUTH_PASSWORD"\n\echo "✓ Username: $AUTH_USERNAME"\n\echo "✓ Password: [HIDDEN]"\n\echo ""\n\\n\# 启动 Xvfb\n\echo "Starting Xvfb..."\n\Xvfb :99 -screen 0 1024x768x24 >/dev/null 2>&1 &\n\export DISPLAY=:99\n\sleep 2\n\echo "✓ Xvfb started"\n\echo ""\n\\n\# 启动 kkFileView(后台)\n\echo "Starting kkFileView on port 8012..."\n\/opt/kkFileView/bin/kkFileView --server.port=8012 &\n\KKFILEVIEW_PID=$!\n\echo "✓ kkFileView PID: $KKFILEVIEW_PID"\n\sleep 5\n\echo ""\n\\n\# 启动 Nginx(前台)\n\echo "Starting Nginx on port 7860..."\n\echo "✓ Ready! Access with username and password"\n\echo ""\n\nginx -g "daemon off;"\n' > /start.sh && \chmod +x /start.shEXPOSE 7860
Dockerfile+51-99