-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
66 lines (61 loc) · 1.98 KB
/
index.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
const http = require('http');
const config = require('./lib/config');
const log = require('./lib/logger');
const mozilla = require('./lib/mozilla');
const prometheus = require('./lib/prometheus');
const url = require('./lib/url');
/**
* Checks a single url
*
* @param {String} hostname - hostname to check
* @param {Number} port - port to use
* @param {Object} additionalMetadata - additional key-value based metadata
*/
async function updateRouteInfo (hostname, port, additionalMetadata) {
mozilla.triggerScan(hostname, port);
// defer read results
setTimeout(() => mozilla.receiveScanResult(hostname, additionalMetadata), 200);
}
/**
* Checks a list of urls
*
* @param {Array} hostEntries - hostnames to check (may include ports ... e.g. example.com:8443)
* @param {Object} additionalMetadata - additional key-value based metadata
*/
async function updateRoutesInfo (hostEntries, additionalMetadata) {
log.info(`Triggering scan for ${hostEntries}`);
// reset data on route update
const domainList = [];
url.extractHostnamesAndPort(hostEntries).forEach((hostEntry) => {
domainList.push(hostEntry.domain);
});
prometheus.updateHosts(domainList);
url.extractHostnamesAndPort(hostEntries).forEach((hostEntry) => {
mozilla.triggerScan(hostEntry.domain, hostEntry.port);
// defer read results
setTimeout(() => mozilla.receiveScanResult(hostEntry.domain, additionalMetadata), 200);
});
}
// start http server
function startPrometheusListener () {
const server = http.createServer((req, res) => {
switch (req.url) {
case '/':
return res.end(prometheus.renderMetrics());
default:
return res.end('404');
}
});
server.listen(config.port);
log.info(`prometheus-exporter listening at ${config.port}`);
}
module.exports = exports = {
config,
updateRouteInfo,
updateRoutesInfo,
updateHosts: prometheus.updateHosts,
resetRouteInfo: prometheus.reset,
triggerScan: mozilla.triggerScan,
startPrometheusListener,
logger: log
};