-
Notifications
You must be signed in to change notification settings - Fork 4
/
checkOriginReached.js
384 lines (326 loc) · 14 KB
/
checkOriginReached.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
var request = require('request');
var fs = require('fs')
var async = require('async');
var settings = require('./settings.js');
var spv = require('./spv.js');
var utils = require("./utils");
var outputData = [];
var originProtectInfo = [];
var serverNameIndex = 0;
var totalNumServers = 0;
var getSubAccountsInfo = settings.getSubAccountsInfo;
var printDebugInfo = settings.printDebugInfo;
//Getting arguments
function getOriginServerInfo(originData, originDataOutpt, informCaller)
{
console.log("Check " + originData.length/settings.originServerPorts.length + " Origin Servers access (http & https) - this may take a while");
totalNumServers = originData.length;
if(printDebugInfo)
console.time("Check Origin Servers - total time");
var numRequests = 0;
//Limit number of parallel requests as there is a limit in the Operating system
async.forEachLimit(originData, settings.originServerReqSize ,function(site, cb){
checkOriginServer(site.subAccountId, site.siteId, site.Name, site.serverName, site.Protocol, site.portNum, originDataOutpt, cb);
//Let user know it is still working
if (numRequests % settings.originServerReqSize == 0)
console.log("Retrieving data...");
numRequests++;
}, function(err){
if (err){
//deal with the error
console.log("error in checking sites")
informCaller();
}
if(printDebugInfo)
console.timeEnd("Check Origin Servers - total time")
informCaller();
});
}
function checkOriginServer(subAccountId, siteId, siteName, serverNamesList, protocol, portNum, origDataOutpt, siteCb)
{
var serverNames = serverNamesList.split(';'); // split string on comma space
async.forEach(serverNames, function(serverName, cb){
serverNameIndex++
checkIfReachable(serverNameIndex, subAccountId, siteId, siteName, serverName, protocol, portNum, origDataOutpt, cb);
}, function(err){
if (err){
//deal with the error
console.log("error in checking origin servers")
}
siteCb();
});
}
function checkIfReachable(index, subAccountId, siteId, siteName, serverName, protocol, portNum, origDataOutpt, cb)
{
RequestUrl = protocol + '://' + serverName + ':' + portNum;
if(printDebugInfo)
console.log(RequestUrl)
var result;
if(printDebugInfo)
console.log("Check if " + serverName + " is reachable");
request({ url: RequestUrl, method: "GET", followRedirect: false, timeout:settings.originServerConnectionTimeout},
function (err, resp, data)
{
var isProtected = false;
if(printDebugInfo)
console.log("origin server # " + totalNumServers-- + " Server answered: " + serverName + " " + protocol + " " + portNum)
if (err)
{
//Protected Only if error code is in list, else it means origin server is reachable
for (var i = 0; i < settings.originServerProtectedCode.length; i++)
{
if (err.code == settings.originServerProtectedCode[i])
{
isProtected = true;
break;
}
}
if (isProtected == true)
{
addOriginInfoToSite(subAccountId, siteId, siteName, serverName, protocol, portNum, true, err.code, origDataOutpt);
}
else
{
if (!err.code)
err.code = err.reason;
addOriginInfoToSite(subAccountId, siteId, siteName, serverName, protocol, portNum, false, err.code, origDataOutpt);
}
}
else //No error, checking if http code is also considered "protected"
{
for (var i = 0; i < settings.originServerHttpProtectedCode.length; i++)
{
if (resp.statusCode == settings.originServerHttpProtectedCode[i])
{
isProtected = true;
break;
}
}
addOriginInfoToSite(subAccountId, siteId, siteName, serverName, protocol, portNum, isProtected, resp.statusCode, origDataOutpt);
}
cb();
});
}
function addOriginInfoToSite(subAccountId, siteId, siteName, serverName, protocol, portNum, isProtected, code, origDataOutpt)
{
var notFound = true;
for (var i=0; i<origDataOutpt.length; i++)
{
if (origDataOutpt[i].siteId == siteId)
{
origDataOutpt[i].originServers.push({"serverName": serverName, "protocol": protocol, "portNum": portNum, "isProtected": isProtected, "code": code})
notFound = false;
break;
}
}
if (notFound)
origDataOutpt.push({"isFullySorted": false, "isSorted": false, "subAccountId": subAccountId, "siteId": siteId, "domain": siteName,
"originServers":[{"serverName": serverName, "protocol": protocol, "portNum": portNum, "isProtected": isProtected, "code": code}]})
}
//Colored html status
var htmlYesStr = '<td align="left" style="background-color:green;color:white;">Yes</td>';
var htmlNoStr = '<td align="left"style="background-color:red;color:white;">No</td>';
if (settings.useLegacyDisplay)
{
htmlYesStr = '<td align="left" style="color:green">Yes</td>';
htmlNoStr = '<td align="left" style="color:red">No</td>';
}
function buildOriginServersReport(domain, sitesOriginServersInfo, siteSummaryObject)
{
var originServersOutput = '\n<h3>Origin Servers</h3>\n'
var origServers = null;
for (var i = 0; i < sitesOriginServersInfo.originServers.length; i++)
{
if (sitesOriginServersInfo.originServers[i].domain == domain)
{
sitesOriginServersInfo.originServers[i] = sortOriginServers(sitesOriginServersInfo.originServers[i]);
origServers = sitesOriginServersInfo.originServers[i].originServers;
break;
}
}
// Origin servers for this domain were found, create report
if (origServers)
{
originServersOutput += '<table border="1">';
originServersOutput += '<tr><th align="left">Origin Server</th> <th align="left">Protocol</th><th align="left">Is Protected <small>(No direct access)</small></th><th align="left">Code</th> </tr>\n';
for (var i = 0; i < origServers.length; i++)
{
if (origServers[i].isProtected == true)
originServersOutput += '<tr><td align="left">' + origServers[i].serverName + '</td>' +
'<td align="left">' + origServers[i].protocol + ' (' + origServers[i].portNum + ')</td>' +
htmlYesStr +
'<td align="left">' + origServers[i].code + '</td></tr>\n';
else
{
originServersOutput += '<tr><td align="left">' + origServers[i].serverName + '</td>' +
'<td align="left">' + origServers[i].protocol + ' (' + origServers[i].portNum + ')</td>' +
htmlNoStr +
'<td align="left">' + origServers[i].code + '</td></tr>\n';
//Flag this origin server as accessible
spv.setSecurityIssue(domain, "originServer", origServers[i].serverName, "Accessible")
setOrigServerNotProtectedInHtmlSummaryTable(domain, siteSummaryObject);
}
}
originServersOutput += '</table>';
}
return (originServersOutput);
}
function setOrigServerNotProtectedInHtmlSummaryTable(domain, siteSummaryObject)
{
for (var i=0; i < siteSummaryObject.length; i++)
{
if (siteSummaryObject[i].site == domain)
{
siteSummaryObject[i].origServerProtected = "N";
break;
}
}
}
function buildOriginServerSummary(sitesOriginServersInfo, mainAccountInfo, subAccountsOutput)
{
var originServersOutput = '\n<h2><a name="OriginServers">Origin Servers</a></h2>\n'
var originServerStatusStr;
var domainStr = '';
var curAccountName = '';
originServersOutput += '<table border="1">\n<tr>';
//If account info is added, data should also be sorted differently.
if (getSubAccountsInfo)
originServersOutput += '<th align="left">Account</th>';
//Sort
sitesOriginServersInfo = sortSiteOrigServers(sitesOriginServersInfo, mainAccountInfo, subAccountsOutput);
originServersOutput += '<th align="left">Site</th><th align="left">Origin Server</th><th align="left">Protocol</th><th align="left">Is Protected <small>(No direct access)</small></th><th align="left">Reason</th> </tr>\n';
for (var i = 0; i < sitesOriginServersInfo.originServers.length; i++)
{
domainStr = '';
if (getSubAccountsInfo)
{
if (curAccountName != sitesOriginServersInfo.originServers[i].accountName)
{
domainStr += '<td align="left">' + sitesOriginServersInfo.originServers[i].accountName + '</td>';
curAccountName = sitesOriginServersInfo.originServers[i].accountName;
}
else
domainStr += '<td align="left"></td>';
}
domainStr += '<td align="left">' + sitesOriginServersInfo.originServers[i].domain + '</td>';
//Sort results by serverName since responses arrive async
sitesOriginServersInfo.originServers[i] = sortOriginServers(sitesOriginServersInfo.originServers[i]);
for (var j = 0; j < sitesOriginServersInfo.originServers[i].originServers.length; j++)
{
//In order to write site only once per all origin servers
if (j != 0)
{
domainStr = '<td align="left"></td>';
if (getSubAccountsInfo)
domainStr += '<td align="left"></td>';
}
if (sitesOriginServersInfo.originServers[i].originServers[j].isProtected == true)
originServerStatusStr = htmlYesStr;
else
originServerStatusStr = htmlNoStr;
originServersOutput += '<tr>' + domainStr + '<td align="left">' + sitesOriginServersInfo.originServers[i].originServers[j].serverName + '</td>' +
'<td align="left">' + sitesOriginServersInfo.originServers[i].originServers[j].protocol + ' (' + sitesOriginServersInfo.originServers[i].originServers[j].portNum + ')</td>' +
originServerStatusStr + '<td align="left">' + sitesOriginServersInfo.originServers[i].originServers[j].code + '</td></tr>\n';
}
}
originServersOutput += '</table>\n';
return (originServersOutput);
}
function createOriginServerCsv(fileName, sitesOriginServersInfo, mainAccountInfo, subAccountsOutput )
{
var csvFileOutput = 'Index,';
var index = 1;
var originServerCsvPrefix = settings.originServerFileNamePrefix;
if (originServerCsvPrefix == "")
originServerCsvPrefix = 'Origin-servers'
if (getSubAccountsInfo)
csvFileOutput += 'Account,';
//Sort
sitesOriginServersInfo = sortSiteOrigServers(sitesOriginServersInfo, mainAccountInfo, subAccountsOutput);
csvFileOutput += 'Site,Origin Server,Protocol, Port, Is Protected, Reason, Account ID, Site ID\r\n';
for (var i = 0; i < sitesOriginServersInfo.originServers.length; i++)
{
sitesOriginServersInfo.originServers[i] = sortOriginServers(sitesOriginServersInfo.originServers[i]);
for (var j = 0; j < sitesOriginServersInfo.originServers[i].originServers.length; j++)
{
csvFileOutput += index + ',';
index++;
if(getSubAccountsInfo)
{
csvFileOutput += sitesOriginServersInfo.originServers[i].accountName + ',';
}
csvFileOutput += sitesOriginServersInfo.originServers[i].domain + ',' +
sitesOriginServersInfo.originServers[i].originServers[j].serverName + ',' + sitesOriginServersInfo.originServers[i].originServers[j].protocol + ',' +
sitesOriginServersInfo.originServers[i].originServers[j].portNum + ',';
//In order to write site only once per all origin servers
if (sitesOriginServersInfo.originServers[i].originServers[j].isProtected == true)
csvFileOutput += 'Y' + ',';
else
csvFileOutput += 'N' + ',';
csvFileOutput += sitesOriginServersInfo.originServers[i].originServers[j].code + "," +
sitesOriginServersInfo.originServers[i].subAccountId + "," + sitesOriginServersInfo.originServers[i].siteId + '\r\n';
}
}
utils.saveToFile(originServerCsvPrefix + ' ' + fileName + '.csv', csvFileOutput);
}
/*** Sort */
function sortOriginServers(siteOriginServers)
{
//If already sorted, no need to do this again
if (siteOriginServers.isSorted)
return (siteOriginServers);
siteOriginServers.isSorted = true;
siteOriginServers.originServers.sort(function (a,b) {
if((a.serverName < b.serverName) ||
((a.serverName == b.serverName) && (a.protocol < b.protocol)))
return (-1);
if((a.serverName > b.serverName) ||
((a.serverName == b.serverName) && (a.protocol > b.protocol)))
return (1);
});
return (siteOriginServers);
}
function sortSiteOrigServers(siteOrigServers, mainAccountInfo, subAccountsOutput)
{
//Check if full array was already sorted, it it was, get out. If not, set flag to true on all elements so sort won't be performed any more.
if (siteOrigServers.originServers[0].isFullySorted == true)
return (siteOrigServers);
for (var i; i < siteOrigServers.originServers.length; i++)
siteOrigServers.originServers[i].isFullySorted = true;
//If account info is added, data should also be sorted differently.
if (getSubAccountsInfo)
{
// Enrich data with account name
enrichOriginServerInfoWithSubAccount(siteOrigServers, mainAccountInfo, subAccountsOutput);
siteOrigServers.originServers.sort(function (a,b) {
if ((a.sortByAccountName < b.sortByAccountName) ||
((a.sortByAccountName == b.sortByAccountName) && (a.domain < b.domain)))
return (-1);
if ((a.sortByAccountName > b.sortByAccountName) ||
((a.sortByAccountName == b.sortByAccountName) && (a.domain > b.domain)))
return (1);
});
}
else
{
siteOrigServers.originServers.sort(function (a,b) {
if ((a.domain < b.domain) ||
((a.domain == b.domain) && (a.serverName < b.serverName)) ||
((a.domain == b.domain) && (a.serverName == b.serverName) && (a.protocol < b.protocol)))
return (-1);
if ((a.domain > b.domain) ||
((a.domain == b.domain) && (a.serverName > b.serverName)) ||
((a.domain == b.domain) && (a.serverName == b.serverName) && (a.protocol > b.protocol)))
return (1);
});
}
return (siteOrigServers);
}
function enrichOriginServerInfoWithSubAccount(sitesOriginServersInfo, mainAccountInfo, subAccountsOutput)
{
spv.enrichArrayElementsWithSubAccount(sitesOriginServersInfo.originServers, mainAccountInfo, subAccountsOutput);
}
module.exports.getOriginServerInfo = getOriginServerInfo;
module.exports.buildOriginServersReport = buildOriginServersReport;
module.exports.buildOriginServerSummary = buildOriginServerSummary;
module.exports.createOriginServerCsv = createOriginServerCsv;