@bsv/overlay-express 2.0.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/mod.js +7 -1
- package/dist/cjs/mod.js.map +1 -1
- package/dist/cjs/package.json +4 -2
- package/dist/cjs/src/BanAwareLookupWrapper.js +76 -0
- package/dist/cjs/src/BanAwareLookupWrapper.js.map +1 -0
- package/dist/cjs/src/BanService.js +118 -0
- package/dist/cjs/src/BanService.js.map +1 -0
- package/dist/cjs/src/JanitorService.js +206 -93
- package/dist/cjs/src/JanitorService.js.map +1 -1
- package/dist/cjs/src/OverlayExpress.js +522 -71
- package/dist/cjs/src/OverlayExpress.js.map +1 -1
- package/dist/cjs/src/makeUserInterface.js +889 -217
- package/dist/cjs/src/makeUserInterface.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/mod.js +4 -1
- package/dist/esm/mod.js.map +1 -1
- package/dist/esm/src/BanAwareLookupWrapper.js +78 -0
- package/dist/esm/src/BanAwareLookupWrapper.js.map +1 -0
- package/dist/esm/src/BanService.js +115 -0
- package/dist/esm/src/BanService.js.map +1 -0
- package/dist/esm/src/JanitorService.js +199 -92
- package/dist/esm/src/JanitorService.js.map +1 -1
- package/dist/esm/src/OverlayExpress.js +528 -72
- package/dist/esm/src/OverlayExpress.js.map +1 -1
- package/dist/esm/src/makeUserInterface.js +889 -217
- package/dist/esm/src/makeUserInterface.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/mod.d.ts +4 -1
- package/dist/types/mod.d.ts.map +1 -1
- package/dist/types/src/BanAwareLookupWrapper.d.ts +31 -0
- package/dist/types/src/BanAwareLookupWrapper.d.ts.map +1 -0
- package/dist/types/src/BanService.d.ts +81 -0
- package/dist/types/src/BanService.d.ts.map +1 -0
- package/dist/types/src/JanitorService.d.ts +69 -13
- package/dist/types/src/JanitorService.d.ts.map +1 -1
- package/dist/types/src/OverlayExpress.d.ts +28 -4
- package/dist/types/src/OverlayExpress.d.ts.map +1 -1
- package/dist/types/src/makeUserInterface.d.ts +2 -0
- package/dist/types/src/makeUserInterface.d.ts.map +1 -1
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/mod.ts +4 -1
- package/package.json +4 -2
- package/src/BanAwareLookupWrapper.ts +95 -0
- package/src/BanService.ts +154 -0
- package/src/JanitorService.ts +277 -101
- package/src/OverlayExpress.ts +575 -76
- package/src/__tests__/BanAwareLookupWrapper.test.ts +255 -0
- package/src/__tests__/BanService.test.ts +310 -0
- package/src/__tests__/JanitorService.test.ts +342 -5
- package/src/__tests__/OverlayExpress.test.ts +19 -6
- package/src/__tests__/makeUserInterface.test.ts +4 -3
- package/src/makeUserInterface.ts +892 -217
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import express from 'express';
|
|
2
2
|
import bodyParser from 'body-parser';
|
|
3
3
|
import { Engine, KnexStorage, KnexStorageMigrations } from '@bsv/overlay';
|
|
4
|
-
import { ARC, MerklePath, WhatsOnChain, HTTPSOverlayBroadcastFacilitator, DEFAULT_TESTNET_SLAP_TRACKERS, DEFAULT_SLAP_TRACKERS, Utils, Beef, Transaction } from '@bsv/sdk';
|
|
4
|
+
import { ARC, MerklePath, WhatsOnChain, HTTPSOverlayBroadcastFacilitator, DEFAULT_TESTNET_SLAP_TRACKERS, DEFAULT_SLAP_TRACKERS, Utils, Beef, Transaction, PrivateKey, KeyDeriver } from '@bsv/sdk';
|
|
5
5
|
import Knex from 'knex';
|
|
6
6
|
import { MongoClient } from 'mongodb';
|
|
7
7
|
import makeUserInterface from './makeUserInterface.js';
|
|
@@ -10,6 +10,10 @@ import chalk from 'chalk';
|
|
|
10
10
|
import util from 'util';
|
|
11
11
|
import { v4 as uuidv4 } from 'uuid';
|
|
12
12
|
import { JanitorService } from './JanitorService.js';
|
|
13
|
+
import { BanService } from './BanService.js';
|
|
14
|
+
import { BanAwareLookupWrapper } from './BanAwareLookupWrapper.js';
|
|
15
|
+
import { Wallet, WalletSigner, WalletStorageManager, Services } from '@bsv/wallet-toolbox-client';
|
|
16
|
+
import { createAuthMiddleware } from '@bsv/auth-express-middleware';
|
|
13
17
|
/**
|
|
14
18
|
* In-memory migration source for Knex migrations.
|
|
15
19
|
* Allows running migrations defined in code rather than files.
|
|
@@ -94,8 +98,17 @@ export default class OverlayExpress {
|
|
|
94
98
|
// Configuration for the janitor service
|
|
95
99
|
janitorConfig = {
|
|
96
100
|
requestTimeoutMs: 10000, // 10 seconds
|
|
97
|
-
hostDownRevokeScore: 3
|
|
101
|
+
hostDownRevokeScore: 3,
|
|
102
|
+
autoBanOnRemoval: true
|
|
98
103
|
};
|
|
104
|
+
// Ban service for persistent domain/outpoint blocking
|
|
105
|
+
banService;
|
|
106
|
+
// Admin identity key for wallet-based admin detection on the frontend
|
|
107
|
+
adminIdentityKey;
|
|
108
|
+
// Server-side wallet (WalletInterface) used for BSV mutual authentication
|
|
109
|
+
serverWallet;
|
|
110
|
+
// Server start time for uptime tracking
|
|
111
|
+
startTime;
|
|
99
112
|
/**
|
|
100
113
|
* Constructs an instance of OverlayExpress.
|
|
101
114
|
* @param name - The name of the service
|
|
@@ -109,7 +122,7 @@ export default class OverlayExpress {
|
|
|
109
122
|
this.privateKey = privateKey;
|
|
110
123
|
this.advertisableFQDN = advertisableFQDN;
|
|
111
124
|
this.app = express();
|
|
112
|
-
this.logger.log(chalk.green.bold(`${name} constructed
|
|
125
|
+
this.logger.log(chalk.green.bold(`${name} constructed`));
|
|
113
126
|
this.adminToken = adminToken ?? uuidv4(); // generate random if not provided
|
|
114
127
|
}
|
|
115
128
|
/**
|
|
@@ -124,7 +137,7 @@ export default class OverlayExpress {
|
|
|
124
137
|
*/
|
|
125
138
|
configurePort(port) {
|
|
126
139
|
this.port = port;
|
|
127
|
-
this.logger.log(chalk.blue(
|
|
140
|
+
this.logger.log(chalk.blue(`Server port set to ${port}`));
|
|
128
141
|
}
|
|
129
142
|
/**
|
|
130
143
|
* Configures the web user interface
|
|
@@ -132,20 +145,32 @@ export default class OverlayExpress {
|
|
|
132
145
|
*/
|
|
133
146
|
configureWebUI(config) {
|
|
134
147
|
this.webUIConfig = config;
|
|
135
|
-
this.logger.log(chalk.blue('
|
|
148
|
+
this.logger.log(chalk.blue('Web UI has been configured.'));
|
|
136
149
|
}
|
|
137
150
|
/**
|
|
138
151
|
* Configures the janitor service parameters
|
|
139
152
|
* @param config - Janitor configuration options
|
|
140
153
|
* - requestTimeoutMs: Timeout for health check requests (default: 10000ms)
|
|
141
154
|
* - hostDownRevokeScore: Number of consecutive failures before deleting output (default: 3)
|
|
155
|
+
* - autoBanOnRemoval: Whether to auto-ban domains when removed by janitor (default: true)
|
|
142
156
|
*/
|
|
143
157
|
configureJanitor(config) {
|
|
144
158
|
this.janitorConfig = {
|
|
145
159
|
...this.janitorConfig,
|
|
146
160
|
...config
|
|
147
161
|
};
|
|
148
|
-
this.logger.log(chalk.blue('
|
|
162
|
+
this.logger.log(chalk.blue('Janitor service has been configured.'));
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Configures the admin identity key for wallet-based admin detection.
|
|
166
|
+
* When set, the frontend can compare the user's wallet identity key against this
|
|
167
|
+
* to determine whether to show the admin dashboard.
|
|
168
|
+
*
|
|
169
|
+
* @param identityKey - The hex-encoded public key of the admin
|
|
170
|
+
*/
|
|
171
|
+
configureAdminIdentityKey(identityKey) {
|
|
172
|
+
this.adminIdentityKey = identityKey;
|
|
173
|
+
this.logger.log(chalk.blue('Admin identity key has been configured.'));
|
|
149
174
|
}
|
|
150
175
|
/**
|
|
151
176
|
* Configures the logger to be used by the server.
|
|
@@ -153,7 +178,7 @@ export default class OverlayExpress {
|
|
|
153
178
|
*/
|
|
154
179
|
configureLogger(logger) {
|
|
155
180
|
this.logger = logger;
|
|
156
|
-
this.logger.log(chalk.blue('
|
|
181
|
+
this.logger.log(chalk.blue('Logger has been configured.'));
|
|
157
182
|
}
|
|
158
183
|
/**
|
|
159
184
|
* Configures the BSV Blockchain network to be used ('main' or 'test').
|
|
@@ -163,7 +188,7 @@ export default class OverlayExpress {
|
|
|
163
188
|
configureNetwork(network) {
|
|
164
189
|
this.network = network;
|
|
165
190
|
this.chainTracker = new WhatsOnChain(this.network);
|
|
166
|
-
this.logger.log(chalk.blue(
|
|
191
|
+
this.logger.log(chalk.blue(`Network set to ${network}`));
|
|
167
192
|
}
|
|
168
193
|
/**
|
|
169
194
|
* Configures the ChainTracker to be used.
|
|
@@ -172,7 +197,7 @@ export default class OverlayExpress {
|
|
|
172
197
|
*/
|
|
173
198
|
configureChainTracker(chainTracker = new WhatsOnChain(this.network)) {
|
|
174
199
|
this.chainTracker = chainTracker;
|
|
175
|
-
this.logger.log(chalk.blue('
|
|
200
|
+
this.logger.log(chalk.blue('ChainTracker has been configured.'));
|
|
176
201
|
}
|
|
177
202
|
/**
|
|
178
203
|
* Configures the ARC API key.
|
|
@@ -180,7 +205,7 @@ export default class OverlayExpress {
|
|
|
180
205
|
*/
|
|
181
206
|
configureArcApiKey(apiKey) {
|
|
182
207
|
this.arcApiKey = apiKey;
|
|
183
|
-
this.logger.log(chalk.blue('
|
|
208
|
+
this.logger.log(chalk.blue('ARC API key has been configured.'));
|
|
184
209
|
}
|
|
185
210
|
/**
|
|
186
211
|
* Enables or disables GASP synchronization (high-level setting).
|
|
@@ -189,7 +214,7 @@ export default class OverlayExpress {
|
|
|
189
214
|
*/
|
|
190
215
|
configureEnableGASPSync(enable) {
|
|
191
216
|
this.enableGASPSync = enable;
|
|
192
|
-
this.logger.log(chalk.blue(
|
|
217
|
+
this.logger.log(chalk.blue(`GASP synchronization ${enable ? 'enabled' : 'disabled'}.`));
|
|
193
218
|
}
|
|
194
219
|
/**
|
|
195
220
|
* Enables or disables verbose request logging.
|
|
@@ -197,7 +222,7 @@ export default class OverlayExpress {
|
|
|
197
222
|
*/
|
|
198
223
|
configureVerboseRequestLogging(enable) {
|
|
199
224
|
this.verboseRequestLogging = enable;
|
|
200
|
-
this.logger.log(chalk.blue(
|
|
225
|
+
this.logger.log(chalk.blue(`Verbose request logging ${enable ? 'enabled' : 'disabled'}.`));
|
|
201
226
|
}
|
|
202
227
|
/**
|
|
203
228
|
* Configure Knex (SQL) database connection.
|
|
@@ -211,10 +236,11 @@ export default class OverlayExpress {
|
|
|
211
236
|
};
|
|
212
237
|
}
|
|
213
238
|
this.knex = Knex(config);
|
|
214
|
-
this.logger.log(chalk.blue('
|
|
239
|
+
this.logger.log(chalk.blue('Knex successfully configured.'));
|
|
215
240
|
}
|
|
216
241
|
/**
|
|
217
242
|
* Configures the MongoDB database connection.
|
|
243
|
+
* Also initializes the BanService for persistent ban tracking.
|
|
218
244
|
* @param connectionString - MongoDB connection string
|
|
219
245
|
*/
|
|
220
246
|
async configureMongo(connectionString) {
|
|
@@ -222,7 +248,10 @@ export default class OverlayExpress {
|
|
|
222
248
|
await mongoClient.connect();
|
|
223
249
|
const db = mongoClient.db(`${this.name}_lookup_services`);
|
|
224
250
|
this.mongoDb = db;
|
|
225
|
-
|
|
251
|
+
// Initialize the BanService
|
|
252
|
+
this.banService = new BanService(db);
|
|
253
|
+
await this.banService.ensureIndexes();
|
|
254
|
+
this.logger.log(chalk.blue('MongoDB successfully configured and connected.'));
|
|
226
255
|
}
|
|
227
256
|
/**
|
|
228
257
|
* Configures a Topic Manager.
|
|
@@ -231,7 +260,7 @@ export default class OverlayExpress {
|
|
|
231
260
|
*/
|
|
232
261
|
configureTopicManager(name, manager) {
|
|
233
262
|
this.managers[name] = manager;
|
|
234
|
-
this.logger.log(chalk.blue(
|
|
263
|
+
this.logger.log(chalk.blue(`Configured topic manager ${name}`));
|
|
235
264
|
}
|
|
236
265
|
/**
|
|
237
266
|
* Configures a Lookup Service.
|
|
@@ -240,7 +269,7 @@ export default class OverlayExpress {
|
|
|
240
269
|
*/
|
|
241
270
|
configureLookupService(name, service) {
|
|
242
271
|
this.services[name] = service;
|
|
243
|
-
this.logger.log(chalk.blue(
|
|
272
|
+
this.logger.log(chalk.blue(`Configured lookup service ${name}`));
|
|
244
273
|
}
|
|
245
274
|
/**
|
|
246
275
|
* Configures a Lookup Service using Knex (SQL) database.
|
|
@@ -248,11 +277,11 @@ export default class OverlayExpress {
|
|
|
248
277
|
* @param serviceFactory - A factory function that creates a LookupService instance using Knex
|
|
249
278
|
*/
|
|
250
279
|
configureLookupServiceWithKnex(name, serviceFactory) {
|
|
251
|
-
this.ensureKnex();
|
|
252
|
-
const factoryResult = serviceFactory(
|
|
280
|
+
const knex = this.ensureKnex();
|
|
281
|
+
const factoryResult = serviceFactory(knex);
|
|
253
282
|
this.services[name] = factoryResult.service;
|
|
254
283
|
this.migrationsToRun.push(...factoryResult.migrations);
|
|
255
|
-
this.logger.log(chalk.blue(
|
|
284
|
+
this.logger.log(chalk.blue(`Configured lookup service ${name} with Knex`));
|
|
256
285
|
}
|
|
257
286
|
/**
|
|
258
287
|
* Configures a Lookup Service using MongoDB.
|
|
@@ -260,9 +289,9 @@ export default class OverlayExpress {
|
|
|
260
289
|
* @param serviceFactory - A factory function that creates a LookupService instance using MongoDB
|
|
261
290
|
*/
|
|
262
291
|
configureLookupServiceWithMongo(name, serviceFactory) {
|
|
263
|
-
this.ensureMongo();
|
|
264
|
-
this.services[name] = serviceFactory(
|
|
265
|
-
this.logger.log(chalk.blue(
|
|
292
|
+
const mongoDb = this.ensureMongo();
|
|
293
|
+
this.services[name] = serviceFactory(mongoDb);
|
|
294
|
+
this.logger.log(chalk.blue(`Configured lookup service ${name} with MongoDB`));
|
|
266
295
|
}
|
|
267
296
|
/**
|
|
268
297
|
* Advanced configuration method for setting or overriding any
|
|
@@ -283,17 +312,21 @@ export default class OverlayExpress {
|
|
|
283
312
|
...this.engineConfig,
|
|
284
313
|
...params
|
|
285
314
|
};
|
|
286
|
-
this.logger.log(chalk.blue('
|
|
315
|
+
this.logger.log(chalk.blue('Advanced Engine configuration params have been updated.'));
|
|
287
316
|
}
|
|
288
317
|
/**
|
|
289
318
|
* Configures the Overlay Engine itself.
|
|
290
319
|
* By default, auto-configures SHIP and SLAP unless autoConfigureShipSlap = false
|
|
291
320
|
* Then it merges in any advanced engine config from `this.engineConfig`.
|
|
292
321
|
*
|
|
322
|
+
* When a BanService is available (from configureMongo), SHIP and SLAP lookup
|
|
323
|
+
* services are automatically wrapped with BanAwareLookupWrapper to prevent
|
|
324
|
+
* GASP from re-syncing banned tokens.
|
|
325
|
+
*
|
|
293
326
|
* @param autoConfigureShipSlap - Whether to auto-configure SHIP and SLAP services (default: true)
|
|
294
327
|
*/
|
|
295
328
|
async configureEngine(autoConfigureShipSlap = true) {
|
|
296
|
-
this.ensureKnex();
|
|
329
|
+
const knex = this.ensureKnex();
|
|
297
330
|
if (autoConfigureShipSlap) {
|
|
298
331
|
// Auto-configure SHIP and SLAP services
|
|
299
332
|
this.configureTopicManager('tm_ship', new DiscoveryServices.SHIPTopicManager());
|
|
@@ -301,6 +334,18 @@ export default class OverlayExpress {
|
|
|
301
334
|
this.configureLookupServiceWithMongo('ls_ship', (db) => new DiscoveryServices.SHIPLookupService(new DiscoveryServices.SHIPStorage(db)));
|
|
302
335
|
this.configureLookupServiceWithMongo('ls_slap', (db) => new DiscoveryServices.SLAPLookupService(new DiscoveryServices.SLAPStorage(db)));
|
|
303
336
|
}
|
|
337
|
+
// Wrap SHIP/SLAP lookup services with ban-aware wrappers if BanService is available.
|
|
338
|
+
// This prevents GASP from re-syncing tokens whose domains or outpoints have been banned.
|
|
339
|
+
if (this.banService !== undefined) {
|
|
340
|
+
if (this.services.ls_ship !== undefined) {
|
|
341
|
+
this.services.ls_ship = new BanAwareLookupWrapper(this.services.ls_ship, this.banService, 'SHIP', this.logger);
|
|
342
|
+
this.logger.log(chalk.blue('SHIP lookup service wrapped with ban-aware filter.'));
|
|
343
|
+
}
|
|
344
|
+
if (this.services.ls_slap !== undefined) {
|
|
345
|
+
this.services.ls_slap = new BanAwareLookupWrapper(this.services.ls_slap, this.banService, 'SLAP', this.logger);
|
|
346
|
+
this.logger.log(chalk.blue('SLAP lookup service wrapped with ban-aware filter.'));
|
|
347
|
+
}
|
|
348
|
+
}
|
|
304
349
|
// Construct a default sync configuration, in case the user doesn't want GASP at all:
|
|
305
350
|
let syncConfig = {};
|
|
306
351
|
if (!this.enableGASPSync) {
|
|
@@ -314,7 +359,7 @@ export default class OverlayExpress {
|
|
|
314
359
|
syncConfig = this.engineConfig.syncConfiguration ?? {};
|
|
315
360
|
}
|
|
316
361
|
// Build the actual Storage
|
|
317
|
-
const storage = new KnexStorage(
|
|
362
|
+
const storage = new KnexStorage(knex);
|
|
318
363
|
// Include the KnexStorage migrations
|
|
319
364
|
this.migrationsToRun = [...KnexStorageMigrations.default, ...this.migrationsToRun];
|
|
320
365
|
// Prepare broadcaster if arcApiKey is set
|
|
@@ -376,51 +421,85 @@ export default class OverlayExpress {
|
|
|
376
421
|
this.logger,
|
|
377
422
|
// suppressDefaultSyncAdvertisements
|
|
378
423
|
this.engineConfig.suppressDefaultSyncAdvertisements ?? true);
|
|
379
|
-
|
|
424
|
+
// Create the server wallet for BSV mutual authentication.
|
|
425
|
+
// This uses the same private key as the WalletAdvertiser.
|
|
426
|
+
try {
|
|
427
|
+
const keyDeriver = new KeyDeriver(new PrivateKey(this.privateKey, 'hex'));
|
|
428
|
+
const storageManager = new WalletStorageManager(keyDeriver.identityKey);
|
|
429
|
+
const signer = new WalletSigner(this.network, keyDeriver, storageManager);
|
|
430
|
+
const services = new Services(this.network);
|
|
431
|
+
const wallet = new Wallet(signer, services);
|
|
432
|
+
this.serverWallet = wallet;
|
|
433
|
+
// Auto-set the admin identity key from the server's own key if not configured
|
|
434
|
+
if (typeof this.adminIdentityKey === 'undefined') {
|
|
435
|
+
this.adminIdentityKey = keyDeriver.identityKey;
|
|
436
|
+
}
|
|
437
|
+
this.logger.log(chalk.blue('Server wallet initialized for BSV mutual authentication.'));
|
|
438
|
+
}
|
|
439
|
+
catch (e) {
|
|
440
|
+
this.logger.log(chalk.yellow('Server wallet could not be initialized. BSV auth will not be available.'));
|
|
441
|
+
}
|
|
442
|
+
this.logger.log(chalk.green('Engine has been configured.'));
|
|
380
443
|
}
|
|
381
444
|
/**
|
|
382
|
-
* Ensures that Knex is configured.
|
|
445
|
+
* Ensures that Knex is configured and returns it.
|
|
383
446
|
* @throws Error if Knex is not configured
|
|
384
447
|
*/
|
|
385
448
|
ensureKnex() {
|
|
386
449
|
if (typeof this.knex === 'undefined') {
|
|
387
450
|
throw new Error('You must configure your SQL database with the .configureKnex() method first!');
|
|
388
451
|
}
|
|
452
|
+
return this.knex;
|
|
389
453
|
}
|
|
390
454
|
/**
|
|
391
|
-
* Ensures that MongoDB is configured.
|
|
455
|
+
* Ensures that MongoDB is configured and returns it.
|
|
392
456
|
* @throws Error if MongoDB is not configured
|
|
393
457
|
*/
|
|
394
458
|
ensureMongo() {
|
|
395
459
|
if (typeof this.mongoDb === 'undefined') {
|
|
396
460
|
throw new Error('You must configure your MongoDB connection with the .configureMongo() method first!');
|
|
397
461
|
}
|
|
462
|
+
return this.mongoDb;
|
|
398
463
|
}
|
|
399
464
|
/**
|
|
400
|
-
* Ensures that the Overlay Engine is configured.
|
|
465
|
+
* Ensures that the Overlay Engine is configured and returns it.
|
|
401
466
|
* @throws Error if the Engine is not configured
|
|
402
467
|
*/
|
|
403
468
|
ensureEngine() {
|
|
404
469
|
if (typeof this.engine === 'undefined') {
|
|
405
470
|
throw new Error('You must configure your Overlay Services engine with the .configureEngine() method first!');
|
|
406
471
|
}
|
|
472
|
+
return this.engine;
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Creates a JanitorService instance with current configuration.
|
|
476
|
+
*/
|
|
477
|
+
createJanitor() {
|
|
478
|
+
const mongoDb = this.ensureMongo();
|
|
479
|
+
return new JanitorService({
|
|
480
|
+
mongoDb,
|
|
481
|
+
logger: this.logger,
|
|
482
|
+
requestTimeoutMs: this.janitorConfig.requestTimeoutMs,
|
|
483
|
+
hostDownRevokeScore: this.janitorConfig.hostDownRevokeScore,
|
|
484
|
+
banService: this.banService,
|
|
485
|
+
autoBanOnRemoval: this.janitorConfig.autoBanOnRemoval
|
|
486
|
+
});
|
|
407
487
|
}
|
|
408
488
|
/**
|
|
409
489
|
* Starts the Express server.
|
|
410
490
|
* Sets up routes and begins listening on the configured port.
|
|
411
491
|
*/
|
|
412
492
|
async start() {
|
|
413
|
-
this.ensureEngine();
|
|
414
|
-
this.ensureKnex();
|
|
415
|
-
|
|
416
|
-
const knex = this.knex;
|
|
493
|
+
const engine = this.ensureEngine();
|
|
494
|
+
const knex = this.ensureKnex();
|
|
495
|
+
this.startTime = new Date();
|
|
417
496
|
this.app.use(bodyParser.json({ limit: '1gb', type: 'application/json' }));
|
|
418
497
|
this.app.use(bodyParser.raw({ limit: '1gb', type: 'application/octet-stream' }));
|
|
419
498
|
if (this.verboseRequestLogging) {
|
|
420
499
|
this.app.use((req, res, next) => {
|
|
421
500
|
const startTime = Date.now();
|
|
422
501
|
// Log incoming request details
|
|
423
|
-
this.logger.log(chalk.magenta.bold(
|
|
502
|
+
this.logger.log(chalk.magenta.bold(`Incoming Request: ${String(req.method)} ${String(req.originalUrl)}`));
|
|
424
503
|
// Pretty-print headers
|
|
425
504
|
this.logger.log(chalk.cyan('Headers:'));
|
|
426
505
|
this.logger.log(util.inspect(req.headers, { colors: true, depth: null }));
|
|
@@ -455,7 +534,7 @@ export default class OverlayExpress {
|
|
|
455
534
|
// Log outgoing response details after the response is finished
|
|
456
535
|
res.on('finish', () => {
|
|
457
536
|
const duration = Date.now() - startTime;
|
|
458
|
-
this.logger.log(chalk.magenta.bold(
|
|
537
|
+
this.logger.log(chalk.magenta.bold(`Outgoing Response: ${String(req.method)} ${String(req.originalUrl)} - Status: ${String(res.statusCode)} - Duration: ${String(duration)}ms`));
|
|
459
538
|
this.logger.log(chalk.cyan('Response Headers:'));
|
|
460
539
|
this.logger.log(util.inspect(res.getHeaders(), { colors: true, depth: null }));
|
|
461
540
|
// Handle response body
|
|
@@ -503,7 +582,10 @@ export default class OverlayExpress {
|
|
|
503
582
|
// Serve a static documentation site or user interface
|
|
504
583
|
this.app.get('/', (req, res) => {
|
|
505
584
|
res.set('content-type', 'text/html');
|
|
506
|
-
res.send(makeUserInterface(
|
|
585
|
+
res.send(makeUserInterface({
|
|
586
|
+
...this.webUIConfig,
|
|
587
|
+
adminIdentityKey: this.adminIdentityKey
|
|
588
|
+
}));
|
|
507
589
|
});
|
|
508
590
|
// Serve a health check endpoint
|
|
509
591
|
this.app.get('/health', (_, res) => {
|
|
@@ -524,7 +606,6 @@ export default class OverlayExpress {
|
|
|
524
606
|
});
|
|
525
607
|
}
|
|
526
608
|
})().catch(() => {
|
|
527
|
-
// This catch is for any unforeseen errors in the async IIFE itself
|
|
528
609
|
res.status(500).json({
|
|
529
610
|
status: 'error',
|
|
530
611
|
message: 'Unexpected error'
|
|
@@ -632,7 +713,7 @@ export default class OverlayExpress {
|
|
|
632
713
|
}
|
|
633
714
|
}
|
|
634
715
|
catch (error) {
|
|
635
|
-
console.error(chalk.red('
|
|
716
|
+
console.error(chalk.red('Error in /submit:'), error);
|
|
636
717
|
return res.status(400).json({
|
|
637
718
|
status: 'error',
|
|
638
719
|
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
@@ -719,7 +800,7 @@ export default class OverlayExpress {
|
|
|
719
800
|
return res.status(200).json({ status: 'success', message: 'Transaction status updated' });
|
|
720
801
|
}
|
|
721
802
|
catch (error) {
|
|
722
|
-
console.error(chalk.red('
|
|
803
|
+
console.error(chalk.red('Error in /arc-ingest:'), error);
|
|
723
804
|
return res.status(400).json({
|
|
724
805
|
status: 'error',
|
|
725
806
|
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
@@ -734,7 +815,7 @@ export default class OverlayExpress {
|
|
|
734
815
|
});
|
|
735
816
|
}
|
|
736
817
|
else {
|
|
737
|
-
this.logger.warn(chalk.yellow('
|
|
818
|
+
this.logger.warn(chalk.yellow('Disabling ARC because no ARC API key was provided.'));
|
|
738
819
|
}
|
|
739
820
|
// GASP sync routes if enabled
|
|
740
821
|
if (this.enableGASPSync) {
|
|
@@ -747,7 +828,7 @@ export default class OverlayExpress {
|
|
|
747
828
|
return res.status(200).json(response);
|
|
748
829
|
}
|
|
749
830
|
catch (error) {
|
|
750
|
-
console.error(chalk.red('
|
|
831
|
+
console.error(chalk.red('Error in /requestSyncResponse:'), error);
|
|
751
832
|
return res.status(400).json({
|
|
752
833
|
status: 'error',
|
|
753
834
|
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
@@ -769,7 +850,7 @@ export default class OverlayExpress {
|
|
|
769
850
|
return res.status(200).json(response);
|
|
770
851
|
}
|
|
771
852
|
catch (error) {
|
|
772
|
-
console.error(chalk.red('
|
|
853
|
+
console.error(chalk.red('Error in /requestForeignGASPNode:'), error);
|
|
773
854
|
return res.status(400).json({
|
|
774
855
|
status: 'error',
|
|
775
856
|
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
@@ -784,29 +865,410 @@ export default class OverlayExpress {
|
|
|
784
865
|
});
|
|
785
866
|
}
|
|
786
867
|
else {
|
|
787
|
-
this.logger.warn(chalk.yellow('
|
|
868
|
+
this.logger.warn(chalk.yellow('GASP sync is disabled.'));
|
|
788
869
|
}
|
|
789
870
|
/**
|
|
790
871
|
* ============== ADMIN ROUTES ==============
|
|
791
|
-
* These routes expose advanced engine operations
|
|
792
|
-
*
|
|
872
|
+
* These routes expose advanced engine operations.
|
|
873
|
+
* Authentication: Bearer token OR BSV mutual auth (identity key match).
|
|
874
|
+
*/
|
|
875
|
+
/**
|
|
876
|
+
* Set up BSV mutual authentication middleware if a server wallet is available.
|
|
877
|
+
* This handles the /.well-known/auth handshake automatically.
|
|
878
|
+
* With allowUnauthenticated: true, it passes through when no BSV auth headers
|
|
879
|
+
* are present, allowing Bearer token fallback.
|
|
793
880
|
*/
|
|
881
|
+
if (this.serverWallet !== undefined) {
|
|
882
|
+
const bsvAuth = createAuthMiddleware({
|
|
883
|
+
wallet: this.serverWallet,
|
|
884
|
+
allowUnauthenticated: true
|
|
885
|
+
});
|
|
886
|
+
this.app.use(bsvAuth);
|
|
887
|
+
this.logger.log(chalk.blue('BSV mutual authentication middleware enabled.'));
|
|
888
|
+
}
|
|
794
889
|
/**
|
|
795
|
-
* Middleware for checking
|
|
890
|
+
* Middleware for checking admin authentication.
|
|
891
|
+
* Supports two authentication methods:
|
|
892
|
+
* 1. Bearer token (Authorization: Bearer <token>) - for cron jobs, scripts, and fallback
|
|
893
|
+
* 2. BSV mutual auth - if req.auth.identityKey matches the admin identity key
|
|
796
894
|
*/
|
|
797
895
|
const checkAdminAuth = (req, res, next) => {
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
896
|
+
// Method 1: BSV mutual authentication (identity key match)
|
|
897
|
+
const authReq = req;
|
|
898
|
+
if (typeof this.adminIdentityKey === 'string' &&
|
|
899
|
+
authReq.auth !== undefined &&
|
|
900
|
+
typeof authReq.auth.identityKey === 'string' &&
|
|
901
|
+
authReq.auth.identityKey !== 'unknown' &&
|
|
902
|
+
authReq.auth.identityKey === this.adminIdentityKey) {
|
|
903
|
+
next();
|
|
801
904
|
return;
|
|
802
905
|
}
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
906
|
+
// Method 2: Bearer token authentication
|
|
907
|
+
const authHeader = req.headers.authorization;
|
|
908
|
+
if (typeof authHeader === 'string' && authHeader.startsWith('Bearer ')) {
|
|
909
|
+
const token = authHeader.substring('Bearer '.length);
|
|
910
|
+
if (token === this.adminToken) {
|
|
911
|
+
next();
|
|
912
|
+
return;
|
|
913
|
+
}
|
|
914
|
+
res.status(403).json({ status: 'error', message: 'Forbidden: Invalid credentials' });
|
|
806
915
|
return;
|
|
807
916
|
}
|
|
808
|
-
|
|
917
|
+
res.status(401).json({ status: 'error', message: 'Unauthorized: Provide a Bearer token or authenticate with your wallet' });
|
|
809
918
|
};
|
|
919
|
+
/**
|
|
920
|
+
* Public endpoint that returns the admin identity key (if configured).
|
|
921
|
+
* This allows the frontend to detect whether the current wallet user
|
|
922
|
+
* is the admin by comparing their identity key against this value.
|
|
923
|
+
* The identity key is a public key, so exposing it is safe.
|
|
924
|
+
*/
|
|
925
|
+
this.app.get('/admin/config', (_, res) => {
|
|
926
|
+
res.status(200).json({
|
|
927
|
+
adminIdentityKey: this.adminIdentityKey ?? null,
|
|
928
|
+
nodeName: this.name
|
|
929
|
+
});
|
|
930
|
+
});
|
|
931
|
+
/**
|
|
932
|
+
* Admin route: Get server statistics and overview.
|
|
933
|
+
*/
|
|
934
|
+
this.app.get('/admin/stats', checkAdminAuth, (req, res) => {
|
|
935
|
+
;
|
|
936
|
+
(async () => {
|
|
937
|
+
try {
|
|
938
|
+
const db = this.ensureMongo();
|
|
939
|
+
const [shipCount, slapCount, banStats] = await Promise.all([
|
|
940
|
+
db.collection('shipRecords').countDocuments(),
|
|
941
|
+
db.collection('slapRecords').countDocuments(),
|
|
942
|
+
this.banService?.getStats() ?? { domainBans: 0, outpointBans: 0, totalBans: 0 }
|
|
943
|
+
]);
|
|
944
|
+
return res.status(200).json({
|
|
945
|
+
status: 'success',
|
|
946
|
+
data: {
|
|
947
|
+
nodeName: this.name,
|
|
948
|
+
network: this.network,
|
|
949
|
+
uptime: this.startTime !== undefined ? Date.now() - this.startTime.getTime() : 0,
|
|
950
|
+
startedAt: this.startTime?.toISOString(),
|
|
951
|
+
shipRecordCount: shipCount,
|
|
952
|
+
slapRecordCount: slapCount,
|
|
953
|
+
bannedDomains: banStats.domainBans,
|
|
954
|
+
bannedOutpoints: banStats.outpointBans,
|
|
955
|
+
totalBans: banStats.totalBans,
|
|
956
|
+
topicManagers: Object.keys(this.managers),
|
|
957
|
+
lookupServices: Object.keys(this.services),
|
|
958
|
+
gaspSyncEnabled: this.enableGASPSync
|
|
959
|
+
}
|
|
960
|
+
});
|
|
961
|
+
}
|
|
962
|
+
catch (error) {
|
|
963
|
+
return res.status(400).json({
|
|
964
|
+
status: 'error',
|
|
965
|
+
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
966
|
+
});
|
|
967
|
+
}
|
|
968
|
+
})().catch(() => {
|
|
969
|
+
res.status(500).json({ status: 'error', message: 'Unexpected error' });
|
|
970
|
+
});
|
|
971
|
+
});
|
|
972
|
+
/**
|
|
973
|
+
* Admin route: List all SHIP records with full details.
|
|
974
|
+
*/
|
|
975
|
+
this.app.get('/admin/ship-records', checkAdminAuth, (req, res) => {
|
|
976
|
+
;
|
|
977
|
+
(async () => {
|
|
978
|
+
try {
|
|
979
|
+
const db = this.ensureMongo();
|
|
980
|
+
const collection = db.collection('shipRecords');
|
|
981
|
+
const search = typeof req.query.search === 'string' ? req.query.search : undefined;
|
|
982
|
+
const rawPage = parseInt(req.query.page, 10);
|
|
983
|
+
const page = Math.max(1, Number.isNaN(rawPage) ? 1 : rawPage);
|
|
984
|
+
const rawLimit = parseInt(req.query.limit, 10);
|
|
985
|
+
const limit = Math.min(200, Math.max(1, Number.isNaN(rawLimit) ? 50 : rawLimit));
|
|
986
|
+
const skip = (page - 1) * limit;
|
|
987
|
+
const query = {};
|
|
988
|
+
if (typeof search === 'string' && search.length > 0) {
|
|
989
|
+
query.$or = [
|
|
990
|
+
{ domain: { $regex: search, $options: 'i' } },
|
|
991
|
+
{ topic: { $regex: search, $options: 'i' } },
|
|
992
|
+
{ identityKey: { $regex: search, $options: 'i' } },
|
|
993
|
+
{ txid: { $regex: search, $options: 'i' } }
|
|
994
|
+
];
|
|
995
|
+
}
|
|
996
|
+
const [records, total] = await Promise.all([
|
|
997
|
+
collection.find(query).sort({ createdAt: -1 }).skip(skip).limit(limit).toArray(),
|
|
998
|
+
collection.countDocuments(query)
|
|
999
|
+
]);
|
|
1000
|
+
return res.status(200).json({
|
|
1001
|
+
status: 'success',
|
|
1002
|
+
data: { records, total, page, limit, pages: Math.ceil(total / limit) }
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
1005
|
+
catch (error) {
|
|
1006
|
+
return res.status(400).json({
|
|
1007
|
+
status: 'error',
|
|
1008
|
+
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
1009
|
+
});
|
|
1010
|
+
}
|
|
1011
|
+
})().catch(() => {
|
|
1012
|
+
res.status(500).json({ status: 'error', message: 'Unexpected error' });
|
|
1013
|
+
});
|
|
1014
|
+
});
|
|
1015
|
+
/**
|
|
1016
|
+
* Admin route: List all SLAP records with full details.
|
|
1017
|
+
*/
|
|
1018
|
+
this.app.get('/admin/slap-records', checkAdminAuth, (req, res) => {
|
|
1019
|
+
;
|
|
1020
|
+
(async () => {
|
|
1021
|
+
try {
|
|
1022
|
+
const db = this.ensureMongo();
|
|
1023
|
+
const collection = db.collection('slapRecords');
|
|
1024
|
+
const search = typeof req.query.search === 'string' ? req.query.search : undefined;
|
|
1025
|
+
const rawPage = parseInt(req.query.page, 10);
|
|
1026
|
+
const page = Math.max(1, Number.isNaN(rawPage) ? 1 : rawPage);
|
|
1027
|
+
const rawLimit = parseInt(req.query.limit, 10);
|
|
1028
|
+
const limit = Math.min(200, Math.max(1, Number.isNaN(rawLimit) ? 50 : rawLimit));
|
|
1029
|
+
const skip = (page - 1) * limit;
|
|
1030
|
+
const query = {};
|
|
1031
|
+
if (typeof search === 'string' && search.length > 0) {
|
|
1032
|
+
query.$or = [
|
|
1033
|
+
{ domain: { $regex: search, $options: 'i' } },
|
|
1034
|
+
{ service: { $regex: search, $options: 'i' } },
|
|
1035
|
+
{ identityKey: { $regex: search, $options: 'i' } },
|
|
1036
|
+
{ txid: { $regex: search, $options: 'i' } }
|
|
1037
|
+
];
|
|
1038
|
+
}
|
|
1039
|
+
const [records, total] = await Promise.all([
|
|
1040
|
+
collection.find(query).sort({ createdAt: -1 }).skip(skip).limit(limit).toArray(),
|
|
1041
|
+
collection.countDocuments(query)
|
|
1042
|
+
]);
|
|
1043
|
+
return res.status(200).json({
|
|
1044
|
+
status: 'success',
|
|
1045
|
+
data: { records, total, page, limit, pages: Math.ceil(total / limit) }
|
|
1046
|
+
});
|
|
1047
|
+
}
|
|
1048
|
+
catch (error) {
|
|
1049
|
+
return res.status(400).json({
|
|
1050
|
+
status: 'error',
|
|
1051
|
+
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
1052
|
+
});
|
|
1053
|
+
}
|
|
1054
|
+
})().catch(() => {
|
|
1055
|
+
res.status(500).json({ status: 'error', message: 'Unexpected error' });
|
|
1056
|
+
});
|
|
1057
|
+
});
|
|
1058
|
+
/**
|
|
1059
|
+
* Admin route: Check health of a specific URL.
|
|
1060
|
+
*/
|
|
1061
|
+
this.app.post('/admin/health-check', checkAdminAuth, (req, res) => {
|
|
1062
|
+
;
|
|
1063
|
+
(async () => {
|
|
1064
|
+
try {
|
|
1065
|
+
const { url } = req.body;
|
|
1066
|
+
if (typeof url !== 'string' || url.length === 0) {
|
|
1067
|
+
return res.status(400).json({ status: 'error', message: 'url is required' });
|
|
1068
|
+
}
|
|
1069
|
+
const janitor = this.createJanitor();
|
|
1070
|
+
const result = await janitor.checkHost(url);
|
|
1071
|
+
return res.status(200).json({ status: 'success', data: { url, ...result } });
|
|
1072
|
+
}
|
|
1073
|
+
catch (error) {
|
|
1074
|
+
return res.status(400).json({
|
|
1075
|
+
status: 'error',
|
|
1076
|
+
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
1077
|
+
});
|
|
1078
|
+
}
|
|
1079
|
+
})().catch(() => {
|
|
1080
|
+
res.status(500).json({ status: 'error', message: 'Unexpected error' });
|
|
1081
|
+
});
|
|
1082
|
+
});
|
|
1083
|
+
/**
|
|
1084
|
+
* Admin route: Ban a domain or outpoint.
|
|
1085
|
+
*/
|
|
1086
|
+
this.app.post('/admin/ban', checkAdminAuth, (req, res) => {
|
|
1087
|
+
;
|
|
1088
|
+
(async () => {
|
|
1089
|
+
try {
|
|
1090
|
+
if (this.banService === undefined) {
|
|
1091
|
+
return res.status(400).json({ status: 'error', message: 'Ban service not available (MongoDB not configured)' });
|
|
1092
|
+
}
|
|
1093
|
+
const { type, value, reason } = req.body;
|
|
1094
|
+
if (type !== 'domain' && type !== 'outpoint') {
|
|
1095
|
+
return res.status(400).json({ status: 'error', message: 'type must be "domain" or "outpoint"' });
|
|
1096
|
+
}
|
|
1097
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
1098
|
+
return res.status(400).json({ status: 'error', message: 'value is required' });
|
|
1099
|
+
}
|
|
1100
|
+
if (type === 'domain') {
|
|
1101
|
+
await this.banService.banDomain(value, reason);
|
|
1102
|
+
// Also remove any existing records for this domain from SHIP and SLAP
|
|
1103
|
+
const db = this.ensureMongo();
|
|
1104
|
+
const [shipDeleted, slapDeleted] = await Promise.all([
|
|
1105
|
+
db.collection('shipRecords').deleteMany({ domain: value }),
|
|
1106
|
+
db.collection('slapRecords').deleteMany({ domain: value })
|
|
1107
|
+
]);
|
|
1108
|
+
return res.status(200).json({
|
|
1109
|
+
status: 'success',
|
|
1110
|
+
message: `Domain "${value}" banned. Removed ${shipDeleted.deletedCount} SHIP and ${slapDeleted.deletedCount} SLAP records.`
|
|
1111
|
+
});
|
|
1112
|
+
}
|
|
1113
|
+
else {
|
|
1114
|
+
// Parse outpoint: "txid.outputIndex"
|
|
1115
|
+
const dotIndex = value.lastIndexOf('.');
|
|
1116
|
+
if (dotIndex === -1) {
|
|
1117
|
+
return res.status(400).json({ status: 'error', message: 'Outpoint format must be "txid.outputIndex"' });
|
|
1118
|
+
}
|
|
1119
|
+
const txid = value.substring(0, dotIndex);
|
|
1120
|
+
const outputIndex = parseInt(value.substring(dotIndex + 1));
|
|
1121
|
+
if (isNaN(outputIndex)) {
|
|
1122
|
+
return res.status(400).json({ status: 'error', message: 'Invalid outputIndex in outpoint' });
|
|
1123
|
+
}
|
|
1124
|
+
await this.banService.banOutpoint(txid, outputIndex, reason);
|
|
1125
|
+
// Also evict from lookup services
|
|
1126
|
+
const services = Object.values(engine.lookupServices);
|
|
1127
|
+
for (const service of services) {
|
|
1128
|
+
try {
|
|
1129
|
+
await service.outputEvicted(txid, outputIndex);
|
|
1130
|
+
}
|
|
1131
|
+
catch {
|
|
1132
|
+
continue;
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
return res.status(200).json({
|
|
1136
|
+
status: 'success',
|
|
1137
|
+
message: `Outpoint "${value}" banned and evicted from lookup services.`
|
|
1138
|
+
});
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
catch (error) {
|
|
1142
|
+
return res.status(400).json({
|
|
1143
|
+
status: 'error',
|
|
1144
|
+
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
1145
|
+
});
|
|
1146
|
+
}
|
|
1147
|
+
})().catch(() => {
|
|
1148
|
+
res.status(500).json({ status: 'error', message: 'Unexpected error' });
|
|
1149
|
+
});
|
|
1150
|
+
});
|
|
1151
|
+
/**
|
|
1152
|
+
* Admin route: Remove a ban.
|
|
1153
|
+
*/
|
|
1154
|
+
this.app.post('/admin/unban', checkAdminAuth, (req, res) => {
|
|
1155
|
+
;
|
|
1156
|
+
(async () => {
|
|
1157
|
+
try {
|
|
1158
|
+
if (this.banService === undefined) {
|
|
1159
|
+
return res.status(400).json({ status: 'error', message: 'Ban service not available' });
|
|
1160
|
+
}
|
|
1161
|
+
const { type, value } = req.body;
|
|
1162
|
+
if (type !== 'domain' && type !== 'outpoint') {
|
|
1163
|
+
return res.status(400).json({ status: 'error', message: 'type must be "domain" or "outpoint"' });
|
|
1164
|
+
}
|
|
1165
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
1166
|
+
return res.status(400).json({ status: 'error', message: 'value is required' });
|
|
1167
|
+
}
|
|
1168
|
+
await this.banService.removeBan(type, value);
|
|
1169
|
+
return res.status(200).json({ status: 'success', message: `${type} "${String(value)}" unbanned.` });
|
|
1170
|
+
}
|
|
1171
|
+
catch (error) {
|
|
1172
|
+
return res.status(400).json({
|
|
1173
|
+
status: 'error',
|
|
1174
|
+
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
1175
|
+
});
|
|
1176
|
+
}
|
|
1177
|
+
})().catch(() => {
|
|
1178
|
+
res.status(500).json({ status: 'error', message: 'Unexpected error' });
|
|
1179
|
+
});
|
|
1180
|
+
});
|
|
1181
|
+
/**
|
|
1182
|
+
* Admin route: List all bans.
|
|
1183
|
+
*/
|
|
1184
|
+
this.app.get('/admin/bans', checkAdminAuth, (req, res) => {
|
|
1185
|
+
;
|
|
1186
|
+
(async () => {
|
|
1187
|
+
try {
|
|
1188
|
+
if (this.banService === undefined) {
|
|
1189
|
+
return res.status(200).json({ status: 'success', data: { bans: [] } });
|
|
1190
|
+
}
|
|
1191
|
+
const type = req.query.type;
|
|
1192
|
+
const validType = type === 'domain' || type === 'outpoint' ? type : undefined;
|
|
1193
|
+
const bans = await this.banService.listBans(validType);
|
|
1194
|
+
return res.status(200).json({ status: 'success', data: { bans } });
|
|
1195
|
+
}
|
|
1196
|
+
catch (error) {
|
|
1197
|
+
return res.status(400).json({
|
|
1198
|
+
status: 'error',
|
|
1199
|
+
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
1200
|
+
});
|
|
1201
|
+
}
|
|
1202
|
+
})().catch(() => {
|
|
1203
|
+
res.status(500).json({ status: 'error', message: 'Unexpected error' });
|
|
1204
|
+
});
|
|
1205
|
+
});
|
|
1206
|
+
/**
|
|
1207
|
+
* Admin route: Remove a token by outpoint, optionally banning the domain.
|
|
1208
|
+
*/
|
|
1209
|
+
this.app.post('/admin/remove-token', checkAdminAuth, (req, res) => {
|
|
1210
|
+
;
|
|
1211
|
+
(async () => {
|
|
1212
|
+
try {
|
|
1213
|
+
const { txid, outputIndex, service, ban, banDomain: shouldBanDomain } = req.body;
|
|
1214
|
+
if (typeof txid !== 'string' || typeof outputIndex !== 'number') {
|
|
1215
|
+
return res.status(400).json({ status: 'error', message: 'txid (string) and outputIndex (number) are required' });
|
|
1216
|
+
}
|
|
1217
|
+
// Get the domain before removing (for ban option)
|
|
1218
|
+
let removedDomain;
|
|
1219
|
+
if (shouldBanDomain === true || ban === true) {
|
|
1220
|
+
const db = this.ensureMongo();
|
|
1221
|
+
const shipRecord = await db.collection('shipRecords').findOne({ txid, outputIndex });
|
|
1222
|
+
const slapRecord = await db.collection('slapRecords').findOne({ txid, outputIndex });
|
|
1223
|
+
removedDomain = (shipRecord?.domain ?? slapRecord?.domain);
|
|
1224
|
+
}
|
|
1225
|
+
// Evict from specified service or all services
|
|
1226
|
+
if (typeof service === 'string') {
|
|
1227
|
+
const svc = engine.lookupServices[service];
|
|
1228
|
+
if (svc !== undefined) {
|
|
1229
|
+
await svc.outputEvicted(txid, outputIndex);
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
else {
|
|
1233
|
+
const services = Object.values(engine.lookupServices);
|
|
1234
|
+
for (const svc of services) {
|
|
1235
|
+
try {
|
|
1236
|
+
await svc.outputEvicted(txid, outputIndex);
|
|
1237
|
+
}
|
|
1238
|
+
catch {
|
|
1239
|
+
continue;
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
// Ban the outpoint if requested
|
|
1244
|
+
if (ban === true && this.banService !== undefined) {
|
|
1245
|
+
await this.banService.banOutpoint(txid, outputIndex, 'Manually removed by admin', removedDomain);
|
|
1246
|
+
}
|
|
1247
|
+
// Ban the domain if requested
|
|
1248
|
+
if (shouldBanDomain === true && typeof removedDomain === 'string' && this.banService !== undefined) {
|
|
1249
|
+
await this.banService.banDomain(removedDomain, 'Domain banned by admin via token removal');
|
|
1250
|
+
// Remove all records for this domain
|
|
1251
|
+
const banDb = this.ensureMongo();
|
|
1252
|
+
await Promise.all([
|
|
1253
|
+
banDb.collection('shipRecords').deleteMany({ domain: removedDomain }),
|
|
1254
|
+
banDb.collection('slapRecords').deleteMany({ domain: removedDomain })
|
|
1255
|
+
]);
|
|
1256
|
+
}
|
|
1257
|
+
return res.status(200).json({
|
|
1258
|
+
status: 'success',
|
|
1259
|
+
message: `Token ${txid}.${outputIndex} removed.${ban === true ? ' Outpoint banned.' : ''}${shouldBanDomain === true && typeof removedDomain === 'string' ? ` Domain "${removedDomain}" banned.` : ''}`
|
|
1260
|
+
});
|
|
1261
|
+
}
|
|
1262
|
+
catch (error) {
|
|
1263
|
+
return res.status(400).json({
|
|
1264
|
+
status: 'error',
|
|
1265
|
+
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
1266
|
+
});
|
|
1267
|
+
}
|
|
1268
|
+
})().catch(() => {
|
|
1269
|
+
res.status(500).json({ status: 'error', message: 'Unexpected error' });
|
|
1270
|
+
});
|
|
1271
|
+
});
|
|
810
1272
|
/**
|
|
811
1273
|
* Admin route to manually sync advertisements, calling `engine.syncAdvertisements()`.
|
|
812
1274
|
*/
|
|
@@ -818,7 +1280,7 @@ export default class OverlayExpress {
|
|
|
818
1280
|
return res.status(200).json({ status: 'success', message: 'Advertisements synced successfully' });
|
|
819
1281
|
}
|
|
820
1282
|
catch (error) {
|
|
821
|
-
console.error(chalk.red('
|
|
1283
|
+
console.error(chalk.red('Error in /admin/syncAdvertisements:'), error);
|
|
822
1284
|
return res.status(400).json({
|
|
823
1285
|
status: 'error',
|
|
824
1286
|
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
@@ -842,7 +1304,7 @@ export default class OverlayExpress {
|
|
|
842
1304
|
return res.status(200).json({ status: 'success', message: 'GASP sync started and completed' });
|
|
843
1305
|
}
|
|
844
1306
|
catch (error) {
|
|
845
|
-
console.error(chalk.red('
|
|
1307
|
+
console.error(chalk.red('Error in /admin/startGASPSync:'), error);
|
|
846
1308
|
return res.status(400).json({
|
|
847
1309
|
status: 'error',
|
|
848
1310
|
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
@@ -880,7 +1342,7 @@ export default class OverlayExpress {
|
|
|
880
1342
|
return res.status(200).json({ status: 'success', message: 'Outpoint evicted' });
|
|
881
1343
|
}
|
|
882
1344
|
catch (error) {
|
|
883
|
-
console.error(chalk.red('
|
|
1345
|
+
console.error(chalk.red('Error in /admin/evictOutpoint:'), error);
|
|
884
1346
|
return res.status(400).json({
|
|
885
1347
|
status: 'error',
|
|
886
1348
|
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
@@ -894,24 +1356,18 @@ export default class OverlayExpress {
|
|
|
894
1356
|
});
|
|
895
1357
|
});
|
|
896
1358
|
/**
|
|
897
|
-
* Admin route to run the janitor service.
|
|
1359
|
+
* Admin route to run the janitor service with enhanced reporting.
|
|
898
1360
|
*/
|
|
899
1361
|
this.app.post('/admin/janitor', checkAdminAuth, (req, res) => {
|
|
900
1362
|
;
|
|
901
1363
|
(async () => {
|
|
902
1364
|
try {
|
|
903
|
-
this.
|
|
904
|
-
const
|
|
905
|
-
|
|
906
|
-
logger: this.logger,
|
|
907
|
-
requestTimeoutMs: this.janitorConfig.requestTimeoutMs,
|
|
908
|
-
hostDownRevokeScore: this.janitorConfig.hostDownRevokeScore
|
|
909
|
-
});
|
|
910
|
-
await janitor.run();
|
|
911
|
-
return res.status(200).json({ status: 'success', message: 'Janitor run completed' });
|
|
1365
|
+
const janitor = this.createJanitor();
|
|
1366
|
+
const report = await janitor.run();
|
|
1367
|
+
return res.status(200).json({ status: 'success', message: 'Janitor run completed', data: report });
|
|
912
1368
|
}
|
|
913
1369
|
catch (error) {
|
|
914
|
-
console.error(chalk.red('
|
|
1370
|
+
console.error(chalk.red('Error in /admin/janitor:'), error);
|
|
915
1371
|
return res.status(400).json({
|
|
916
1372
|
status: 'error',
|
|
917
1373
|
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
@@ -929,10 +1385,10 @@ export default class OverlayExpress {
|
|
|
929
1385
|
const result = await knex.migrate.latest({
|
|
930
1386
|
migrationSource
|
|
931
1387
|
});
|
|
932
|
-
this.logger.log(chalk.green('
|
|
1388
|
+
this.logger.log(chalk.green('Knex migrations run'), result);
|
|
933
1389
|
// 404 handler for all other routes
|
|
934
1390
|
this.app.use((req, res) => {
|
|
935
|
-
this.logger.log(chalk.red('
|
|
1391
|
+
this.logger.log(chalk.red('404 Not Found:'), req.url);
|
|
936
1392
|
res.status(404).json({
|
|
937
1393
|
status: 'error',
|
|
938
1394
|
code: 'ERR_ROUTE_NOT_FOUND',
|
|
@@ -954,7 +1410,7 @@ export default class OverlayExpress {
|
|
|
954
1410
|
await this.engine?.syncAdvertisements();
|
|
955
1411
|
}
|
|
956
1412
|
catch (e) {
|
|
957
|
-
this.logger.log(chalk.red('
|
|
1413
|
+
this.logger.log(chalk.red('Error syncing advertisements:'), e);
|
|
958
1414
|
}
|
|
959
1415
|
// Attempt to do GASP sync if enabled
|
|
960
1416
|
if (this.enableGASPSync) {
|
|
@@ -964,7 +1420,7 @@ export default class OverlayExpress {
|
|
|
964
1420
|
this.logger.log(chalk.green('GASP sync complete!'));
|
|
965
1421
|
}
|
|
966
1422
|
catch (e) {
|
|
967
|
-
console.error(chalk.red('
|
|
1423
|
+
console.error(chalk.red('Failed to GASP sync'), e);
|
|
968
1424
|
}
|
|
969
1425
|
}
|
|
970
1426
|
else {
|
|
@@ -972,7 +1428,7 @@ export default class OverlayExpress {
|
|
|
972
1428
|
}
|
|
973
1429
|
// Start listening on the configured port
|
|
974
1430
|
this.app.listen(this.port, () => {
|
|
975
|
-
this.logger.log(chalk.green.bold(
|
|
1431
|
+
this.logger.log(chalk.green.bold(`${this.name} is ready and listening on local port ${this.port}`));
|
|
976
1432
|
});
|
|
977
1433
|
}
|
|
978
1434
|
}
|