-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
423 lines (373 loc) · 12.9 KB
/
app.js
File metadata and controls
423 lines (373 loc) · 12.9 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
require("dotenv").config();
const express = require("express");
const helmet = require("helmet");
const cors = require("cors");
const crypto = require("crypto");
const path = require("path");
const compression = require("compression");
const cookieParser = require("cookie-parser");
const favicon = require("serve-favicon");
const fs = require("fs");
const { doubleCsrf } = require("csrf-csrf");
const http = require("http");
// Local services and routes
const checkAuth = require("./services/checkauth");
const cameraRoutes = require("./routes/cameraRoutes");
const userRouter = require("./routes/userRouters");
const dvrRoutes = require("./routes/dvrs");
const settingsRoutes = require("./routes/settingsRoutes");
const analyticsRoutes = require("./routes/analyticsRoutes");
const publicRoutes = require("./routes/publicRoutes");
const streamStore = require("./utils/streamStore");
const { getRecentActivities } = require("./utils/activityLogger");
const logger = require("./utils/logger");
const morgan = require("morgan");
const { apiLimiter, authLimiter } = require("./middleware/security");
const pkg = require("./package.json");
const app = express();
const PORT = process.env.PORT || 5000;
// Trust Proxy: Essential for AWS Load Balancers and Cloudflare
// Ensures req.ip, rate limiting, and audit logs use the real client IP
app.set("trust proxy", true);
// Global App Version (Injected via Docker Build ARG / System Env or package.json)
app.locals.appVersion = process.env.APP_VERSION || `v${pkg.version}`;
// =================== Security & Middleware ===================
// Disable ETag to reduce server overhead
app.set("etag", false);
// Global connection optimization
app.use((req, res, next) => {
res.setHeader("Connection", "keep-alive");
next();
});
// =================== Global Performance & Security ===================
app.use(compression()); // Compress all responses
// Optimize nonce generation: Only for HTML/EJS requests to reduce crypto overhead
app.use((req, res, next) => {
const isHtml = req.accepts("html");
if (isHtml) {
res.locals.nonce = crypto.randomBytes(16).toString("base64");
} else {
res.locals.nonce = "";
}
next();
});
// Apply general API rate limiting (Exempting public API endpoints)
app.use("/api/", (req, res, next) => {
// Check against originalUrl to be absolutely sure
if (req.originalUrl.includes("/api/public/")) {
return next();
}
apiLimiter(req, res, next);
});
// Middleware to inject Recent Activities into every render with simple caching
let cachedActivities = null;
let lastFetchTime = 0;
const CACHE_DURATION = 30000; // 30 seconds
app.use(async (req, res, next) => {
try {
// Only fetch for pages that might render the navbar
if (
req.method === "GET" &&
!req.path.startsWith("/api/") &&
!req.path.startsWith("/public/dvr/")
) {
const now = Date.now();
if (!cachedActivities || now - lastFetchTime > CACHE_DURATION) {
cachedActivities = await getRecentActivities(10);
lastFetchTime = now;
}
res.locals.recentActivities = cachedActivities;
}
} catch (err) {
res.locals.recentActivities = [];
}
next();
});
const skipVideo = (req, res) => {
// Skip video chunks and streaming manifests to prevent log spam
return (
req.url.includes(".ts") ||
req.url.includes(".m3u8") ||
req.url.includes("/hls/") ||
req.url.includes("/streams/")
);
};
app.use(
morgan(
process.env.NODE_ENV === "production" ? "tiny" : "dev", // Use faster 'tiny' in prod
{
skip: skipVideo,
stream: {
write: (message) => {
// Log to winston only if it's not a generic asset or use a faster logger format
logger.info(message.trim());
},
},
}
)
);
app.disable("x-powered-by");
// Consolidated Helmet Configuration
app.use(
helmet({
contentSecurityPolicy: {
useDefaults: true,
directives: {
"default-src": ["'self'", "ws:", "wss:"],
"script-src": [
"'self'",
"'unsafe-inline'",
"'unsafe-eval'",
"https://cdn.jsdelivr.net",
"https://cdnjs.cloudflare.com",
"https://cloud.umami.is",
"https://cctvweblink.in",
"https://ajax.cloudflare.com",
],
"worker-src": ["'self'", "blob:"],
"style-src": [
"'self'",
"https://fonts.googleapis.com",
"https://cdnjs.cloudflare.com",
"'unsafe-inline'",
],
"font-src": ["'self'", "https://fonts.gstatic.com", "https://cdnjs.cloudflare.com"],
"media-src": ["'self'", "blob:"],
"img-src": [
"'self'",
"data:",
"blob:",
"https://avatars.githubusercontent.com",
"https://images.unsplash.com",
"https://ui-avatars.com",
],
"object-src": ["'self'"],
"frame-src": ["'self'", "https://docs.google.com"],
"connect-src": [
"'self'",
"https://cdnjs.cloudflare.com",
"https://cdn.jsdelivr.net",
"https://cloud.umami.is",
"https://api-gateway.umami.dev",
"https://cctvweblink.in",
"https://ajax.cloudflare.com",
],
"form-action": ["'self'", "https://docs.google.com"],
},
},
referrerPolicy: { policy: "strict-origin-when-cross-origin" },
frameguard: { action: "deny" },
xssFilter: true,
noSniff: true,
hsts: { maxAge: 31536000 },
})
);
app.use(favicon(path.join(__dirname, "public", "images", "fci.png")));
app.use(
cors({
origin: ["https://cctvweblink.in"],
credentials: true,
})
);
app.use(express.json({ limit: "1mb" }));
app.use(express.urlencoded({ extended: true, limit: "1mb" }));
app.use(cookieParser());
// CSRF Protection Initialization
const { generateCsrfToken, doubleCsrfProtection } = doubleCsrf({
getSecret: () => process.env.jwt_token || "super-secret-key",
getSessionIdentifier: (req) => req.cookies.token || "guest-session", // Bind token to JWT cookie
cookieName: "x-csrf-token",
cookieOptions: {
httpOnly: true,
sameSite: "lax",
path: "/",
secure: process.env.NODE_ENV === "production",
},
getTokenFromRequest: (req) => req.headers["x-csrf-token"] || req.body?._csrf,
});
// Allow Cloudflare and browsers to cache static assets aggressively (30 days)
app.use(
express.static(path.join(__dirname, "public"), {
maxAge: "30d",
immutable: true,
setHeaders: (res, filePath) => {
res.setHeader("Cache-Control", "public, max-age=2592000, immutable");
if (filePath.endsWith(".svg")) {
res.setHeader("Content-Type", "image/svg+xml");
}
},
})
);
app.use("/flowbite", express.static(path.join(__dirname, "node_modules/flowbite/dist")));
// =================== View Engine ===================
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
// Enable view caching in production to improve RPS
if (process.env.NODE_ENV === "production") {
app.set("view cache", true);
}
// =================== HLS Stream Serving ===================
const streamDir = path.join(__dirname, "streams");
if (!fs.existsSync(streamDir)) fs.mkdirSync(streamDir, { recursive: true });
// Heartbeat Middleware: Every request to /hls resets the 4-minute timeout for that stream
app.use("/hls", (req, res, next) => {
const parts = req.path.split("/").filter(Boolean);
if (parts.length > 0) {
const streamId = parts[0];
streamStore.resetStreamTimeout(streamId);
}
next();
});
app.use(
"/hls",
express.static(streamDir, {
etag: false,
lastModified: false,
setHeaders: (res, filePath) => {
res.setHeader("Access-Control-Allow-Origin", "*");
if (filePath.endsWith(".m3u8")) {
res.setHeader(
"Cache-Control",
"no-cache, no-store, must-revalidate, max-age=0, s-maxage=0"
);
res.setHeader("Pragma", "no-cache");
res.setHeader("Expires", "0");
res.setHeader("Content-Type", "application/vnd.apple.mpegurl");
} else if (filePath.endsWith(".ts")) {
// Segments are now unique per session, but we still disable cache just in case
res.setHeader(
"Cache-Control",
"no-cache, no-store, must-revalidate, max-age=0, s-maxage=0"
);
res.setHeader("Pragma", "no-cache");
res.setHeader("Expires", "0");
res.setHeader("Content-Type", "video/mp2t");
}
},
})
);
// =================== Optimal HLS Caching strategy ===================
app.use(
"/streams",
(req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
if (req.path.endsWith(".m3u8")) {
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0, s-maxage=0");
res.setHeader("Pragma", "no-cache");
res.setHeader("Expires", "0");
} else if (req.path.endsWith(".ts")) {
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0, s-maxage=0");
res.setHeader("Pragma", "no-cache");
res.setHeader("Expires", "0");
}
next();
},
express.static(path.join(__dirname, "public", "streams"), {
etag: false,
lastModified: false,
})
);
// 1. Public Routes (No Auth, No strict Rate Limiting)
app.use(publicRoutes);
// 2. Specific Security Middleware for Admin/API
app.use("/", (req, res, next) => {
// Skip CSRF for purely public streaming APIs if they are GET only
if (req.path.startsWith("/api/public") && req.method === "GET") {
return next();
}
// Skip CSRF for public preview APIs on the index page
if (req.path === "/api/start-stream" || req.path === "/api/stop-stream") {
return next();
}
// Skip CSRF for public DVR pages
if (req.path.startsWith("/public/")) {
return next();
}
doubleCsrfProtection(req, res, next);
});
// Middleware to inject CSRF token into all views
app.use((req, res, next) => {
res.locals.csrfToken = generateCsrfToken(req, res);
next();
});
// 3. Admin/Protected Routes
// Note: Sensitive rate limiting is now applied inside userRouter specifically for login/password
app.use("/", userRouter);
app.use("/camera", checkAuth, cameraRoutes);
app.use("/dvr", checkAuth, dvrRoutes);
app.use(settingsRoutes);
app.use(analyticsRoutes);
// =================== API Endpoints ===================
app.post("/api/start-stream", async (req, res) => {
const { rtspUrl } = req.body;
if (!rtspUrl || !rtspUrl.startsWith("rtsp://")) {
return res.status(400).json({ error: "Invalid RTSP URL" });
}
try {
const result = await streamStore.startHlsStream(rtspUrl, null, null);
res.json({ hlsUrl: result.hlsUrl });
} catch (err) {
logger.error("Error triggering stream via API:", err);
res.status(500).json({ error: "Failed to start stream" });
}
});
app.post("/api/stop-stream", (req, res) => {
const { rtspUrl } = req.body;
if (!rtspUrl) return res.status(400).json({ error: "Missing RTSP URL" });
const stopped = streamStore.stopHlsStream(rtspUrl);
if (stopped) {
return res.json({ message: "Stream stopped manually" });
}
res.status(404).json({ error: "Stream not found" });
});
app.get("/api/public/camera/:id/hls", async (req, res) => {
try {
const cameraId = req.params.id;
if (!/^\d+$/.test(cameraId)) {
return res.status(400).json({ error: "Invalid camera ID" });
}
const [rows] = await require("./config/db").execute(
`SELECT rtsp_url, dvr_id FROM cameras WHERE id = ? AND enabled = 1`,
[cameraId]
);
if (!rows.length) {
return res.status(404).json({ error: "Camera not found or disabled" });
}
const rtspUrl = rows[0].rtsp_url;
if (!rtspUrl || !rtspUrl.startsWith("rtsp://")) {
return res.status(400).json({ error: "Invalid RTSP URL configured for this camera" });
}
const { hlsUrl } = await streamStore.startHlsStream(rtspUrl, cameraId, rows[0].dvr_id);
res.json({ hlsUrl });
} catch (err) {
logger.error("Error in public HLS endpoint:", err);
res.status(500).json({ error: "Internal server error" });
}
});
// =================== Start Server ===================
const server = http.createServer(app);
// Global cleanup of potentially stale streams from previous crash/run
streamStore.cleanupAll();
// Performance Tuning: Keep-Alive
// Set timeout higher than proxy (Cloudflare/Traefik) to reuse connections efficiently
server.keepAliveTimeout = 65000;
server.headersTimeout = 66000;
server.listen(PORT, () => {
logger.info(`🚀 Server running at http://localhost:${PORT}`);
});
// =================== Graceful Shutdown ===================
const gracefulShutdown = () => {
logger.info("Termination signal received. Shutting down gracefully...");
streamStore.cleanupAll();
server.close(() => {
logger.info("HTTP server closed.");
process.exit(0);
});
// Force exit if server doesn't close in 5 seconds
setTimeout(() => {
logger.error("Could not close connections in time, forcefully shutting down");
process.exit(1);
}, 5000);
};
process.on("SIGINT", gracefulShutdown);
process.on("SIGTERM", gracefulShutdown);