-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
2066 lines (1903 loc) · 88.7 KB
/
server.js
File metadata and controls
2066 lines (1903 loc) · 88.7 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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Sentinel dVPN Network Audit — Server
* Thin Express server: API routes, SSE, imports from modular architecture.
*/
import express from 'express';
import cookieParser from 'cookie-parser';
import path from 'path';
import { fileURLToPath } from 'url';
import { EventEmitter } from 'events';
import { existsSync } from 'fs';
import { adminOnly, attachAdminFlag, safeEq, setAdminSessionValidator } from './core/auth.js';
import { rateLimit, sseLimit } from './core/rate-limit.js';
import { MNEMONIC, DENOM, GAS_PRICE, PORT, LCD_ENDPOINTS, PROJECT_ROOT, DNS_PRESETS, ACTIVE_DNS, setActiveDns } from './core/constants.js';
import { cachedWalletSetup, createFreshClient } from './core/wallet.js';
import { ensureLcd, getActiveLcd, cleanupRpc, getAllNodes } from './core/chain.js';
import { nodeStatusV3 } from './protocol/v3protocol.js';
import { createState, runAudit, runRetestSkips, runPlanTest, runSubPlanTest, getResults, saveResults, setActiveRunDir, setActiveDbRunId, triggerPipelineStop } from './audit/pipeline.js';
import {
insertRun, updateRunOnFinish,
insertResult, insertErrorLog,
searchNodes, getNodeDetail, getNodeErrors, getCountryList,
getActiveRun, getLastCompletedRun, getBandwidthHistory,
searchErrors, getNetworkStats,
listBatches, getBatchResults, getActiveBatch, getLastBatch,
insertBatch, updateBatchOnFinish, insertBatchResult,
getDb,
} from './core/db.js';
import * as continuous from './audit/continuous.js';
import { getInstalledVersions, verifyAllSdks, verifySdk } from './core/sdk-verify.js';
// Platform-aware WireGuard import — Windows has full implementation, others get stubs
let emergencyCleanupSync, watchdogCheck, WG_AVAILABLE, IS_ADMIN;
if (process.platform === 'win32') {
({ emergencyCleanupSync, watchdogCheck, WG_AVAILABLE, IS_ADMIN } = await import('./platforms/windows/wireguard.js'));
} else {
emergencyCleanupSync = () => {};
watchdogCheck = () => {};
WG_AVAILABLE = false;
IS_ADMIN = process.getuid?.() === 0 || false;
}
import { loadTransportCache, getCacheStats } from './core/transport-cache.js';
import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing';
import { SigningStargateClient, GasPrice } from '@cosmjs/stargate';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
process.env.PATH = path.join(__dirname, 'bin') + path.delimiter + (process.env.PATH || '');
// ─── Env sanity check ───────────────────────────────────────────────────────
const PUBLIC_MODE = process.env.PUBLIC_MODE === 'true';
const ADMIN_PATH = process.env.ADMIN_PATH || '/admin';
const ADMIN_TOKEN = process.env.ADMIN_TOKEN || '';
// M-05: use ephemeral per-process secret when ADMIN_TOKEN is absent; forbids
// forged signed cookies even in single-user/dev mode.
import crypto from 'node:crypto';
const COOKIE_SECRET = ADMIN_TOKEN || crypto.randomBytes(32).toString('hex');
// ─── Admin session store (H-02) ─────────────────────────────────────────────
// Map<sessionId, expiryMs>. Session ID is stored in the signed cookie instead
// of the raw ADMIN_TOKEN so cookie theft cannot recover the backend token.
// In-memory only: admin logouts drop entries; process restart invalidates all sessions.
const ADMIN_SESSIONS = new Map();
const ADMIN_SESSION_TTL_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
export function createAdminSession() {
const id = crypto.randomBytes(32).toString('hex');
ADMIN_SESSIONS.set(id, Date.now() + ADMIN_SESSION_TTL_MS);
return id;
}
export function isValidAdminSession(id) {
if (!id || typeof id !== 'string') return false;
const exp = ADMIN_SESSIONS.get(id);
if (!exp) return false;
if (exp < Date.now()) { ADMIN_SESSIONS.delete(id); return false; }
return true;
}
export function revokeAdminSession(id) {
if (id) ADMIN_SESSIONS.delete(id);
}
// Periodic cleanup of expired sessions — 1-hour interval
setInterval(() => {
const now = Date.now();
for (const [id, exp] of ADMIN_SESSIONS) {
if (exp < now) ADMIN_SESSIONS.delete(id);
}
}, 60 * 60 * 1000).unref();
// Inject the validator into the auth middleware. Must run before any admin request.
setAdminSessionValidator(isValidAdminSession);
if (PUBLIC_MODE && !ADMIN_TOKEN) {
console.error('');
console.error('ERROR: PUBLIC_MODE=true requires ADMIN_TOKEN to be set.');
console.error(' Without ADMIN_TOKEN, the admin surface has no protection.');
console.error(' Generate one: node -e "console.log(require(\'crypto\').randomBytes(32).toString(\'hex\'))"');
console.error(' Then add ADMIN_TOKEN=<value> to your .env file.');
console.error('');
process.exit(1);
}
if (!MNEMONIC || !MNEMONIC.trim()) {
console.warn('');
console.warn('⚠ MNEMONIC is not set.');
console.warn(' The server will start, but any test that signs a TX will fail.');
console.warn(' Fix: copy .env.example to .env and set MNEMONIC to a 12-word Cosmos phrase.');
console.warn('');
}
// ─── WireGuard Safety: cleanup on ANY exit ──────────────────────────────────
emergencyCleanupSync();
function onProcessExit() { cleanupRpc(); emergencyCleanupSync(); }
process.on('exit', onProcessExit);
// Graceful shutdown: stop the continuous loop before exit so it can't keep
// writing `runs` rows after the HTTP listener closes. Best-effort only; the
// hard exit fires after 2s regardless so Ctrl-C is still snappy.
function gracefulShutdown(signal, exitCode) {
console.log(`[server] ${signal} received — stopping continuous loop`);
try { continuous.stop(); } catch {}
onProcessExit();
setTimeout(() => process.exit(exitCode), 2_000).unref();
}
process.on('SIGINT', () => gracefulShutdown('SIGINT', 130));
process.on('SIGTERM', () => gracefulShutdown('SIGTERM', 143));
process.on('uncaughtException', (err) => {
const msg = err?.stack || err?.message || String(err);
console.error(`[uncaughtException] ${msg}`);
emergencyCleanupSync();
});
process.on('unhandledRejection', (reason) => {
console.error(`[unhandledRejection] ${String(reason)}`);
emergencyCleanupSync();
});
// Watchdog: every 5s, check if a tunnel has been up too long
setInterval(() => {
if (watchdogCheck()) {
broadcast('log', { msg: '⚠ WATCHDOG: Force-killed stale WireGuard tunnel' });
}
}, 5_000);
// ─── SSE ────────────────────────────────────────────────────────────────────
const emitter = new EventEmitter();
emitter.setMaxListeners(100);
const LOG_BUFFER_MAX = 100;
const logBuffer = [];
// ─── State Snapshot (persists volatile fields across restarts) ───────────────
const STATE_SNAPSHOT_FILE = path.join(__dirname, 'results', '.state-snapshot.json');
let _lastSnapshotTs = 0;
function saveStateSnapshot() {
// Throttle: save at most every 5 seconds to avoid disk thrashing
const now = Date.now();
if (now - _lastSnapshotTs < 5_000) return;
_lastSnapshotTs = now;
try {
_wfs(STATE_SNAPSHOT_FILE, JSON.stringify({
baselineHistory: state.baselineHistory,
nodeSpeedHistory: state.nodeSpeedHistory,
spentUdvpn: state.spentUdvpn,
balanceUdvpn: state.balanceUdvpn,
balance: state.balance,
estimatedTotalCost: state.estimatedTotalCost,
startedAt: state.startedAt,
baselineMbps: state.baselineMbps,
totalNodes: state.totalNodes,
status: state.status,
// Run-mode context — without this, /api/resume after a process bounce
// silently demotes a subscription run to P2P (the C-1 family of bugs).
runMode: state.runMode,
testRun: state.testRun,
runPlanId: state.runPlanId,
runSubscriptionId: state.runSubscriptionId,
runGranter: state.runGranter,
pricingMode: state.pricingMode,
activeSDK: state.activeSDK,
continuousLoop: state.continuousLoop,
}), 'utf8');
} catch { }
}
function broadcast(type, data = {}) {
if (type === 'log' && data.msg) {
logBuffer.push(data.msg);
if (logBuffer.length > LOG_BUFFER_MAX) logBuffer.shift();
}
if (type === 'state' || type === 'result') saveStateSnapshot();
// NOTE: spread `data` FIRST so a payload field named `type` (e.g. the node's
// service-type like 'wireguard') cannot clobber the SSE event type. The
// event type is the dispatch key — clients switch on d.type — so it must win.
emitter.emit('update', { ...data, type });
}
// ─── Continuous Loop SSE forwarding ─────────────────────────────────────────
// Forward loop and batch events from the continuous runner into the broadcast bus.
{
const LOOP_EVENTS = [
'loop:started', 'loop:stopping', 'loop:stopped', 'loop:error',
'iteration:start', 'iteration:end',
];
for (const evt of LOOP_EVENTS) {
continuous.on(evt, (data) => broadcast(evt, data || {}));
}
const BATCH_EVENTS = ['batch:start', 'batch:node:result', 'batch:end', 'batch:gap'];
for (const evt of BATCH_EVENTS) {
continuous.on(evt, (data) => broadcast(evt, data || {}));
}
// Forward per-node log/state/result/progress from inside the continuous
// pipeline so the live dashboard mirrors the admin dashboard 1:1 during
// continuous-loop runs (not only direct /api/start runs).
const LIVE_EVENTS = ['log', 'state', 'result', 'progress'];
for (const evt of LIVE_EVENTS) {
continuous.on(evt, (data) => broadcast(evt, data || {}));
}
}
// ─── State ──────────────────────────────────────────────────────────────────
const state = createState();
// Persist Broadcast Live across restarts so the operator's choice survives
// process bounces — without this, every restart silently flips public /live
// back to "paused" even though the admin UI still shows BROADCAST ON.
const BROADCAST_PREF_FILE = path.join(__dirname, 'results', '.broadcast-live');
try { state.broadcastLive = _rfs(BROADCAST_PREF_FILE, 'utf8').trim() === '1'; } catch { state.broadcastLive = false; }
function persistBroadcastPref() {
try { _wfs(BROADCAST_PREF_FILE, state.broadcastLive ? '1' : '0'); } catch {}
}
// Persist SDK choice to disk so it survives restarts
const SDK_PREF_FILE = path.join(__dirname, 'results', '.sdk-preference');
try { state.activeSDK = _rfs(SDK_PREF_FILE, 'utf8').trim() || 'js'; } catch { state.activeSDK = 'js'; }
// ─── Restore log buffer from most recent audit log (survives server restart) ─
try {
const logDir = path.join(__dirname, 'results');
const logFiles = _rd(logDir).filter(f => /^(audit|retest)-.*\.log$/.test(f)).sort().reverse();
if (logFiles.length > 0) {
const lastLog = _rfs(path.join(logDir, logFiles[0]), 'utf8');
const lines = lastLog.split('\n').filter(l => l.trim());
const tail = lines.slice(-LOG_BUFFER_MAX);
logBuffer.push(...tail);
}
} catch { }
// ─── Test Run Management ─────────────────────────────────────────────────────
import { readFileSync as _rfs, writeFileSync as _wfs, mkdirSync as _mkd, existsSync as _ex, readdirSync as _rd, copyFileSync as _cp } from 'fs';
const RUNS_DIR = path.join(__dirname, 'results', 'runs');
const RUNS_INDEX = path.join(RUNS_DIR, 'index.json');
if (!_ex(RUNS_DIR)) _mkd(RUNS_DIR, { recursive: true });
function loadRunsIndex() {
if (!_ex(RUNS_INDEX)) return { runs: [], activeRun: null };
return JSON.parse(_rfs(RUNS_INDEX, 'utf8'));
}
function saveRunsIndex(index) {
_wfs(RUNS_INDEX, JSON.stringify(index, null, 2), 'utf8');
}
function getNextRunNumber() {
const index = loadRunsIndex();
return (index.runs.length > 0 ? Math.max(...index.runs.map(r => r.number)) : 0) + 1;
}
function saveCurrentRun(label) {
const results = getResults();
if (results.length === 0) return null;
const num = getNextRunNumber();
const runDir = path.join(RUNS_DIR, `test-${String(num).padStart(3, '0')}`);
_mkd(runDir, { recursive: true });
// Save results
_wfs(path.join(runDir, 'results.json'), JSON.stringify(results, null, 2), 'utf8');
// Save summary
const passed = results.filter(r => r.actualMbps != null);
const failed = results.filter(r => r.actualMbps == null);
const pass10 = passed.filter(r => r.actualMbps >= 10);
const summary = [
`Test #${num} — ${label || 'Full Audit'}`,
`Date: ${new Date().toISOString()}`,
`${'='.repeat(60)}`,
`Total: ${results.length} | Passed: ${passed.length} | Failed: ${failed.length}`,
`Success Rate: ${(passed.length / results.length * 100).toFixed(1)}%`,
`Pass 10 Mbps SLA: ${pass10.length} (${(pass10.length / passed.length * 100).toFixed(1)}%)`,
``,
`── Passed Nodes (${passed.length}) ──`,
...passed.map(r => ` ${r.address.slice(0, 25)}... | ${r.actualMbps} Mbps | ${r.type} | ${r.moniker} | ${r.city}, ${r.country} | Google: ${r.googleAccessible === true ? 'YES' : r.googleAccessible === false ? 'NO' : '?'}`),
``,
`── Failed Nodes (${failed.length}) ──`,
...failed.map(r => ` ${r.address.slice(0, 25)}... | ${r.type} | ${r.moniker} | ${r.city}, ${r.country} | peers=${r.peers ?? '?'} | ${(r.error || '').slice(0, 80)}`),
].join('\n');
_wfs(path.join(runDir, 'summary.txt'), summary, 'utf8');
// Copy failures log
const failLog = path.join(__dirname, 'results', 'failures.jsonl');
if (_ex(failLog)) _cp(failLog, path.join(runDir, 'failures.jsonl'));
// Update index
const index = loadRunsIndex();
index.runs.push({
number: num,
label: label || 'Full Audit',
date: new Date().toISOString(),
total: results.length,
passed: passed.length,
failed: failed.length,
pass10: pass10.length,
sdk: state.activeSDK,
});
index.activeRun = num;
saveRunsIndex(index);
// ─── SQLite: mark the run as finished ────────────────────────────────────
if (state.activeDbRunId) {
try {
updateRunOnFinish(state.activeDbRunId, {
finished_at: Date.now(),
node_count: results.length,
pass_count: passed.length,
});
} catch (dbErr) {
console.error(`[db] updateRunOnFinish failed: ${dbErr.message}`);
}
}
return num;
}
function loadRun(num) {
const runDir = path.join(RUNS_DIR, `test-${String(num).padStart(3, '0')}`);
const resultsPath = path.join(runDir, 'results.json');
if (!_ex(resultsPath)) return null;
return JSON.parse(_rfs(resultsPath, 'utf8'));
}
// ─── Rehydrate state from results.json on startup ───────────────────────────
function rehydrateState(results) {
state.testedNodes = results.filter(r => r.actualMbps != null).length;
state.failedNodes = results.filter(r => r.actualMbps == null && !r.skipped && r.errorCode !== 'TEST_RUN_SKIP').length;
state.skippedNodes = results.filter(r => r.skipped || r.errorCode === 'TEST_RUN_SKIP').length;
// Do NOT set totalNodes here — it must come from snapshot (last known chain total).
// results.length = how many we tested, NOT how many exist on chain.
state.passed10 = results.filter(r => r.actualMbps != null && r.actualMbps >= 10).length;
state.passed15 = results.filter(r => r.actualMbps != null && r.actualMbps >= 15).length;
state.passedBaseline = results.filter(r => {
const thresh = r.dynamicThreshold != null ? r.dynamicThreshold : (r.baselineAtTest != null ? r.baselineAtTest * 0.5 : null);
return thresh != null && r.actualMbps >= thresh;
}).length;
}
{
const results = getResults();
if (results.length > 0) {
rehydrateState(results);
state.status = 'idle';
// Restore volatile state (history, balance, baseline) from snapshot
try {
const snap = JSON.parse(_rfs(STATE_SNAPSHOT_FILE, 'utf8'));
if (snap.baselineHistory?.length) state.baselineHistory = snap.baselineHistory;
if (snap.nodeSpeedHistory?.length) state.nodeSpeedHistory = snap.nodeSpeedHistory;
// Don't restore spentUdvpn from snapshot — it accumulates across restarts
// and causes negative balance display. Balance is queried fresh from chain on audit start.
// Only restore for display purposes, capped to prevent negative.
if (snap.balanceUdvpn) state.balanceUdvpn = snap.balanceUdvpn;
if (snap.spentUdvpn) state.spentUdvpn = Math.min(snap.spentUdvpn, state.balanceUdvpn);
const remaining = Math.max(0, state.balanceUdvpn - state.spentUdvpn);
state.balance = `${(remaining / 1_000_000).toFixed(4)} P2P`;
if (snap.estimatedTotalCost) state.estimatedTotalCost = snap.estimatedTotalCost;
if (snap.startedAt) state.startedAt = snap.startedAt;
if (snap.baselineMbps) state.baselineMbps = snap.baselineMbps;
if (snap.totalNodes) state.totalNodes = snap.totalNodes;
// Restore run-mode context so /api/resume after a process bounce can
// route to the correct pipeline (P2P vs subscription vs test).
if (snap.runMode) state.runMode = snap.runMode;
if (snap.testRun != null) state.testRun = snap.testRun;
if (snap.runPlanId) state.runPlanId = snap.runPlanId;
if (snap.runSubscriptionId) state.runSubscriptionId = snap.runSubscriptionId;
if (snap.runGranter) state.runGranter = snap.runGranter;
if (snap.pricingMode) state.pricingMode = snap.pricingMode;
if (snap.activeSDK) state.activeSDK = snap.activeSDK;
if (snap.continuousLoop != null) state.continuousLoop = snap.continuousLoop;
console.log(`State snapshot restored: baseline=${snap.baselineHistory?.length || 0} readings, speeds=${snap.nodeSpeedHistory?.length || 0} nodes, total=${state.totalNodes}, runMode=${state.runMode || 'none'}`);
} catch { }
// Resume the active test — DON'T create a new one on restart
const index = loadRunsIndex();
if (index.runs.length === 0) {
// First ever boot — save as Test #1
const num = saveCurrentRun('Initial Audit');
console.log(`Saved existing data as Test #${num}`);
}
// Always resume the last active test number
state.activeRunNumber = index.activeRun || (index.runs.length > 0 ? index.runs[index.runs.length - 1].number : 1);
console.log(`Resuming Test #${state.activeRunNumber} | ${results.length} results: ${state.testedNodes} passed, ${state.failedNodes} failed | SDK: ${state.activeSDK}`);
}
}
// ─── Express ────────────────────────────────────────────────────────────────
const app = express();
// Trust exactly one proxy hop so that req.ip is populated from X-Forwarded-For
// only when a real reverse proxy (nginx, Caddy, etc.) sits in front.
// Without this, req.ip is always the direct socket address — which is what
// clientIp() in core/rate-limit.js now uses exclusively (F-02).
app.set('trust proxy', 1);
app.use(express.json());
// cookie-parser with HMAC signing so admin_token cookie cannot be forged
app.use(cookieParser(COOKIE_SECRET));
// Serve static assets (logo, fonts etc.) but do NOT auto-serve index files.
// Routes below explicitly control which HTML file each path gets.
app.use(express.static(__dirname, { index: false }));
// ─── Security headers (all responses) ───────────────────────────────────────
app.use((req, res, next) => {
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('Referrer-Policy', 'no-referrer');
// M-01: clickjacking defence covers admin routes (public CSP has frame-ancestors).
res.setHeader('X-Frame-Options', 'DENY');
next();
});
// ─── CSP helper (public HTML responses only) ─────────────────────────────────
const PUBLIC_CSP = [
"default-src 'self'",
// flagcdn.com serves ISO 3166 country flag PNGs. Needed because Windows
// Chrome/Edge don't render regional-indicator emoji as flag glyphs — they
// fall back to letter tiles ("US", "DE") which users reported as "distorted".
"img-src 'self' data: https://flagcdn.com",
// sentinel.css @imports Noto Sans Mono from jsDelivr (Plan Manager canon).
// Europa Bold is self-hosted from /fonts/, so no external font-src needed.
"style-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net",
"script-src 'self' 'unsafe-inline'",
"connect-src 'self'",
"font-src 'self' data: https://cdn.jsdelivr.net",
"frame-ancestors 'none'",
"base-uri 'self'",
"form-action 'self'",
].join('; ');
function setPublicCsp(res) {
res.setHeader('Content-Security-Policy', PUBLIC_CSP);
}
// ─── Rate-limit tiers ────────────────────────────────────────────────────────
// "public-read": 120 req / 60 s for all read-only public API endpoints.
const rlPublicRead = rateLimit({ windowMs: 60_000, max: 120, bucket: 'public-read' });
// "public-sse": max 5 concurrent SSE connections per IP.
const rlPublicSse = sseLimit({ maxPerIp: 5, bucket: 'public-sse' });
// ─── Public routes: no auth, read-only ──────────────────────────────────────
// Root: serve public dashboard when PUBLIC_MODE=true, otherwise admin.html (or redirect to login)
app.get('/', attachAdminFlag, (req, res) => {
if (PUBLIC_MODE) {
setPublicCsp(res);
return res.sendFile(path.join(__dirname, 'public.html'));
}
// PUBLIC_MODE=false: no auth check needed for local/single-user setups
if (!ADMIN_TOKEN || req.admin) {
return res.sendFile(path.join(__dirname, 'admin.html'));
}
res.redirect(ADMIN_PATH + '/login');
});
// Per-node detail page (public, read-only SPA served on any /node/:addr path)
app.get('/node/:addr', attachAdminFlag, (req, res) => {
setPublicCsp(res);
res.sendFile(path.join(__dirname, 'node.html'));
});
// Public live-testing view — shareable URL, zero action buttons
app.get('/live', attachAdminFlag, (req, res) => {
setPublicCsp(res);
res.sendFile(path.join(__dirname, 'live.html'));
});
// Public about page — static, no action buttons
app.get('/about', attachAdminFlag, (req, res) => {
setPublicCsp(res);
res.sendFile(path.join(__dirname, 'about.html'));
});
// ─── Public API: read-only, no wallet or chain writes ────────────────────────
// NOTE: these handlers MUST NOT import from audit/, core/wallet.js, or chain write paths.
// A grep-based assertion in test/security.test.js enforces this invariant on every build.
/**
* GET /api/public/nodes
* Query params: q, country, service, sort, window, limit, offset
* Returns one row per node with pass_count, pass_rate, pass_bar.
*/
app.get('/api/public/nodes', attachAdminFlag, rlPublicRead, (req, res) => {
try {
const q = req.query.q || null;
const country = req.query.country || null;
const service = req.query.service || null;
const sort = req.query.sort || 'tested_desc';
const win = Math.min(parseInt(req.query.window || '25', 10), 100);
const limit = Math.min(parseInt(req.query.limit || '50', 10), 500);
const offset = parseInt(req.query.offset || '0', 10);
const runId = req.query.runId ? parseInt(req.query.runId, 10) : null;
const nodes = searchNodes({ q, country, service, sort, window: win, limit, offset, runId });
res.json({ total: nodes.length, offset, limit, window: win, results: nodes });
} catch (err) {
console.error('[api/public/nodes]', err);
res.status(500).json({ error: 'Internal error' });
}
});
/**
* GET /api/public/node/:addr?historyLimit=N
* Returns { node, history, errors } for a single node.
*/
app.get('/api/public/node/:addr', attachAdminFlag, rlPublicRead, (req, res) => {
try {
const addr = req.params.addr;
const hLimit = parseInt(req.query.historyLimit || '100', 10);
const detail = getNodeDetail(addr, { historyLimit: hLimit });
if (!detail.node) {
return res.status(404).json({ error: 'Node not found' });
}
res.json(detail);
} catch (err) {
console.error('[api/public/node]', err);
res.status(500).json({ error: 'Internal error' });
}
});
/**
* GET /api/public/node/:addr/errors?limit=N&stage=X
* Returns error_log rows for a node, optionally filtered by stage.
*/
app.get('/api/public/node/:addr/errors', attachAdminFlag, rlPublicRead, (req, res) => {
try {
const addr = req.params.addr;
const limit = Math.min(parseInt(req.query.limit || '50', 10) || 50, 500);
const stage = req.query.stage || null;
const errors = getNodeErrors(addr, { limit, stage });
res.json({ node_addr: addr, total: errors.length, errors });
} catch (err) {
console.error('[api/public/node/errors]', err);
res.status(500).json({ error: 'Internal error' });
}
});
/**
* GET /api/public/errors?q=&stage=&limit=&offset=
* Cross-node error search. Returns recent failures across ALL nodes.
* q matches node_addr, moniker, or error_message (LIKE, case-insensitive).
* stage filters error_logs.stage exactly.
* limit default 100 cap 500; offset default 0. Ordered by captured_at DESC.
*/
app.get('/api/public/errors', attachAdminFlag, rlPublicRead, (req, res) => {
try {
const q = req.query.q || null;
const stage = req.query.stage || null;
const limit = Math.min(parseInt(req.query.limit || '100', 10) || 100, 500);
const offset = Math.max(parseInt(req.query.offset || '0', 10) || 0, 0);
const { total, items } = searchErrors({ q, stage, limit, offset });
res.json({ total, items });
} catch (err) {
console.error('[api/public/errors]', err);
res.status(500).json({ error: 'Internal error' });
}
});
/**
* GET /api/public/countries
* Returns distinct countries with node counts.
*/
app.get('/api/public/countries', attachAdminFlag, rlPublicRead, (req, res) => {
try {
const countries = getCountryList();
res.json({ total: countries.length, countries });
} catch (err) {
console.error('[api/public/countries]', err);
res.status(500).json({ error: 'Internal error' });
}
});
/**
* GET /api/public/runs/current — current in-progress batch (+ per-node
* results so far) so /live can hydrate on refresh without waiting for SSE.
* Returns 404 when nothing is mid-flight.
*/
app.get('/api/public/runs/current', attachAdminFlag, rlPublicRead, (req, res) => {
try {
const data = state.broadcastLive ? getActiveBatch() : getLastBatch();
if (!data) return res.status(404).json({ error: 'No active run' });
const { batch, nodes } = data;
res.json({
id: batch.id,
started_at: batch.started_at,
finished_at: batch.finished_at,
snapshot_size: batch.snapshot_size,
passed: batch.passed,
failed: batch.failed,
mode: batch.mode,
// Mirror the in-memory run mode so /live renders the same badge as admin
// before any SSE state event arrives. runPlanId is null unless plan mode.
runMode: state.runMode || null,
runPlanId: state.runPlanId || null,
nodes,
});
} catch (err) {
console.error('[api/public/runs/current]', err);
res.status(500).json({ error: 'Internal error' });
}
});
/**
* GET /api/public/runs/last — most recent completed run, or 404.
*/
app.get('/api/public/runs/last', attachAdminFlag, rlPublicRead, (req, res) => {
try {
// Prefer the last completed batch (has nodes) so /live can hydrate fully
// on refresh without waiting for SSE. Fall back to legacy run row only
// when no batch has ever been recorded.
const last = getLastBatch();
if (last) {
const { batch, nodes } = last;
return res.json({
id: batch.id,
started_at: batch.started_at,
finished_at: batch.finished_at,
snapshot_size: batch.snapshot_size,
passed: batch.passed,
failed: batch.failed,
mode: batch.mode,
nodes,
});
}
const run = getLastCompletedRun();
if (!run) return res.status(404).json({ error: 'No completed runs' });
res.json(run);
} catch (err) {
console.error('[api/public/runs/last]', err);
res.status(500).json({ error: 'Internal error' });
}
});
/**
* GET /api/public/node/:addr/bandwidth?limit=N — bandwidth chart data.
*/
app.get('/api/public/node/:addr/bandwidth', attachAdminFlag, rlPublicRead, (req, res) => {
try {
const addr = req.params.addr;
const limit = Math.min(parseInt(req.query.limit || '100', 10), 500);
const history = getBandwidthHistory(addr, { limit });
res.json({ node_addr: addr, total: history.length, history });
} catch (err) {
console.error('[api/public/node/bandwidth]', err);
res.status(500).json({ error: 'Internal error' });
}
});
app.get('/api/public/runs', attachAdminFlag, rlPublicRead, (req, res) => {
const index = loadRunsIndex();
const safe = (index.runs || []).map(r => ({
number: r.number,
label: r.label,
date: r.date,
total: r.total,
passed: r.passed,
failed: r.failed,
pass10: r.pass10,
}));
res.json({ runs: safe, total: safe.length });
});
app.get('/api/public/stats', attachAdminFlag, rlPublicRead, (req, res) => {
try {
// Use DB aggregate instead of iterating the in-memory results array (F-13).
const { totalNodes, passingPct, medianMbps, lastRunAt } = getNetworkStats();
res.json({
totalNodes,
passingPct,
medianMbps,
lastRunAt,
status: continuous.status().running ? 'running' : 'idle',
});
} catch (err) {
console.error('[api/public/stats]', err);
res.status(500).json({ error: 'Internal error' });
}
});
/**
* GET /api/public/batches?limit=N
* Returns the last N batches (default 50, max 100), newest first.
* Each batch has: id, started_at, finished_at, snapshot_size, passed, failed, mode.
*/
app.get('/api/public/batches', attachAdminFlag, rlPublicRead, (req, res) => {
try {
const limit = Math.min(parseInt(req.query.limit || '50', 10) || 50, 100);
const batches = listBatches({ limit });
res.json({ total: batches.length, batches });
} catch (err) {
console.error('[api/public/batches]', err);
res.status(500).json({ error: 'Internal error' });
}
});
/**
* GET /api/public/batch/:id?limit=N&offset=N
* Returns the batch header + public-safe node results for one batch.
* Strips wallet, SDK, OS, diag fields — only:
* node_address, type, moniker, country, city, actual_mbps,
* peers, max_peers, error, error_code, tested_at
*/
app.get('/api/public/batch/:id', attachAdminFlag, rlPublicRead, (req, res) => {
try {
const batchId = parseInt(req.params.id, 10);
if (!batchId || batchId < 1) return res.status(400).json({ error: 'Invalid batch id' });
const limit = Math.min(parseInt(req.query.limit || '500', 10) || 500, 1000);
const offset = Math.max(parseInt(req.query.offset || '0', 10) || 0, 0);
const { batch, results } = getBatchResults(batchId, { limit, offset });
if (!batch) return res.status(404).json({ error: 'Batch not found' });
const { snapshot_addresses: _addrs, ...batchPublic } = batch;
res.json({ batch: batchPublic, results, total: results.length });
} catch (err) {
console.error('[api/public/batch]', err);
res.status(500).json({ error: 'Internal error' });
}
});
// ─── Admin login / logout ─────────────────────────────────────────────────────
app.get(ADMIN_PATH + '/login', (req, res) => {
res.send(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Sentinel Audit — Admin Login</title>
<link rel="stylesheet" href="/sentinel.css">
<style>
body { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login-card { max-width: 380px; width: 100%; padding: 40px 36px; }
.login-title { font-family: var(--font-display); font-size: 20px; font-weight: 700; letter-spacing: 2px; margin: 0 0 6px; color: var(--text); }
.login-sub { font-size: 12px; color: var(--text-dim); margin: 0 0 28px; }
.login-label { font-size: 10px; text-transform: uppercase; letter-spacing: 1.5px; color: var(--text-dim); display: block; margin-bottom: 6px; }
.login-input { width: 100%; margin-bottom: 20px; }
</style>
</head>
<body class="boot-pending">
<script>document.documentElement.dataset.theme = localStorage.getItem('theme') || 'dark'; document.addEventListener('DOMContentLoaded', () => document.body.classList.remove('boot-pending'));</script>
<div class="card login-card">
<h1 class="login-title">SENTINEL AUDIT</h1>
<p class="login-sub">Admin access required</p>
<form method="POST" action="${ADMIN_PATH}/login">
<label class="login-label" for="token">Admin Token</label>
<input class="login-input" type="password" id="token" name="token" placeholder="Enter admin token" autocomplete="current-password" required>
<button class="btn-primary btn-block" type="submit">Sign In</button>
</form>
</div>
</body>
</html>`);
});
app.post(ADMIN_PATH + '/login', rateLimit({ windowMs: 60_000, max: 10, bucket: 'login' }), (req, res) => {
const { token } = req.body || {};
if (token && ADMIN_TOKEN && safeEq(token, ADMIN_TOKEN)) {
// H-02: store opaque session ID in the cookie, not the raw ADMIN_TOKEN.
// Cookie theft (XSS, stolen jar) no longer recovers the backend token.
const sessionId = createAdminSession();
res.cookie('admin_session', sessionId, {
signed: true,
httpOnly: true,
sameSite: 'strict',
secure: process.env.INSECURE_COOKIE !== 'true',
maxAge: ADMIN_SESSION_TTL_MS,
});
// Clear any legacy admin_token cookie from earlier deploys
res.clearCookie('admin_token');
return res.redirect(ADMIN_PATH);
}
res.status(401).send(`<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Login failed</title>
<link rel="stylesheet" href="/sentinel.css">
<style>body{display:flex;align-items:center;justify-content:center;min-height:100vh;padding:20px}.fail{max-width:380px;width:100%;text-align:center}.fail a{color:var(--accent);text-decoration:none;font-weight:600}.fail a:hover{color:var(--accent-bright)}</style>
</head><body class="boot-pending">
<script>document.documentElement.dataset.theme = localStorage.getItem('theme') || 'dark'; document.addEventListener('DOMContentLoaded', () => document.body.classList.remove('boot-pending'));</script>
<div class="fail">
<div class="callout-error" style="margin-bottom:16px">Invalid token</div>
<a href="${ADMIN_PATH}/login">Try again</a>
</div></body></html>`);
});
app.post(ADMIN_PATH + '/logout', (req, res) => {
if (req.headers['x-admin-request'] !== '1') {
return res.status(403).json({ error: 'Forbidden', hint: 'Include X-Admin-Request: 1 header' });
}
const sid = req.signedCookies?.admin_session;
if (sid) revokeAdminSession(sid);
res.clearCookie('admin_session');
res.clearCookie('admin_token'); // legacy
res.redirect(PUBLIC_MODE ? '/' : ADMIN_PATH + '/login');
});
// ─── Admin dashboard ──────────────────────────────────────────────────────────
app.get(ADMIN_PATH, adminOnly, (req, res) => {
res.sendFile(path.join(__dirname, 'admin.html'));
});
// Fast stats-only (no results payload) — admin only (includes wallet balance)
app.get('/api/stats', adminOnly, (req, res) => {
res.json({ state });
});
// ─── SDK version + GitHub parity endpoints ──────────────────────────────────
/** Installed SDK versions — instant, no network. */
app.get('/api/sdk-versions', adminOnly, async (req, res) => {
try {
const { readFileSync } = await import('fs');
const versions = getInstalledVersions(__dirname);
const pkg = JSON.parse(readFileSync(path.join(__dirname, 'package.json'), 'utf8'));
res.json({ tester: { version: pkg.version, name: pkg.name }, sdks: versions });
} catch (err) {
console.error('[api/sdk-versions]', err);
res.status(500).json({ error: 'Internal error' });
}
});
/** Cached verification state (avoid re-downloading on every UI poll). */
let _sdkVerifyCache = { ts: 0, data: null };
const SDK_VERIFY_TTL_MS = 5 * 60 * 1000;
/** Verify every SDK matches its GitHub tag. Slow (~5s) — downloads tarballs. */
app.get('/api/sdk-verify', adminOnly, async (req, res) => {
const now = Date.now();
const forceRefresh = req.query.refresh === '1';
if (!forceRefresh && _sdkVerifyCache.data && (now - _sdkVerifyCache.ts) < SDK_VERIFY_TTL_MS) {
res.setHeader('x-cache', 'hit');
return res.json(_sdkVerifyCache.data);
}
try {
const results = await verifyAllSdks(__dirname);
_sdkVerifyCache = { ts: now, data: results };
res.setHeader('x-cache', 'miss');
res.json(results);
} catch (err) {
console.error('[api/sdk-verify]', err);
res.status(500).json({ error: 'Internal error' });
}
});
/** Verify one SDK by key. ?key=blue-js or ?key=tkd-js */
app.get('/api/sdk-verify/:key', adminOnly, async (req, res) => {
try {
const result = await verifySdk(req.params.key, __dirname);
res.json(result);
} catch (err) {
console.error('[api/sdk-verify]', err);
res.status(500).json({ error: 'Internal error' });
}
});
// Full state + results
app.get('/api/state', adminOnly, (req, res) => {
const results = getResults();
res.json({ state, results });
});
app.get('/api/results', adminOnly, (req, res) => {
const results = getResults();
const page = parseInt(req.query.page || '1', 10);
const limit = parseInt(req.query.limit || '100', 10);
const start = (page - 1) * limit;
res.json({ total: results.length, page, results: results.slice(start, start + limit) });
});
// ─── Public SSE stream ──────────────────────────────────────────────────────
// Broadcasts the same events but strips any operator-only fields.
const PUBLIC_EVENT_WHITELIST = new Set([
'loop:started',
'loop:stopping',
'loop:stopped',
'loop:error',
'iteration:start',
'iteration:end',
// Batch-model events (each full node-sweep is one batch)
'batch:start',
'batch:node:result',
'batch:end',
'batch:gap',
// Live operator log lines — needed so /live shows real-time activity.
// sanitizeForPublic already truncates evt.msg to 400 chars.
'log',
// Full state + per-node result events — /live mirrors the admin dashboard
// counters and per-row results 1:1 when broadcastLive is on.
'state',
'result',
'progress',
]);
// Keep only the counters / progress fields a public viewer needs.
// Strips wallet, balance*, spent*, MNEMONIC-derived data, errorMessage internals.
const PUBLIC_STATE_KEYS = [
'status',
'totalNodes',
'testedNodes',
'failedNodes',
'skippedNodes',
'passed10',
'passed15',
'passedBaseline',
'baselineMbps',
'baselineHistory',
'nodeSpeedHistory',
'currentNode',
'currentType',
'currentLocation',
'startedAt',
'completedAt',
'activeRunNumber',
'testRun',
'continuousLoop',
'pricingMode',
// Surfaces the active mode + plan id so /live can render the same
// "Plan #N / P2P / Test Run" badge the admin shows.
'runMode',
'runPlanId',
];
function sanitizePublicState(s) {
if (!s || typeof s !== 'object') return {};
const out = {};
for (const k of PUBLIC_STATE_KEYS) if (s[k] !== undefined) out[k] = s[k];
return out;
}
// Mirror of admin's per-node result row, minus operator-internal fields.
function sanitizePublicResult(r) {
if (!r || typeof r !== 'object') return null;
return {
address: r.address,
moniker: r.moniker,
serviceType: r.type ?? r.serviceType,
countryCode: r.countryCode,
city: r.city,
actualMbps: r.actualMbps,
advertisedMbps: r.advertisedMbps,
peers: r.peers,
maxPeers: r.maxPeers,
errorCode: r.errorCode,
error: r.error ? String(r.error).slice(0, 200) : null,
skipped: r.skipped === true ? true : undefined,
inPlan: r.inPlan === true ? true : undefined,
testedAt: r.testedAt,
baselineAtTest: r.baselineAtTest,
dynamicThreshold: r.dynamicThreshold,
pass10mbps: r.pass10mbps,
latencyMs: r.latencyMs,
handshakeMs: r.handshakeMs,
sessionMs: r.sessionMs,
};
}
function sanitizeForPublic(evt) {
const safe = { type: evt.type };
// Nested state/result payloads (admin emits broadcast('state', { state }) / broadcast('result', { result }))
if (evt.state && typeof evt.state === 'object') safe.state = sanitizePublicState(evt.state);
if (evt.result && typeof evt.result === 'object') {
const sr = sanitizePublicResult(evt.result);
if (sr) safe.result = sr;
}
if (evt.iteration != null) safe.iteration = evt.iteration;
if (evt.mode != null) safe.mode = evt.mode;
if (evt.passed != null) safe.passed = evt.passed;
if (evt.failed != null) safe.failed = evt.failed;
if (evt.durationMs != null) safe.durationMs = evt.durationMs;
if (evt.error != null) safe.error = String(evt.error).slice(0, 200);
// batch:* event fields — only public-safe node-level data
if (evt.batchId != null) safe.batchId = evt.batchId;
if (evt.snapshotSize != null) safe.snapshotSize = evt.snapshotSize;
if (evt.startedAt != null) safe.startedAt = evt.startedAt;
if (evt.gapMs != null) safe.gapMs = evt.gapMs;
if (evt.nextBatchAt != null) safe.nextBatchAt = evt.nextBatchAt;
// batch:node:result public-safe fields.
// The payload's `type` field (service type: 'wireguard' / 'v2ray' / 1 / 2)
// would collide with the SSE dispatch `type`, so forward it as `serviceType`.
if (evt.address != null) safe.address = evt.address;
if (evt.serviceType != null) safe.serviceType = evt.serviceType;
if (evt.countryCode != null) safe.countryCode = evt.countryCode;
if (evt.city != null) safe.city = evt.city;
if (evt.actualMbps != null) safe.actualMbps = evt.actualMbps;
if (evt.peers != null) safe.peers = evt.peers;