forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
82 lines (76 loc) · 1.92 KB
/
index.js
File metadata and controls
82 lines (76 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { config } from 'dotenv-safe'
import fs from 'fs'
import { connect } from 'nats'
import { differenceInTime } from './differenceInTime.js'
config()
const { NODE_ENV, SUBSCRIBE_TO: subject, NATS_SERVERS } = process.env
const events = []
let count = 0
let first, last
const exit = () => {
const elapsedTime = differenceInTime(first, last)
if (first && last) {
console.log({ elapsedTime })
if (NODE_ENV !== 'production') {
console.log(`Writing the ${count} events of this run to events.json`)
fs.writeFileSync(
'events.json',
JSON.stringify(events, null, 2),
'utf8',
console.log,
)
}
}
process.exit(0)
}
process.on('SIGTERM', exit)
process.on('SIGINT', exit)
;(async () => {
const nc = await connect({ servers: NATS_SERVERS.split(',') })
const sub = nc.subscribe(subject)
for await (const m of sub) {
const now = Date.now()
if (!first) first = now
last = now
count++
let service
const subject = m.subject
switch (subject) {
case subject.match(/https.processed/)?.input:
service = 'https-processor'
break
case subject.match(/dns.processed/)?.input:
service = 'dns-processor'
break
case subject.match(/tls.processed/)?.input: {
service = 'tls-processor'
break
}
case subject.match(/https$/)?.input: {
service = 'https-scanner'
break
}
case subject.match(/tls$/)?.input: {
service = 'tls-scanner'
break
}
case subject.match(/dns$/)?.input: {
service = 'dns-scanner'
break
}
default: {
service = 'domain-dispatcher'
break
}
}
const [_, domain] = subject.split('.')
events.push({
id: count,
subject: m.subject,
service,
domain,
time: Math.floor(now / 1000),
})
console.log({ count, subject: m.subject, time: now })
}
})()