-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
118 lines (89 loc) · 2.74 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
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
const _ = require('lodash');
const express = require('express');
const prometheusClient = require('prom-client');
const { spawn } = require('child_process');
const metricsServer = express();
var hostname;
const DEBUG = process.env['CIPHERSCAN_EXPORTER_DEBUG'] || false;
const collectDefaultMetrics = prometheusClient.collectDefaultMetrics;
collectDefaultMetrics();
const up = new prometheusClient.Gauge({name: 'up', help: 'UP Status'});
const ciphers = new prometheusClient.Gauge({
name: 'cipherscan_ciphers',
help: 'Number of enabled ciphers',
labelNames: ['protocol', 'cipher', 'priority']
});
if (require.main === module) {
const options = {};
startServer();
}
function init (options) {
hostname = options.hostname;
}
function startServer () {
metricsServer.get('/probe', async (req, res) => {
res.contentType(prometheusClient.register.contentType);
try {
if (!req.query.target) {
throw new Error("hostname must be provided as query param");
}
hostname = req.query.target;
resetStats();
const response = await runCipherscan(hostname);
res.send(prometheusClient.register.metrics());
} catch (error) {
// error connecting
up.set(0);
res.header('X-Error', error.message || error);
res.send(prometheusClient.register.getSingleMetricAsString(up.name));
}
});
console.log('Server listening to 9209, metrics exposed on /metrics endpoint');
metricsServer.listen(9209);
}
function shutdown () {
metricsServer.close();
}
function resetStats () {
up.set(1);
ciphers.reset()
}
async function runCipherscan (hostname) {
const child = await spawn('./cipherscan/cipherscan', ['-j', hostname]);
process.stdin.pipe(child.stdin)
var jsonString = "";
child.stdout.on('data', (data) => {
jsonString += data;
});
const promise = new Promise((resolve, reject) => {
child.on('exit', function (code, signal) {
var response = JSON.parse(jsonString);
if (!response) {
throw new Error('error retrieving response from cipherscan process');
}
if (!response.ciphersuite) {
throw new Error('ciphersuite not found in cipherscan result');
}
_.each(response.ciphersuite, (ciphersuite, priority) => {
const cipher = ciphersuite.cipher;
if (!ciphersuite.protocols) {
throw new Error('protocols not found in ciphersuite object');
}
_.each(ciphersuite.protocols, (protocol) => {
ciphers.set({
protocol: protocol,
cipher: cipher,
priority: (priority + 1)
}, 1);
});
});
resolve();
});
});
return promise;
};
module.exports = {
init: init,
runCipherscan: runCipherscan,
shutdown: shutdown
};