@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
package/src/JanitorService.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Db } from 'mongodb'
|
|
2
2
|
import chalk from 'chalk'
|
|
3
|
+
import { BanService } from './BanService.js'
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Configuration for the Janitor Service
|
|
@@ -9,98 +10,306 @@ export interface JanitorConfig {
|
|
|
9
10
|
logger?: typeof console
|
|
10
11
|
requestTimeoutMs?: number
|
|
11
12
|
hostDownRevokeScore?: number
|
|
13
|
+
/** Optional BanService for persisting bans when hosts are removed */
|
|
14
|
+
banService?: BanService
|
|
15
|
+
/** Whether to auto-ban domains when they exceed the down threshold (default: true) */
|
|
16
|
+
autoBanOnRemoval?: boolean
|
|
12
17
|
}
|
|
13
18
|
|
|
14
19
|
/**
|
|
15
|
-
*
|
|
20
|
+
* Health status of a single record
|
|
21
|
+
*/
|
|
22
|
+
export interface HostHealthResult {
|
|
23
|
+
txid: string
|
|
24
|
+
outputIndex: number
|
|
25
|
+
domain: string
|
|
26
|
+
topic?: string
|
|
27
|
+
service?: string
|
|
28
|
+
identityKey?: string
|
|
29
|
+
createdAt?: Date
|
|
30
|
+
healthy: boolean
|
|
31
|
+
downCount: number
|
|
32
|
+
responseTimeMs?: number
|
|
33
|
+
statusCode?: number
|
|
34
|
+
error?: string
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Summary report from a janitor run
|
|
39
|
+
*/
|
|
40
|
+
export interface JanitorReport {
|
|
41
|
+
startedAt: Date
|
|
42
|
+
completedAt: Date
|
|
43
|
+
durationMs: number
|
|
44
|
+
shipResults: HostHealthResult[]
|
|
45
|
+
slapResults: HostHealthResult[]
|
|
46
|
+
summary: {
|
|
47
|
+
totalChecked: number
|
|
48
|
+
healthy: number
|
|
49
|
+
unhealthy: number
|
|
50
|
+
removed: number
|
|
51
|
+
banned: number
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* JanitorService runs health checks on SHIP and SLAP outputs.
|
|
16
57
|
* It validates domain names and checks /health endpoints to ensure services are operational.
|
|
17
58
|
*
|
|
18
59
|
* When a service is down, it increments a "down" counter. When healthy, it decrements.
|
|
19
|
-
* If the down counter reaches
|
|
20
|
-
*
|
|
21
|
-
* This service is designed to be run periodically via external schedulers (e.g., cron, docker-compose).
|
|
60
|
+
* If the down counter reaches hostDownRevokeScore, the output is deleted and optionally
|
|
61
|
+
* the domain is added to the persistent ban list to prevent GASP re-sync.
|
|
22
62
|
*/
|
|
23
63
|
export class JanitorService {
|
|
24
64
|
private readonly mongoDb: Db
|
|
25
65
|
private readonly logger: typeof console
|
|
26
66
|
private readonly requestTimeoutMs: number
|
|
27
67
|
private readonly hostDownRevokeScore: number
|
|
68
|
+
private readonly banService?: BanService
|
|
69
|
+
private readonly autoBanOnRemoval: boolean
|
|
28
70
|
|
|
29
71
|
constructor (config: JanitorConfig) {
|
|
30
72
|
this.mongoDb = config.mongoDb
|
|
31
73
|
this.logger = config.logger ?? console
|
|
32
|
-
this.requestTimeoutMs = config.requestTimeoutMs ?? 10000
|
|
74
|
+
this.requestTimeoutMs = config.requestTimeoutMs ?? 10000
|
|
33
75
|
this.hostDownRevokeScore = config.hostDownRevokeScore ?? 3
|
|
76
|
+
this.banService = config.banService
|
|
77
|
+
this.autoBanOnRemoval = config.autoBanOnRemoval ?? true
|
|
34
78
|
}
|
|
35
79
|
|
|
36
80
|
/**
|
|
37
|
-
* Runs a
|
|
81
|
+
* Runs a full pass of health checks on all SHIP and SLAP outputs.
|
|
82
|
+
* Returns a detailed report of the results.
|
|
38
83
|
*/
|
|
39
|
-
async run (): Promise<
|
|
40
|
-
|
|
84
|
+
async run (): Promise<JanitorReport> {
|
|
85
|
+
const startedAt = new Date()
|
|
86
|
+
this.logger.log(chalk.blue('Running janitor health checks...'))
|
|
87
|
+
|
|
88
|
+
let shipResults: HostHealthResult[] = []
|
|
89
|
+
let slapResults: HostHealthResult[] = []
|
|
90
|
+
let removed = 0
|
|
91
|
+
let banned = 0
|
|
41
92
|
|
|
42
93
|
try {
|
|
43
|
-
|
|
44
|
-
|
|
94
|
+
const shipCheckResult = await this.checkTopicOutputs('shipRecords', 'topic')
|
|
95
|
+
shipResults = shipCheckResult.results
|
|
96
|
+
removed += shipCheckResult.removed
|
|
97
|
+
banned += shipCheckResult.banned
|
|
45
98
|
|
|
46
|
-
|
|
47
|
-
|
|
99
|
+
const slapCheckResult = await this.checkTopicOutputs('slapRecords', 'service')
|
|
100
|
+
slapResults = slapCheckResult.results
|
|
101
|
+
removed += slapCheckResult.removed
|
|
102
|
+
banned += slapCheckResult.banned
|
|
48
103
|
|
|
49
|
-
this.logger.log(chalk.green('
|
|
104
|
+
this.logger.log(chalk.green('Janitor health checks completed'))
|
|
50
105
|
} catch (error) {
|
|
51
|
-
this.logger.error(chalk.red('
|
|
106
|
+
this.logger.error(chalk.red('Error during health checks:'), error)
|
|
52
107
|
throw error
|
|
53
108
|
}
|
|
109
|
+
|
|
110
|
+
const completedAt = new Date()
|
|
111
|
+
const allResults = [...shipResults, ...slapResults]
|
|
112
|
+
return {
|
|
113
|
+
startedAt,
|
|
114
|
+
completedAt,
|
|
115
|
+
durationMs: completedAt.getTime() - startedAt.getTime(),
|
|
116
|
+
shipResults,
|
|
117
|
+
slapResults,
|
|
118
|
+
summary: {
|
|
119
|
+
totalChecked: allResults.length,
|
|
120
|
+
healthy: allResults.filter(r => r.healthy).length,
|
|
121
|
+
unhealthy: allResults.filter(r => !r.healthy).length,
|
|
122
|
+
removed,
|
|
123
|
+
banned
|
|
124
|
+
}
|
|
125
|
+
}
|
|
54
126
|
}
|
|
55
127
|
|
|
56
128
|
/**
|
|
57
|
-
* Checks
|
|
129
|
+
* Checks a single URL's health endpoint. Used by the admin dashboard for on-demand checks.
|
|
58
130
|
*/
|
|
59
|
-
|
|
131
|
+
async checkHost (url: string): Promise<{ healthy: boolean, responseTimeMs: number, statusCode?: number, error?: string }> {
|
|
132
|
+
const startTime = Date.now()
|
|
133
|
+
|
|
134
|
+
if (!this.isValidDomain(url)) {
|
|
135
|
+
return {
|
|
136
|
+
healthy: false,
|
|
137
|
+
responseTimeMs: Date.now() - startTime,
|
|
138
|
+
error: 'Invalid domain'
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
const fullURL = url.startsWith('http') ? url : `https://${url}`
|
|
144
|
+
const healthURL = new URL('/health', fullURL).toString()
|
|
145
|
+
|
|
146
|
+
const controller = new AbortController()
|
|
147
|
+
const timeout = setTimeout(() => controller.abort(), this.requestTimeoutMs)
|
|
148
|
+
|
|
149
|
+
try {
|
|
150
|
+
const response = await fetch(healthURL, {
|
|
151
|
+
method: 'GET',
|
|
152
|
+
signal: controller.signal,
|
|
153
|
+
headers: { Accept: 'application/json' }
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
clearTimeout(timeout)
|
|
157
|
+
const responseTimeMs = Date.now() - startTime
|
|
158
|
+
|
|
159
|
+
if (!response.ok) {
|
|
160
|
+
return { healthy: false, responseTimeMs, statusCode: response.status, error: `HTTP ${response.status}` }
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const data = await response.json()
|
|
164
|
+
const healthy = data?.status === 'ok'
|
|
165
|
+
return { healthy, responseTimeMs, statusCode: response.status, error: healthy ? undefined : 'Unexpected response' }
|
|
166
|
+
} catch (error: any) {
|
|
167
|
+
clearTimeout(timeout)
|
|
168
|
+
const responseTimeMs = Date.now() - startTime
|
|
169
|
+
if (error.name === 'AbortError') {
|
|
170
|
+
return { healthy: false, responseTimeMs, error: 'Timeout' }
|
|
171
|
+
}
|
|
172
|
+
return { healthy: false, responseTimeMs, error: error.message ?? 'Connection failed' }
|
|
173
|
+
}
|
|
174
|
+
} catch (error: any) {
|
|
175
|
+
return { healthy: false, responseTimeMs: Date.now() - startTime, error: error.message ?? 'Invalid URL' }
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Gets health status for all records without modifying them.
|
|
181
|
+
* Used by the dashboard to display current state.
|
|
182
|
+
*/
|
|
183
|
+
async getHealthStatus (): Promise<{ ship: HostHealthResult[], slap: HostHealthResult[] }> {
|
|
184
|
+
const shipCollection = this.mongoDb.collection('shipRecords')
|
|
185
|
+
const slapCollection = this.mongoDb.collection('slapRecords')
|
|
186
|
+
|
|
187
|
+
const [shipOutputs, slapOutputs] = await Promise.all([
|
|
188
|
+
shipCollection.find({}).toArray(),
|
|
189
|
+
slapCollection.find({}).toArray()
|
|
190
|
+
])
|
|
191
|
+
|
|
192
|
+
const shipResults: HostHealthResult[] = shipOutputs.map(output => ({
|
|
193
|
+
txid: output.txid as string,
|
|
194
|
+
outputIndex: output.outputIndex as number,
|
|
195
|
+
domain: this.extractURLFromOutput(output) ?? 'unknown',
|
|
196
|
+
topic: output.topic as string,
|
|
197
|
+
identityKey: output.identityKey as string,
|
|
198
|
+
createdAt: output.createdAt as Date,
|
|
199
|
+
healthy: true, // Will be updated if health check is run
|
|
200
|
+
downCount: typeof output.down === 'number' ? output.down : 0
|
|
201
|
+
}))
|
|
202
|
+
|
|
203
|
+
const slapResults: HostHealthResult[] = slapOutputs.map(output => ({
|
|
204
|
+
txid: output.txid as string,
|
|
205
|
+
outputIndex: output.outputIndex as number,
|
|
206
|
+
domain: this.extractURLFromOutput(output) ?? 'unknown',
|
|
207
|
+
service: output.service as string,
|
|
208
|
+
identityKey: output.identityKey as string,
|
|
209
|
+
createdAt: output.createdAt as Date,
|
|
210
|
+
healthy: true,
|
|
211
|
+
downCount: typeof output.down === 'number' ? output.down : 0
|
|
212
|
+
}))
|
|
213
|
+
|
|
214
|
+
return { ship: shipResults, slap: slapResults }
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Checks all outputs for a specific collection and returns results.
|
|
219
|
+
*/
|
|
220
|
+
private async checkTopicOutputs (
|
|
221
|
+
collectionName: string,
|
|
222
|
+
typeField: 'topic' | 'service'
|
|
223
|
+
): Promise<{ results: HostHealthResult[], removed: number, banned: number }> {
|
|
224
|
+
const results: HostHealthResult[] = []
|
|
225
|
+
let removed = 0
|
|
226
|
+
let banned = 0
|
|
227
|
+
|
|
60
228
|
try {
|
|
61
229
|
const collection = this.mongoDb.collection(collectionName)
|
|
62
230
|
const outputs = await collection.find({}).toArray()
|
|
63
231
|
|
|
64
|
-
this.logger.log(chalk.cyan(
|
|
232
|
+
this.logger.log(chalk.cyan(`Checking ${outputs.length} ${collectionName} outputs...`))
|
|
65
233
|
|
|
66
234
|
for (const output of outputs) {
|
|
67
|
-
await this.checkOutput(output, collection)
|
|
235
|
+
const result = await this.checkOutput(output, collection, typeField)
|
|
236
|
+
results.push(result)
|
|
237
|
+
if (result.error === 'REMOVED') {
|
|
238
|
+
removed++
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Count auto-bans that happened during this run
|
|
243
|
+
if (this.banService !== undefined && this.autoBanOnRemoval) {
|
|
244
|
+
banned = removed // Each removal triggers a ban
|
|
68
245
|
}
|
|
69
246
|
} catch (error) {
|
|
70
|
-
this.logger.error(chalk.red(
|
|
247
|
+
this.logger.error(chalk.red(`Error checking ${collectionName} outputs:`), error)
|
|
71
248
|
}
|
|
249
|
+
|
|
250
|
+
return { results, removed, banned }
|
|
72
251
|
}
|
|
73
252
|
|
|
74
253
|
/**
|
|
75
|
-
* Checks a single output for health
|
|
254
|
+
* Checks a single output for health and returns the result.
|
|
76
255
|
*/
|
|
77
|
-
private async checkOutput (
|
|
256
|
+
private async checkOutput (
|
|
257
|
+
output: Record<string, any>,
|
|
258
|
+
collection: any,
|
|
259
|
+
typeField: 'topic' | 'service'
|
|
260
|
+
): Promise<HostHealthResult> {
|
|
261
|
+
const domain = this.extractURLFromOutput(output) ?? 'unknown'
|
|
262
|
+
const baseResult: HostHealthResult = {
|
|
263
|
+
txid: output.txid as string,
|
|
264
|
+
outputIndex: output.outputIndex as number,
|
|
265
|
+
domain,
|
|
266
|
+
identityKey: output.identityKey as string,
|
|
267
|
+
createdAt: output.createdAt as Date,
|
|
268
|
+
healthy: false,
|
|
269
|
+
downCount: typeof output.down === 'number' ? output.down : 0
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (typeField === 'topic') {
|
|
273
|
+
baseResult.topic = output.topic as string
|
|
274
|
+
} else {
|
|
275
|
+
baseResult.service = output.service as string
|
|
276
|
+
}
|
|
277
|
+
|
|
78
278
|
try {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return
|
|
279
|
+
if (domain === 'unknown') {
|
|
280
|
+
baseResult.error = 'No URL found'
|
|
281
|
+
await this.handleUnhealthyOutput(output, collection, domain)
|
|
282
|
+
return baseResult
|
|
83
283
|
}
|
|
84
284
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
this.
|
|
88
|
-
|
|
89
|
-
return
|
|
285
|
+
if (!this.isValidDomain(domain)) {
|
|
286
|
+
baseResult.error = 'Invalid domain'
|
|
287
|
+
await this.handleUnhealthyOutput(output, collection, domain)
|
|
288
|
+
return baseResult
|
|
90
289
|
}
|
|
91
290
|
|
|
92
|
-
|
|
93
|
-
|
|
291
|
+
const healthResult = await this.checkHost(domain)
|
|
292
|
+
baseResult.healthy = healthResult.healthy
|
|
293
|
+
baseResult.responseTimeMs = healthResult.responseTimeMs
|
|
294
|
+
baseResult.statusCode = healthResult.statusCode
|
|
295
|
+
baseResult.error = healthResult.error
|
|
94
296
|
|
|
95
|
-
if (
|
|
297
|
+
if (healthResult.healthy) {
|
|
96
298
|
await this.handleHealthyOutput(output, collection)
|
|
299
|
+
baseResult.downCount = Math.max(0, baseResult.downCount - 1)
|
|
97
300
|
} else {
|
|
98
|
-
await this.handleUnhealthyOutput(output, collection)
|
|
301
|
+
const wasRemoved = await this.handleUnhealthyOutput(output, collection, domain)
|
|
302
|
+
baseResult.downCount++
|
|
303
|
+
if (wasRemoved) {
|
|
304
|
+
baseResult.error = 'REMOVED'
|
|
305
|
+
}
|
|
99
306
|
}
|
|
100
|
-
} catch (error) {
|
|
101
|
-
|
|
102
|
-
await this.handleUnhealthyOutput(output, collection).catch(() => {})
|
|
307
|
+
} catch (error: any) {
|
|
308
|
+
baseResult.error = error.message ?? 'Check failed'
|
|
309
|
+
await this.handleUnhealthyOutput(output, collection, domain).catch(() => {})
|
|
103
310
|
}
|
|
311
|
+
|
|
312
|
+
return baseResult
|
|
104
313
|
}
|
|
105
314
|
|
|
106
315
|
/**
|
|
@@ -108,8 +317,6 @@ export class JanitorService {
|
|
|
108
317
|
*/
|
|
109
318
|
private extractURLFromOutput (output: Record<string, any>): string | null {
|
|
110
319
|
try {
|
|
111
|
-
// SHIP and SLAP outputs typically have a domain field or URL field
|
|
112
|
-
// Check for common field names
|
|
113
320
|
if (typeof output.domain === 'string') {
|
|
114
321
|
return output.domain
|
|
115
322
|
}
|
|
@@ -119,7 +326,6 @@ export class JanitorService {
|
|
|
119
326
|
if (typeof output.serviceURL === 'string') {
|
|
120
327
|
return output.serviceURL
|
|
121
328
|
}
|
|
122
|
-
// SLAP outputs may have a protocols array with URLs
|
|
123
329
|
if (Array.isArray(output.protocols) && output.protocols.length > 0) {
|
|
124
330
|
const httpsProtocol = output.protocols.find((p: any) =>
|
|
125
331
|
typeof p === 'string' && p.startsWith('https://')
|
|
@@ -139,109 +345,79 @@ export class JanitorService {
|
|
|
139
345
|
*/
|
|
140
346
|
private isValidDomain (url: string): boolean {
|
|
141
347
|
try {
|
|
142
|
-
// Parse the URL
|
|
143
348
|
const parsedURL = new URL(url.startsWith('http') ? url : `https://${url}`)
|
|
144
|
-
|
|
145
|
-
// Check if hostname is valid
|
|
146
349
|
const hostname = parsedURL.hostname
|
|
147
|
-
|
|
148
|
-
// Basic domain validation
|
|
149
350
|
const domainRegex = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$/i
|
|
150
351
|
const localhostRegex = /^localhost$/i
|
|
151
352
|
const ipv4Regex = /^(\d{1,3}\.){3}\d{1,3}$/
|
|
152
|
-
|
|
153
353
|
return domainRegex.test(hostname) || localhostRegex.test(hostname) || ipv4Regex.test(hostname)
|
|
154
354
|
} catch {
|
|
155
355
|
return false
|
|
156
356
|
}
|
|
157
357
|
}
|
|
158
358
|
|
|
159
|
-
/**
|
|
160
|
-
* Checks the /health endpoint of a service
|
|
161
|
-
*/
|
|
162
|
-
private async checkHealthEndpoint (url: string): Promise<boolean> {
|
|
163
|
-
try {
|
|
164
|
-
// Ensure URL has protocol
|
|
165
|
-
const fullURL = url.startsWith('http') ? url : `https://${url}`
|
|
166
|
-
const healthURL = new URL('/health', fullURL).toString()
|
|
167
|
-
|
|
168
|
-
// Create AbortController for timeout
|
|
169
|
-
const controller = new AbortController()
|
|
170
|
-
const timeout = setTimeout(() => controller.abort(), this.requestTimeoutMs)
|
|
171
|
-
|
|
172
|
-
try {
|
|
173
|
-
const response = await fetch(healthURL, {
|
|
174
|
-
method: 'GET',
|
|
175
|
-
signal: controller.signal,
|
|
176
|
-
headers: {
|
|
177
|
-
Accept: 'application/json'
|
|
178
|
-
}
|
|
179
|
-
})
|
|
180
|
-
|
|
181
|
-
clearTimeout(timeout)
|
|
182
|
-
|
|
183
|
-
if (!response.ok) {
|
|
184
|
-
return false
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
const data = await response.json()
|
|
188
|
-
return data?.status === 'ok'
|
|
189
|
-
} catch (error: any) {
|
|
190
|
-
clearTimeout(timeout)
|
|
191
|
-
if (error.name === 'AbortError') {
|
|
192
|
-
this.logger.log(chalk.yellow(`⏱️ Health check timeout for ${healthURL}`))
|
|
193
|
-
}
|
|
194
|
-
return false
|
|
195
|
-
}
|
|
196
|
-
} catch (error) {
|
|
197
|
-
return false
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
359
|
/**
|
|
202
360
|
* Handles a healthy output by decrementing its down counter
|
|
203
361
|
*/
|
|
204
362
|
private async handleHealthyOutput (output: Record<string, any>, collection: any): Promise<void> {
|
|
205
363
|
try {
|
|
206
364
|
const currentDown = typeof output.down === 'number' ? output.down : 0
|
|
207
|
-
|
|
208
365
|
if (currentDown > 0) {
|
|
209
|
-
// Decrement the down counter
|
|
210
366
|
await collection.updateOne(
|
|
211
367
|
{ _id: output._id },
|
|
212
368
|
{ $inc: { down: -1 } }
|
|
213
369
|
)
|
|
214
|
-
this.logger.log(chalk.green(`✅ Output ${String(output.txid)}:${String(output.outputIndex)} is healthy (down: ${currentDown} -> ${currentDown - 1})`))
|
|
215
370
|
}
|
|
216
371
|
} catch (error) {
|
|
217
|
-
this.logger.error(chalk.red(
|
|
372
|
+
this.logger.error(chalk.red(`Error handling healthy output ${String(output.txid)}:${String(output.outputIndex)}:`), error)
|
|
218
373
|
}
|
|
219
374
|
}
|
|
220
375
|
|
|
221
376
|
/**
|
|
222
|
-
* Handles an unhealthy output by incrementing its down counter
|
|
377
|
+
* Handles an unhealthy output by incrementing its down counter.
|
|
378
|
+
* If the threshold is reached, deletes the record and optionally bans the domain.
|
|
379
|
+
* Returns true if the record was removed.
|
|
223
380
|
*/
|
|
224
|
-
private async handleUnhealthyOutput (output: Record<string, any>, collection: any): Promise<
|
|
381
|
+
private async handleUnhealthyOutput (output: Record<string, any>, collection: any, domain?: string): Promise<boolean> {
|
|
225
382
|
try {
|
|
226
383
|
const currentDown = typeof output.down === 'number' ? output.down : 0
|
|
227
384
|
const newDown = currentDown + 1
|
|
228
385
|
|
|
229
|
-
this.logger.log(chalk.yellow(`⚠️ Output ${String(output.txid)}:${String(output.outputIndex)} is unhealthy (down: ${currentDown} -> ${newDown})`))
|
|
230
|
-
|
|
231
|
-
// Check if we should delete the record
|
|
232
386
|
if (newDown >= this.hostDownRevokeScore) {
|
|
233
|
-
this.logger.log(chalk.red(
|
|
387
|
+
this.logger.log(chalk.red(`Removing output ${String(output.txid)}:${String(output.outputIndex)} (down: ${newDown} >= ${this.hostDownRevokeScore})`))
|
|
234
388
|
await collection.deleteOne({ _id: output._id })
|
|
235
|
-
|
|
389
|
+
|
|
390
|
+
// Auto-ban the domain and outpoint to prevent GASP re-sync
|
|
391
|
+
if (this.banService !== undefined && this.autoBanOnRemoval) {
|
|
392
|
+
const txid = output.txid as string
|
|
393
|
+
const outputIndex = output.outputIndex as number
|
|
394
|
+
|
|
395
|
+
await this.banService.banOutpoint(
|
|
396
|
+
txid,
|
|
397
|
+
outputIndex,
|
|
398
|
+
`Auto-banned by janitor: host down ${newDown} consecutive checks`,
|
|
399
|
+
domain
|
|
400
|
+
)
|
|
401
|
+
|
|
402
|
+
if (typeof domain === 'string' && domain !== 'unknown') {
|
|
403
|
+
await this.banService.banDomain(
|
|
404
|
+
domain,
|
|
405
|
+
`Auto-banned by janitor: host unresponsive after ${newDown} checks`
|
|
406
|
+
)
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
return true
|
|
236
411
|
} else {
|
|
237
|
-
// Increment the down counter
|
|
238
412
|
await collection.updateOne(
|
|
239
413
|
{ _id: output._id },
|
|
240
414
|
{ $inc: { down: 1 } }
|
|
241
415
|
)
|
|
416
|
+
return false
|
|
242
417
|
}
|
|
243
418
|
} catch (error) {
|
|
244
|
-
this.logger.error(chalk.red(
|
|
419
|
+
this.logger.error(chalk.red(`Error handling unhealthy output ${String(output.txid)}:${String(output.outputIndex)}:`), error)
|
|
420
|
+
return false
|
|
245
421
|
}
|
|
246
422
|
}
|
|
247
423
|
}
|