Spaces:
Sleeping
Sleeping
Update Dockerfile
Browse files1 unmodified line2345567891011111213142 unmodified lines171819202021223 unmodified lines26272830293031323433343536373938394043414245434447454647484950515253545550565758596061626353646566671 unmodified line697071617273747576776768697879808182728384858687887889909181828384859293949596878897989990919210010110294103104969798991001051061071081091101031111121131141151161171181191201081091101111211221231241 unmodified line
USER root
RUN apt-get update && apt-get install -y xvfb python3 && rm -rf /var/lib/apt/lists/*RUN 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 验证脚本(添加更多日志)# 创建 Token 验证脚本RUN printf '#!/usr/bin/env python3\n\import http.server\n\import socketserver\n\2 unmodified linesimport sys\n\from urllib.parse import urlparse, parse_qs\n\\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\3 unmodified lines\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 configured: {TOKEN[:5]}...", 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"[TOKEN_PROXY] {format % args}", flush=True)\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\ \n\ provided_token = params.get("token", [""])[0]\n\ \n\ if provided_token != TOKEN:\n\ print(f"[TOKEN_PROXY] Access denied - invalid token", flush=True)\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")\n\ self.send_header("Content-type", "text/html; charset=utf-8")\n\ self.end_headers()\n\ self.wfile.write(b"<h1>Access Denied</h1><p>Please provide valid token in URL: ?token=YOUR_TOKEN</p>")\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"[TOKEN_PROXY] Valid token - forwarding request", flush=True)\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(url, timeout=30)\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\1 unmodified line self.end_headers()\n\ self.wfile.write(response.read())\n\ except Exception as e:\n\ print(f"[TOKEN_PROXY] Backend error: {str(e)}", flush=True)\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(("", PORT), TokenHandler) as httpd:\n\ print(f"[TOKEN_PROXY] Server running successfully!", flush=True)\n\ print(f"[TOKEN_PROXY] Access URL: http://localhost:{PORT}/onlinePreview?url=YOUR_FILE&token={TOKEN}", flush=True)\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"[TOKEN_PROXY] FATAL ERROR: {str(e)}", flush=True)\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\\n\echo "=== Starting kkFileView with Token Auth ==="\n\echo "Step 1: Starting Xvfb..."\n\Xvfb :99 -screen 0 1024x768x24 &\n\XVFB_PID=$!\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\echo "Xvfb started with PID: $XVFB_PID"\n\sleep 2\n\sleep 1\n\echo "✓ Xvfb started"\n\\n\echo ""\n\echo "Step 2: Starting kkFileView backend on port 8012..."\n\/opt/kkFileView/bin/kkFileView --server.port=8012 &\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 started with PID: $KKFILEVIEW_PID"\n\echo "✓ kkFileView PID: $KKFILEVIEW_PID"\n\\n\echo ""\n\echo "Step 3: Waiting for backend to be ready..."\n\for i in {1..30}; do\n\ if curl -s http://127.0.0.1:8012 > /dev/null 2>&1; then\n\ echo "Backend is ready!"\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\ echo "Waiting... ($i/30)"\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\echo "Step 4: Starting Token Auth Proxy on port 7860..."\n\echo "Python version: $(python3 --version)"\n\echo ""\n\python3 -u /token_check.py\n' > /start.sh && \exec python3 -u /token_check.py\n' > /start.sh && \ chmod +x /start.sh
EXPOSE 7860Dockerfile+48-38