forked from imperva/site-protection-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckOriginReached.js
107 lines (95 loc) · 3.27 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
var request = require('request');
var fs = require('fs')
var async = require('async');
var settings = require('./settings.js');
var outputData = [];
var originProtectInfo = [];
var serverNameIndex = 0;
var totalNumServers = 0;
//Getting arguments
function getOriginServerInfo(originData, originDataOutpt, informCaller)
{
console.log("Check " + originData.length/2 + " Origin Servers access (http & https) - this may take a while");
totalNumServers = originData.length;
if(settings.printDebugInfo)
console.time("Check Origin Servers - total time");
async.forEach(originData, function(site, cb){
checkOriginServer(site.subAccountId, site.Name, site.serverName, site.Protocol, originDataOutpt, cb);
}, function(err){
if (err){
//deal with the error
console.log("error in checking sites")
informCaller();
}
if(settings.printDebugInfo)
console.timeEnd("Check Origin Servers - total time")
informCaller();
});
}
function checkOriginServer(subAccountId, siteName, serverNamesList, protocol, origDataOutpt, siteCb)
{
var serverNames = serverNamesList.split(';'); // split string on comma space
async.forEach(serverNames, function(serverName, cb){
serverNameIndex++
checkIfReachable(serverNameIndex, subAccountId, siteName, serverName, protocol, origDataOutpt, cb);
}, function(err){
if (err){
//deal with the error
console.log("error in checking origin servers")
}
siteCb();
});
}
function checkIfReachable(index, subAccountId, siteName, serverName, protocol, origDataOutpt, cb)
{
RequestUrl = protocol + '://' + serverName;
var result;
if(settings.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(settings.printDebugInfo)
console.log("origin server # " + totalNumServers-- + " Server answered: " + serverName)
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, siteName, serverName, protocol, true, err.code, origDataOutpt);
else
{
if (!err.code)
err.code = err.reason;
addOriginInfoToSite(subAccountId, siteName, serverName, protocol, false, err.code, origDataOutpt);
}
}
else
addOriginInfoToSite(subAccountId, siteName, serverName, protocol, false, resp.statusCode, origDataOutpt);
cb();
});
}
function addOriginInfoToSite(subAccountId, siteName, serverName, protocol, isProtected, code, origDataOutpt)
{
var notFound = true;
for (var i=0; i<origDataOutpt.length; i++)
{
if (origDataOutpt[i].domain == siteName)
{
origDataOutpt[i].originServers.push({"serverName": serverName, "protocol": protocol, "isProtected": isProtected, "code": code})
notFound = false;
break;
}
}
if (notFound)
origDataOutpt.push({"subAccountId": subAccountId,"domain": siteName,"originServers":[{"serverName": serverName, "protocol": protocol, "isProtected": isProtected, "code": code}]})
}
module.exports.getOriginServerInfo = getOriginServerInfo;