Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
178 lines (159 loc) · 5.6 KB
/
Copy pathserver.js
File metadata and controls
178 lines (159 loc) · 5.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
'use strict';
import http from 'node:http';
import fs from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import episodesHandler from './api/episodes.js';
import searchHandler from './api/search.js';
import sportsSearchHandler from './api/sports-search.js';
import sportsEventsHandler from './api/sports-events.js';
import moviesSearchHandler from './api/movies-search.js';
import moviesHandler from './api/movies.js';
import calendarHandler from './api/calendar.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const publicDir = path.resolve(__dirname, 'public');
const port = Number.parseInt(process.env.PORT || '3000', 10);
const contentTypes = {
'.html': 'text/html; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.svg': 'image/svg+xml; charset=utf-8'
};
async function serveStatic(req, res) {
const requestUrl = new URL(req.url, `http://${req.headers.host || 'localhost'}`);
let decodedPathname;
try {
decodedPathname = decodeURIComponent(requestUrl.pathname);
} catch (err) {
res.writeHead(400);
res.end('Bad Request');
return;
}
if (decodedPathname.includes('..') || decodedPathname.includes('\0')) {
res.writeHead(400);
res.end('Bad Request');
return;
}
const normalizedPath = path.normalize(decodedPathname);
const relativePath = normalizedPath === '/' ? 'index.html' : normalizedPath.replace(/^\//, '');
const filePath = path.resolve(publicDir, relativePath);
// Robust path-traversal check using path.relative
const relative = path.relative(publicDir, filePath);
const isOutside = relative.startsWith('..') || path.isAbsolute(relative);
if (isOutside) {
res.writeHead(403);
res.end('Forbidden');
return;
}
try {
const contents = await fs.readFile(filePath);
res.writeHead(200, { 'Content-Type': contentTypes[path.extname(filePath)] || 'application/octet-stream' });
res.end(contents);
} catch {
const contents = await fs.readFile(path.join(publicDir, 'index.html'));
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end(contents);
}
}
function validateQueryParams(req, res) {
try {
const requestUrl = new URL(req.url, `http://${req.headers.host || 'localhost'}`);
for (const [key, val] of requestUrl.searchParams.entries()) {
if (key.includes('\0')) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: `Parameter name contains invalid null bytes.` }));
return false;
}
if (typeof val !== 'string') continue;
let maxLen = ['shows', 'teamIds', 'movies'].includes(key) ? 2200 : 120;
if (key === 'tz' || key === 'timezone') maxLen = 50;
else if (key === 'since') maxLen = 30;
else if (key === 'type') maxLen = 30;
else if (key === 'format') maxLen = 10;
else if (key === 'teamId') maxLen = 50;
if (val.length > maxLen) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: `Parameter '${key}' exceeds maximum length of ${maxLen} characters.` }));
return false;
}
if (['q', 'show', 'shows', 'movies'].includes(key)) {
if (/[\0\r\n]/.test(val)) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: `Parameter '${key}' contains invalid or unsafe characters.` }));
return false;
}
} else {
if (/[\0\r\n<>`$]/.test(val)) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: `Parameter '${key}' contains invalid or unsafe characters.` }));
return false;
}
}
}
return true;
} catch (err) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Invalid request URL.' }));
return false;
}
}
async function handleRoute(handler, req, res) {
try {
await handler(req, res);
} catch (err) {
console.error('Error handling route:', err);
if (!res.headersSent) {
res.writeHead(500, { 'Content-Type': 'application/json; charset=utf-8' });
res.end(JSON.stringify({ error: err.message || 'Internal server error' }));
}
}
}
const server = http.createServer((req, res) => {
const url = req.url || '';
if (url.startsWith('/api/')) {
if (!validateQueryParams(req, res)) {
return;
}
}
if (url.startsWith('/api/episodes')) {
handleRoute(episodesHandler, req, res);
return;
}
if (url.startsWith('/api/calendar')) {
handleRoute(calendarHandler, req, res);
return;
}
if (url.startsWith('/api/search')) {
handleRoute(searchHandler, req, res);
return;
}
if (url.startsWith('/api/sports-search')) {
handleRoute(sportsSearchHandler, req, res);
return;
}
if (url.startsWith('/api/sports-events')) {
handleRoute(sportsEventsHandler, req, res);
return;
}
if (url.startsWith('/api/movies-search')) {
handleRoute(moviesSearchHandler, req, res);
return;
}
if (url.startsWith('/api/movies')) {
handleRoute(moviesHandler, req, res);
return;
}
handleRoute(serveStatic, req, res);
});
server.listen(port, () => {
console.log(`MakeICS TV Episodes running at http://localhost:${port}`);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
process.exit(1);
});
process.on('uncaughtException', (err) => {
console.error('Uncaught Exception thrown:', err);
process.exit(1);
});