-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
261 lines (203 loc) · 7.52 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
var util = require('util');
var events = require('events').EventEmitter;
var options = require('./libs/options');
var async = require('async');
var request = require('request');
var fs = require('fs');
var findup = require('findup-sync');
var multimeter = require('multimeter');
var multi = multimeter(process);
var EOL = require('os').EOL;
var package = JSON.parse(fs.readFileSync(findup('package.json')));
var sslscan = function(opts,cb) {
if (false === (this instanceof sslscan)) {
return new sslscan(opts,cb);
}
var self = this;
this.lastMessage = '';
this.paused = false;
this.progress = 0;
this.progressTimer = null;
this.activeAssessments = 0
this.maxAssessments = -1
this.retryCount = 0;
this.command = '';
this.analyseUrl = '';
this.bars = {};
this.cmdLine = true;
this.options = opts;
this.currentEndPointId = -1;
this.previousProgress;
if (!opts.ips) {
// Called from a js script, reparse options
options.parse(opts,function(err,opts) {
self.cmdLine = false;
self.options = opts;
self.init();
events.call(self);
if (cb) cb(self);
});
} else {
this.init();
events.call(this);
}
};
util.inherits(sslscan, events);
sslscan.prototype.shellOutput = function(msg) {
process.stdout.write(msg);
};
sslscan.prototype.getOptions = function() {
return this.options;
};
sslscan.prototype.init = function() {
this.command = "analyze?host=" + this.options.host + "&all=done"
if (this.options.fromCache == 'on') {
this.command+="&fromCache=on"
if (this.options.globalMaxAge != 0) {
this.command+="&maxAge=" + this.options.globalMaxAge;
}
} else if (this.options.startNew == 'on') {
this.command+="&startNew=on"
}
if (this.options.globalIgnoreMismatch == 'on') {
this.command+="&ignoreMismatch=on"
}
this.analyseUrl = this.options.apiLocation+'/'+this.command;
var logLevel = null;
if (this.options.debug) logLevel = 'debug';
this.log = require('./libs/logger').log(logLevel);
};
sslscan.prototype.loopAnalyse = function(cb) {
var self = this;
this.retryCount++;
var reqId = this.retryCount;
if (reqId>1) {
// startNew only for the first call
this.analyseUrl = this.analyseUrl.replace(/&startNew=on/,'');
} else {
self.emit('start');
}
this.log.debug('#'+reqId,"Request ",this.analyseUrl);
var options = {
url: this.analyseUrl,
headers: {
'User-Agent': package.name+' v'+package.version
}
};
request(options,function(err, response, body) {
if (err) {
if (self.retryCount > 5) {
self.lastMessage = "Too many HTTP requests failed";
} else {
self.lastMessage = "HTTP request failed: "+err;
self.retryCount++;
}
self.emit('error',self.lastMessage);
return cb(err);
}
self.log.debug('#'+this.reqId,'Response status: ',response.statusCode);
var message = response.headers['x-message'];
if (message) {
self.log.debug('x-message',response.headers['x-message']);
}
// Adjust maximum concurrent requests.
var headerValue = parseInt(response.headers['x-max-assessments']);
if (headerValue <= 0) {
// https://github.com/ssllabs/ssllabs-scan/issues/56
self.lastMessage = 'Your IP has been blacklisted !';
self.emit('error',self.lastMessage);
return cb(msg);
}
if (headerValue > 1) {
if (self.maxAssessments != headerValue) {
self.maxAssessments = headerValue;
self.log.debug("Server set max concurrent assessments to", headerValue);
}
}
var status = response.statusCode;
if (status == 429) {
//429 - client request rate too high
self.lastMessage = 'Sleeping for 30 seconds after a ',status,' response status';
self.emit('sleep',self.lastMessage);
return setTimeout(function() {
self.loopAnalyse(cb);
},30*1000);
} else if (status == 503) {
// 503 - the service is not available (e.g., down for maintenance)
self.lastMessage = 'SSLLabs service is not available';
self.emit('error',self.lastMessage);
return cb(self.lastMessage);
} else if (status == 529) {
// 529 - the service is overloaded
self.lastMessage = 'SSLLabs service overloaded';
self.emit('error',self.lastMessage);
return cb(self.lastMessage);
} else if (status != 200 && status != 400) {
self.lastMessage = 'Unexpected response status code '+status;
self.emit('error',self.lastMessage);
return cb(self.lastMessage);
}
// Everything fine, let's see if job is done
try {
var result = JSON.parse(response.body);
if (result.status === "READY") {
// assessments are available(s) = job done
return cb(null,result);
}
self.log.debug(JSON.stringify(result,null,4));
if (result.endpoints && result.endpoints.length) {
if (self.currentEndPointId == -1) {
var endpoints = [];
for (var i = 0; i<result.endpoints.length;i++) {
endpoints.push(result.endpoints[i].serverName);
}
self.emit('endpoints',endpoints);
self.currentEndPointId = 0;
} else {
for (var i = 0; i<result.endpoints.length;i++) {
var endpoint = result.endpoints[i];
if (i == self.currentEndPointId && endpoint.statusMessage == 'Ready') {
self.emit('progress',{
message:endpoint.serverName+': 100% done',
endpointId:self.currentEndPointId,
endpoint:endpoint
})
self.currentEndPointId++;
}
}
var currentEndPoint = result.endpoints[self.currentEndPointId];
if (currentEndPoint) {
var p = currentEndPoint.progress;
if (p == -1) p = 0;
self.emit('progress',{
message:currentEndPoint.serverName+': '+p+"% "+currentEndPoint.statusDetailsMessage,
endpointId:self.currentEndPointId,
endpoint:result.endpoints[currentEndPoint]
});
}
}
}
} catch(e) {
// json badly formated
return cb(e);
}
// In progress ...
return setTimeout(function() {
self.loopAnalyse(cb);
},5*1000);
}.bind({reqId:reqId}));
};
sslscan.prototype.run = function() {
this.shellOutput("SSLLabs assessments of "+this.options.host+EOL);
var self = this;
this.init();
this.loopAnalyse(function(err,result) {
if (err) {
self.shellOutput('Error',err);
return;
}
self.emit('end',result);
return;
});
};
module.exports = sslscan;