-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
201 lines (178 loc) · 6.76 KB
/
Copy pathserver.js
File metadata and controls
201 lines (178 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* DeepFlow Dashboard Server
*
* 用法:
* BACKEND=http://your-deepflow-server:20416 node server.js
* BACKEND=https://cloud.deepflow.yunshan.net node server.js
*
* 前端文件本地服务,所有 /api/* 请求实时代理到你的 DeepFlow 后端。
* 不是离线缓存,是动态代理——后端数据变,页面实时变。
*/
const http = require('http');
const https = require('https');
const fs = require('fs');
const path = require('path');
const url = require('url');
const PORT = process.env.PORT || 8888;
const BACKEND = process.env.BACKEND || 'https://cloud.deepflow.yunshan.net';
const STATIC_DIR = path.join(__dirname, 'cloud.deepflow.yunshan.net');
const backendUrl = new URL(BACKEND);
const isHttps = backendUrl.protocol === 'https:';
const backendHost = backendUrl.hostname;
const backendPort = backendUrl.port || (isHttps ? 443 : 80);
const transport = isHttps ? https : http;
const MIME_TYPES = {
'.html': 'text/html', '.js': 'application/javascript', '.css': 'text/css',
'.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg',
'.gif': 'image/gif', '.svg': 'image/svg+xml', '.ico': 'image/x-icon',
'.ttf': 'font/ttf', '.woff': 'font/woff', '.woff2': 'font/woff2',
'.map': 'application/json', '.webp': 'image/webp',
};
// ============================================================
// 反向代理:/api/* → 后端
// ============================================================
function proxyRequest(req, res) {
const opts = {
hostname: backendHost,
port: backendPort,
path: req.url,
method: req.method,
headers: {
...req.headers,
host: backendHost,
},
rejectUnauthorized: false,
};
delete opts.headers['accept-encoding']; // 避免压缩,简化处理
// Capture request body for logging.
let reqBody = '';
req.on('data', chunk => { reqBody += chunk; });
req.on('end', () => {
if (req.method === 'POST' && reqBody) {
const pathShort = req.url.substring(0, 60);
const bodyShort = reqBody.length > 500 ? reqBody.substring(0, 500) + '...' : reqBody;
console.log(`📤 POST ${pathShort} body=${bodyShort}`);
}
});
const proxyReq = transport.request(opts, (proxyRes) => {
// 注入 CORS
const headers = { ...proxyRes.headers };
headers['access-control-allow-origin'] = req.headers.origin || '*';
headers['access-control-allow-credentials'] = 'true';
// 替换 cookie 域名
if (headers['set-cookie']) {
headers['set-cookie'] = [].concat(headers['set-cookie']).map(c =>
c.replace(/domain=[^;]+/gi, 'domain=localhost')
);
}
delete headers['content-security-policy'];
delete headers['x-frame-options'];
res.writeHead(proxyRes.statusCode, headers);
proxyRes.pipe(res);
const size = proxyRes.headers['content-length'] || '?';
console.log(`🔄 [${proxyRes.statusCode}] ${req.method} ${req.url.substring(0, 80)} (${size}b)`);
});
proxyReq.on('error', (err) => {
console.error(`❌ PROXY ERROR ${req.method} ${req.url}: ${err.message}`);
res.writeHead(502, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ OPT_STATUS: 'SERVER_ERROR', DESCRIPTION: err.message }));
});
req.pipe(proxyReq);
}
// ============================================================
// 静态文件服务
// ============================================================
function serveStatic(req, res) {
const parsedUrl = url.parse(req.url);
const filePath = path.join(STATIC_DIR, parsedUrl.pathname);
if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
const ext = path.extname(filePath).toLowerCase();
const ct = MIME_TYPES[ext] || 'application/octet-stream';
res.writeHead(200, { 'Content-Type': ct, 'Cache-Control': 'public, max-age=31536000' });
fs.createReadStream(filePath).pipe(res);
return true;
}
return false;
}
// ============================================================
// 主服务器
// ============================================================
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url);
const pathname = parsedUrl.pathname;
// CORS preflight
if (req.method === 'OPTIONS') {
res.writeHead(204, {
'access-control-allow-origin': req.headers.origin || '*',
'access-control-allow-methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
'access-control-allow-headers': req.headers['access-control-request-headers'] || '*',
'access-control-allow-credentials': 'true',
});
res.end();
return;
}
// /api/* → 代理到后端(动态数据)
if (pathname.startsWith('/api/')) {
proxyRequest(req, res);
return;
}
// 非 GET → 代理
if (req.method !== 'GET') {
proxyRequest(req, res);
return;
}
// 本地静态文件
if (serveStatic(req, res)) return;
// 静态文件扩展名但本地没有 → 404
const ext = path.extname(pathname).toLowerCase();
if (Object.keys(MIME_TYPES).includes(ext)) {
res.writeHead(404);
res.end('Not Found');
return;
}
// SPA fallback → index.html
const indexPath = path.join(STATIC_DIR, 'index.html');
if (fs.existsSync(indexPath)) {
res.writeHead(200, { 'Content-Type': 'text/html' });
fs.createReadStream(indexPath).pipe(res);
} else {
res.writeHead(404);
res.end('Not Found');
}
});
// WebSocket 代理
server.on('upgrade', (req, socket, head) => {
const opts = {
hostname: backendHost,
port: backendPort,
path: req.url,
method: 'GET',
headers: { ...req.headers, host: backendHost },
};
const proxyReq = transport.request(opts);
proxyReq.on('upgrade', (proxyRes, proxySocket, proxyHead) => {
socket.write(
`HTTP/1.1 101 Switching Protocols\r\n` +
Object.entries(proxyRes.headers).map(([k, v]) => `${k}: ${v}`).join('\r\n') +
'\r\n\r\n'
);
if (proxyHead.length) socket.write(proxyHead);
proxySocket.pipe(socket);
socket.pipe(proxySocket);
});
proxyReq.on('error', () => socket.destroy());
proxyReq.end();
});
server.listen(PORT, '0.0.0.0', () => {
console.log(`
╔══════════════════════════════════════════════════════╗
║ DeepFlow Dashboard ║
║ ║
║ 前端: http://localhost:${PORT} ║
║ 后端: ${BACKEND.padEnd(43)}║
║ ║
║ 所有 /api/* 请求实时代理到后端 ║
║ 前端静态文件本地服务 ║
╚══════════════════════════════════════════════════════╝
`);
});