mirror of
https://github.com/margrop/wrong-notebook.git
synced 2026-12-02 06:20:23 +08:00
feat: 新增内置 HTTPS 支持以启用截图功能
- 新增轻量级 HTTPS 代理 [https-server.js] - 更新 Docker 配置,支持启动时自动生成自签名证书 - 新增 `docker-compose.https.yml` 便于 HTTPS 部署 - 优化 `upload-zone.tsx` 代码格式与类型定义 - 添加 HTTPS 配置指南文档
This commit is contained in:
100
https-server.js
Normal file
100
https-server.js
Normal file
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* HTTPS wrapper for Next.js standalone server
|
||||
*
|
||||
* 使用方法:
|
||||
* 1. 生成证书放到 /app/certs/ 目录
|
||||
* 2. 设置环境变量 HTTPS_ENABLED=true
|
||||
* 3. 容器会自动使用 HTTPS
|
||||
*/
|
||||
const https = require('https');
|
||||
const http = require('http');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const CERT_DIR = process.env.CERT_DIR || '/app/certs';
|
||||
const CERT_FILE = path.join(CERT_DIR, 'cert.pem');
|
||||
const KEY_FILE = path.join(CERT_DIR, 'key.pem');
|
||||
|
||||
// 超时配置(毫秒)- 与 AI 分析超时保持一致
|
||||
const PROXY_TIMEOUT = 300000; // 5 分钟,足够 AI 分析 + 重试
|
||||
|
||||
// 检查证书是否存在
|
||||
if (!fs.existsSync(CERT_FILE) || !fs.existsSync(KEY_FILE)) {
|
||||
console.error('[HTTPS] 证书文件不存在:', CERT_DIR);
|
||||
console.error('[HTTPS] 请确保 cert.pem 和 key.pem 存在于该目录');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// 读取证书
|
||||
const options = {
|
||||
key: fs.readFileSync(KEY_FILE),
|
||||
cert: fs.readFileSync(CERT_FILE),
|
||||
};
|
||||
|
||||
const port = parseInt(process.env.PORT || '3000', 10);
|
||||
|
||||
console.log(`[HTTPS] 启动 HTTPS 代理,端口 443 -> HTTP ${port}`);
|
||||
console.log(`[HTTPS] 代理超时设置: ${PROXY_TIMEOUT / 1000} 秒`);
|
||||
|
||||
const httpsServer = https.createServer(options, (req, res) => {
|
||||
const proxyReq = http.request({
|
||||
hostname: '127.0.0.1',
|
||||
port: port,
|
||||
path: req.url,
|
||||
method: req.method,
|
||||
headers: {
|
||||
...req.headers,
|
||||
// 确保 Host 头正确
|
||||
host: req.headers.host || `127.0.0.1:${port}`,
|
||||
},
|
||||
// 设置代理请求超时
|
||||
timeout: PROXY_TIMEOUT,
|
||||
}, (proxyRes) => {
|
||||
// 复制响应头
|
||||
res.writeHead(proxyRes.statusCode, proxyRes.headers);
|
||||
// 流式传输响应体
|
||||
proxyRes.pipe(res);
|
||||
});
|
||||
|
||||
// 设置客户端连接超时
|
||||
req.setTimeout(PROXY_TIMEOUT);
|
||||
res.setTimeout(PROXY_TIMEOUT);
|
||||
|
||||
// 代理请求超时处理
|
||||
proxyReq.on('timeout', () => {
|
||||
console.error('[HTTPS] 代理请求超时');
|
||||
proxyReq.destroy();
|
||||
if (!res.headersSent) {
|
||||
res.writeHead(504);
|
||||
res.end('Gateway Timeout');
|
||||
}
|
||||
});
|
||||
|
||||
// 代理错误处理
|
||||
proxyReq.on('error', (err) => {
|
||||
console.error('[HTTPS] 代理错误:', err.message);
|
||||
if (!res.headersSent) {
|
||||
res.writeHead(502);
|
||||
res.end('Bad Gateway');
|
||||
}
|
||||
});
|
||||
|
||||
// 客户端断开连接时,终止代理请求
|
||||
req.on('close', () => {
|
||||
proxyReq.destroy();
|
||||
});
|
||||
|
||||
// 将请求体转发到代理
|
||||
req.pipe(proxyReq);
|
||||
});
|
||||
|
||||
// 服务器级别超时配置
|
||||
httpsServer.timeout = PROXY_TIMEOUT;
|
||||
httpsServer.keepAliveTimeout = 65000; // 65 秒 Keep-Alive
|
||||
httpsServer.headersTimeout = 66000; // 略大于 keepAliveTimeout
|
||||
|
||||
const httpsPort = parseInt(process.env.HTTPS_PORT || '443', 10);
|
||||
|
||||
httpsServer.listen(httpsPort, '0.0.0.0', () => {
|
||||
console.log(`[HTTPS] HTTPS 服务器已启动,监听端口 ${httpsPort}`);
|
||||
});
|
||||
Reference in New Issue
Block a user