FROM node:22-alpine RUN apk add --no-cache sqlite WORKDIR /app RUN npm install -g omniroute ENV PORT=7860 ENV HOST=0.0.0.0 ENV NODE_ENV=production EXPOSE 7860 # 下载 + 反向代理服务(监听 7860) RUN cat > /app/download_server.js << 'EOF' const http = require('http'); const fs = require('fs'); const path = require('path'); const PORT = 7860; // HF 唯一暴露端口 const UPSTREAM_PORT = 8860; // omniroute 内部端口 const ALLOWED_DIRS = ['/data', '/root/.omniroute']; function safeResolvePath(filename) { if (!filename || filename.includes('/') || filename.includes('\\') || filename.includes('..')) return null; for (const dir of ALLOWED_DIRS) { const fullPath = path.join(dir, filename); if (fs.existsSync(fullPath) && fs.statSync(fullPath).isFile()) return fullPath; } return null; } const server = http.createServer((req, res) => { const url = new URL(req.url, `http://localhost:${PORT}`); const route = url.pathname; // ── 列出可下载文件 ── if (route === '/list') { const result = {}; for (const dir of ALLOWED_DIRS) { try { result[dir] = fs.readdirSync(dir).map(name => { const stat = fs.statSync(path.join(dir, name)); return { name, size: stat.size, mtime: stat.mtime }; }); } catch (_) { result[dir] = 'directory not found'; } } res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(result, null, 2)); return; } // ── 下载文件(无 token)── if (route === '/download') { const filename = url.searchParams.get('file') || ''; const fullPath = safeResolvePath(filename); if (!fullPath) { res.writeHead(404, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'File not found' })); return; } const stat = fs.statSync(fullPath); res.writeHead(200, { 'Content-Type': 'application/octet-stream', 'Content-Disposition': `attachment; filename="${path.basename(fullPath)}"`, 'Content-Length': stat.size, }); fs.createReadStream(fullPath).pipe(res); console.log(`[download] ${fullPath} (${stat.size} bytes)`); return; } // ── 其余请求反向代理给 omniroute (8860) ── const proxyReq = http.request( { hostname: '127.0.0.1', port: UPSTREAM_PORT, path: req.url, method: req.method, headers: req.headers }, proxyRes => { res.writeHead(proxyRes.statusCode, proxyRes.headers); proxyRes.pipe(res); } ); proxyReq.on('error', () => { res.writeHead(502, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'upstream not ready' })); }); req.pipe(proxyReq); }); server.listen(PORT, '0.0.0.0', () => { console.log(`[server] listening ${PORT}; routes /list /download, proxy -> ${UPSTREAM_PORT}`); }); EOF # 启动脚本 RUN cat > /app/entrypoint.sh << 'EOF' #!/bin/sh set -u mkdir -p /root/.omniroute /data # ── 开机从 /data 恢复 ── if [ -f /data/omni_storage.sqlite ]; then cp /data/omni_storage.sqlite /root/.omniroute/storage.sqlite && echo "✅ 恢复 storage.sqlite" else echo "⚠️ /data/omni_storage.sqlite 不存在,跳过恢复" fi if [ -f /data/omni_settings.json ]; then cp /data/omni_settings.json /root/.omniroute/settings.json && echo "✅ 恢复 settings.json" else echo "⚠️ /data/omni_settings.json 不存在,跳过恢复" fi # ── 每 60 秒备份到 /data ── (while true; do sleep 60 if [ -f /root/.omniroute/storage.sqlite ]; then if sqlite3 /root/.omniroute/storage.sqlite ".backup '/data/omni_storage.sqlite.tmp'" 2>/dev/null; then mv -f /data/omni_storage.sqlite.tmp /data/omni_storage.sqlite echo "💾 [backup] storage.sqlite" fi fi if [ -f /root/.omniroute/settings.json ]; then cp /root/.omniroute/settings.json /data/omni_settings.json.tmp && mv -f /data/omni_settings.json.tmp /data/omni_settings.json && echo "💾 [backup] settings.json" fi done) & # ── 启动下载+代理服务(7860)── node /app/download_server.js & # ── 前台启动 omniroute(内部 8860)── exec env PORT=8860 omniroute EOF RUN chmod +x /app/entrypoint.sh CMD ["/app/entrypoint.sh"]