-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
diagnostics-server.js
149 lines (140 loc) · 5.37 KB
/
diagnostics-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var os = require('os')
var http = require('http')
var fs = require('fs')
var pump = require('pump')
var speedometer = require('speedometer')
var CircularAppendFile = require('circular-append-file')
// capture the last 10 minutes of stats
const HISTORY_LIMIT = 60
const HISTORY_INTERVAL = 10e3
const LOG_FILE_PATH = './diagnostics-server.log'
const LOG_SIZE_LIMIT = 1024 /* 1kb */ * 1024 /* 1mb */ * 32 /* 32mb */
var queriesSpeed = speedometer()
var multicastQueriesSpeed = speedometer()
var queriesPS = []
var multicastQueriesPS = []
exports.createServer = function (disc, opts = {}) {
// logging
var logFile = CircularAppendFile(LOG_FILE_PATH, {maxSize: LOG_SIZE_LIMIT})
function track (evt) {
disc.on(evt, (...args) => {
logFile.append(renderLogEntry(evt, (new Date()).toLocaleString(), args))
})
}
track('traffic')
track('secrets-rotated')
track('error')
track('listening')
track('close')
track('peer')
// stats
disc.on('traffic', (type) => {
if (type === 'in:query') {
queriesSpeed(1)
}
if (type === 'in:multicastquery') {
multicastQueriesSpeed(1)
}
})
setInterval(() => {
queriesPS.unshift(queriesSpeed())
if (queriesPS.length > HISTORY_LIMIT) queriesPS.pop()
multicastQueriesPS.unshift(multicastQueriesSpeed())
if (multicastQueriesPS.length > HISTORY_LIMIT) multicastQueriesPS.pop()
}, HISTORY_INTERVAL)
// server
return http.createServer((req, res) => {
// auth
if (opts.password) {
var auth = req.headers.authorization
if (!auth) {
res.writeHead(401, {'WWW-Authenticate': 'Basic realm="Password needed"', 'Content-Type': 'text/plain'})
return res.end('need password')
}
var givenPW = Buffer.from(auth.split(' ')[1], 'base64').toString().split(':')[1]
if (givenPW !== opts.password) {
res.writeHead(401, {'WWW-Authenticate': 'Basic realm="Password needed"', 'Content-Type': 'text/plain'})
return res.end('bad password')
}
}
// serve
if (req.url === '/' || req.url === '/index.html') {
res.writeHead(200, {'Content-Type': 'text/html'})
pump(fs.createReadStream('./diagnostics-server/index.html'), res)
} else if (req.url === '/index.css') {
res.writeHead(200, {'Content-Type': 'text/css'})
pump(fs.createReadStream('./diagnostics-server/index.css'), res)
} else if (req.url === '/index.js') {
res.writeHead(200, {'Content-Type': 'application/javascript'})
pump(fs.createReadStream('./diagnostics-server/index.js'), res)
} else if (req.url === '/state.json') {
res.writeHead(200, {'Content-Type': 'application/json'})
res.end(JSON.stringify({
stats: {
queriesPS,
multicastQueriesPS,
loadavg: os.loadavg(),
topKeys: disc._domainStore.getTopKeyStats()
},
peers: disc.toJSON()
}))
} else if (req.url === '/log.txt') {
res.writeHead(200, {'Content-Type': 'text/plain'})
pump(logFile.createReadStream(), res)
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' })
res.end('Not found')
}
})
}
function renderLogEntry (evt, ts, args) {
switch (evt) {
case 'listening':
return `${ts} Listening\n`
case 'traffic':
let info = args[1]
switch (args[0]) {
case 'in:query':
return `${ts} <- query (from: ${info.peer.host}:${info.peer.port}) ${renderDNSMsg(info.message)}\n`
case 'in:response':
return `${ts} <- response (to: ${info.peer.host}:${info.peer.port}) ${renderDNSMsg(info.message)}\n`
case 'in:multicastquery':
return `${ts} <- multicast query (from: ${info.peer.address}:${info.peer.port}) ${renderDNSMsg(info.message)}\n`
case 'in:multicastresponse':
return `${ts} <- multicast response (from: ${info.peer.address}:${info.peer.port}) ${renderDNSMsg(info.message)}\n`
case 'out:response':
return `${ts} -> response (to: ${info.peer.host}:${info.peer.port}) ${renderDNSMsg(info.message)}\n`
case 'out:multicastresponse':
return `${ts} -> multicast response ${renderDNSMsg(info.message)}\n`
case 'out:query':
return `${ts} -> query (to: ${info.peer.host}:${info.peer.port}) ${renderDNSMsg(info.message)}\n`
case 'out:multicastquery':
return `${ts} -> multicast query ${renderDNSMsg(info.message)}\n`
default:
return `${ts} TODO ${JSON.stringify(args)}\n`
}
case 'peer':
return `${ts} Peer for "${args[0]}" at ${args[1].host}:${args[1].port}\n`
case 'close':
return `${ts} Closed\n`
case 'secrets-rotated':
return `${ts} Secrets rotated\n`
default:
return `${ts} Unknown event: ${JSON.stringify({evt, args})}\n`
}
}
function renderDNSMsg ({id, questions, answers, additionals}) {
function item (prefix) {
return ({type, name}) => {
return `${safen(prefix)}.${safen(type)}:${safen(name)}`
}
}
function list (l, prefix) {
if (!l || !l.length) return ''
return l.map(item(prefix)).join(' ')
}
return ((id) ? `id=${safen(id)} ` : '') + `${list(questions, 'Q')} ${list(answers, 'A')} ${list(additionals, 'ADD')}`
}
function safen (str) {
return ('' + str).replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&').replace(/"/g, '')
}