forked from xdkaine/uma-proxmox-wrapper-public
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
508 lines (407 loc) · 17.6 KB
/
server.js
File metadata and controls
508 lines (407 loc) · 17.6 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
const { createServer } = require('http');
const { parse } = require('url');
const next = require('next');
const tls = require('tls');
const fs = require('fs');
const path = require('path');
const dev = process.env.NODE_ENV !== 'production';
const hostname = process.env.HOSTNAME || 'localhost';
const port = parseInt(process.env.PORT || '3004', 10);
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = createServer(async (req, res) => {
try {
const parsedUrl = parse(req.url, true);
const { pathname } = parsedUrl;
// Serve uploaded files from public/uploads/
if (pathname && pathname.startsWith('/uploads/')) {
const filePath = path.resolve(path.join(__dirname, 'public', pathname));
const publicDir = path.resolve(path.join(__dirname, 'public'));
// Path traversal protection
if (!filePath.startsWith(publicDir + path.sep)) {
res.writeHead(403);
res.end('Forbidden');
return;
}
if (fs.existsSync(filePath)) {
const ext = path.extname(filePath).toLowerCase();
let contentType = 'application/octet-stream';
if (ext === '.png') contentType = 'image/png';
else if (ext === '.jpg' || ext === '.jpeg') contentType = 'image/jpeg';
else if (ext === '.gif') contentType = 'image/gif';
else if (ext === '.webp') contentType = 'image/webp';
else if (ext === '.svg') contentType = 'image/svg+xml';
const stat = fs.statSync(filePath);
res.writeHead(200, {
'Content-Type': contentType,
'Content-Length': stat.size,
'X-Content-Type-Options': 'nosniff',
'Content-Security-Policy': "default-src 'none'",
});
const readStream = fs.createReadStream(filePath);
readStream.pipe(res);
return;
}
}
await handle(req, res, parsedUrl);
} catch (err) {
console.error('Error occurred handling', req.url, err);
res.statusCode = 500;
res.end('internal server error');
}
});
const { setupSocketServer } = require('./lib/socket-server-js');
const io = setupSocketServer(server);
console.log('> Socket.IO Server initialized');
server.on('upgrade', (req, clientSocket, head) => {
const { pathname } = parse(req.url || '', true);
if (pathname?.startsWith('/api/socket/io')) {
return;
}
console.log(`[Server] Upgrade request for: ${pathname}`);
if (pathname === '/api/proxy/vnc') {
handleVncProxy(req, clientSocket, head);
} else {
if (app.getUpgradeHandler) {
app.getUpgradeHandler()(req, clientSocket, head);
} else {
clientSocket.destroy();
}
}
});
server.listen(port, (err) => {
if (err) throw err;
console.log(`> Ready on http://${hostname}:${port}`);
console.log(`> WebSocket proxy enabled for /api/proxy/vnc`);
console.log(`> Proxmox URL: ${process.env.PROXMOX_URL || '(not set)'}`);
});
});
/**
* Handle VNC WebSocket proxy using native TLS tunnel
* This bypasses http-proxy's problematic HTTPS WebSocket handling
*/
async function handleVncProxy(req, clientSocket, head) {
console.log('[VNC Proxy] Handling VNC WebSocket upgrade');
const normalizeOrigin = (value) => {
try {
return new URL(value).origin;
} catch {
return null;
}
};
try {
const { getIronSession } = await import('iron-session');
const cookiePassword = process.env.SECRET_COOKIE_PASSWORD;
if (!cookiePassword || cookiePassword.length < 32) {
console.error('[VNC Proxy] SECRET_COOKIE_PASSWORD not configured');
clientSocket.destroy();
return;
}
const sessionOpts = {
password: cookiePassword,
cookieName: 'proxmox-wrapper-session',
cookieOptions: { secure: process.env.USE_SECURE_COOKIE !== 'false', httpOnly: true, sameSite: 'lax', path: '/' },
};
const res = { getHeader: () => {}, setHeader: () => {} };
const session = await getIronSession(req, res, sessionOpts);
if (!session?.user?.isLoggedIn) {
console.error('[VNC Proxy] Unauthenticated WebSocket upgrade rejected');
clientSocket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
clientSocket.destroy();
return;
}
console.log(`[VNC Proxy] Authenticated user: ${session.user.username}`);
const checkUrl = new URL(req.url || '', `http://${req.headers.host}`);
const checkVmid = checkUrl.searchParams.get('vmid');
if (checkVmid && !session.user.isAdmin) {
try {
const hasAccess = await checkVMAccessInline(
session.user.username,
session.user.groups || [],
checkVmid
);
if (!hasAccess) {
console.error(`[VNC Proxy] User ${session.user.username} denied access to VM ${checkVmid}`);
clientSocket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
clientSocket.destroy();
return;
}
console.log(`[VNC Proxy] Access granted for user ${session.user.username} to VM ${checkVmid}`);
} catch (accessErr) {
console.error('[VNC Proxy] VM access check failed:', accessErr.message);
clientSocket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
clientSocket.destroy();
return;
}
}
} catch (authErr) {
console.error('[VNC Proxy] Auth check failed:', authErr.message);
clientSocket.destroy();
return;
}
const originHeader = req.headers['origin'];
const host = req.headers['host'];
const allowMissingOrigin = process.env.ALLOW_MISSING_WS_ORIGIN === 'true';
const originCandidates = (process.env.ALLOWED_WS_ORIGINS || process.env.APP_ORIGIN || '')
.split(',')
.map((entry) => entry.trim())
.filter(Boolean)
.map((entry) => normalizeOrigin(entry))
.filter(Boolean);
if (!originHeader) {
if (!allowMissingOrigin) {
console.error('[VNC Proxy] Missing Origin header; rejecting by policy');
clientSocket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
clientSocket.destroy();
return;
}
} else {
const requestOrigin = normalizeOrigin(originHeader);
if (!requestOrigin) {
console.error('[VNC Proxy] Invalid origin header');
clientSocket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
clientSocket.destroy();
return;
}
if (originCandidates.length > 0) {
const isAllowedOrigin = originCandidates.includes(requestOrigin);
if (!isAllowedOrigin) {
console.error(`[VNC Proxy] Origin not in allowlist: ${requestOrigin}`);
clientSocket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
clientSocket.destroy();
return;
}
} else {
const requestOriginHost = new URL(requestOrigin).host;
if (requestOriginHost !== host) {
console.error(`[VNC Proxy] Origin mismatch: ${requestOriginHost} !== ${host}`);
clientSocket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
clientSocket.destroy();
return;
}
}
}
const urlObj = new URL(req.url || '', `http://${req.headers.host}`);
const node = urlObj.searchParams.get('node');
const type = urlObj.searchParams.get('type');
const vmid = urlObj.searchParams.get('vmid');
const portParam = urlObj.searchParams.get('port');
const ticket = urlObj.searchParams.get('ticket');
if (!node || !type || !vmid || !portParam || !ticket) {
console.error("[VNC Proxy] Missing required VNC parameters");
clientSocket.destroy();
return;
}
const safeIdPattern = /^[a-zA-Z0-9@._-]+$/;
if (!safeIdPattern.test(node) || !safeIdPattern.test(vmid)) {
console.error('[VNC Proxy] Invalid node or vmid parameter');
clientSocket.destroy();
return;
}
if (!['qemu', 'lxc'].includes(type)) {
console.error('[VNC Proxy] Invalid type parameter');
clientSocket.destroy();
return;
}
const portNum = parseInt(portParam, 10);
if (isNaN(portNum) || portNum < 1 || portNum > 65535) {
console.error('[VNC Proxy] Invalid port parameter');
clientSocket.destroy();
return;
}
const proxmoxUrl = new URL(process.env.PROXMOX_URL || '');
const proxmoxHost = proxmoxUrl.hostname;
const proxmoxPort = parseInt(proxmoxUrl.port || '8006', 10);
// URL-encode the ticket (contains +, /, = chars)
const wsPath = `/api2/json/nodes/${node}/${type}/${vmid}/vncwebsocket?port=${portParam}&vncticket=${encodeURIComponent(ticket)}`;
console.log(`[VNC Proxy] Connecting to Proxmox at ${proxmoxHost}:${proxmoxPort}`);
const rejectUnauthorized = process.env.PROXMOX_SSL_INSECURE !== 'true';
const proxmoxSocket = tls.connect({
host: proxmoxHost,
port: proxmoxPort,
rejectUnauthorized,
}, () => {
console.log('[VNC Proxy] TLS connection established to Proxmox');
const tokenId = process.env.PROXMOX_TOKEN_ID || '';
const tokenSecret = process.env.PROXMOX_TOKEN_SECRET || '';
const authHeader = `PVEAPIToken=${tokenId}=${tokenSecret}`;
const upgradeRequest = [
`GET ${wsPath} HTTP/1.1`,
`Host: ${proxmoxHost}:${proxmoxPort}`,
'Upgrade: websocket',
'Connection: Upgrade',
`Sec-WebSocket-Key: ${req.headers['sec-websocket-key']}`,
`Sec-WebSocket-Version: ${req.headers['sec-websocket-version'] || '13'}`,
`Authorization: ${authHeader}`,
'', // Empty line to end headers
'', // Extra empty line for HTTP/1.1
].join('\r\n');
console.log('[VNC Proxy] Sending WebSocket upgrade request to Proxmox');
proxmoxSocket.write(upgradeRequest);
if (head && head.length > 0) {
proxmoxSocket.write(head);
}
let responseBuffer = Buffer.alloc(0);
let headersParsed = false;
proxmoxSocket.on('data', (data) => {
if (!headersParsed) {
responseBuffer = Buffer.concat([responseBuffer, data]);
const headerEnd = responseBuffer.indexOf('\r\n\r\n');
if (headerEnd !== -1) {
headersParsed = true;
const headers = responseBuffer.slice(0, headerEnd).toString();
const body = responseBuffer.slice(headerEnd + 4);
console.log('[VNC Proxy] Received response from Proxmox:');
console.log(headers.split('\r\n')[0]); // Log status line
if (headers.includes('101') && headers.toLowerCase().includes('upgrade')) {
console.log('[VNC Proxy] WebSocket upgrade successful!');
clientSocket.write(`HTTP/1.1 101 Switching Protocols\r\n`);
clientSocket.write(`Upgrade: websocket\r\n`);
clientSocket.write(`Connection: Upgrade\r\n`);
const acceptMatch = headers.match(/Sec-WebSocket-Accept:\s*([^\r\n]+)/i);
if (acceptMatch) {
clientSocket.write(`Sec-WebSocket-Accept: ${acceptMatch[1]}\r\n`);
}
clientSocket.write(`\r\n`);
if (body.length > 0) {
clientSocket.write(body);
}
proxmoxSocket.pipe(clientSocket);
clientSocket.pipe(proxmoxSocket);
console.log('[VNC Proxy] WebSocket tunnel established');
} else {
console.error('[VNC Proxy] Proxmox rejected WebSocket upgrade:', headers.split('\r\n')[0]);
clientSocket.end();
proxmoxSocket.end();
}
}
}
});
});
proxmoxSocket.on('error', (err) => {
console.error('[VNC Proxy] TLS connection error:', err.message);
if (!clientSocket.destroyed) {
clientSocket.destroy();
}
});
proxmoxSocket.on('close', () => {
console.log('[VNC Proxy] Proxmox socket closed');
if (!clientSocket.destroyed) {
clientSocket.end();
}
});
clientSocket.on('error', (err) => {
console.error('[VNC Proxy] Client socket error:', err.message);
if (!proxmoxSocket.destroyed) {
proxmoxSocket.destroy();
}
});
clientSocket.on('close', () => {
console.log('[VNC Proxy] Client socket closed');
if (!proxmoxSocket.destroyed) {
proxmoxSocket.end();
}
});
}
/**
* Lightweight VM access check for server.js (CJS context).
* Mirrors the logic in lib/acl.ts checkVMAccess() but calls the Proxmox API directly.
*/
async function checkVMAccessInline(username, userGroups, vmid) {
const proxmoxUrl = process.env.PROXMOX_URL;
const tokenId = process.env.PROXMOX_TOKEN_ID;
const tokenSecret = process.env.PROXMOX_TOKEN_SECRET;
if (!proxmoxUrl || !tokenId || !tokenSecret) {
console.error('[VNC Access Check] Missing Proxmox credentials');
return false;
}
const authHeader = `PVEAPIToken=${tokenId}=${tokenSecret}`;
const actionRoles = new Set(['Administrator', 'PVEAdmin', 'PVEVMAdmin', 'PVEVMUser']);
const fetchOpts = {
headers: { 'Authorization': authHeader },
};
if (process.env.PROXMOX_SSL_INSECURE === 'true') {
const https = require('https');
fetchOpts.agent = new https.Agent({ rejectUnauthorized: false });
}
const groupVariants = new Set();
const envRealm = process.env.PROXMOX_USER_REALM;
for (const group of (userGroups || [])) {
if (!group) continue;
const raw = group.trim();
if (raw) {
groupVariants.add(raw);
groupVariants.add(raw.toLowerCase());
}
const cnMatch = group.match(/^CN=([^,]+)/i);
const name = cnMatch ? cnMatch[1].trim() : raw;
if (name) {
groupVariants.add(name);
groupVariants.add(name.toLowerCase());
if (envRealm && !name.endsWith(`-${envRealm}`)) {
const withRealm = `${name}-${envRealm}`;
groupVariants.add(withRealm);
groupVariants.add(withRealm.toLowerCase());
}
}
}
const [aclRes, resourcesRes] = await Promise.all([
fetch(`${proxmoxUrl}/api2/json/access/acl`, fetchOpts),
fetch(`${proxmoxUrl}/api2/json/cluster/resources?type=vm`, fetchOpts),
]);
if (!aclRes.ok || !resourcesRes.ok) {
console.error('[VNC Access Check] Failed to fetch from Proxmox API');
return false;
}
const aclData = await aclRes.json();
const resourcesData = await resourcesRes.json();
const acls = aclData.data || [];
const resources = resourcesData.data || [];
const vmPath = `/vms/${vmid}`;
const hasDirectAccess = acls.some(acl => {
if (acl.path !== vmPath) return false;
if (!actionRoles.has(acl.roleid)) return false;
if (acl.type === 'user') {
const aclUser = acl.ugid.split('@')[0];
return aclUser === username || acl.ugid === username;
}
if (acl.type === 'group') {
return groupVariants.has(acl.ugid) || groupVariants.has(acl.ugid.toLowerCase());
}
return false;
});
if (hasDirectAccess) return true;
const vmResource = resources.find(r =>
r.id === `qemu/${vmid}` || r.id === `lxc/${vmid}` || String(r.vmid) === String(vmid)
);
if (vmResource && vmResource.pool) {
const poolId = vmResource.pool;
if (poolId.startsWith(`DEV_${username}_`)) return true;
for (const group of (userGroups || [])) {
if (!group) continue;
const cnMatch = group.match(/^CN=([^,]+)/i);
const name = (cnMatch ? cnMatch[1] : group).trim().replace(/[^a-zA-Z0-9\-_]/g, '_');
if (name && poolId.startsWith(`DEV_${name}_`)) return true;
if (envRealm && !name.endsWith(`-${envRealm}`)) {
const withRealm = `${name}-${envRealm}`;
if (poolId.startsWith(`DEV_${withRealm}_`)) return true;
}
}
const poolPath = `/pool/${poolId}`;
const hasPoolAccess = acls.some(acl => {
if (acl.path !== poolPath) return false;
if (!actionRoles.has(acl.roleid)) return false;
if (acl.type === 'user') {
const aclUser = acl.ugid.split('@')[0];
return aclUser === username || acl.ugid === username;
}
if (acl.type === 'group') {
return groupVariants.has(acl.ugid) || groupVariants.has(acl.ugid.toLowerCase());
}
return false;
});
if (hasPoolAccess) return true;
}
return false;
}