-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
2574 lines (2398 loc) · 114 KB
/
server.js
File metadata and controls
2574 lines (2398 loc) · 114 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, RPC_ENDPOINTS, PROJECT_ROOT, DNS_PRESETS, ACTIVE_DNS, setActiveDns } from './core/constants.js';
import { getSettings, updateSettings, getDefaultSettings } from './core/settings.js';
import { queryReports as queryOnchainReports } from './core/onchain-report.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, getRunStats,
listBatches, getBatchResults, getActiveBatch, getLastBatch,
insertBatch, updateBatchOnFinish, insertBatchResult,
reopenBatch, getBatchById, getBatchWithNodes,
getDb,
} from './core/db.js';
import * as continuous from './audit/continuous.js';
import { getInstalledVersions, verifyAllSdks, verifySdk } from './core/sdk-verify.js';
// Force line-buffered stdout/stderr so boot diagnostics flush immediately
// even when redirected to a file (Windows defaults to block-buffering, which
// hides every console.log if the process hangs before app.listen).
try { process.stdout._handle?.setBlocking?.(true); } catch (e) { console.error('[boot] stdout setBlocking failed:', e.message); }
try { process.stderr._handle?.setBlocking?.(true); } catch (e) { console.error('[boot] stderr setBlocking failed:', e.message); }
// Platform-aware WireGuard import — Windows / Linux / macOS each have full implementations
// Wrapped in a 5s timeout: the windows module runs sync `execSync` probes
// (`net session`, `where wireguard.exe`, `wg-quick --version`) at its own
// module scope. Any of those can stall on a slow Service Control Manager
// and deadlock the entire server boot. Falling back to WG_AVAILABLE=false
// is preferable to a silent zombie.
let emergencyCleanupSync, watchdogCheck, WG_AVAILABLE, IS_ADMIN;
const _wgFallback = () => {
console.error('[boot] WireGuard module init timed out — continuing with WG disabled');
emergencyCleanupSync = () => {};
watchdogCheck = () => {};
WG_AVAILABLE = false;
IS_ADMIN = false;
};
const _wgImport = (() => {
if (process.platform === 'win32') return import('./platforms/windows/wireguard.js');
if (process.platform === 'linux') return import('./platforms/linux/wireguard.js');
if (process.platform === 'darwin') return import('./platforms/macos/wireguard.js');
return null;
})();
if (_wgImport) {
const _wgTimeout = new Promise((_, reject) => setTimeout(() => reject(new Error('wg-import-timeout')), 5000));
try {
({ emergencyCleanupSync, watchdogCheck, WG_AVAILABLE, IS_ADMIN } = await Promise.race([_wgImport, _wgTimeout]));
} catch (e) {
console.error(`[boot] WireGuard import failed: ${e.message}`);
_wgFallback();
}
} 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';
// Walk RPC_ENDPOINTS in order and return the first SigningStargateClient that
// connects. Replaces a hardcoded `rpc.sentinel.co:443` connect that returned
// stale balances when that node fell behind tip while reporting catching_up=false.
async function connectWithRpcFailover(wallet) {
const opts = { gasPrice: GasPrice.fromString(GAS_PRICE) };
let lastErr;
for (const url of RPC_ENDPOINTS) {
try {
return await SigningStargateClient.connectWithSigner(url, wallet, opts);
} catch (e) {
lastErr = e;
}
}
throw lastErr || new Error('All RPC endpoints unreachable');
}
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
// Cap to bound memory under sustained brute-force or buggy clients that never
// log out. Map iteration order is insertion order — drop the oldest entry when
// we exceed the cap. 1000 sessions × ~80 bytes = ~80 KB worst case.
const ADMIN_SESSIONS_MAX = 1000;
export function createAdminSession() {
const id = crypto.randomBytes(32).toString('hex');
if (ADMIN_SESSIONS.size >= ADMIN_SESSIONS_MAX) {
const oldest = ADMIN_SESSIONS.keys().next().value;
if (oldest) ADMIN_SESSIONS.delete(oldest);
}
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 ──────────────────────────────────
// Boot-time cleanup is deferred until AFTER app.listen — running it inline
// here can block the event loop for 10–30s on a slow Service Control Manager
// (sc query / sc stop / sc delete each carry their own 5s timeouts).
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));
// Crash loud, crash fast. Without process.exit, the handler runs cleanup and
// the event loop keeps going on a half-initialised state — silent zombie.
process.on('uncaughtException', (err) => {
const msg = err?.stack || err?.message || String(err);
console.error(`[uncaughtException] ${msg}`);
try { emergencyCleanupSync(); } catch (e) { console.error('[uncaughtException] cleanup failed:', e.message); }
setTimeout(() => process.exit(1), 500).unref();
});
process.on('unhandledRejection', (reason) => {
const msg = reason?.stack || reason?.message || String(reason);
console.error(`[unhandledRejection] ${msg}`);
try { emergencyCleanupSync(); } catch (e) { console.error('[unhandledRejection] cleanup failed:', e.message); }
setTimeout(() => process.exit(1), 500).unref();
});
// 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);
// Sized to comfortably hold the full log of a typical run (~10–20 lines per
// node × hundreds of nodes) plus headroom. SSE init replays this to admin and
// live so a refresh / reconnect / resume sees the run's full prior history,
// not just the last few lines.
const LOG_BUFFER_MAX = 5000;
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,
// Persist the open batch handle so /api/resume after a process bounce
// can re-attach to the same batches row instead of starting a new one.
activeBatchId: state.activeBatchId || 0,
// Address of the in-flight node when stop hit, so resume can hoist
// it back to the front of the next scan order.
resumeHeadAddr: state.resumeHeadAddr || null,
// Path of the audit log file currently being appended to. Survives
// process bounce so /api/resume reuses the same file instead of
// creating a fresh `audit-<ts>.log` and orphaning prior entries.
auditLogPath: state.auditLogPath || null,
// SQLite runs.id of the in-flight run. Without this, /api/resume after
// a process bounce leaves _activeDbRunId=null and post-resume failures
// skip insertErrorLog — the node-detail popup then has nothing to show.
activeDbRunId: state.activeDbRunId || null,
}), '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 });
}
// Use this for any state change where the client must replace its row table:
// run start, /api/clear, retest, load. The admin client treats `msg.results`
// presence as the wipe signal — omitting it leaves stale rows on screen, which
// has burned us before (5 stale TEST_RUN_SKIP rows after New Test).
function broadcastStateFresh(extra = {}) {
broadcast('state', { state, results: getResults(), ...extra });
}
// ─── Batch persistence wrapper for direct pipeline calls ────────────────────
// audit/continuous.js writes batches/batch_results for continuous-loop runs.
// Direct pipeline calls (subscription start/resume, p2p start/resume, plan-test,
// retest) bypass continuous.js entirely — without this wrapper they never
// produce a `batches` row, so /api/public/runs/current returns 404 and the
// /live page can't reconstruct in-flight progress on refresh.
//
// `mode` MUST match the run intent so the dashboard never confuses subscription
// (Plan #N), p2p (pay-per-GB), and test (TEST_RUN_SKIP) runs.
function withBatchTracking(baseBroadcast, mode, opts = {}) {
// Resume re-attaches to the previously-open batch via opts.existingBatchId so
// /live's hydrate-from-DB path returns the full pre-pause + post-resume row
// set as one batch. Without this, every resume opens a fresh batches row and
// the table on /live wipes back to whatever was tested AFTER resume only.
let batchId = opts.existingBatchId ? Number(opts.existingBatchId) : 0;
let opened = batchId > 0;
let closed = false;
let startEmitted = false;
if (opened) {
state.activeBatchId = batchId;
try { reopenBatch(batchId, 'real'); } catch (e) { console.error('[withBatchTracking reopen]', e.message); }
}
return function tracked(type, data = {}) {
try {
if (!closed && !opened && type === 'result' && data && data.result) {
batchId = insertBatch({
started_at: Date.now(),
snapshot_size: state.totalNodes || 0,
mode,
}, 'real');
state.activeBatchId = batchId;
opened = true;
}
if (opened && !closed && type === 'result' && data && data.result) {
const r = data.result;
insertBatchResult(batchId, {
address: r.address || '',
moniker: r.moniker || null,
country: r.country || null,
country_code: r.countryCode || r.country_code || null,
city: r.city || null,
type: r.type || null,
actual_mbps: r.actualMbps ?? null,
peers: r.peers ?? null,
max_peers: r.maxPeers ?? null,
error: r.error ? String(r.error).slice(0, 200) : null,
error_code: r.errorCode || null,
tested_at: Date.now(),
baseline_mbps: r.baselineAtTest ?? r.baselineMbps ?? null,
}, 'real');
// Emit batch:start once + batch:node:result per row so /live's Current
// Batch panel ticks for direct-pipeline runs (sub-plan, p2p, retest)
// exactly like continuous.js does. Without this, /live falls back on
// resultsArr.length which can desync if any 'result' SSE event is
// dropped (broadcastLive flip race, reconnect gap), leaving the
// counter stuck at the count from the last full REST hydrate.
if (!startEmitted) {
baseBroadcast('batch:start', {
batchId,
iteration: null,
startedAt: new Date().toISOString(),
snapshotSize: state.totalNodes || 0,
mode,
});
startEmitted = true;
}
baseBroadcast('batch:node:result', {
batchId,
address: r.address || '',
moniker: r.moniker || null,
country: r.country || null,
countryCode: r.countryCode || r.country_code || null,
city: r.city || null,
serviceType: r.type || null,
actualMbps: r.actualMbps ?? null,
baselineMbps: r.baselineAtTest ?? r.baselineMbps ?? null,
peers: r.peers ?? null,
maxPeers: r.maxPeers ?? null,
error: r.error ? String(r.error).slice(0, 200) : null,
errorCode: r.errorCode || null,
testedAt: Date.now(),
});
}
if (opened && !closed && type === 'state' && data && data.state) {
const status = data.state.status;
if (status === 'done' || status === 'error' || status === 'stopped') {
const passed = data.state.testedNodes || 0;
const failed = data.state.failedNodes || 0;
updateBatchOnFinish(batchId, {
finished_at: Date.now(),
passed,
failed,
}, 'real');
closed = true;
if (startEmitted) {
baseBroadcast('batch:end', {
batchId,
passed,
failed,
durationMs: null,
});
}
// Keep state.activeBatchId so /api/resume can find this batch and
// reopen it. /api/start clears it explicitly when a new test begins.
}
}
} catch (err) {
console.error(`[withBatchTracking ${mode}] ${err.message}`);
}
baseBroadcast(type, data);
};
}
// ─── 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'; }
// Helper: re-hydrate logBuffer from a specific log file on disk. Used both at
// boot (after snapshot restore) and on /api/resume so the SSE init replay
// always carries the in-flight run's full prior log history — not the last
// few lines, and not a different file's contents.
function hydrateLogBufferFromFile(filePath) {
try {
const txt = _rfs(filePath, 'utf8');
const lines = txt.split('\n').filter(l => l.trim());
const tail = lines.slice(-LOG_BUFFER_MAX);
logBuffer.length = 0;
logBuffer.push(...tail);
return tail.length;
} catch { return 0; }
}
// ─── 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 };
try {
return JSON.parse(_rfs(RUNS_INDEX, 'utf8'));
} catch (err) {
console.error(`[loadRunsIndex] corrupt or unreadable ${RUNS_INDEX}: ${err.message}`);
return { runs: [], activeRun: null };
}
}
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: ${results.length > 0 ? (passed.length / results.length * 100).toFixed(1) : '0.0'}%`,
`Pass 10 Mbps SLA: ${pass10.length} (${passed.length > 0 ? (pass10.length / passed.length * 100).toFixed(1) : '0.0'}%)`,
``,
`── 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,
spent_udvpn: Number(state.spentUdvpn) || 0,
refunded_udvpn: Number(state.refundedUdvpn) || 0,
});
} 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;
if (snap.activeBatchId) state.activeBatchId = snap.activeBatchId;
if (snap.resumeHeadAddr) state.resumeHeadAddr = snap.resumeHeadAddr;
if (snap.auditLogPath) state.auditLogPath = snap.auditLogPath;
if (snap.activeDbRunId) {
state.activeDbRunId = Number(snap.activeDbRunId);
try { setActiveDbRunId(state.activeDbRunId); }
catch (e) { console.error('[boot] setActiveDbRunId failed:', e.message); }
}
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 (e) { console.error('[boot] state snapshot restore failed:', e.message); }
// Hydrate logBuffer from the IN-FLIGHT audit log (the file the run was
// appending to before the bounce), so SSE init replays the actual run's
// history. Falls back to the alphabetically-newest file only if the
// snapshot didn't preserve a path or the file vanished.
try {
const logDir = path.join(__dirname, 'results');
let used = null;
if (state.auditLogPath && _ex(state.auditLogPath)) {
if (hydrateLogBufferFromFile(state.auditLogPath) > 0) used = state.auditLogPath;
}
if (!used) {
const logFiles = _rd(logDir).filter(f => /^(audit|retest)-.*\.log$/.test(f)).sort().reverse();
if (logFiles.length > 0) {
const candidate = path.join(logDir, logFiles[0]);
if (hydrateLogBufferFromFile(candidate) > 0) used = candidate;
}
}
if (used) console.log(`Log buffer hydrated from ${path.basename(used)} (${logBuffer.length} lines)`);
} 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.disable('x-powered-by');
app.use(express.json({ limit: '512kb' }));
// 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');
res.setHeader('Permissions-Policy', 'geolocation=(), camera=(), microphone=(), payment=(), usb=()');
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
res.setHeader('Cross-Origin-Resource-Policy', 'same-origin');
// HSTS only when reachable over TLS (operator opts in via env var so local
// http://localhost dev is unaffected).
if (process.env.ENABLE_HSTS === 'true') {
res.setHeader('Strict-Transport-Security', 'max-age=63072000; includeSubDomains');
}
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'));
});
// /about is now a modal on /live (and /). Redirect direct hits to /live so
// users land on the canonical page where the About button opens the modal.
app.get('/about', (req, res) => {
res.redirect(302, '/live');
});
// ─── 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 {
// The admin's own /live view must mirror the admin dashboard 1:1 regardless
// of the public broadcast toggle — operator needs to see resume-in-progress
// even with broadcast off. Public visitors still gated by broadcastLive.
const showLive = state.broadcastLive || req.admin === true;
let data = showLive ? getActiveBatch() : getLastBatch();
// Stopped-but-resumable: the batch was closed (finished_at set) on stop,
// but state.activeBatchId still points at it for the eventual resume.
// /live should keep painting it so a refresh while stopped doesn't go
// blank or fall through to a different historical run.
if (!data
&& showLive
&& state.status === 'stopped'
&& state.activeBatchId) {
data = getBatchWithNodes(state.activeBatchId);
}
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,
// Tell /live this is a paused-but-pinned run so it paints rows even
// though finished_at is set. Without this flag, the client treats
// any finished_at as "historical" and skips painting under broadcast.
stopped: state.status === 'stopped' && state.activeBatchId === batch.id,
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 {
// Per-run scoping: live page wants the *current* sweep's numbers, not
// lifetime DB averages. Active > last completed > lifetime fallback for
// totalNodes / passingPct / lastRunAt. medianMbps is intentionally NOT
// backfilled from lifetime — a stale historical median painted on a fresh
// server boot makes the /live "Network Median" tile look hardcoded.
const lifetime = getNetworkStats();
const active = getActiveRun();
const last = active ? null : getLastCompletedRun();
const scopedRunId = active?.id || last?.id || null;
const scoped = scopedRunId ? getRunStats(scopedRunId) : null;
const useScoped = scoped && scoped.processed > 0;
// medianMbps is intentionally gated on an *active* run. A "last completed"
// median is stale by definition — painting it on /live makes the tile look
// hardcoded between sweeps. When idle, the tile collapses to "—".
const medianMbps = active && useScoped && scoped.medianMbps != null && scoped.medianMbps > 0
? scoped.medianMbps
: null;
res.json({
totalNodes: useScoped ? scoped.totalNodes : lifetime.totalNodes,
passingPct: useScoped ? scoped.passingPct : lifetime.passingPct,
medianMbps,
lastRunAt: lifetime.lastRunAt,
runId: scopedRunId,
runScope: useScoped ? (active ? 'active' : 'last') : 'lifetime',
status: continuous.status().running ? 'running' : 'idle',
});
} catch (err) {
console.error('[api/public/stats]', err);
res.status(500).json({ error: 'Internal error' });
}
});
/**
* GET /api/public/sdk-info
* Read-only: which SDK the tester is currently using + its installed version.
* Used by /live to render the SDK badge next to the run-mode label so the
* public can see exactly which client implementation produced the numbers.
* Maps state.activeSDK ('js' | 'tkd' | 'csharp') → tracked SDK key + display
* name. C# isn't tracked by sdk-verify (no npm pkg), so version is null.
*/
app.get('/api/public/sdk-info', attachAdminFlag, rlPublicRead, async (req, res) => {
try {
const active = state.activeSDK || 'js';
const ACTIVE_TO_TRACKED = { js: 'blue-js', tkd: 'tkd-js' };
const DISPLAY_NAME = { js: 'Blue JS', tkd: 'TKD JS', csharp: 'Blue C#' };