-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
893 lines (761 loc) · 26.9 KB
/
index.js
File metadata and controls
893 lines (761 loc) · 26.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
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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
import path from 'path';
import { fileURLToPath } from 'url';
import crypto from 'crypto';
import dotenv from 'dotenv';
import express from 'express';
import http from 'http';
import https from 'https';
import { validateRoutesConfig } from './logic/validateConfig.js';
import { getTemplateCatalog, replaceTemplateRegistry } from './logic/templateRegistry.js';
import { sequelize, Ingress, Egress, Template, RouteConfig } from './models/index.js';
import { runMigrations } from './models/db-config.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const nodeEnv = process.env.NODE_ENV || 'development';
const envFile = `.env.${nodeEnv}`;
dotenv.config({ path: path.join(__dirname, envFile) });
const app = express();
const webhookPathPrefix = '/webhook/';
const serveUi = process.env.SERVE_UI !== 'false';
const adminApiToken = (process.env.ADMIN_API_TOKEN || '').trim();
const adminProxySharedSecret = (process.env.ADMIN_PROXY_SHARED_SECRET || '').trim();
const frontendOnlyApi = process.env.FRONTEND_ONLY_API === 'true';
const frontendAuthEnabled = process.env.FRONTEND_AUTH_ENABLED === 'true';
const frontendAuthUsername = (process.env.FRONTEND_AUTH_USERNAME || '').trim();
const frontendAuthPassword = process.env.FRONTEND_AUTH_PASSWORD || '';
const frontendAuthSessionSecret = process.env.FRONTEND_AUTH_SESSION_SECRET || '';
const frontendAuthCookieName = 'gateherald_ui_session';
const allowedCorsOrigins = new Set(
(process.env.ALLOWED_ORIGINS || '')
.split(',')
.map((origin) => origin.trim())
.filter(Boolean)
);
const parsePositiveInteger = (value, fallbackValue) => {
const parsed = Number.parseInt(value, 10);
if (Number.isNaN(parsed) || parsed < 1) {
return fallbackValue;
}
return parsed;
};
const forwardRequestTimeoutMs = parsePositiveInteger(process.env.FORWARD_REQUEST_TIMEOUT_MS, 15000);
const forwardMaxConcurrent = parsePositiveInteger(process.env.FORWARD_MAX_CONCURRENT, 20);
const forwardQueueWarnThreshold = parsePositiveInteger(process.env.FORWARD_QUEUE_WARN_THRESHOLD, 250);
const frontendAuthSessionTtlMinutes = parsePositiveInteger(process.env.FRONTEND_AUTH_SESSION_TTL_MINUTES, 480);
const debugTransformedPayload = process.env.DEBUG_TRANSFORMED_PAYLOAD === 'true';
const parseCookies = (cookieHeader = '') => {
const cookieMap = {};
cookieHeader
.split(';')
.map((chunk) => chunk.trim())
.filter(Boolean)
.forEach((chunk) => {
const separatorIndex = chunk.indexOf('=');
if (separatorIndex < 1) return;
const key = chunk.slice(0, separatorIndex).trim();
const value = chunk.slice(separatorIndex + 1).trim();
cookieMap[key] = decodeURIComponent(value);
});
return cookieMap;
};
const createFrontendSessionToken = () => {
const expiresAt = Date.now() + (frontendAuthSessionTtlMinutes * 60 * 1000);
const payload = Buffer.from(JSON.stringify({ exp: expiresAt })).toString('base64url');
const signature = crypto
.createHmac('sha256', frontendAuthSessionSecret)
.update(payload)
.digest('base64url');
return `${payload}.${signature}`;
};
const isValidFrontendSessionToken = (token) => {
if (!token || typeof token !== 'string') return false;
const [payload, signature] = token.split('.');
if (!payload || !signature) return false;
const expectedSignature = crypto
.createHmac('sha256', frontendAuthSessionSecret)
.update(payload)
.digest('base64url');
if (!timingSafeEquals(signature, expectedSignature)) {
return false;
}
try {
const decoded = JSON.parse(Buffer.from(payload, 'base64url').toString('utf8'));
return Number.isFinite(decoded.exp) && Date.now() < decoded.exp;
} catch {
return false;
}
};
const setFrontendSessionCookie = (req, res, token) => {
const cookieParts = [
`${frontendAuthCookieName}=${encodeURIComponent(token)}`,
'Path=/',
`Max-Age=${frontendAuthSessionTtlMinutes * 60}`,
'HttpOnly',
'SameSite=Lax'
];
const forwardedProto = typeof req.headers['x-forwarded-proto'] === 'string'
? req.headers['x-forwarded-proto']
: '';
if (req.secure || forwardedProto.includes('https')) {
cookieParts.push('Secure');
}
res.setHeader('Set-Cookie', cookieParts.join('; '));
};
const clearFrontendSessionCookie = (req, res) => {
const cookieParts = [
`${frontendAuthCookieName}=`,
'Path=/',
'Max-Age=0',
'HttpOnly',
'SameSite=Lax'
];
const forwardedProto = typeof req.headers['x-forwarded-proto'] === 'string'
? req.headers['x-forwarded-proto']
: '';
if (req.secure || forwardedProto.includes('https')) {
cookieParts.push('Secure');
}
res.setHeader('Set-Cookie', cookieParts.join('; '));
};
const shouldProtectWithFrontendSession = (requestPath) => {
return requestPath.startsWith('/ui')
|| requestPath === '/api'
|| requestPath.startsWith('/api/ui')
|| requestPath.startsWith('/api/templates')
|| requestPath.startsWith('/api/configs');
};
const isPublicUiAssetPath = (requestPath) => requestPath.startsWith('/ui/dist/');
const frontendSessionAuthMiddleware = (req, res, next) => {
if (!serveUi || !frontendAuthEnabled) {
next();
return;
}
if (
req.path === '/auth/login'
|| req.path === '/auth/logout'
|| isPublicUiAssetPath(req.path)
|| req.path.startsWith(webhookPathPrefix)
|| !shouldProtectWithFrontendSession(req.path)
) {
next();
return;
}
const cookies = parseCookies(req.headers.cookie || '');
const sessionToken = cookies[frontendAuthCookieName];
if (isValidFrontendSessionToken(sessionToken)) {
next();
return;
}
if (req.path.startsWith('/api')) {
res.status(401).json({ error: 'Login required' });
return;
}
const nextTarget = encodeURIComponent(req.originalUrl || '/ui/config-builder');
res.redirect(`/auth/login?next=${nextTarget}`);
};
const apiCorsMiddleware = (req, res, next) => {
const requestOrigin = req.headers.origin;
if (!requestOrigin) {
next();
return;
}
if (allowedCorsOrigins.has(requestOrigin)) {
res.setHeader('Access-Control-Allow-Origin', requestOrigin);
res.setHeader('Vary', 'Origin');
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS');
res.setHeader(
'Access-Control-Allow-Headers',
req.headers['access-control-request-headers'] || 'Content-Type, Authorization'
);
if (req.method === 'OPTIONS') {
res.status(204).end();
return;
}
} else if (req.method === 'OPTIONS') {
res.status(403).json({ error: 'CORS origin denied' });
return;
}
next();
};
const parseJson = (value, fallbackValue) => {
if (!value) return fallbackValue;
try {
return JSON.parse(value);
} catch {
return fallbackValue;
}
};
const serializeJson = (value, fallbackValue) => {
try {
return JSON.stringify(value ?? fallbackValue);
} catch {
return JSON.stringify(fallbackValue);
}
};
const normalizeMethod = (method) => (method || 'POST').toUpperCase();
const routeCacheKey = (routePath, method) => `${normalizeMethod(method)}:${routePath}`;
const timingSafeEquals = (left, right) => {
const leftBuffer = Buffer.from(left);
const rightBuffer = Buffer.from(right);
if (leftBuffer.length !== rightBuffer.length) {
return false;
}
return crypto.timingSafeEqual(leftBuffer, rightBuffer);
};
const extractBearerToken = (authorizationHeader) => {
if (typeof authorizationHeader !== 'string') return '';
const match = authorizationHeader.match(/^Bearer\s+(.+)$/i);
return match?.[1]?.trim() || '';
};
const frontendApiAccessMiddleware = (req, res, next) => {
if (!frontendOnlyApi) {
next();
return;
}
const bearerToken = extractBearerToken(req.headers.authorization);
const headerToken = typeof req.headers['x-gateherald-admin-token'] === 'string'
? req.headers['x-gateherald-admin-token'].trim()
: '';
const proxySecret = typeof req.headers['x-gateherald-proxy-secret'] === 'string'
? req.headers['x-gateherald-proxy-secret'].trim()
: '';
const tokenAuthorized = Boolean(adminApiToken)
&& [bearerToken, headerToken].some((candidate) => candidate && timingSafeEquals(candidate, adminApiToken));
const proxyAuthorized = Boolean(adminProxySharedSecret)
&& proxySecret
&& timingSafeEquals(proxySecret, adminProxySharedSecret);
if (tokenAuthorized || proxyAuthorized) {
next();
return;
}
res.status(403).json({ error: 'API access is restricted to frontend/proxy requests' });
};
const adminAuthMiddleware = (req, res, next) => {
if (!adminApiToken && !adminProxySharedSecret) {
next();
return;
}
const bearerToken = extractBearerToken(req.headers.authorization);
const headerToken = typeof req.headers['x-gateherald-admin-token'] === 'string'
? req.headers['x-gateherald-admin-token'].trim()
: '';
const proxySecret = typeof req.headers['x-gateherald-proxy-secret'] === 'string'
? req.headers['x-gateherald-proxy-secret'].trim()
: '';
const tokenAuthorized = Boolean(adminApiToken)
&& [bearerToken, headerToken].some((candidate) => candidate && timingSafeEquals(candidate, adminApiToken));
const proxyAuthorized = Boolean(adminProxySharedSecret)
&& proxySecret
&& timingSafeEquals(proxySecret, adminProxySharedSecret);
if (tokenAuthorized || proxyAuthorized) {
next();
return;
}
if (adminApiToken) {
res.setHeader('WWW-Authenticate', 'Bearer realm="gateherald-admin"');
}
res.status(401).json({ error: 'Admin authentication required' });
};
const sanitizeRouteModuleOptions = (moduleOptions = {}) => {
const sanitized = { ...moduleOptions };
delete sanitized.templateOverrides;
return sanitized;
};
const toRoutePayloadFromModel = (record) => ({
id: record.id,
path: record.path,
method: normalizeMethod(record.method),
enabled: Boolean(record.enabled),
moduleOptions: sanitizeRouteModuleOptions(parseJson(record.module_options_json, {})),
headers: parseJson(record.headers_json, []),
targets: parseJson(record.targets_json, [])
});
const toTemplatePayloadFromModel = (record) => ({
id: record.id,
name: record.name,
needsDocumentationRules: Boolean(record.needs_documentation_rules),
documentationTargetField: record.documentation_target_field || null,
fields: parseJson(record.fields_json, {})
});
const toRouteModelPayload = (route) => ({
path: route.path,
method: normalizeMethod(route.method),
enabled: route.enabled !== false,
module_options_json: serializeJson(sanitizeRouteModuleOptions(route.moduleOptions || {}), {}),
headers_json: serializeJson(route.headers || [], []),
targets_json: serializeJson(route.targets || [], [])
});
const toTemplateModelPayload = (template) => ({
name: template.name,
needs_documentation_rules: template.needsDocumentationRules === true,
documentation_target_field: template.documentationTargetField || null,
fields_json: serializeJson(template.fields || {}, {})
});
const validateTemplatePayload = (template) => {
if (!template || typeof template !== 'object') return 'Template payload must be an object';
if (!template.name || typeof template.name !== 'string') return 'Template name is required';
if (!template.fields || typeof template.fields !== 'object' || Array.isArray(template.fields)) {
return 'Template fields must be an object';
}
if (template.needsDocumentationRules === true) {
if (!template.documentationTargetField || typeof template.documentationTargetField !== 'string') {
return 'documentationTargetField is required when needsDocumentationRules is true';
}
if (!Object.prototype.hasOwnProperty.call(template.fields, template.documentationTargetField)) {
return `documentationTargetField '${template.documentationTargetField}' must exist in template fields`;
}
}
return null;
};
const validateRoutePayload = (route) => {
if (!route || typeof route !== 'object') return 'Route payload must be an object';
if (!route.path || typeof route.path !== 'string') return 'Route path is required';
if (!route.path.startsWith(webhookPathPrefix)) return `Route path must start with '${webhookPathPrefix}'`;
if (!Array.isArray(route.targets) || route.targets.length === 0) {
return 'At least one target URL is required';
}
const validation = validateRoutesConfig([route]);
if (!validation.valid) {
return validation.errors.join('; ');
}
return null;
};
let routeConfigs = [];
const routeConfigIndex = new Map();
const forwardQueue = [];
let activeForwardCount = 0;
let hasWarnedOnForwardQueue = false;
const modules = {};
async function loadModules() {
try {
const moduleExport = await import('./logic/templateDriver.js');
const moduleName = moduleExport.default?.name;
if (moduleName) {
modules[moduleName] = moduleExport;
} else {
console.warn('templateDriver.js does not export a moduleName');
}
} catch (err) {
console.error('Error loading templateDriver module:', err);
}
}
const refreshTemplateRegistryFromDb = async () => {
const templateRows = await Template.findAll({ order: [['name', 'ASC']] });
const templateEntries = templateRows.map(toTemplatePayloadFromModel);
replaceTemplateRegistry(templateEntries);
return templateEntries;
};
const refreshRoutesFromDb = async () => {
const routeRows = await RouteConfig.findAll({ order: [['id', 'ASC']] });
routeConfigs = routeRows.map(toRoutePayloadFromModel);
routeConfigIndex.clear();
routeConfigs.forEach((route) => {
if (route.enabled === false) return;
routeConfigIndex.set(routeCacheKey(route.path, route.method), route);
});
return routeConfigs;
};
app.use('/api', apiCorsMiddleware);
app.use(frontendSessionAuthMiddleware);
app.use('/api', express.json({ limit: '10mb' }));
app.use('/api', frontendApiAccessMiddleware);
app.use('/api/templates', adminAuthMiddleware);
app.use('/api/configs', adminAuthMiddleware);
app.use('/api/ui/templates', adminAuthMiddleware);
if (serveUi) {
app.use('/ui', express.static(path.join(__dirname, 'ui')));
}
app.use(webhookPathPrefix, express.raw({ type: '*/*', limit: '10mb' }));
if (serveUi) {
app.get('/auth/login', (req, res) => {
if (!frontendAuthEnabled) {
res.redirect('/ui/config-builder');
return;
}
res.sendFile(path.join(__dirname, 'ui', 'login.html'));
});
app.post('/auth/login', express.urlencoded({ extended: false }), (req, res) => {
if (!frontendAuthEnabled) {
res.redirect('/ui/config-builder');
return;
}
const username = typeof req.body?.username === 'string' ? req.body.username.trim() : '';
const password = typeof req.body?.password === 'string' ? req.body.password : '';
const redirectTarget = typeof req.body?.next === 'string' && req.body.next.startsWith('/')
? req.body.next
: '/ui/config-builder';
if (!timingSafeEquals(username, frontendAuthUsername) || !timingSafeEquals(password, frontendAuthPassword)) {
const loginErrorTarget = encodeURIComponent(redirectTarget);
res.redirect(`/auth/login?error=1&next=${loginErrorTarget}`);
return;
}
const token = createFrontendSessionToken();
setFrontendSessionCookie(req, res, token);
res.redirect(redirectTarget);
});
app.post('/auth/logout', (req, res) => {
clearFrontendSessionCookie(req, res);
res.redirect('/auth/login?loggedOut=1');
});
app.get('/auth/logout', (req, res) => {
clearFrontendSessionCookie(req, res);
res.redirect('/auth/login?loggedOut=1');
});
}
app.get('/api/ui/templates', async (req, res) => {
try {
await refreshTemplateRegistryFromDb();
res.json({ templates: getTemplateCatalog() });
} catch (err) {
console.error('Failed to load UI templates:', err);
res.status(500).json({ error: 'Failed to load templates' });
}
});
app.get('/api/templates', async (req, res) => {
try {
const rows = await Template.findAll({ order: [['name', 'ASC']] });
res.json({ templates: rows.map(toTemplatePayloadFromModel) });
} catch (err) {
console.error('Failed to list templates:', err);
res.status(500).json({ error: 'Failed to list templates' });
}
});
app.post('/api/templates', async (req, res) => {
try {
const payload = req.body;
const validationError = validateTemplatePayload(payload);
if (validationError) {
res.status(400).json({ error: validationError });
return;
}
const row = await Template.create(toTemplateModelPayload(payload));
await refreshTemplateRegistryFromDb();
res.status(201).json({ template: toTemplatePayloadFromModel(row) });
} catch (err) {
console.error('Failed to create template:', err);
res.status(500).json({ error: 'Failed to create template' });
}
});
app.put('/api/templates/:id', async (req, res) => {
try {
const payload = req.body;
const validationError = validateTemplatePayload(payload);
if (validationError) {
res.status(400).json({ error: validationError });
return;
}
const row = await Template.findByPk(req.params.id);
if (!row) {
res.status(404).json({ error: 'Template not found' });
return;
}
await row.update(toTemplateModelPayload(payload));
await refreshTemplateRegistryFromDb();
res.json({ template: toTemplatePayloadFromModel(row) });
} catch (err) {
console.error('Failed to update template:', err);
res.status(500).json({ error: 'Failed to update template' });
}
});
app.delete('/api/templates/:id', async (req, res) => {
try {
const row = await Template.findByPk(req.params.id);
if (!row) {
res.status(404).json({ error: 'Template not found' });
return;
}
const templateCount = await Template.count();
if (templateCount <= 1) {
res.status(400).json({ error: 'Cannot delete the last template record' });
return;
}
await row.destroy();
await refreshTemplateRegistryFromDb();
res.status(204).end();
} catch (err) {
console.error('Failed to delete template:', err);
res.status(500).json({ error: 'Failed to delete template' });
}
});
app.get('/api/configs', async (req, res) => {
try {
const rows = await RouteConfig.findAll({ order: [['id', 'ASC']] });
res.json({ configs: rows.map(toRoutePayloadFromModel) });
} catch (err) {
console.error('Failed to list configs:', err);
res.status(500).json({ error: 'Failed to list configs' });
}
});
app.post('/api/configs', async (req, res) => {
try {
const payload = {
...req.body,
method: normalizeMethod(req.body?.method)
};
const validationError = validateRoutePayload(payload);
if (validationError) {
res.status(400).json({ error: validationError });
return;
}
const row = await RouteConfig.create(toRouteModelPayload(payload));
await refreshRoutesFromDb();
res.status(201).json({ config: toRoutePayloadFromModel(row) });
} catch (err) {
console.error('Failed to create config:', err);
res.status(500).json({ error: 'Failed to create config' });
}
});
app.put('/api/configs/:id', async (req, res) => {
try {
const payload = {
...req.body,
method: normalizeMethod(req.body?.method)
};
const validationError = validateRoutePayload(payload);
if (validationError) {
res.status(400).json({ error: validationError });
return;
}
const row = await RouteConfig.findByPk(req.params.id);
if (!row) {
res.status(404).json({ error: 'Config not found' });
return;
}
await row.update(toRouteModelPayload(payload));
await refreshRoutesFromDb();
res.json({ config: toRoutePayloadFromModel(row) });
} catch (err) {
console.error('Failed to update config:', err);
res.status(500).json({ error: 'Failed to update config' });
}
});
app.delete('/api/configs/:id', async (req, res) => {
try {
const row = await RouteConfig.findByPk(req.params.id);
if (!row) {
res.status(404).json({ error: 'Config not found' });
return;
}
await row.destroy();
await refreshRoutesFromDb();
res.status(204).end();
} catch (err) {
console.error('Failed to delete config:', err);
res.status(500).json({ error: 'Failed to delete config' });
}
});
if (serveUi) {
app.get('/ui/config-builder', (req, res) => {
res.sendFile(path.join(__dirname, 'ui', 'config-builder.html'));
});
app.get('/ui/template-builder', (req, res) => {
res.sendFile(path.join(__dirname, 'ui', 'template-builder.html'));
});
app.get('/', (req, res) => {
res.redirect('/ui/config-builder');
});
app.get('/ui', (req, res) => {
res.redirect('/ui/config-builder');
});
} else {
app.get('/', (req, res) => {
res.json({ status: 'ok', message: 'Gateherald API server' });
});
}
function forwardRequest(targetUrl, method, headers, body, requestId) {
return new Promise((resolve) => {
const parsedUrl = new URL(targetUrl);
const isHttps = parsedUrl.protocol === 'https:';
const client = isHttps ? https : http;
const options = {
hostname: parsedUrl.hostname,
port: parsedUrl.port || (isHttps ? 443 : 80),
path: `${parsedUrl.pathname}${parsedUrl.search}`,
method: method,
headers: headers
};
const req = client.request(options, (res) => {
res.on('data', () => {});
res.on('end', async () => {
try {
await Egress.create({
request_id: requestId,
target_url: targetUrl,
status: 'success',
response_status: res.statusCode
});
resolve({ status: 'success', responseStatus: res.statusCode });
} catch (err) {
console.error('Error inserting forwarding record:', err);
resolve({ status: 'success', responseStatus: res.statusCode });
}
});
});
req.setTimeout(forwardRequestTimeoutMs, () => {
req.destroy(new Error(`Forwarding request timed out after ${forwardRequestTimeoutMs}ms`));
});
req.on('error', async (err) => {
try {
await Egress.create({
request_id: requestId,
target_url: targetUrl,
status: 'error',
error_message: err.message
});
resolve({ status: 'error', error: err.message });
} catch (dbErr) {
console.error('Error inserting forwarding record:', dbErr);
resolve({ status: 'error', error: err.message });
}
});
if (body) {
req.write(body);
}
req.end();
});
}
const pumpForwardQueue = () => {
while (activeForwardCount < forwardMaxConcurrent && forwardQueue.length > 0) {
const task = forwardQueue.shift();
activeForwardCount += 1;
Promise.resolve()
.then(task)
.catch((err) => {
console.error('Unexpected forwarding queue error:', err);
})
.finally(() => {
activeForwardCount -= 1;
if (forwardQueue.length < forwardQueueWarnThreshold) {
hasWarnedOnForwardQueue = false;
}
pumpForwardQueue();
});
}
};
const enqueueForwardRequest = (task) => {
forwardQueue.push(task);
if (!hasWarnedOnForwardQueue && forwardQueue.length >= forwardQueueWarnThreshold) {
console.warn(
`Forward queue depth is ${forwardQueue.length}; consider increasing FORWARD_MAX_CONCURRENT or checking downstream latency.`
);
hasWarnedOnForwardQueue = true;
}
pumpForwardQueue();
};
const resolveHeaderValue = (value) => {
if (typeof value !== 'string') return value;
if (!value.startsWith('process.env.')) return value;
const envKey = value.slice('process.env.'.length);
return process.env[envKey] || '';
};
app.all(`${webhookPathPrefix}*routePath`, async (req, res) => {
const route = routeConfigIndex.get(routeCacheKey(req.path, req.method));
if (!route) {
res.status(404).send('Route not configured');
return;
}
try {
const headers = JSON.stringify(req.headers);
const queryParams = JSON.stringify(req.query);
const body = req.body ? req.body.toString() : null;
const webhookRecord = await Ingress.create({
method: req.method,
url: req.originalUrl,
headers,
query_params: queryParams,
body
});
const requestId = webhookRecord.id;
let bodyToForward = req.body;
if (modules.TemplateDriver) {
const transformed = modules.TemplateDriver.handle({
body: req.body,
headers: req.headers,
route: {
path: route.path,
method: route.method,
module: 'TemplateDriver'
},
options: route.moduleOptions || {}
});
if (transformed) {
bodyToForward = Buffer.from(JSON.stringify(transformed));
if (debugTransformedPayload) {
console.log('Transformed payload:', JSON.stringify(transformed, null, 2));
}
}
}
res.status(200).send('Picked up by Herald at the Gate');
route.targets.forEach((target) => {
const customHeaders = {};
if (Array.isArray(route.headers)) {
route.headers.forEach((header) => {
if (!header?.name) return;
customHeaders[header.name] = resolveHeaderValue(header.value);
});
}
const forwardHeaders = {
...req.headers,
...customHeaders,
'Content-Type': 'application/json'
};
enqueueForwardRequest(() => forwardRequest(target.url, req.method, forwardHeaders, bodyToForward, requestId));
});
} catch (err) {
console.error('Error processing webhook:', err);
res.status(500).send('Internal Server Error');
}
});
const PORT = process.env.PORT || 3000;
app.get('/api', (req, res) => {
res.json({
status: 'ok',
message: 'Request received'
});
});
if (serveUi) {
app.get(/.*/, (req, res, next) => {
if (
req.path.startsWith('/api')
|| req.path.startsWith(webhookPathPrefix)
) {
next();
return;
}
res.redirect('/ui/config-builder');
});
}
async function startServer() {
if (frontendAuthEnabled && (!frontendAuthUsername || !frontendAuthPassword || !frontendAuthSessionSecret)) {
throw new Error(
'FRONTEND_AUTH_ENABLED=true requires FRONTEND_AUTH_USERNAME, FRONTEND_AUTH_PASSWORD, and FRONTEND_AUTH_SESSION_SECRET.'
);
}
if (frontendOnlyApi && !adminProxySharedSecret) {
throw new Error(
'FRONTEND_ONLY_API=true requires ADMIN_PROXY_SHARED_SECRET so trusted frontend proxy requests can be verified.'
);
}
if (!frontendOnlyApi && !adminApiToken && !adminProxySharedSecret) {
console.warn(
'Security warning: FRONTEND_ONLY_API=false and no admin secrets are configured. /api routes may be reachable without authentication depending on network/proxy exposure.'
);
}
await runMigrations(sequelize);
await refreshTemplateRegistryFromDb();
await refreshRoutesFromDb();
const validation = validateRoutesConfig(routeConfigs);
if (!validation.valid) {
console.error('Invalid route/module configuration detected:');
validation.errors.forEach((error) => console.error(`- ${error}`));
throw new Error('Configuration validation failed. Fix database config records before starting the server.');
}
await loadModules();
app.listen(PORT, () => {
console.log(`Webhook proxy server running on port ${PORT}`);
});
}
startServer().catch(err => {
console.error('Failed to start server:', err);
process.exit(1);
});