-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·731 lines (657 loc) · 22.1 KB
/
Copy pathserver.js
File metadata and controls
executable file
·731 lines (657 loc) · 22.1 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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const multer = require('multer');
const nodemailer = require('nodemailer');
const rateLimit = require('express-rate-limit');
const helmet = require('helmet');
const cors = require('cors');
const winston = require('winston');
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
// Ensure logs directory exists
const LOGS_DIR = path.join(__dirname, 'logs');
try {
fs.mkdirSync(LOGS_DIR, { recursive: true });
} catch (e) {
/* ignore */
}
const bodyParser = require('body-parser');
const { body, validationResult } = require('express-validator');
const { nanoid } = require('nanoid');
const next = require('next');
const {
seoRedirectMiddleware,
CANONICAL_ORIGIN,
} = require('./lib/seo-redirects.cjs');
// Setup Next.js
const dev = process.env.NODE_ENV !== 'production';
const nextApp = next({ dev });
const handle = nextApp.getRequestHandler();
const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.errors({ stack: true }),
winston.format.json()
),
transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' }),
],
});
if (process.env.NODE_ENV !== 'production') {
logger.add(
new winston.transports.Console({
format: winston.format.simple(),
})
);
}
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
const port = process.env.PORT || 3001;
// Database connection
let isDbConnected = false;
let db = {};
// Mock DB Implementation
class MockModel {
constructor(data) {
Object.assign(this, data);
this._id = data._id || Date.now().toString();
this.createdAt = new Date();
}
save() {
const collection = MockModel.collections[this.constructor.modelName];
if (!this._id) this._id = Date.now().toString();
const existingIndex = collection.findIndex((i) => i._id === this._id);
if (existingIndex >= 0) {
collection[existingIndex] = this;
} else {
collection.push(this);
}
return Promise.resolve(this);
}
static findOne(query) {
const collection = MockModel.collections[this.modelName] || [];
const item = collection.find((item) => {
return Object.keys(query).every((key) => item[key] === query[key]);
});
return Promise.resolve(item ? new this(item) : null);
}
static find(query) {
const collection = MockModel.collections[this.modelName] || [];
if (query.content && query.content instanceof RegExp) {
const results = collection.filter((item) =>
query.content.test(item.content)
);
return Promise.resolve(results);
}
if (query.userId) {
const results = collection.filter((item) => item.userId == query.userId);
return Promise.resolve(results);
}
return Promise.resolve(collection);
}
static findById(id) {
const collection = MockModel.collections[this.modelName] || [];
const item = collection.find((i) => i._id == id);
return Promise.resolve(item ? new this(item) : null);
}
static findByIdAndUpdate(id, update) {
const collection = MockModel.collections[this.modelName] || [];
const index = collection.findIndex((i) => i._id == id);
if (index > -1) {
if (update.$set) Object.assign(collection[index], update.$set);
else Object.assign(collection[index], update);
return Promise.resolve(new this(collection[index]));
}
return Promise.resolve(null);
}
static findByIdAndDelete(id) {
const collection = MockModel.collections[this.modelName] || [];
const index = collection.findIndex((i) => i._id == id);
if (index > -1) {
collection.splice(index, 1);
}
return Promise.resolve();
}
static deleteMany(query) {
if (query.userId) {
const collection = MockModel.collections[this.modelName] || [];
const newCollection = collection.filter(
(item) => item.userId != query.userId
);
MockModel.collections[this.modelName] = newCollection;
}
return Promise.resolve();
}
}
MockModel.collections = { User: [], Note: [], AuditLog: [], CallLog: [] };
function initializeModels() {
if (isDbConnected) {
const UserSchema = new mongoose.Schema({
username: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
role: { type: String, default: 'agent' },
createdAt: { type: Date, default: Date.now },
settings: { type: mongoose.Schema.Types.Mixed, default: {} },
twilio: {
accountSid: { type: String, default: '' },
authToken: { type: String, default: '' },
phoneNumber: { type: String, default: '' },
},
});
db.User = mongoose.model('User', UserSchema);
const NoteSchema = new mongoose.Schema({
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
content: String,
createdAt: { type: Date, default: Date.now },
});
db.Note = mongoose.model('Note', NoteSchema);
const CallLogSchema = new mongoose.Schema({
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
callerName: String,
callerPhone: String,
callType: String,
startTime: Date,
endTime: Date,
duration: Number,
notes: String,
status: String,
customData: { type: mongoose.Schema.Types.Mixed },
accountNumber: String,
ssn: String,
createdAt: { type: Date, default: Date.now },
});
db.CallLog = mongoose.model('CallLog', CallLogSchema);
const AuditLogSchema = new mongoose.Schema({
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
action: { type: String, required: true },
resource: { type: String, required: true },
details: { type: mongoose.Schema.Types.Mixed },
ip: String,
userAgent: String,
timestamp: { type: Date, default: Date.now },
});
db.AuditLog = mongoose.model('AuditLog', AuditLogSchema);
logger.info('Using MongoDB Models');
} else {
db.User = class User extends MockModel {
static modelName = 'User';
};
db.Note = class Note extends MockModel {
static modelName = 'Note';
};
db.CallLog = class CallLog extends MockModel {
static modelName = 'CallLog';
};
db.AuditLog = class AuditLog extends MockModel {
static modelName = 'AuditLog';
};
logger.warn(
'WARNING: Using In-Memory Mock Database. Data will be lost on restart.'
);
}
}
mongoose
.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/callcenter', {
serverSelectionTimeoutMS: 5000,
})
.then(() => {
logger.info('MongoDB connected');
isDbConnected = true;
initializeModels();
})
.catch((err) => {
logger.error(
'MongoDB connection error - Falling back to Mock DB',
err.message
);
isDbConnected = false;
initializeModels();
});
initializeModels();
const Models = {
get User() {
return db.User;
},
get Note() {
return db.Note;
},
get CallLog() {
return db.CallLog;
},
get AuditLog() {
return db.AuditLog;
},
};
async function logAudit(userId, action, resource, details, req) {
try {
const auditEntry = new Models.AuditLog({
userId,
action,
resource,
details,
ip: req.ip,
userAgent: req.get('User-Agent'),
});
await auditEntry.save();
} catch (err) {
logger.error('Audit log error:', err);
}
}
// prepare Next.js
nextApp.prepare().then(() => {
app.use(
helmet({
// CSP is owned by nginx (single source of truth for every app it fronts:
// Next.js, /adamas, lgenia, webexpressstudio). Defining it here too sent
// two Content-Security-Policy headers, so the browser enforced the
// stricter intersection — e.g. blocking the data: fonts and Google /
// reCAPTCHA frames the nginx policy intentionally allows.
contentSecurityPolicy: false,
})
);
app.use(cors());
// Canonical URLs, legacy paths, and Search Console cleanup (before static)
app.use(seoRedirectMiddleware);
// Forward to Next before body parsers consume the raw body
app.post('/api/analytics', (req, res) => handle(req, res));
app.use(bodyParser.json({ limit: '10mb' }));
app.use(bodyParser.urlencoded({ extended: true }));
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 300,
standardHeaders: 'draft-7',
legacyHeaders: false,
// Behind nginx+Cloudflare every request reaches Node from 127.0.0.1, so
// req.ip would bucket the entire internet together. Key on the real
// client IP that Cloudflare forwards instead.
keyGenerator: (req) => req.get('CF-Connecting-IP') || req.ip,
// We key on CF-Connecting-IP (not the spoofable XFF), so suppress
// express-rate-limit's trust-proxy validation warning.
validate: { trustProxy: false },
// Never throttle static assets: a single page load pulls ~15+ of them,
// and they're immutable + meant to be edge-cached.
skip: (req) =>
req.method === 'GET' &&
(req.path.startsWith('/_next/') ||
req.path.startsWith('/assets/') ||
req.path === '/favicon.ico'),
});
app.use(limiter);
const auth = (req, res, next) => {
const token = req.header('Authorization')?.replace('Bearer ', '');
// Allow Next.js assets to bypass auth
if (req.path.startsWith('/_next') || req.path.startsWith('/assets'))
return next();
// Check if it is an API call
if (req.path.startsWith('/api/') || req.path.startsWith('/adamas/')) {
next();
return;
}
next();
};
// Re-define auth properly for route usage
const authMiddleware = (req, res, next) => {
const token = req.header('Authorization')?.replace('Bearer ', '');
if (!token) return res.status(401).json({ error: 'Access denied' });
try {
const verified = jwt.verify(token, process.env.JWT_SECRET || 'secret');
req.user = verified;
next();
} catch {
res.status(400).json({ error: 'Invalid token' });
}
};
const storage = multer.diskStorage({
destination: (req, file, cb) => cb(null, 'uploads/'),
filename: (req, file, cb) =>
cb(null, Date.now() + path.extname(file.originalname)),
});
const upload = multer({ storage });
// Correct path for Adamas files on server (root/adamas)
const adamasPath = path.join(__dirname, 'adamas');
const popupsDir = path.join(adamasPath, 'popups');
const uploadsDir = path.join(__dirname, 'uploads');
// Create directories if they don't exist
fs.mkdirSync(popupsDir, { recursive: true });
fs.mkdirSync(uploadsDir, { recursive: true });
// Canonical Adamas URLs (before static; absolute URLs for Express 5 redirect)
app.get('/adamas', (req, res) =>
res.redirect(301, `${CANONICAL_ORIGIN}/adamas/`)
);
['contact', 'privacy', 'terms', 'settings'].forEach((page) => {
app.get(`/adamas/${page}.html`, (req, res) => {
res.redirect(301, `${CANONICAL_ORIGIN}/adamas/${page}`);
});
});
// Serve Adamas static files
app.use('/adamas', express.static(adamasPath));
app.use('/uploads', express.static(uploadsDir));
app.use(
'/socket.io',
express.static(path.join(__dirname, 'node_modules/socket.io/client-dist'))
);
// Admas Static Routes
app.get('/adamas/privacy', (req, res) => {
res.sendFile(path.join(adamasPath, 'privacy.html'));
});
app.get('/adamas/terms', (req, res) => {
res.sendFile(path.join(adamasPath, 'terms.html'));
});
app.get('/adamas/contact', (req, res) => {
res.sendFile(path.join(adamasPath, 'contact.html'));
});
app.get('/adamas/settings', (req, res) => {
res.sendFile(path.join(adamasPath, 'settings.html'));
});
// Ensure JS charset
app.use((req, res, next) => {
if (req.path.endsWith('.js')) {
res.setHeader('Content-Type', 'text/javascript; charset=utf-8');
}
next();
});
const popupStore = new Map();
// --- API ROUTES (Adamas) ---
// Using authMiddleware where appropriate
app.post(
'/api/register',
[
body('username').isLength({ min: 3 }).trim().escape(),
body('email').isEmail().normalizeEmail(),
body('password').isLength({ min: 6 }),
],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty())
return res.status(400).json({ errors: errors.array() });
const { username, email, password, role = 'agent' } = req.body;
const hashedPassword = await bcrypt.hash(password, 10);
const user = new Models.User({
username,
email,
password: hashedPassword,
role,
});
try {
await user.save();
res.status(201).json({ message: 'User registered' });
} catch {
res.status(400).json({ error: 'User already exists' });
}
}
);
app.post(
'/api/login',
[body('email').isEmail().normalizeEmail(), body('password').exists()],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty())
return res.status(400).json({ errors: errors.array() });
const { email, password } = req.body;
const user = await Models.User.findOne({ email });
if (!user || !(await bcrypt.compare(password, user.password))) {
return res.status(400).json({ error: 'Invalid credentials' });
}
const token = jwt.sign(
{ _id: user._id, role: user.role },
process.env.JWT_SECRET || 'secret'
);
res.json({
token,
user: {
_id: user._id,
username: user.username,
email: user.email,
role: user.role,
},
});
}
);
// --- Admin Logic & Analytics (In-Memory Session Fallback) ---
const ADMIN_SESSION_NAME = 'admin_session';
const ADMIN_SESSION_TTL_MS = 30 * 60 * 1000; // 30 minutes
const adminSessions = new Map(); // In-memory session store (replaces Redis)
function generateSessionToken() {
return crypto.randomBytes(32).toString('hex');
}
function verifySessionToken(token) {
if (!token) return false;
const session = adminSessions.get(token);
if (!session) return false;
if (Date.now() > session.exp) {
adminSessions.delete(token);
return false;
}
return true;
}
function parseCookies(req) {
const header = req.headers.cookie || '';
return header
.split(';')
.map((s) => s.trim())
.filter(Boolean)
.reduce((acc, kv) => {
const [k, ...v] = kv.split('=');
acc[k] = decodeURIComponent((v || []).join('='));
return acc;
}, {});
}
function getRealIP(req) {
const cf = req.get('CF-Connecting-IP');
if (cf && cf !== '127.0.0.1') return cf;
const forwarded = req.get('X-Forwarded-For');
if (forwarded) return forwarded.split(',')[0].trim();
return req.ip || req.connection?.remoteAddress || 'unknown';
}
// Login
app.post('/api/admin/login', async (req, res) => {
const secret = (req.body && req.body.secret) || req.get('x-admin-secret');
if (!process.env.ADMIN_SECRET)
return res.status(403).json({ error: 'Admin login not enabled' });
if (!secret || secret !== process.env.ADMIN_SECRET)
return res.status(401).json({ error: 'Unauthorized' });
const token = generateSessionToken();
adminSessions.set(token, {
ts: Date.now(),
exp: Date.now() + ADMIN_SESSION_TTL_MS,
});
const cookieParts = [
`${ADMIN_SESSION_NAME}=${token}`,
'HttpOnly',
'Path=/',
`Max-Age=${Math.floor(ADMIN_SESSION_TTL_MS / 1000)}`,
'SameSite=Strict',
];
if (process.env.NODE_ENV === 'production') cookieParts.push('Secure');
res.setHeader('Set-Cookie', cookieParts.join('; '));
return res.status(204).end();
});
// Logout
app.post('/api/admin/logout', (req, res) => {
const cookies = parseCookies(req);
const token = cookies[ADMIN_SESSION_NAME];
if (token) adminSessions.delete(token);
res.setHeader(
'Set-Cookie',
`${ADMIN_SESSION_NAME}=; HttpOnly; Path=/; Max-Age=0; SameSite=Strict${
process.env.NODE_ENV === 'production' ? '; Secure' : ''
}`
);
return res.status(204).end();
});
// Analytics ingest: handled by Next.js route at src/app/api/analytics/route.js
// Admin Analytics Read
app.get('/api/admin/analytics', async (req, res) => {
const cookies = parseCookies(req);
const token = cookies[ADMIN_SESSION_NAME];
const headerSecret = req.get('x-admin-secret');
const authHeader = req.get('authorization') || '';
const bearer = authHeader.replace(/^Bearer\s+/i, '');
const isAuth =
(token && verifySessionToken(token)) ||
headerSecret === process.env.ADMIN_SECRET ||
bearer === process.env.ADMIN_SECRET;
if (!process.env.ADMIN_SECRET)
return res.status(403).json({ error: 'Admin disabled' });
if (!isAuth) return res.status(401).json({ error: 'Unauthorized' });
const limit = Math.min(Number(req.query.limit) || 100, 1000);
try {
const files = fs
.readdirSync(LOGS_DIR)
.filter((f) => f.startsWith('analytics-'))
.sort()
.reverse();
if (files.length === 0)
return res.json({ source: 'file', count: 0, events: [] });
const latest = path.join(LOGS_DIR, files[0]);
const raw = fs
.readFileSync(latest, 'utf8')
.trim()
.split('\n')
.filter(Boolean);
// Read last N lines
const selected = raw
.slice(-limit)
.reverse()
.map((line) => {
try {
return JSON.parse(line);
} catch {
return { raw: line };
}
});
return res.json({
source: 'file',
file: files[0],
count: selected.length,
events: selected,
});
} catch (err) {
return res.status(500).json({ error: 'Read failed' });
}
});
// CSV Export
app.get('/api/admin/analytics.csv', async (req, res) => {
const cookies = parseCookies(req);
const token = cookies[ADMIN_SESSION_NAME];
const headerSecret = req.get('x-admin-secret');
const authHeader = req.get('authorization') || '';
const bearer = authHeader.replace(/^Bearer\s+/i, '');
const isAuth =
(token && verifySessionToken(token)) ||
headerSecret === process.env.ADMIN_SECRET ||
bearer === process.env.ADMIN_SECRET;
if (!process.env.ADMIN_SECRET)
return res.status(403).send('Admin disabled');
if (!isAuth) return res.status(401).send('Unauthorized');
const limit = Math.min(Number(req.query.limit) || 1000, 10000);
res.setHeader('Content-Type', 'text/csv; charset=utf-8');
res.setHeader(
'Content-Disposition',
'attachment; filename="analytics.csv"'
);
res.write('timestamp,ip,ua,path,referrer,event,data,country,city\n');
try {
const files = fs
.readdirSync(LOGS_DIR)
.filter((f) => f.startsWith('analytics-'))
.sort()
.reverse();
if (files.length === 0) {
res.end();
return;
}
const latest = path.join(LOGS_DIR, files[0]);
const stream = fs.createReadStream(latest, { encoding: 'utf8' });
let leftover = '';
let count = 0;
stream.on('data', (chunk) => {
leftover += chunk;
const parts = leftover.split('\n');
leftover = parts.pop();
// Traverse backwards to get newest first? The legacy code did that in memory but for CSV stream it just dumped.
// Actually legacy code did: `for (let i = parts.length - 1; i >= 0; i--)`. Let's match that.
for (let i = parts.length - 1; i >= 0; i--) {
const line = parts[i].trim();
if (!line) continue;
try {
const e = JSON.parse(line);
const row = [
`"${e.timestamp || ''}"`,
`"${e.ip || ''}"`,
`"${(e.ua || '').replace(/"/g, '""')}"`,
`"${(e.path || '').replace(/"/g, '""')}"`,
`"${(e.referrer || '').replace(/"/g, '""')}"`,
`"${e.event || ''}"`,
`"${JSON.stringify(e.data || '').replace(/"/g, '""')}"`,
`"${e.country || ''}"`,
`"${e.city || ''}"`,
];
res.write(row.join(',') + '\n');
count++;
if (count >= limit) {
stream.destroy();
res.end();
return;
}
} catch {}
}
});
stream.on('end', () => res.end());
} catch {
res.status(500).end();
}
});
// End Admin Logic
// The user can add specific missing API endpoints if needed later.
// BUT the user's specific request "new files aren't deploying" was about the MAIN SITE (Next.js).
// CRM/Finesse/Etc routes would go here.
// Contact Form
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: { user: process.env.EMAIL_USER, pass: process.env.EMAIL_PASS },
});
app.post('/api/contact', async (req, res) => {
// reuse logic from original or just pass for now
const { name, email, message } = req.body;
if (!name || !email || !message)
return res.status(400).json({ error: 'Missing fields' });
try {
await transporter.sendMail({
from: `"${name}" <${process.env.EMAIL_USER}>`,
replyTo: email,
to: process.env.EMAIL_USER,
subject: `Adamas Contact: ${name}`,
text: message,
});
res.json({ success: true });
} catch (e) {
res.status(500).json({ error: 'Failed' });
}
});
// Explicitly serve admin index for /admin path to avoid Next.js 404
app.get(['/admin', '/admin/'], (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'admin', 'index.html'));
});
// Explicitly serve admin analytics for /admin/analytics
app.get(['/admin/analytics', '/admin/analytics/'], (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'admin', 'analytics.html'));
});
// Default Catch-All: Next.js
app.all(/.*/, (req, res) => {
return handle(req, res);
});
server.listen(port, (err) => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
});