-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
592 lines (503 loc) · 16.7 KB
/
server.js
File metadata and controls
592 lines (503 loc) · 16.7 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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
const fs = require('fs');
const path = require('path');
const os = require('os');
const express = require('express');
const { WebSocketServer, WebSocket } = require('ws');
const isDev = process.argv.includes('--dev');
const allowInsecureHttp = process.argv.includes('--insecure-http') || process.env.ALLOW_INSECURE_HTTP === '1';
const PORT = parseInt(process.env.PORT || '3000', 10);
const HOST = process.env.HOST || (isDev ? '127.0.0.1' : '0.0.0.0');
const wsJoinToken = (process.env.ZAP_JOIN_TOKEN || '').trim();
function parsePositiveInt(value, fallback) {
const parsed = Number.parseInt(value, 10);
return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback;
}
const MAX_WS_PAYLOAD = parsePositiveInt(process.env.ZAP_WS_MAX_PAYLOAD, 256 * 1024);
const WS_RATE_LIMIT_WINDOW_MS = parsePositiveInt(process.env.ZAP_WS_RATE_WINDOW_MS, 5000);
const WS_RATE_LIMIT_MAX_MESSAGES = parsePositiveInt(process.env.ZAP_WS_RATE_MAX_MESSAGES, 120);
const MAX_DEVICE_NAME_LENGTH = parsePositiveInt(process.env.ZAP_MAX_DEVICE_NAME_LENGTH, 30);
const MAX_FILE_NAME_LENGTH = parsePositiveInt(process.env.ZAP_MAX_FILE_NAME_LENGTH, 255);
const MAX_TRANSFER_BYTES = parsePositiveInt(process.env.ZAP_MAX_TRANSFER_BYTES, 2 * 1024 * 1024 * 1024);
const MAX_SDP_LENGTH = parsePositiveInt(process.env.ZAP_MAX_SDP_LENGTH, 64 * 1024);
const MAX_ICE_CANDIDATE_LENGTH = parsePositiveInt(process.env.ZAP_MAX_ICE_CANDIDATE_LENGTH, 8192);
const MAX_CHAT_MESSAGE_LENGTH = parsePositiveInt(process.env.ZAP_MAX_CHAT_MESSAGE_LENGTH, 400);
const MAX_CLIPBOARD_SNIPPET_LENGTH = parsePositiveInt(process.env.ZAP_MAX_CLIPBOARD_SNIPPET_LENGTH, 800);
const MAX_CLIPBOARD_ITEMS = parsePositiveInt(process.env.ZAP_MAX_CLIPBOARD_ITEMS, 200);
const MAX_TRACKED_CHAT_MESSAGES = parsePositiveInt(process.env.ZAP_MAX_TRACKED_CHAT_MESSAGES, 500);
const MAX_MIME_TYPE_LENGTH = 128;
const RELAY_TYPES = new Set([
'offer',
'answer',
'ice-candidate',
'file-request',
'file-accept',
'file-decline',
'transfer-cancel',
]);
const ALLOWED_DEVICE_TYPES = new Set(['phone', 'tablet', 'desktop', 'unknown']);
const app = express();
app.disable('x-powered-by');
app.use((req, res, next) => {
res.set('X-Content-Type-Options', 'nosniff');
res.set('X-Frame-Options', 'DENY');
res.set('Referrer-Policy', 'no-referrer');
res.set('Cross-Origin-Resource-Policy', 'same-origin');
res.set('Cross-Origin-Opener-Policy', 'same-origin');
res.set('Permissions-Policy', 'camera=(self), microphone=(), geolocation=()');
if (req.socket.encrypted) {
res.set('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
}
next();
});
function getLanIPv4Addresses() {
const interfaces = os.networkInterfaces();
const addresses = new Set();
for (const entries of Object.values(interfaces)) {
for (const info of entries || []) {
const isIPv4 = info.family === 'IPv4' || info.family === 4;
if (!isIPv4 || info.internal) continue;
if (info.address.startsWith('169.254.')) continue;
addresses.add(info.address);
}
}
return [...addresses];
}
function normalizeHost(value) {
if (typeof value !== 'string') return '';
return value.trim().toLowerCase();
}
function isSameOriginWebSocket(req) {
const hostHeader = normalizeHost(req.headers.host);
const originHeader = req.headers.origin;
if (!hostHeader) return false;
if (!originHeader) return isDev;
try {
const origin = new URL(originHeader);
return normalizeHost(origin.host) === hostHeader;
} catch {
return false;
}
}
function hasValidJoinToken(urlPath, hostHeader) {
if (!wsJoinToken) return true;
try {
const parsed = new URL(urlPath || '/', `http://${hostHeader || 'localhost'}`);
const token = parsed.searchParams.get('token');
return token === wsJoinToken;
} catch {
return false;
}
}
function requireJoinToken(req, res, next) {
if (!wsJoinToken) {
next();
return;
}
const token = typeof req.query.token === 'string' ? req.query.token : '';
if (token === wsJoinToken) {
next();
return;
}
res.status(401).json({ error: 'Missing or invalid token' });
}
function buildShareUrl(protocol, ip, portSegment) {
const base = `${protocol}://${ip}${portSegment}/`;
return wsJoinToken
? `${base}?token=${encodeURIComponent(wsJoinToken)}`
: base;
}
app.get('/api/local-urls', requireJoinToken, (req, res) => {
const protocol = req.socket.encrypted ? 'https' : 'http';
const omitPort = (protocol === 'http' && PORT === 80) || (protocol === 'https' && PORT === 443);
const portSegment = omitPort ? '' : `:${PORT}`;
const urls = getLanIPv4Addresses().map((ip) => buildShareUrl(protocol, ip, portSegment));
res.set('Cache-Control', 'no-store');
res.json({ urls });
});
app.get('/app-config.js', (req, res) => {
res.type('application/javascript');
res.set('Cache-Control', 'no-store');
res.send(
`window.__ZAP_CONFIG__ = { isDev: ${JSON.stringify(isDev)}, tokenRequired: ${JSON.stringify(Boolean(wsJoinToken))} };`
);
});
app.use(express.static(path.join(__dirname, 'public')));
let server;
const certPath = path.join(__dirname, 'certs', 'fullchain.pem');
const keyPath = path.join(__dirname, 'certs', 'privkey.pem');
const hasTlsCerts = fs.existsSync(certPath) && fs.existsSync(keyPath);
if (!isDev && !hasTlsCerts && !allowInsecureHttp) {
console.error('TLS certificate files were not found in certs/.');
console.error('Expected: certs/fullchain.pem and certs/privkey.pem');
console.error('Refusing to start without HTTPS. For local-only testing, use npm run dev or pass --insecure-http.');
process.exit(1);
}
if (!isDev && hasTlsCerts) {
const https = require('https');
server = https.createServer(
{ cert: fs.readFileSync(certPath), key: fs.readFileSync(keyPath) },
app
);
console.log('Starting HTTPS server (TLS enabled)');
} else {
const http = require('http');
server = http.createServer(app);
if (!isDev) {
console.log('Warning: Running plain HTTP due to explicit insecure override.');
console.log('WebRTC will NOT work on iOS Safari without HTTPS.');
console.log('Starting HTTP server (insecure override)');
} else {
console.log('Starting HTTP server (dev mode)');
}
}
let startupFailed = false;
function failStartup(err) {
if (startupFailed) return;
startupFailed = true;
if (err.code === 'EADDRINUSE') {
console.error(`Port ${PORT} is already in use. Set PORT to another value and retry.`);
} else if (err.code === 'EPERM' || err.code === 'EACCES') {
console.error(`Permission denied while binding ${HOST}:${PORT} (${err.code}).`);
console.error(`Try a different port with: PORT=3001 npm run dev`);
} else {
console.error(`Failed to start server on ${HOST}:${PORT}.`, err);
}
process.exit(1);
}
// --- Signaling ---
const wss = new WebSocketServer({
noServer: true,
maxPayload: MAX_WS_PAYLOAD,
});
wss.on('error', failStartup);
const peers = new Map(); // peerId -> { ws, name, deviceType }
const clipboardSnippets = [];
const chatMessageOwners = new Map(); // messageId -> peerId
const chatMessageOrder = [];
let nextChatId = 1;
let nextClipboardId = 1;
let nextId = 1;
function safeSend(ws, payload) {
if (ws.readyState !== WebSocket.OPEN) return false;
try {
ws.send(payload);
return true;
} catch {
return false;
}
}
function broadcastPeerList() {
const list = [];
for (const [id, peer] of peers) {
list.push({ id, name: peer.name, deviceType: peer.deviceType });
}
const msg = JSON.stringify({ type: 'peers', peers: list });
const stale = [];
for (const [id, peer] of peers) {
if (!safeSend(peer.ws, msg)) stale.push(id);
}
for (const id of stale) {
peers.delete(id);
}
}
function relay(fromId, toId, message) {
const target = peers.get(toId);
if (target) {
safeSend(target.ws, JSON.stringify({ ...message, from: fromId }));
}
}
function isPlainObject(value) {
return value !== null && typeof value === 'object' && !Array.isArray(value);
}
function sanitizeDeviceType(value) {
return ALLOWED_DEVICE_TYPES.has(value) ? value : 'unknown';
}
function sanitizeDeviceName(value, fallback) {
if (typeof value !== 'string') return fallback;
const trimmed = value.trim().slice(0, MAX_DEVICE_NAME_LENGTH);
return trimmed || fallback;
}
function sanitizeSdp(value, expectedType) {
if (!isPlainObject(value)) return null;
if (value.type !== expectedType) return null;
if (typeof value.sdp !== 'string') return null;
if (value.sdp.length === 0 || value.sdp.length > MAX_SDP_LENGTH) return null;
return { type: value.type, sdp: value.sdp };
}
function sanitizeIceCandidate(value) {
if (!isPlainObject(value)) return null;
if (typeof value.candidate !== 'string' || value.candidate.length > MAX_ICE_CANDIDATE_LENGTH) {
return null;
}
const candidate = {
candidate: value.candidate,
sdpMid: typeof value.sdpMid === 'string' ? value.sdpMid : null,
sdpMLineIndex: Number.isInteger(value.sdpMLineIndex) ? value.sdpMLineIndex : null,
};
if (typeof value.usernameFragment === 'string') {
candidate.usernameFragment = value.usernameFragment;
}
return candidate;
}
function sanitizeFileMeta(value) {
if (!isPlainObject(value)) return null;
if (typeof value.name !== 'string') return null;
const name = value.name.trim().slice(0, MAX_FILE_NAME_LENGTH);
if (!name) return null;
const size = Number(value.size);
if (!Number.isFinite(size) || size < 0 || size > MAX_TRANSFER_BYTES || !Number.isInteger(size)) {
return null;
}
let mimeType = '';
if (typeof value.mimeType === 'string') {
mimeType = value.mimeType.slice(0, MAX_MIME_TYPE_LENGTH);
}
return {
name,
size,
mimeType,
};
}
function sanitizeRelayMessage(type, msg) {
switch (type) {
case 'offer':
case 'answer': {
const sdp = sanitizeSdp(msg.sdp, type);
return sdp ? { type, sdp } : null;
}
case 'ice-candidate': {
const candidate = sanitizeIceCandidate(msg.candidate);
return candidate ? { type, candidate } : null;
}
case 'file-request': {
const meta = sanitizeFileMeta(msg.meta);
return meta ? { type, meta } : null;
}
case 'file-accept':
case 'file-decline':
case 'transfer-cancel':
return { type };
default:
return null;
}
}
function sanitizeChatText(value) {
if (typeof value !== 'string') return null;
const trimmed = value.trim();
if (!trimmed || trimmed.length > MAX_CHAT_MESSAGE_LENGTH) return null;
return trimmed;
}
function sanitizeClipboardText(value) {
if (typeof value !== 'string') return null;
const trimmed = value.trim();
if (!trimmed || trimmed.length > MAX_CLIPBOARD_SNIPPET_LENGTH) return null;
return trimmed;
}
function sanitizeItemId(value) {
if (typeof value !== 'string') return null;
const id = value.trim();
if (!id || id.length > 64) return null;
return id;
}
server.on('upgrade', (req, socket, head) => {
const host = normalizeHost(req.headers.host);
if (!isSameOriginWebSocket(req)) {
socket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
socket.destroy();
return;
}
if (!hasValidJoinToken(req.url, host)) {
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
socket.destroy();
return;
}
wss.handleUpgrade(req, socket, head, (ws) => {
wss.emit('connection', ws, req);
});
});
wss.on('connection', (ws) => {
const peerId = String(nextId++);
let registered = false;
let cleanedUp = false;
let windowStartedAt = Date.now();
let messagesInWindow = 0;
function cleanup() {
if (cleanedUp) return;
cleanedUp = true;
if (peers.delete(peerId)) {
broadcastPeerList();
}
}
function closePolicy(reason) {
try {
ws.close(1008, reason);
} catch {
cleanup();
}
}
ws.on('message', (raw, isBinary) => {
if (isBinary) {
closePolicy('Binary signaling is not supported');
return;
}
const now = Date.now();
if (now - windowStartedAt > WS_RATE_LIMIT_WINDOW_MS) {
windowStartedAt = now;
messagesInWindow = 0;
}
messagesInWindow += 1;
if (messagesInWindow > WS_RATE_LIMIT_MAX_MESSAGES) {
closePolicy('Too many signaling messages');
return;
}
let msg;
try {
const text = typeof raw === 'string' ? raw : raw.toString('utf8');
msg = JSON.parse(text);
} catch {
return;
}
if (!isPlainObject(msg) || typeof msg.type !== 'string') return;
switch (msg.type) {
case 'register': {
peers.set(peerId, {
ws,
name: sanitizeDeviceName(msg.name, `Device ${peerId}`),
deviceType: sanitizeDeviceType(msg.deviceType),
});
if (!registered) {
registered = true;
safeSend(ws, JSON.stringify({ type: 'registered', id: peerId }));
}
safeSend(ws, JSON.stringify({ type: 'clipboard-state', snippets: clipboardSnippets }));
broadcastPeerList();
break;
}
case 'chat-message': {
if (!registered) break;
const text = sanitizeChatText(msg.text);
if (!text) break;
const sender = peers.get(peerId);
if (!sender) break;
const id = String(nextChatId++);
chatMessageOwners.set(id, peerId);
chatMessageOrder.push(id);
if (chatMessageOrder.length > MAX_TRACKED_CHAT_MESSAGES) {
const removed = chatMessageOrder.shift();
if (removed) chatMessageOwners.delete(removed);
}
const payload = JSON.stringify({
type: 'chat-message',
id,
from: peerId,
name: sender.name,
text,
ts: Date.now(),
});
const stale = [];
for (const [id, peer] of peers) {
if (!safeSend(peer.ws, payload)) stale.push(id);
}
for (const id of stale) {
peers.delete(id);
}
break;
}
case 'chat-delete': {
if (!registered) break;
const id = sanitizeItemId(msg.id);
if (!id) break;
const owner = chatMessageOwners.get(id);
if (owner !== peerId) break;
chatMessageOwners.delete(id);
const idx = chatMessageOrder.indexOf(id);
if (idx >= 0) chatMessageOrder.splice(idx, 1);
const payload = JSON.stringify({
type: 'chat-delete',
id,
});
const stale = [];
for (const [targetId, peer] of peers) {
if (!safeSend(peer.ws, payload)) stale.push(targetId);
}
for (const targetId of stale) {
peers.delete(targetId);
}
break;
}
case 'clipboard-add': {
if (!registered) break;
const text = sanitizeClipboardText(msg.text);
if (!text) break;
const sender = peers.get(peerId);
if (!sender) break;
const snippet = {
id: String(nextClipboardId++),
from: peerId,
name: sender.name,
text,
ts: Date.now(),
};
clipboardSnippets.push(snippet);
if (clipboardSnippets.length > MAX_CLIPBOARD_ITEMS) {
clipboardSnippets.splice(0, clipboardSnippets.length - MAX_CLIPBOARD_ITEMS);
}
const payload = JSON.stringify({
type: 'clipboard-add',
snippet,
});
const stale = [];
for (const [id, peer] of peers) {
if (!safeSend(peer.ws, payload)) stale.push(id);
}
for (const id of stale) {
peers.delete(id);
}
break;
}
case 'clipboard-delete': {
if (!registered) break;
const id = sanitizeItemId(msg.id);
if (!id) break;
const index = clipboardSnippets.findIndex((snippet) => snippet.id === id);
if (index < 0) break;
clipboardSnippets.splice(index, 1);
const payload = JSON.stringify({
type: 'clipboard-delete',
id,
});
const stale = [];
for (const [targetId, peer] of peers) {
if (!safeSend(peer.ws, payload)) stale.push(targetId);
}
for (const targetId of stale) {
peers.delete(targetId);
}
break;
}
default: {
if (!registered || !RELAY_TYPES.has(msg.type)) break;
if (typeof msg.to !== 'string' || msg.to.length === 0 || msg.to.length > 32) break;
if (msg.to === peerId) break;
if (!peers.has(msg.to)) break;
const sanitized = sanitizeRelayMessage(msg.type, msg);
if (!sanitized) break;
relay(peerId, msg.to, sanitized);
break;
}
}
});
ws.on('close', cleanup);
ws.on('error', cleanup);
});
server.on('error', failStartup);
server.listen(PORT, HOST, () => {
console.log(`Zap server listening on ${HOST}:${PORT}`);
if (wsJoinToken) {
console.log('Join token auth enabled for WebSocket and local URL API.');
}
if (isDev) {
const localHost = HOST === '0.0.0.0' ? 'localhost' : HOST;
console.log(` http://${localHost}:${PORT}`);
}
});