@bsv/overlay-express 2.0.2 → 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 +3 -1
- 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 +3 -1
- 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
package/src/OverlayExpress.ts
CHANGED
|
@@ -15,7 +15,10 @@ import {
|
|
|
15
15
|
DEFAULT_SLAP_TRACKERS,
|
|
16
16
|
Utils,
|
|
17
17
|
Beef,
|
|
18
|
-
Transaction
|
|
18
|
+
Transaction,
|
|
19
|
+
PrivateKey,
|
|
20
|
+
KeyDeriver,
|
|
21
|
+
WalletInterface
|
|
19
22
|
} from '@bsv/sdk'
|
|
20
23
|
import Knex from 'knex'
|
|
21
24
|
import { MongoClient, Db } from 'mongodb'
|
|
@@ -24,7 +27,11 @@ import * as DiscoveryServices from '@bsv/overlay-discovery-services'
|
|
|
24
27
|
import chalk from 'chalk'
|
|
25
28
|
import util from 'util'
|
|
26
29
|
import { v4 as uuidv4 } from 'uuid'
|
|
27
|
-
import { JanitorService } from './JanitorService.js'
|
|
30
|
+
import { JanitorService, type JanitorReport } from './JanitorService.js'
|
|
31
|
+
import { BanService } from './BanService.js'
|
|
32
|
+
import { BanAwareLookupWrapper } from './BanAwareLookupWrapper.js'
|
|
33
|
+
import { Wallet, WalletSigner, WalletStorageManager, Services } from '@bsv/wallet-toolbox-client'
|
|
34
|
+
import { createAuthMiddleware, type AuthRequest } from '@bsv/auth-express-middleware'
|
|
28
35
|
|
|
29
36
|
/**
|
|
30
37
|
* Knex database migration.
|
|
@@ -152,11 +159,25 @@ export default class OverlayExpress {
|
|
|
152
159
|
janitorConfig: {
|
|
153
160
|
requestTimeoutMs: number
|
|
154
161
|
hostDownRevokeScore: number
|
|
162
|
+
autoBanOnRemoval: boolean
|
|
155
163
|
} = {
|
|
156
164
|
requestTimeoutMs: 10000, // 10 seconds
|
|
157
|
-
hostDownRevokeScore: 3
|
|
165
|
+
hostDownRevokeScore: 3,
|
|
166
|
+
autoBanOnRemoval: true
|
|
158
167
|
}
|
|
159
168
|
|
|
169
|
+
// Ban service for persistent domain/outpoint blocking
|
|
170
|
+
banService?: BanService
|
|
171
|
+
|
|
172
|
+
// Admin identity key for wallet-based admin detection on the frontend
|
|
173
|
+
adminIdentityKey?: string
|
|
174
|
+
|
|
175
|
+
// Server-side wallet (WalletInterface) used for BSV mutual authentication
|
|
176
|
+
serverWallet?: WalletInterface
|
|
177
|
+
|
|
178
|
+
// Server start time for uptime tracking
|
|
179
|
+
private startTime?: Date
|
|
180
|
+
|
|
160
181
|
/**
|
|
161
182
|
* Constructs an instance of OverlayExpress.
|
|
162
183
|
* @param name - The name of the service
|
|
@@ -172,7 +193,7 @@ export default class OverlayExpress {
|
|
|
172
193
|
adminToken?: string
|
|
173
194
|
) {
|
|
174
195
|
this.app = express()
|
|
175
|
-
this.logger.log(chalk.green.bold(`${name} constructed
|
|
196
|
+
this.logger.log(chalk.green.bold(`${name} constructed`))
|
|
176
197
|
this.adminToken = adminToken ?? uuidv4() // generate random if not provided
|
|
177
198
|
}
|
|
178
199
|
|
|
@@ -189,7 +210,7 @@ export default class OverlayExpress {
|
|
|
189
210
|
*/
|
|
190
211
|
configurePort (port: number): void {
|
|
191
212
|
this.port = port
|
|
192
|
-
this.logger.log(chalk.blue(
|
|
213
|
+
this.logger.log(chalk.blue(`Server port set to ${port}`))
|
|
193
214
|
}
|
|
194
215
|
|
|
195
216
|
/**
|
|
@@ -198,7 +219,7 @@ export default class OverlayExpress {
|
|
|
198
219
|
*/
|
|
199
220
|
configureWebUI (config: UIConfig): void {
|
|
200
221
|
this.webUIConfig = config
|
|
201
|
-
this.logger.log(chalk.blue('
|
|
222
|
+
this.logger.log(chalk.blue('Web UI has been configured.'))
|
|
202
223
|
}
|
|
203
224
|
|
|
204
225
|
/**
|
|
@@ -206,13 +227,26 @@ export default class OverlayExpress {
|
|
|
206
227
|
* @param config - Janitor configuration options
|
|
207
228
|
* - requestTimeoutMs: Timeout for health check requests (default: 10000ms)
|
|
208
229
|
* - hostDownRevokeScore: Number of consecutive failures before deleting output (default: 3)
|
|
230
|
+
* - autoBanOnRemoval: Whether to auto-ban domains when removed by janitor (default: true)
|
|
209
231
|
*/
|
|
210
232
|
configureJanitor (config: Partial<typeof this.janitorConfig>): void {
|
|
211
233
|
this.janitorConfig = {
|
|
212
234
|
...this.janitorConfig,
|
|
213
235
|
...config
|
|
214
236
|
}
|
|
215
|
-
this.logger.log(chalk.blue('
|
|
237
|
+
this.logger.log(chalk.blue('Janitor service has been configured.'))
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Configures the admin identity key for wallet-based admin detection.
|
|
242
|
+
* When set, the frontend can compare the user's wallet identity key against this
|
|
243
|
+
* to determine whether to show the admin dashboard.
|
|
244
|
+
*
|
|
245
|
+
* @param identityKey - The hex-encoded public key of the admin
|
|
246
|
+
*/
|
|
247
|
+
configureAdminIdentityKey (identityKey: string): void {
|
|
248
|
+
this.adminIdentityKey = identityKey
|
|
249
|
+
this.logger.log(chalk.blue('Admin identity key has been configured.'))
|
|
216
250
|
}
|
|
217
251
|
|
|
218
252
|
/**
|
|
@@ -221,7 +255,7 @@ export default class OverlayExpress {
|
|
|
221
255
|
*/
|
|
222
256
|
configureLogger (logger: typeof console): void {
|
|
223
257
|
this.logger = logger
|
|
224
|
-
this.logger.log(chalk.blue('
|
|
258
|
+
this.logger.log(chalk.blue('Logger has been configured.'))
|
|
225
259
|
}
|
|
226
260
|
|
|
227
261
|
/**
|
|
@@ -232,7 +266,7 @@ export default class OverlayExpress {
|
|
|
232
266
|
configureNetwork (network: 'main' | 'test'): void {
|
|
233
267
|
this.network = network
|
|
234
268
|
this.chainTracker = new WhatsOnChain(this.network)
|
|
235
|
-
this.logger.log(chalk.blue(
|
|
269
|
+
this.logger.log(chalk.blue(`Network set to ${network}`))
|
|
236
270
|
}
|
|
237
271
|
|
|
238
272
|
/**
|
|
@@ -242,7 +276,7 @@ export default class OverlayExpress {
|
|
|
242
276
|
*/
|
|
243
277
|
configureChainTracker (chainTracker: ChainTracker | 'scripts only' = new WhatsOnChain(this.network)): void {
|
|
244
278
|
this.chainTracker = chainTracker
|
|
245
|
-
this.logger.log(chalk.blue('
|
|
279
|
+
this.logger.log(chalk.blue('ChainTracker has been configured.'))
|
|
246
280
|
}
|
|
247
281
|
|
|
248
282
|
/**
|
|
@@ -251,7 +285,7 @@ export default class OverlayExpress {
|
|
|
251
285
|
*/
|
|
252
286
|
configureArcApiKey (apiKey: string): void {
|
|
253
287
|
this.arcApiKey = apiKey
|
|
254
|
-
this.logger.log(chalk.blue('
|
|
288
|
+
this.logger.log(chalk.blue('ARC API key has been configured.'))
|
|
255
289
|
}
|
|
256
290
|
|
|
257
291
|
/**
|
|
@@ -261,7 +295,7 @@ export default class OverlayExpress {
|
|
|
261
295
|
*/
|
|
262
296
|
configureEnableGASPSync (enable: boolean): void {
|
|
263
297
|
this.enableGASPSync = enable
|
|
264
|
-
this.logger.log(chalk.blue(
|
|
298
|
+
this.logger.log(chalk.blue(`GASP synchronization ${enable ? 'enabled' : 'disabled'}.`))
|
|
265
299
|
}
|
|
266
300
|
|
|
267
301
|
/**
|
|
@@ -270,7 +304,7 @@ export default class OverlayExpress {
|
|
|
270
304
|
*/
|
|
271
305
|
configureVerboseRequestLogging (enable: boolean): void {
|
|
272
306
|
this.verboseRequestLogging = enable
|
|
273
|
-
this.logger.log(chalk.blue(
|
|
307
|
+
this.logger.log(chalk.blue(`Verbose request logging ${enable ? 'enabled' : 'disabled'}.`))
|
|
274
308
|
}
|
|
275
309
|
|
|
276
310
|
/**
|
|
@@ -285,11 +319,12 @@ export default class OverlayExpress {
|
|
|
285
319
|
}
|
|
286
320
|
}
|
|
287
321
|
this.knex = Knex(config)
|
|
288
|
-
this.logger.log(chalk.blue('
|
|
322
|
+
this.logger.log(chalk.blue('Knex successfully configured.'))
|
|
289
323
|
}
|
|
290
324
|
|
|
291
325
|
/**
|
|
292
326
|
* Configures the MongoDB database connection.
|
|
327
|
+
* Also initializes the BanService for persistent ban tracking.
|
|
293
328
|
* @param connectionString - MongoDB connection string
|
|
294
329
|
*/
|
|
295
330
|
async configureMongo (connectionString: string): Promise<void> {
|
|
@@ -297,7 +332,12 @@ export default class OverlayExpress {
|
|
|
297
332
|
await mongoClient.connect()
|
|
298
333
|
const db = mongoClient.db(`${this.name}_lookup_services`)
|
|
299
334
|
this.mongoDb = db
|
|
300
|
-
|
|
335
|
+
|
|
336
|
+
// Initialize the BanService
|
|
337
|
+
this.banService = new BanService(db)
|
|
338
|
+
await this.banService.ensureIndexes()
|
|
339
|
+
|
|
340
|
+
this.logger.log(chalk.blue('MongoDB successfully configured and connected.'))
|
|
301
341
|
}
|
|
302
342
|
|
|
303
343
|
/**
|
|
@@ -307,7 +347,7 @@ export default class OverlayExpress {
|
|
|
307
347
|
*/
|
|
308
348
|
configureTopicManager (name: string, manager: TopicManager): void {
|
|
309
349
|
this.managers[name] = manager
|
|
310
|
-
this.logger.log(chalk.blue(
|
|
350
|
+
this.logger.log(chalk.blue(`Configured topic manager ${name}`))
|
|
311
351
|
}
|
|
312
352
|
|
|
313
353
|
/**
|
|
@@ -317,7 +357,7 @@ export default class OverlayExpress {
|
|
|
317
357
|
*/
|
|
318
358
|
configureLookupService (name: string, service: LookupService): void {
|
|
319
359
|
this.services[name] = service
|
|
320
|
-
this.logger.log(chalk.blue(
|
|
360
|
+
this.logger.log(chalk.blue(`Configured lookup service ${name}`))
|
|
321
361
|
}
|
|
322
362
|
|
|
323
363
|
/**
|
|
@@ -329,11 +369,11 @@ export default class OverlayExpress {
|
|
|
329
369
|
name: string,
|
|
330
370
|
serviceFactory: (knex: Knex.Knex) => { service: LookupService, migrations: Migration[] }
|
|
331
371
|
): void {
|
|
332
|
-
this.ensureKnex()
|
|
333
|
-
const factoryResult = serviceFactory(
|
|
372
|
+
const knex = this.ensureKnex()
|
|
373
|
+
const factoryResult = serviceFactory(knex)
|
|
334
374
|
this.services[name] = factoryResult.service
|
|
335
375
|
this.migrationsToRun.push(...factoryResult.migrations)
|
|
336
|
-
this.logger.log(chalk.blue(
|
|
376
|
+
this.logger.log(chalk.blue(`Configured lookup service ${name} with Knex`))
|
|
337
377
|
}
|
|
338
378
|
|
|
339
379
|
/**
|
|
@@ -342,9 +382,9 @@ export default class OverlayExpress {
|
|
|
342
382
|
* @param serviceFactory - A factory function that creates a LookupService instance using MongoDB
|
|
343
383
|
*/
|
|
344
384
|
configureLookupServiceWithMongo (name: string, serviceFactory: (mongoDb: Db) => LookupService): void {
|
|
345
|
-
this.ensureMongo()
|
|
346
|
-
this.services[name] = serviceFactory(
|
|
347
|
-
this.logger.log(chalk.blue(
|
|
385
|
+
const mongoDb = this.ensureMongo()
|
|
386
|
+
this.services[name] = serviceFactory(mongoDb)
|
|
387
|
+
this.logger.log(chalk.blue(`Configured lookup service ${name} with MongoDB`))
|
|
348
388
|
}
|
|
349
389
|
|
|
350
390
|
/**
|
|
@@ -366,7 +406,7 @@ export default class OverlayExpress {
|
|
|
366
406
|
...this.engineConfig,
|
|
367
407
|
...params
|
|
368
408
|
}
|
|
369
|
-
this.logger.log(chalk.blue('
|
|
409
|
+
this.logger.log(chalk.blue('Advanced Engine configuration params have been updated.'))
|
|
370
410
|
}
|
|
371
411
|
|
|
372
412
|
/**
|
|
@@ -374,10 +414,14 @@ export default class OverlayExpress {
|
|
|
374
414
|
* By default, auto-configures SHIP and SLAP unless autoConfigureShipSlap = false
|
|
375
415
|
* Then it merges in any advanced engine config from `this.engineConfig`.
|
|
376
416
|
*
|
|
417
|
+
* When a BanService is available (from configureMongo), SHIP and SLAP lookup
|
|
418
|
+
* services are automatically wrapped with BanAwareLookupWrapper to prevent
|
|
419
|
+
* GASP from re-syncing banned tokens.
|
|
420
|
+
*
|
|
377
421
|
* @param autoConfigureShipSlap - Whether to auto-configure SHIP and SLAP services (default: true)
|
|
378
422
|
*/
|
|
379
423
|
async configureEngine (autoConfigureShipSlap = true): Promise<void> {
|
|
380
|
-
this.ensureKnex()
|
|
424
|
+
const knex = this.ensureKnex()
|
|
381
425
|
|
|
382
426
|
if (autoConfigureShipSlap) {
|
|
383
427
|
// Auto-configure SHIP and SLAP services
|
|
@@ -391,6 +435,29 @@ export default class OverlayExpress {
|
|
|
391
435
|
))
|
|
392
436
|
}
|
|
393
437
|
|
|
438
|
+
// Wrap SHIP/SLAP lookup services with ban-aware wrappers if BanService is available.
|
|
439
|
+
// This prevents GASP from re-syncing tokens whose domains or outpoints have been banned.
|
|
440
|
+
if (this.banService !== undefined) {
|
|
441
|
+
if (this.services.ls_ship !== undefined) {
|
|
442
|
+
this.services.ls_ship = new BanAwareLookupWrapper(
|
|
443
|
+
this.services.ls_ship,
|
|
444
|
+
this.banService,
|
|
445
|
+
'SHIP',
|
|
446
|
+
this.logger
|
|
447
|
+
)
|
|
448
|
+
this.logger.log(chalk.blue('SHIP lookup service wrapped with ban-aware filter.'))
|
|
449
|
+
}
|
|
450
|
+
if (this.services.ls_slap !== undefined) {
|
|
451
|
+
this.services.ls_slap = new BanAwareLookupWrapper(
|
|
452
|
+
this.services.ls_slap,
|
|
453
|
+
this.banService,
|
|
454
|
+
'SLAP',
|
|
455
|
+
this.logger
|
|
456
|
+
)
|
|
457
|
+
this.logger.log(chalk.blue('SLAP lookup service wrapped with ban-aware filter.'))
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
394
461
|
// Construct a default sync configuration, in case the user doesn't want GASP at all:
|
|
395
462
|
let syncConfig: Record<string, string[] | 'SHIP' | false> = {}
|
|
396
463
|
if (!this.enableGASPSync) {
|
|
@@ -404,7 +471,7 @@ export default class OverlayExpress {
|
|
|
404
471
|
}
|
|
405
472
|
|
|
406
473
|
// Build the actual Storage
|
|
407
|
-
const storage = new KnexStorage(
|
|
474
|
+
const storage = new KnexStorage(knex)
|
|
408
475
|
// Include the KnexStorage migrations
|
|
409
476
|
this.migrationsToRun = [...KnexStorageMigrations.default, ...this.migrationsToRun]
|
|
410
477
|
|
|
@@ -478,37 +545,75 @@ export default class OverlayExpress {
|
|
|
478
545
|
this.engineConfig.suppressDefaultSyncAdvertisements ?? true
|
|
479
546
|
)
|
|
480
547
|
|
|
481
|
-
|
|
548
|
+
// Create the server wallet for BSV mutual authentication.
|
|
549
|
+
// This uses the same private key as the WalletAdvertiser.
|
|
550
|
+
try {
|
|
551
|
+
const keyDeriver = new KeyDeriver(new PrivateKey(this.privateKey, 'hex'))
|
|
552
|
+
const storageManager = new WalletStorageManager(keyDeriver.identityKey)
|
|
553
|
+
const signer = new WalletSigner(this.network, keyDeriver, storageManager)
|
|
554
|
+
const services = new Services(this.network)
|
|
555
|
+
const wallet = new Wallet(signer, services)
|
|
556
|
+
this.serverWallet = wallet
|
|
557
|
+
|
|
558
|
+
// Auto-set the admin identity key from the server's own key if not configured
|
|
559
|
+
if (typeof this.adminIdentityKey === 'undefined') {
|
|
560
|
+
this.adminIdentityKey = keyDeriver.identityKey
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
this.logger.log(chalk.blue('Server wallet initialized for BSV mutual authentication.'))
|
|
564
|
+
} catch (e) {
|
|
565
|
+
this.logger.log(chalk.yellow('Server wallet could not be initialized. BSV auth will not be available.'))
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
this.logger.log(chalk.green('Engine has been configured.'))
|
|
482
569
|
}
|
|
483
570
|
|
|
484
571
|
/**
|
|
485
|
-
* Ensures that Knex is configured.
|
|
572
|
+
* Ensures that Knex is configured and returns it.
|
|
486
573
|
* @throws Error if Knex is not configured
|
|
487
574
|
*/
|
|
488
|
-
private ensureKnex ():
|
|
575
|
+
private ensureKnex (): Knex.Knex {
|
|
489
576
|
if (typeof this.knex === 'undefined') {
|
|
490
577
|
throw new Error('You must configure your SQL database with the .configureKnex() method first!')
|
|
491
578
|
}
|
|
579
|
+
return this.knex
|
|
492
580
|
}
|
|
493
581
|
|
|
494
582
|
/**
|
|
495
|
-
* Ensures that MongoDB is configured.
|
|
583
|
+
* Ensures that MongoDB is configured and returns it.
|
|
496
584
|
* @throws Error if MongoDB is not configured
|
|
497
585
|
*/
|
|
498
|
-
private ensureMongo ():
|
|
586
|
+
private ensureMongo (): Db {
|
|
499
587
|
if (typeof this.mongoDb === 'undefined') {
|
|
500
588
|
throw new Error('You must configure your MongoDB connection with the .configureMongo() method first!')
|
|
501
589
|
}
|
|
590
|
+
return this.mongoDb
|
|
502
591
|
}
|
|
503
592
|
|
|
504
593
|
/**
|
|
505
|
-
* Ensures that the Overlay Engine is configured.
|
|
594
|
+
* Ensures that the Overlay Engine is configured and returns it.
|
|
506
595
|
* @throws Error if the Engine is not configured
|
|
507
596
|
*/
|
|
508
|
-
private ensureEngine ():
|
|
597
|
+
private ensureEngine (): Engine {
|
|
509
598
|
if (typeof this.engine === 'undefined') {
|
|
510
599
|
throw new Error('You must configure your Overlay Services engine with the .configureEngine() method first!')
|
|
511
600
|
}
|
|
601
|
+
return this.engine
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* Creates a JanitorService instance with current configuration.
|
|
606
|
+
*/
|
|
607
|
+
private createJanitor (): JanitorService {
|
|
608
|
+
const mongoDb = this.ensureMongo()
|
|
609
|
+
return new JanitorService({
|
|
610
|
+
mongoDb,
|
|
611
|
+
logger: this.logger,
|
|
612
|
+
requestTimeoutMs: this.janitorConfig.requestTimeoutMs,
|
|
613
|
+
hostDownRevokeScore: this.janitorConfig.hostDownRevokeScore,
|
|
614
|
+
banService: this.banService,
|
|
615
|
+
autoBanOnRemoval: this.janitorConfig.autoBanOnRemoval
|
|
616
|
+
})
|
|
512
617
|
}
|
|
513
618
|
|
|
514
619
|
/**
|
|
@@ -516,10 +621,9 @@ export default class OverlayExpress {
|
|
|
516
621
|
* Sets up routes and begins listening on the configured port.
|
|
517
622
|
*/
|
|
518
623
|
async start (): Promise<void> {
|
|
519
|
-
this.ensureEngine()
|
|
520
|
-
this.ensureKnex()
|
|
521
|
-
|
|
522
|
-
const knex = this.knex!
|
|
624
|
+
const engine = this.ensureEngine()
|
|
625
|
+
const knex = this.ensureKnex()
|
|
626
|
+
this.startTime = new Date()
|
|
523
627
|
|
|
524
628
|
this.app.use(bodyParser.json({ limit: '1gb', type: 'application/json' }))
|
|
525
629
|
this.app.use(bodyParser.raw({ limit: '1gb', type: 'application/octet-stream' }))
|
|
@@ -529,7 +633,7 @@ export default class OverlayExpress {
|
|
|
529
633
|
const startTime = Date.now()
|
|
530
634
|
|
|
531
635
|
// Log incoming request details
|
|
532
|
-
this.logger.log(chalk.magenta.bold(
|
|
636
|
+
this.logger.log(chalk.magenta.bold(`Incoming Request: ${String(req.method)} ${String(req.originalUrl)}`))
|
|
533
637
|
// Pretty-print headers
|
|
534
638
|
this.logger.log(chalk.cyan('Headers:'))
|
|
535
639
|
this.logger.log(util.inspect(req.headers, { colors: true, depth: null }))
|
|
@@ -567,7 +671,7 @@ export default class OverlayExpress {
|
|
|
567
671
|
const duration = Date.now() - startTime
|
|
568
672
|
this.logger.log(
|
|
569
673
|
chalk.magenta.bold(
|
|
570
|
-
|
|
674
|
+
`Outgoing Response: ${String(req.method)} ${String(req.originalUrl)} - Status: ${String(res.statusCode)} - Duration: ${String(duration)}ms`
|
|
571
675
|
)
|
|
572
676
|
)
|
|
573
677
|
this.logger.log(chalk.cyan('Response Headers:'))
|
|
@@ -616,7 +720,10 @@ export default class OverlayExpress {
|
|
|
616
720
|
// Serve a static documentation site or user interface
|
|
617
721
|
this.app.get('/', (req, res) => {
|
|
618
722
|
res.set('content-type', 'text/html')
|
|
619
|
-
res.send(makeUserInterface(
|
|
723
|
+
res.send(makeUserInterface({
|
|
724
|
+
...this.webUIConfig,
|
|
725
|
+
adminIdentityKey: this.adminIdentityKey
|
|
726
|
+
}))
|
|
620
727
|
})
|
|
621
728
|
|
|
622
729
|
// Serve a health check endpoint
|
|
@@ -637,7 +744,6 @@ export default class OverlayExpress {
|
|
|
637
744
|
})
|
|
638
745
|
}
|
|
639
746
|
})().catch(() => {
|
|
640
|
-
// This catch is for any unforeseen errors in the async IIFE itself
|
|
641
747
|
res.status(500).json({
|
|
642
748
|
status: 'error',
|
|
643
749
|
message: 'Unexpected error'
|
|
@@ -742,7 +848,7 @@ export default class OverlayExpress {
|
|
|
742
848
|
res.status(200).json(steak)
|
|
743
849
|
}
|
|
744
850
|
} catch (error) {
|
|
745
|
-
console.error(chalk.red('
|
|
851
|
+
console.error(chalk.red('Error in /submit:'), error)
|
|
746
852
|
return res.status(400).json({
|
|
747
853
|
status: 'error',
|
|
748
854
|
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
@@ -835,7 +941,7 @@ export default class OverlayExpress {
|
|
|
835
941
|
await engine.handleNewMerkleProof(txid, merklePath, blockHeight)
|
|
836
942
|
return res.status(200).json({ status: 'success', message: 'Transaction status updated' })
|
|
837
943
|
} catch (error) {
|
|
838
|
-
console.error(chalk.red('
|
|
944
|
+
console.error(chalk.red('Error in /arc-ingest:'), error)
|
|
839
945
|
return res.status(400).json({
|
|
840
946
|
status: 'error',
|
|
841
947
|
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
@@ -849,7 +955,7 @@ export default class OverlayExpress {
|
|
|
849
955
|
})
|
|
850
956
|
})
|
|
851
957
|
} else {
|
|
852
|
-
this.logger.warn(chalk.yellow('
|
|
958
|
+
this.logger.warn(chalk.yellow('Disabling ARC because no ARC API key was provided.'))
|
|
853
959
|
}
|
|
854
960
|
|
|
855
961
|
// GASP sync routes if enabled
|
|
@@ -861,7 +967,7 @@ export default class OverlayExpress {
|
|
|
861
967
|
const response = await engine.provideForeignSyncResponse(req.body, topic)
|
|
862
968
|
return res.status(200).json(response)
|
|
863
969
|
} catch (error) {
|
|
864
|
-
console.error(chalk.red('
|
|
970
|
+
console.error(chalk.red('Error in /requestSyncResponse:'), error)
|
|
865
971
|
return res.status(400).json({
|
|
866
972
|
status: 'error',
|
|
867
973
|
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
@@ -882,7 +988,7 @@ export default class OverlayExpress {
|
|
|
882
988
|
const response = await engine.provideForeignGASPNode(graphID, txid, outputIndex)
|
|
883
989
|
return res.status(200).json(response)
|
|
884
990
|
} catch (error) {
|
|
885
|
-
console.error(chalk.red('
|
|
991
|
+
console.error(chalk.red('Error in /requestForeignGASPNode:'), error)
|
|
886
992
|
return res.status(400).json({
|
|
887
993
|
status: 'error',
|
|
888
994
|
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
@@ -896,32 +1002,431 @@ export default class OverlayExpress {
|
|
|
896
1002
|
})
|
|
897
1003
|
})
|
|
898
1004
|
} else {
|
|
899
|
-
this.logger.warn(chalk.yellow('
|
|
1005
|
+
this.logger.warn(chalk.yellow('GASP sync is disabled.'))
|
|
900
1006
|
}
|
|
901
1007
|
|
|
902
1008
|
/**
|
|
903
1009
|
* ============== ADMIN ROUTES ==============
|
|
904
|
-
* These routes expose advanced engine operations
|
|
905
|
-
*
|
|
1010
|
+
* These routes expose advanced engine operations.
|
|
1011
|
+
* Authentication: Bearer token OR BSV mutual auth (identity key match).
|
|
906
1012
|
*/
|
|
907
1013
|
|
|
908
1014
|
/**
|
|
909
|
-
*
|
|
1015
|
+
* Set up BSV mutual authentication middleware if a server wallet is available.
|
|
1016
|
+
* This handles the /.well-known/auth handshake automatically.
|
|
1017
|
+
* With allowUnauthenticated: true, it passes through when no BSV auth headers
|
|
1018
|
+
* are present, allowing Bearer token fallback.
|
|
1019
|
+
*/
|
|
1020
|
+
if (this.serverWallet !== undefined) {
|
|
1021
|
+
const bsvAuth = createAuthMiddleware({
|
|
1022
|
+
wallet: this.serverWallet,
|
|
1023
|
+
allowUnauthenticated: true
|
|
1024
|
+
})
|
|
1025
|
+
this.app.use(bsvAuth as any)
|
|
1026
|
+
this.logger.log(chalk.blue('BSV mutual authentication middleware enabled.'))
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
/**
|
|
1030
|
+
* Middleware for checking admin authentication.
|
|
1031
|
+
* Supports two authentication methods:
|
|
1032
|
+
* 1. Bearer token (Authorization: Bearer <token>) - for cron jobs, scripts, and fallback
|
|
1033
|
+
* 2. BSV mutual auth - if req.auth.identityKey matches the admin identity key
|
|
910
1034
|
*/
|
|
911
1035
|
const checkAdminAuth = (req: express.Request, res: express.Response, next: express.NextFunction): void => {
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
1036
|
+
// Method 1: BSV mutual authentication (identity key match)
|
|
1037
|
+
const authReq = req as AuthRequest
|
|
1038
|
+
if (
|
|
1039
|
+
typeof this.adminIdentityKey === 'string' &&
|
|
1040
|
+
authReq.auth !== undefined &&
|
|
1041
|
+
typeof authReq.auth.identityKey === 'string' &&
|
|
1042
|
+
authReq.auth.identityKey !== 'unknown' &&
|
|
1043
|
+
authReq.auth.identityKey === this.adminIdentityKey
|
|
1044
|
+
) {
|
|
1045
|
+
next()
|
|
915
1046
|
return
|
|
916
1047
|
}
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
1048
|
+
|
|
1049
|
+
// Method 2: Bearer token authentication
|
|
1050
|
+
const authHeader = req.headers.authorization
|
|
1051
|
+
if (typeof authHeader === 'string' && authHeader.startsWith('Bearer ')) {
|
|
1052
|
+
const token = authHeader.substring('Bearer '.length)
|
|
1053
|
+
if (token === this.adminToken) {
|
|
1054
|
+
next()
|
|
1055
|
+
return
|
|
1056
|
+
}
|
|
1057
|
+
res.status(403).json({ status: 'error', message: 'Forbidden: Invalid credentials' })
|
|
920
1058
|
return
|
|
921
1059
|
}
|
|
922
|
-
|
|
1060
|
+
|
|
1061
|
+
res.status(401).json({ status: 'error', message: 'Unauthorized: Provide a Bearer token or authenticate with your wallet' })
|
|
923
1062
|
}
|
|
924
1063
|
|
|
1064
|
+
/**
|
|
1065
|
+
* Public endpoint that returns the admin identity key (if configured).
|
|
1066
|
+
* This allows the frontend to detect whether the current wallet user
|
|
1067
|
+
* is the admin by comparing their identity key against this value.
|
|
1068
|
+
* The identity key is a public key, so exposing it is safe.
|
|
1069
|
+
*/
|
|
1070
|
+
this.app.get('/admin/config', (_, res) => {
|
|
1071
|
+
res.status(200).json({
|
|
1072
|
+
adminIdentityKey: this.adminIdentityKey ?? null,
|
|
1073
|
+
nodeName: this.name
|
|
1074
|
+
})
|
|
1075
|
+
})
|
|
1076
|
+
|
|
1077
|
+
/**
|
|
1078
|
+
* Admin route: Get server statistics and overview.
|
|
1079
|
+
*/
|
|
1080
|
+
this.app.get('/admin/stats', checkAdminAuth as any, (req, res) => {
|
|
1081
|
+
; (async () => {
|
|
1082
|
+
try {
|
|
1083
|
+
const db = this.ensureMongo()
|
|
1084
|
+
|
|
1085
|
+
const [shipCount, slapCount, banStats] = await Promise.all([
|
|
1086
|
+
db.collection('shipRecords').countDocuments(),
|
|
1087
|
+
db.collection('slapRecords').countDocuments(),
|
|
1088
|
+
this.banService?.getStats() ?? { domainBans: 0, outpointBans: 0, totalBans: 0 }
|
|
1089
|
+
])
|
|
1090
|
+
|
|
1091
|
+
return res.status(200).json({
|
|
1092
|
+
status: 'success',
|
|
1093
|
+
data: {
|
|
1094
|
+
nodeName: this.name,
|
|
1095
|
+
network: this.network,
|
|
1096
|
+
uptime: this.startTime !== undefined ? Date.now() - this.startTime.getTime() : 0,
|
|
1097
|
+
startedAt: this.startTime?.toISOString(),
|
|
1098
|
+
shipRecordCount: shipCount,
|
|
1099
|
+
slapRecordCount: slapCount,
|
|
1100
|
+
bannedDomains: banStats.domainBans,
|
|
1101
|
+
bannedOutpoints: banStats.outpointBans,
|
|
1102
|
+
totalBans: banStats.totalBans,
|
|
1103
|
+
topicManagers: Object.keys(this.managers),
|
|
1104
|
+
lookupServices: Object.keys(this.services),
|
|
1105
|
+
gaspSyncEnabled: this.enableGASPSync
|
|
1106
|
+
}
|
|
1107
|
+
})
|
|
1108
|
+
} catch (error) {
|
|
1109
|
+
return res.status(400).json({
|
|
1110
|
+
status: 'error',
|
|
1111
|
+
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
1112
|
+
})
|
|
1113
|
+
}
|
|
1114
|
+
})().catch(() => {
|
|
1115
|
+
res.status(500).json({ status: 'error', message: 'Unexpected error' })
|
|
1116
|
+
})
|
|
1117
|
+
})
|
|
1118
|
+
|
|
1119
|
+
/**
|
|
1120
|
+
* Admin route: List all SHIP records with full details.
|
|
1121
|
+
*/
|
|
1122
|
+
this.app.get('/admin/ship-records', checkAdminAuth as any, (req, res) => {
|
|
1123
|
+
; (async () => {
|
|
1124
|
+
try {
|
|
1125
|
+
const db = this.ensureMongo()
|
|
1126
|
+
const collection = db.collection('shipRecords')
|
|
1127
|
+
|
|
1128
|
+
const search = typeof req.query.search === 'string' ? req.query.search : undefined
|
|
1129
|
+
const rawPage = parseInt(req.query.page as string, 10)
|
|
1130
|
+
const page = Math.max(1, Number.isNaN(rawPage) ? 1 : rawPage)
|
|
1131
|
+
const rawLimit = parseInt(req.query.limit as string, 10)
|
|
1132
|
+
const limit = Math.min(200, Math.max(1, Number.isNaN(rawLimit) ? 50 : rawLimit))
|
|
1133
|
+
const skip = (page - 1) * limit
|
|
1134
|
+
|
|
1135
|
+
const query: any = {}
|
|
1136
|
+
if (typeof search === 'string' && search.length > 0) {
|
|
1137
|
+
query.$or = [
|
|
1138
|
+
{ domain: { $regex: search, $options: 'i' } },
|
|
1139
|
+
{ topic: { $regex: search, $options: 'i' } },
|
|
1140
|
+
{ identityKey: { $regex: search, $options: 'i' } },
|
|
1141
|
+
{ txid: { $regex: search, $options: 'i' } }
|
|
1142
|
+
]
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
const [records, total] = await Promise.all([
|
|
1146
|
+
collection.find(query).sort({ createdAt: -1 }).skip(skip).limit(limit).toArray(),
|
|
1147
|
+
collection.countDocuments(query)
|
|
1148
|
+
])
|
|
1149
|
+
|
|
1150
|
+
return res.status(200).json({
|
|
1151
|
+
status: 'success',
|
|
1152
|
+
data: { records, total, page, limit, pages: Math.ceil(total / limit) }
|
|
1153
|
+
})
|
|
1154
|
+
} catch (error) {
|
|
1155
|
+
return res.status(400).json({
|
|
1156
|
+
status: 'error',
|
|
1157
|
+
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
1158
|
+
})
|
|
1159
|
+
}
|
|
1160
|
+
})().catch(() => {
|
|
1161
|
+
res.status(500).json({ status: 'error', message: 'Unexpected error' })
|
|
1162
|
+
})
|
|
1163
|
+
})
|
|
1164
|
+
|
|
1165
|
+
/**
|
|
1166
|
+
* Admin route: List all SLAP records with full details.
|
|
1167
|
+
*/
|
|
1168
|
+
this.app.get('/admin/slap-records', checkAdminAuth as any, (req, res) => {
|
|
1169
|
+
; (async () => {
|
|
1170
|
+
try {
|
|
1171
|
+
const db = this.ensureMongo()
|
|
1172
|
+
const collection = db.collection('slapRecords')
|
|
1173
|
+
|
|
1174
|
+
const search = typeof req.query.search === 'string' ? req.query.search : undefined
|
|
1175
|
+
const rawPage = parseInt(req.query.page as string, 10)
|
|
1176
|
+
const page = Math.max(1, Number.isNaN(rawPage) ? 1 : rawPage)
|
|
1177
|
+
const rawLimit = parseInt(req.query.limit as string, 10)
|
|
1178
|
+
const limit = Math.min(200, Math.max(1, Number.isNaN(rawLimit) ? 50 : rawLimit))
|
|
1179
|
+
const skip = (page - 1) * limit
|
|
1180
|
+
|
|
1181
|
+
const query: any = {}
|
|
1182
|
+
if (typeof search === 'string' && search.length > 0) {
|
|
1183
|
+
query.$or = [
|
|
1184
|
+
{ domain: { $regex: search, $options: 'i' } },
|
|
1185
|
+
{ service: { $regex: search, $options: 'i' } },
|
|
1186
|
+
{ identityKey: { $regex: search, $options: 'i' } },
|
|
1187
|
+
{ txid: { $regex: search, $options: 'i' } }
|
|
1188
|
+
]
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
const [records, total] = await Promise.all([
|
|
1192
|
+
collection.find(query).sort({ createdAt: -1 }).skip(skip).limit(limit).toArray(),
|
|
1193
|
+
collection.countDocuments(query)
|
|
1194
|
+
])
|
|
1195
|
+
|
|
1196
|
+
return res.status(200).json({
|
|
1197
|
+
status: 'success',
|
|
1198
|
+
data: { records, total, page, limit, pages: Math.ceil(total / limit) }
|
|
1199
|
+
})
|
|
1200
|
+
} catch (error) {
|
|
1201
|
+
return res.status(400).json({
|
|
1202
|
+
status: 'error',
|
|
1203
|
+
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
1204
|
+
})
|
|
1205
|
+
}
|
|
1206
|
+
})().catch(() => {
|
|
1207
|
+
res.status(500).json({ status: 'error', message: 'Unexpected error' })
|
|
1208
|
+
})
|
|
1209
|
+
})
|
|
1210
|
+
|
|
1211
|
+
/**
|
|
1212
|
+
* Admin route: Check health of a specific URL.
|
|
1213
|
+
*/
|
|
1214
|
+
this.app.post('/admin/health-check', checkAdminAuth as any, (req, res) => {
|
|
1215
|
+
; (async () => {
|
|
1216
|
+
try {
|
|
1217
|
+
const { url } = req.body
|
|
1218
|
+
if (typeof url !== 'string' || url.length === 0) {
|
|
1219
|
+
return res.status(400).json({ status: 'error', message: 'url is required' })
|
|
1220
|
+
}
|
|
1221
|
+
const janitor = this.createJanitor()
|
|
1222
|
+
const result = await janitor.checkHost(url)
|
|
1223
|
+
return res.status(200).json({ status: 'success', data: { url, ...result } })
|
|
1224
|
+
} catch (error) {
|
|
1225
|
+
return res.status(400).json({
|
|
1226
|
+
status: 'error',
|
|
1227
|
+
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
1228
|
+
})
|
|
1229
|
+
}
|
|
1230
|
+
})().catch(() => {
|
|
1231
|
+
res.status(500).json({ status: 'error', message: 'Unexpected error' })
|
|
1232
|
+
})
|
|
1233
|
+
})
|
|
1234
|
+
|
|
1235
|
+
/**
|
|
1236
|
+
* Admin route: Ban a domain or outpoint.
|
|
1237
|
+
*/
|
|
1238
|
+
this.app.post('/admin/ban', checkAdminAuth as any, (req, res) => {
|
|
1239
|
+
; (async () => {
|
|
1240
|
+
try {
|
|
1241
|
+
if (this.banService === undefined) {
|
|
1242
|
+
return res.status(400).json({ status: 'error', message: 'Ban service not available (MongoDB not configured)' })
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
const { type, value, reason } = req.body
|
|
1246
|
+
if (type !== 'domain' && type !== 'outpoint') {
|
|
1247
|
+
return res.status(400).json({ status: 'error', message: 'type must be "domain" or "outpoint"' })
|
|
1248
|
+
}
|
|
1249
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
1250
|
+
return res.status(400).json({ status: 'error', message: 'value is required' })
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
if (type === 'domain') {
|
|
1254
|
+
await this.banService.banDomain(value, reason)
|
|
1255
|
+
|
|
1256
|
+
// Also remove any existing records for this domain from SHIP and SLAP
|
|
1257
|
+
const db = this.ensureMongo()
|
|
1258
|
+
const [shipDeleted, slapDeleted] = await Promise.all([
|
|
1259
|
+
db.collection('shipRecords').deleteMany({ domain: value }),
|
|
1260
|
+
db.collection('slapRecords').deleteMany({ domain: value })
|
|
1261
|
+
])
|
|
1262
|
+
|
|
1263
|
+
return res.status(200).json({
|
|
1264
|
+
status: 'success',
|
|
1265
|
+
message: `Domain "${value}" banned. Removed ${shipDeleted.deletedCount} SHIP and ${slapDeleted.deletedCount} SLAP records.`
|
|
1266
|
+
})
|
|
1267
|
+
} else {
|
|
1268
|
+
// Parse outpoint: "txid.outputIndex"
|
|
1269
|
+
const dotIndex = value.lastIndexOf('.')
|
|
1270
|
+
if (dotIndex === -1) {
|
|
1271
|
+
return res.status(400).json({ status: 'error', message: 'Outpoint format must be "txid.outputIndex"' })
|
|
1272
|
+
}
|
|
1273
|
+
const txid = value.substring(0, dotIndex)
|
|
1274
|
+
const outputIndex = parseInt(value.substring(dotIndex + 1))
|
|
1275
|
+
if (isNaN(outputIndex)) {
|
|
1276
|
+
return res.status(400).json({ status: 'error', message: 'Invalid outputIndex in outpoint' })
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
await this.banService.banOutpoint(txid, outputIndex, reason)
|
|
1280
|
+
|
|
1281
|
+
// Also evict from lookup services
|
|
1282
|
+
const services = Object.values(engine.lookupServices)
|
|
1283
|
+
for (const service of services) {
|
|
1284
|
+
try {
|
|
1285
|
+
await service.outputEvicted(txid, outputIndex)
|
|
1286
|
+
} catch {
|
|
1287
|
+
continue
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
return res.status(200).json({
|
|
1292
|
+
status: 'success',
|
|
1293
|
+
message: `Outpoint "${value}" banned and evicted from lookup services.`
|
|
1294
|
+
})
|
|
1295
|
+
}
|
|
1296
|
+
} catch (error) {
|
|
1297
|
+
return res.status(400).json({
|
|
1298
|
+
status: 'error',
|
|
1299
|
+
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
1300
|
+
})
|
|
1301
|
+
}
|
|
1302
|
+
})().catch(() => {
|
|
1303
|
+
res.status(500).json({ status: 'error', message: 'Unexpected error' })
|
|
1304
|
+
})
|
|
1305
|
+
})
|
|
1306
|
+
|
|
1307
|
+
/**
|
|
1308
|
+
* Admin route: Remove a ban.
|
|
1309
|
+
*/
|
|
1310
|
+
this.app.post('/admin/unban', checkAdminAuth as any, (req, res) => {
|
|
1311
|
+
; (async () => {
|
|
1312
|
+
try {
|
|
1313
|
+
if (this.banService === undefined) {
|
|
1314
|
+
return res.status(400).json({ status: 'error', message: 'Ban service not available' })
|
|
1315
|
+
}
|
|
1316
|
+
const { type, value } = req.body as { type: unknown, value: unknown }
|
|
1317
|
+
if (type !== 'domain' && type !== 'outpoint') {
|
|
1318
|
+
return res.status(400).json({ status: 'error', message: 'type must be "domain" or "outpoint"' })
|
|
1319
|
+
}
|
|
1320
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
1321
|
+
return res.status(400).json({ status: 'error', message: 'value is required' })
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
await this.banService.removeBan(type, value)
|
|
1325
|
+
return res.status(200).json({ status: 'success', message: `${type} "${String(value)}" unbanned.` })
|
|
1326
|
+
} catch (error) {
|
|
1327
|
+
return res.status(400).json({
|
|
1328
|
+
status: 'error',
|
|
1329
|
+
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
1330
|
+
})
|
|
1331
|
+
}
|
|
1332
|
+
})().catch(() => {
|
|
1333
|
+
res.status(500).json({ status: 'error', message: 'Unexpected error' })
|
|
1334
|
+
})
|
|
1335
|
+
})
|
|
1336
|
+
|
|
1337
|
+
/**
|
|
1338
|
+
* Admin route: List all bans.
|
|
1339
|
+
*/
|
|
1340
|
+
this.app.get('/admin/bans', checkAdminAuth as any, (req, res) => {
|
|
1341
|
+
; (async () => {
|
|
1342
|
+
try {
|
|
1343
|
+
if (this.banService === undefined) {
|
|
1344
|
+
return res.status(200).json({ status: 'success', data: { bans: [] } })
|
|
1345
|
+
}
|
|
1346
|
+
const type = req.query.type as 'domain' | 'outpoint' | undefined
|
|
1347
|
+
const validType = type === 'domain' || type === 'outpoint' ? type : undefined
|
|
1348
|
+
const bans = await this.banService.listBans(validType)
|
|
1349
|
+
return res.status(200).json({ status: 'success', data: { bans } })
|
|
1350
|
+
} catch (error) {
|
|
1351
|
+
return res.status(400).json({
|
|
1352
|
+
status: 'error',
|
|
1353
|
+
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
1354
|
+
})
|
|
1355
|
+
}
|
|
1356
|
+
})().catch(() => {
|
|
1357
|
+
res.status(500).json({ status: 'error', message: 'Unexpected error' })
|
|
1358
|
+
})
|
|
1359
|
+
})
|
|
1360
|
+
|
|
1361
|
+
/**
|
|
1362
|
+
* Admin route: Remove a token by outpoint, optionally banning the domain.
|
|
1363
|
+
*/
|
|
1364
|
+
this.app.post('/admin/remove-token', checkAdminAuth as any, (req, res) => {
|
|
1365
|
+
; (async () => {
|
|
1366
|
+
try {
|
|
1367
|
+
const { txid, outputIndex, service, ban, banDomain: shouldBanDomain } = req.body
|
|
1368
|
+
if (typeof txid !== 'string' || typeof outputIndex !== 'number') {
|
|
1369
|
+
return res.status(400).json({ status: 'error', message: 'txid (string) and outputIndex (number) are required' })
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
// Get the domain before removing (for ban option)
|
|
1373
|
+
let removedDomain: string | undefined
|
|
1374
|
+
if (shouldBanDomain === true || ban === true) {
|
|
1375
|
+
const db = this.ensureMongo()
|
|
1376
|
+
const shipRecord = await db.collection('shipRecords').findOne({ txid, outputIndex })
|
|
1377
|
+
const slapRecord = await db.collection('slapRecords').findOne({ txid, outputIndex })
|
|
1378
|
+
removedDomain = (shipRecord?.domain ?? slapRecord?.domain) as string | undefined
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
// Evict from specified service or all services
|
|
1382
|
+
if (typeof service === 'string') {
|
|
1383
|
+
const svc = engine.lookupServices[service]
|
|
1384
|
+
if (svc !== undefined) {
|
|
1385
|
+
await svc.outputEvicted(txid, outputIndex)
|
|
1386
|
+
}
|
|
1387
|
+
} else {
|
|
1388
|
+
const services = Object.values(engine.lookupServices)
|
|
1389
|
+
for (const svc of services) {
|
|
1390
|
+
try {
|
|
1391
|
+
await svc.outputEvicted(txid, outputIndex)
|
|
1392
|
+
} catch {
|
|
1393
|
+
continue
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
// Ban the outpoint if requested
|
|
1399
|
+
if (ban === true && this.banService !== undefined) {
|
|
1400
|
+
await this.banService.banOutpoint(txid, outputIndex, 'Manually removed by admin', removedDomain)
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
// Ban the domain if requested
|
|
1404
|
+
if (shouldBanDomain === true && typeof removedDomain === 'string' && this.banService !== undefined) {
|
|
1405
|
+
await this.banService.banDomain(removedDomain, 'Domain banned by admin via token removal')
|
|
1406
|
+
|
|
1407
|
+
// Remove all records for this domain
|
|
1408
|
+
const banDb = this.ensureMongo()
|
|
1409
|
+
await Promise.all([
|
|
1410
|
+
banDb.collection('shipRecords').deleteMany({ domain: removedDomain }),
|
|
1411
|
+
banDb.collection('slapRecords').deleteMany({ domain: removedDomain })
|
|
1412
|
+
])
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
return res.status(200).json({
|
|
1416
|
+
status: 'success',
|
|
1417
|
+
message: `Token ${txid}.${outputIndex} removed.${ban === true ? ' Outpoint banned.' : ''}${shouldBanDomain === true && typeof removedDomain === 'string' ? ` Domain "${removedDomain}" banned.` : ''}`
|
|
1418
|
+
})
|
|
1419
|
+
} catch (error) {
|
|
1420
|
+
return res.status(400).json({
|
|
1421
|
+
status: 'error',
|
|
1422
|
+
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
1423
|
+
})
|
|
1424
|
+
}
|
|
1425
|
+
})().catch(() => {
|
|
1426
|
+
res.status(500).json({ status: 'error', message: 'Unexpected error' })
|
|
1427
|
+
})
|
|
1428
|
+
})
|
|
1429
|
+
|
|
925
1430
|
/**
|
|
926
1431
|
* Admin route to manually sync advertisements, calling `engine.syncAdvertisements()`.
|
|
927
1432
|
*/
|
|
@@ -931,7 +1436,7 @@ export default class OverlayExpress {
|
|
|
931
1436
|
await engine.syncAdvertisements()
|
|
932
1437
|
return res.status(200).json({ status: 'success', message: 'Advertisements synced successfully' })
|
|
933
1438
|
} catch (error) {
|
|
934
|
-
console.error(chalk.red('
|
|
1439
|
+
console.error(chalk.red('Error in /admin/syncAdvertisements:'), error)
|
|
935
1440
|
return res.status(400).json({
|
|
936
1441
|
status: 'error',
|
|
937
1442
|
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
@@ -954,7 +1459,7 @@ export default class OverlayExpress {
|
|
|
954
1459
|
await engine.startGASPSync()
|
|
955
1460
|
return res.status(200).json({ status: 'success', message: 'GASP sync started and completed' })
|
|
956
1461
|
} catch (error) {
|
|
957
|
-
console.error(chalk.red('
|
|
1462
|
+
console.error(chalk.red('Error in /admin/startGASPSync:'), error)
|
|
958
1463
|
return res.status(400).json({
|
|
959
1464
|
status: 'error',
|
|
960
1465
|
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
@@ -989,7 +1494,7 @@ export default class OverlayExpress {
|
|
|
989
1494
|
}
|
|
990
1495
|
return res.status(200).json({ status: 'success', message: 'Outpoint evicted' })
|
|
991
1496
|
} catch (error) {
|
|
992
|
-
console.error(chalk.red('
|
|
1497
|
+
console.error(chalk.red('Error in /admin/evictOutpoint:'), error)
|
|
993
1498
|
return res.status(400).json({
|
|
994
1499
|
status: 'error',
|
|
995
1500
|
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
@@ -1004,22 +1509,16 @@ export default class OverlayExpress {
|
|
|
1004
1509
|
})
|
|
1005
1510
|
|
|
1006
1511
|
/**
|
|
1007
|
-
* Admin route to run the janitor service.
|
|
1512
|
+
* Admin route to run the janitor service with enhanced reporting.
|
|
1008
1513
|
*/
|
|
1009
1514
|
this.app.post('/admin/janitor', checkAdminAuth as any, (req, res) => {
|
|
1010
1515
|
; (async () => {
|
|
1011
1516
|
try {
|
|
1012
|
-
this.
|
|
1013
|
-
const
|
|
1014
|
-
|
|
1015
|
-
logger: this.logger,
|
|
1016
|
-
requestTimeoutMs: this.janitorConfig.requestTimeoutMs,
|
|
1017
|
-
hostDownRevokeScore: this.janitorConfig.hostDownRevokeScore
|
|
1018
|
-
})
|
|
1019
|
-
await janitor.run()
|
|
1020
|
-
return res.status(200).json({ status: 'success', message: 'Janitor run completed' })
|
|
1517
|
+
const janitor = this.createJanitor()
|
|
1518
|
+
const report: JanitorReport = await janitor.run()
|
|
1519
|
+
return res.status(200).json({ status: 'success', message: 'Janitor run completed', data: report })
|
|
1021
1520
|
} catch (error) {
|
|
1022
|
-
console.error(chalk.red('
|
|
1521
|
+
console.error(chalk.red('Error in /admin/janitor:'), error)
|
|
1023
1522
|
return res.status(400).json({
|
|
1024
1523
|
status: 'error',
|
|
1025
1524
|
message: error instanceof Error ? error.message : 'An unknown error occurred'
|
|
@@ -1038,11 +1537,11 @@ export default class OverlayExpress {
|
|
|
1038
1537
|
const result = await knex.migrate.latest({
|
|
1039
1538
|
migrationSource
|
|
1040
1539
|
})
|
|
1041
|
-
this.logger.log(chalk.green('
|
|
1540
|
+
this.logger.log(chalk.green('Knex migrations run'), result)
|
|
1042
1541
|
|
|
1043
1542
|
// 404 handler for all other routes
|
|
1044
1543
|
this.app.use((req, res) => {
|
|
1045
|
-
this.logger.log(chalk.red('
|
|
1544
|
+
this.logger.log(chalk.red('404 Not Found:'), req.url)
|
|
1046
1545
|
res.status(404).json({
|
|
1047
1546
|
status: 'error',
|
|
1048
1547
|
code: 'ERR_ROUTE_NOT_FOUND',
|
|
@@ -1070,7 +1569,7 @@ export default class OverlayExpress {
|
|
|
1070
1569
|
try {
|
|
1071
1570
|
await this.engine?.syncAdvertisements()
|
|
1072
1571
|
} catch (e) {
|
|
1073
|
-
this.logger.log(chalk.red('
|
|
1572
|
+
this.logger.log(chalk.red('Error syncing advertisements:'), e)
|
|
1074
1573
|
}
|
|
1075
1574
|
|
|
1076
1575
|
// Attempt to do GASP sync if enabled
|
|
@@ -1080,7 +1579,7 @@ export default class OverlayExpress {
|
|
|
1080
1579
|
await this.engine?.startGASPSync()
|
|
1081
1580
|
this.logger.log(chalk.green('GASP sync complete!'))
|
|
1082
1581
|
} catch (e) {
|
|
1083
|
-
console.error(chalk.red('
|
|
1582
|
+
console.error(chalk.red('Failed to GASP sync'), e)
|
|
1084
1583
|
}
|
|
1085
1584
|
} else {
|
|
1086
1585
|
this.logger.log(chalk.yellow(`${this.name} will not sync because GASP has been disabled.`))
|
|
@@ -1088,7 +1587,7 @@ export default class OverlayExpress {
|
|
|
1088
1587
|
|
|
1089
1588
|
// Start listening on the configured port
|
|
1090
1589
|
this.app.listen(this.port, () => {
|
|
1091
|
-
this.logger.log(chalk.green.bold(
|
|
1590
|
+
this.logger.log(chalk.green.bold(`${this.name} is ready and listening on local port ${this.port}`))
|
|
1092
1591
|
})
|
|
1093
1592
|
}
|
|
1094
1593
|
}
|