Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·383 lines (334 loc) · 12.6 KB
/
Copy pathserver.js
File metadata and controls
executable file
·383 lines (334 loc) · 12.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
#!/usr/bin/env node
const { randomUUID } = require('node:crypto');
const path = require('path');
const fs = require('fs');
const syncloungeServer = require('./packages/syncloungeserver/dist/lib.js'); // eslint-disable-line import-x/extensions
const config = require('./config');
const {
createCache,
roomMetadataCacheKey,
roomPosterMetadataCacheKey,
resolveRoomPosterMetadata,
} = require('./cache');
const { fetchPoster, PosterProxyError } = require('./poster-proxy');
const { createDisconnectController } = require('./request-abort');
const blockList = Object.keys(syncloungeServer.defaultConfig);
const appConfig = config.get(null, blockList);
const publicAppConfig = config.getPublic(appConfig);
const socketConfig = syncloungeServer.getConfig();
function parsePublicOrigin(value) {
if (!value) return null;
let parsed;
try {
parsed = new URL(value);
} catch {
throw new TypeError('PUBLIC_ORIGIN must be an absolute HTTP(S) origin');
}
if (!['http:', 'https:'].includes(parsed.protocol)
|| parsed.username
|| parsed.password
|| parsed.pathname !== '/'
|| parsed.search
|| parsed.hash) {
throw new TypeError('PUBLIC_ORIGIN must be an absolute HTTP(S) origin');
}
return parsed.origin;
}
const publicOrigin = parsePublicOrigin(socketConfig.public_origin);
const ROOM_POSTER_CACHE_MAX_SIZE = 1000;
const ROOM_POSTER_CACHE_TTL_MS = 6 * 60 * 60 * 1000;
const roomPosterPath = (room, revision) => (
`/share/room-poster/${encodeURIComponent(room)}/${encodeURIComponent(revision)}`
);
// Log exactly the browser-safe projection rather than deployment-only configuration.
console.log(publicAppConfig);
const { setMetadata, getMetadata, deleteMetadata } = createCache();
const {
setMetadata: setRoomPosterMetadata,
getMetadata: getRoomPosterMetadata,
} = createCache({
maxSize: ROOM_POSTER_CACHE_MAX_SIZE,
ttlMs: ROOM_POSTER_CACHE_TTL_MS,
});
// --- HTML escaping for XSS prevention ---
function escapeHtml(str) {
if (!str) return '';
return str.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
}
// --- Read index.html once at startup ---
const distPath = path.join(__dirname, 'dist');
let indexHtml = '';
try {
indexHtml = fs.readFileSync(path.join(distPath, 'index.html'), 'utf-8');
if (publicOrigin) {
// Use only the configured origin; request Host headers are not trusted for shared previews.
indexHtml = indexHtml.replace(
/(<meta\s+(?:property="og:image"|name="twitter:image")\s+content=")\/social-card\.png(")/g,
(match, prefix, suffix) => `${prefix}${escapeHtml(publicOrigin)}/social-card.png${suffix}`,
);
}
} catch (e) {
console.warn('Could not read dist/index.html at startup:', e.message);
}
function injectOgTags(html, meta) {
let title;
if (meta.type === 'episode') {
const season = meta.parentIndex != null ? `S${String(meta.parentIndex).padStart(2, '0')}` : '';
const episode = meta.index != null ? `E${String(meta.index).padStart(2, '0')}` : '';
const epNum = season || episode ? `${season}${episode} ` : '';
const parts = [meta.grandparentTitle, `${epNum}${meta.title || ''}`].filter(Boolean);
title = parts.join(' - ').trim();
} else {
title = meta.year ? `${meta.title} (${meta.year})` : (meta.title || '');
}
const ogType = meta.type === 'movie' ? 'video.movie' : 'video.other';
const tags = [
`<meta property="og:title" content="${escapeHtml(title)}" />`,
meta.summary ? `<meta property="og:description" content="${escapeHtml(meta.summary)}" />` : '',
meta.posterProxyUrl ? `<meta property="og:image" content="${escapeHtml(meta.posterProxyUrl)}" />` : '',
`<meta property="og:type" content="${ogType}" />`,
'<meta property="og:site_name" content="SyncLounge" />',
`<meta name="twitter:card" content="${meta.posterProxyUrl ? 'summary_large_image' : 'summary'}" />`,
`<meta name="twitter:title" content="${escapeHtml(title)}" />`,
meta.summary ? `<meta name="twitter:description" content="${escapeHtml(meta.summary)}" />` : '',
meta.posterProxyUrl ? `<meta name="twitter:image" content="${escapeHtml(meta.posterProxyUrl)}" />` : '',
].filter(Boolean).join('\n ');
// Remove existing OG/Twitter meta tags from the static HTML so we replace rather than duplicate
const cleaned = html.replace(
/<meta\s+(?:property="og:[^"]*"|name="twitter:[^"]*")[^>]*\/?\s*>\s*\n?/g,
'',
);
return cleaned.replace('</head>', ` ${tags}\n </head>`);
}
// --- In-memory rate limiter (sliding window, no external deps) ---
// Limits configurable via env vars; set to 0 to disable (e.g. in tests)
function parseRateLimit(name, defaultValue) {
const rawValue = process.env[name] ?? String(defaultValue);
if (!/^\d+$/.test(rawValue)) {
throw new TypeError(`${name} must be a non-negative integer`);
}
const parsedValue = Number(rawValue);
if (!Number.isSafeInteger(parsedValue)) {
throw new TypeError(`${name} must be a non-negative integer`);
}
return parsedValue;
}
const POSTER_RATE_LIMIT = parseRateLimit('SL_POSTER_RATE_LIMIT', 60);
const RATE_LIMIT_MAX_BUCKETS = parseRateLimit('SL_RATE_LIMIT_MAX_BUCKETS', 10000);
const RATE_LIMIT_WINDOW_MS = parseRateLimit('SL_RATE_LIMIT_WINDOW_MS', 60 * 1000);
if (RATE_LIMIT_MAX_BUCKETS === 0 || RATE_LIMIT_WINDOW_MS === 0) {
throw new TypeError('Rate-limit bucket count and window must be positive integers');
}
function createRateLimiter(maxRequests, windowMs, maxBuckets) {
if (!Number.isSafeInteger(maxRequests) || maxRequests < 0) {
throw new TypeError('Rate limit must be a non-negative integer');
}
if (maxRequests === 0) return (req, res, next) => next();
const hits = new Map(); // ip -> [timestamp, ...]
let nextPruneAt = Date.now() + windowMs;
const pruneExpiredBuckets = (cutoff) => {
for (const [ip, timestamps] of hits) {
const activeTimestamps = timestamps.filter((timestamp) => timestamp > cutoff);
if (activeTimestamps.length === 0) {
hits.delete(ip);
} else {
hits.set(ip, activeTimestamps);
}
}
};
return (req, res, next) => {
const { ip } = req;
const now = Date.now();
const cutoff = now - windowMs;
if (now >= nextPruneAt) {
pruneExpiredBuckets(cutoff);
nextPruneAt = now + windowMs;
}
if (!hits.has(ip) && hits.size >= maxBuckets) {
return res.status(429).json({ error: 'Too many clients' });
}
let timestamps = hits.get(ip);
if (timestamps) {
timestamps = timestamps.filter((t) => t > cutoff);
} else {
timestamps = [];
}
if (timestamps.length >= maxRequests) {
return res.status(429).json({ error: 'Too many requests' });
}
timestamps.push(now);
hits.set(ip, timestamps);
return next();
};
}
const posterLimiter = createRateLimiter(
POSTER_RATE_LIMIT,
RATE_LIMIT_WINDOW_MS,
RATE_LIMIT_MAX_BUCKETS,
);
// --- File extension check for SPA fallback ---
const STATIC_EXT_RE = /\.\w{2,}$/;
const ROOM_POSTER_MAX_AGE_SECONDS = 60;
const ROOM_PREVIEW_FIELDS = [
'title',
'year',
'summary',
'type',
'posterUrl',
'machineIdentifier',
'ratingKey',
'grandparentTitle',
'parentIndex',
'index',
];
function decodePathSegment(value) {
try {
return decodeURIComponent(value);
} catch {
return null;
}
}
async function proxyPoster(meta, req, res, cacheControl = 'public, max-age=86400') {
if (!meta?.posterUrl) {
return res.status(404).send('Not found');
}
const disconnect = createDisconnectController(req, res);
try {
const allowedPrivateOrigin = process.env.NODE_ENV === 'test'
? process.env.SL_POSTER_TEST_ORIGIN
: undefined;
const poster = await fetchPoster(meta.posterUrl, {
allowedPrivateOrigin,
signal: disconnect.signal,
});
if (disconnect.signal.aborted) return undefined;
res.set('Content-Type', poster.contentType);
res.set('Cache-Control', cacheControl);
return res.send(poster.body);
} catch (error) {
if (disconnect.signal.aborted) return undefined;
console.error('Poster proxy error:', error.message);
const statusCode = error instanceof PosterProxyError ? error.statusCode : 502;
return res.status(statusCode).send(statusCode === 403 ? 'Forbidden' : 'Failed to fetch poster');
} finally {
disconnect.cleanup();
}
}
const preStaticInjection = (router) => {
// Revalidate stable PWA entry points so installed clients can discover updates.
router.get(['/sw.js', '/manifest.webmanifest'], (req, res, next) => {
res.set('Cache-Control', 'no-cache');
if (req.path === '/manifest.webmanifest') res.type('application/manifest+json');
next();
});
// Add route for config
router.get('/config.json', (req, res) => {
res.json(publicAppConfig);
});
// Legacy global metadata writes had no ownership boundary. Only socket-host
// snapshots may supply shared previews now.
router.post('/api/metadata', (req, res) => res.status(410).json({ error: 'Use room previews' }));
router.get('/share/poster/:machineIdentifier/:ratingKey', (req, res) => res.sendStatus(410));
// Room poster snapshots can only be selected by the current socket host.
router.get('/share/room-poster/:room/:revision', posterLimiter, async (req, res) => {
const meta = resolveRoomPosterMetadata({
room: req.params.room,
revision: req.params.revision,
getCurrentMetadata: getMetadata,
getSnapshotMetadata: getRoomPosterMetadata,
});
return proxyPoster(
meta,
req,
res,
`public, max-age=${ROOM_POSTER_MAX_AGE_SECONDS}`,
);
});
// --- SPA fallback middleware ---
router.use((req, res, next) => {
// Only handle GET requests
if (req.method !== 'GET') return next();
// Skip file extensions (static assets)
if (STATIC_EXT_RE.test(req.path)) return next();
// Skip API, socket.io, and share routes (already handled above)
if (req.path.startsWith('/socket.io')
|| req.path.startsWith('/api/')
|| req.path.startsWith('/share/')
|| req.path === '/health'
|| req.path === '/config.json') {
return next();
}
if (!indexHtml) {
return res.status(500).send('index.html not available');
}
// Navigation HTML may contain the current room's selected media preview.
res.set('Cache-Control', 'no-store');
// A browse link may expose only this room's current host-selected media.
const mediaMatch = req.path.match(
/^\/room\/([^/]+)\/browse\/server\/([^/]+)\/ratingKey\/([^/]+)\/?$/,
);
if (mediaMatch) {
const [, room, machine, rating] = mediaMatch.map(decodePathSegment);
const meta = room == null ? null : getMetadata(roomMetadataCacheKey(room));
if (meta && String(meta.machineIdentifier) === machine && String(meta.ratingKey) === rating) {
const posterProxyUrl = meta.posterUrl && publicOrigin
? `${publicOrigin}${roomPosterPath(room, meta.roomPreviewRevision)}` : null;
res.type('html');
return res.send(injectOgTags(indexHtml, { ...meta, posterProxyUrl }));
}
}
// Check if this is a room invite link — inject OG tags for current room media
const roomMatch = req.path.match(/^\/join\/([^/]+)/);
if (roomMatch) {
const roomCode = decodePathSegment(roomMatch[1]);
const meta = roomCode != null ? getMetadata(roomMetadataCacheKey(roomCode)) : null;
if (meta) {
const posterProxyUrl = meta.posterUrl && publicOrigin
? `${publicOrigin}${roomPosterPath(roomCode, meta.roomPreviewRevision)}`
: null;
const html = injectOgTags(indexHtml, { ...meta, posterProxyUrl });
res.set('Content-Type', 'text/html');
return res.send(html);
}
}
// Default metadata belongs to the built document, shared with static hosting.
res.set('Content-Type', 'text/html');
return res.send(indexHtml);
});
};
const onRoomMediaUpdate = ({ roomId, roomPreview }) => {
const roomKey = roomMetadataCacheKey(roomId);
if (!roomPreview) {
deleteMetadata(roomKey);
return;
}
const currentPreview = getMetadata(roomKey);
const previewUnchanged = currentPreview != null
&& ROOM_PREVIEW_FIELDS.every(
(fieldName) => Object.is(currentPreview[fieldName], roomPreview[fieldName]),
);
if (previewUnchanged) {
setMetadata(roomKey, currentPreview);
return;
}
const previewSnapshot = {
...roomPreview,
roomPreviewRevision: randomUUID(),
};
setMetadata(roomKey, previewSnapshot);
setRoomPosterMetadata(
roomPosterMetadataCacheKey(roomId, previewSnapshot.roomPreviewRevision),
previewSnapshot,
);
};
syncloungeServer.socketServer({
...socketConfig,
authentication: appConfig.authentication,
static_path: path.join(__dirname, 'dist'),
preStaticInjection,
onRoomMediaUpdate,
});