UNPKG

7.92 kBJavaScriptView Raw
1/*
2This code is held in a joint copyright between Nate Watson and Oluwafunmiwo Juda Sholola, 2015. It is released under the MIT license.
3*/
4
5var os = require("os");
6var dns = require("native-dns");
7var http = require("http");
8var https = require("https");
9var ping = require("ping");
10var diagnostics = {};
11var testURL = "google.com";
12diagnostics.getTestURL = function(){ return testURL; };
13diagnostics.setTestURL = function(replacement) {
14 testURL = replacement;
15 return;
16};
17diagnostics.checkInterfaces = function() {
18 var errors = [];
19 var outside = false;
20 var v4 = false;
21 var v6 = false;
22 var interfaces = os.networkInterfaces();
23 for (var interfaceSet in interfaces) {
24 for (var interface in interfaces[interfaceSet]) {
25 if (interfaces[interfaceSet][interface].internal == false) {
26 outside = true;
27 if (interfaces[interfaceSet][interface].family == "IPv4") {
28 v4 = true;
29 }
30 if (interfaces[interfaceSet][interface].family == "IPv6") {
31 v6 = true;
32 }
33 }
34 }
35 }
36 if (outside != true) {
37 errors[errors.length] = 1;
38 }
39 if (v4 != true) {
40 errors[errors.length] = 4;
41 }
42 if (v6 != true) {
43 errors[errors.length] = 6;
44 }
45 return errors;
46};
47diagnostics.haveIPv4 = function(){
48 var errors = diagnostics.checkInterfaces();
49 if(errors.indexOf(4) > -1) {
50 return false;
51 }
52 return true;
53};
54diagnostics.haveIPv6 = function(){
55 var errors = diagnostics.checkInterfaces();
56 if(errors.indexOf(6) > -1) {
57 return false;
58 }
59 return true;
60};
61diagnostics.haveConnection = function(){
62 var errors = diagnostics.checkInterfaces();
63 if(errors.indexOf(1) > -1) {
64 return false;
65 }
66 return true;
67};
68diagnostics.haveConnectionAsync = function(callback) {
69 callback((diagnostics.haveIPv4() == true) || (diagnostics.haveIPv6() == true));
70 return;
71};
72diagnostics.haveIPv4Async = function(callback) {
73 callback((diagnostics.haveIPv4() == true));
74 return;
75};
76diagnostics.haveIPv6Async = function(callback) {
77 callback((diagnostics.haveIPv6() == true));
78 return;
79};
80diagnostics.haveDNS = function(callback) {
81 var checkIPv4DNS = function(){
82 var DNSanswer = "";
83 var dns4Connection = dns.Request({
84 question: dns.Question({
85 name: testURL
86 }),
87 server: {
88 address: '8.8.8.8'
89 }
90 });
91 var deactivate4 = false;
92 dns4Connection.on("timeout", function(){
93 if (!deactivate4) {
94 deactivate4 = true;
95 callback(false);
96 return;
97 }
98 });
99 dns4Connection.on("message", function(DNSerr, section){
100 if (DNSerr) {
101 if (!deactivate4) {
102 deactivate4 = true;
103 callback(false);
104 }
105 return;
106 }
107 DNSanswer += section;
108 });
109 dns4Connection.on("end", function(){
110 if (!deactivate4) {
111 deactivate4 = true;
112 callback(true);
113 }
114 });
115 dns4Connection.send();
116 };
117 var DNSsegments = "";
118 var deactivate6 = false;
119 var dnsConnection = dns.Request({
120 question: dns.Question({
121 name: testURL
122 }),
123 server: {
124 address: '2001:4860:4860::8888'
125 }
126 });
127 dnsConnection.on("timeout", function(){
128 if (!deactivate6) {
129 deactivate6 = true;
130 checkIPv4DNS();
131 }
132 });
133 dnsConnection.on("message", function(DNSerr, section){
134 if (DNSerr) {
135 if (!deactivate6) {
136 deactivate6 = true;
137 checkIPv4DNS();
138 }
139 return;
140 }
141 DNSsegments += section;
142 });
143 dnsConnection.on("end", function(){
144 if (!deactivate6) {
145 deactivate6 = true;
146 callback(true);
147 }
148 return;
149 });
150 dnsConnection.send();
151 return;
152};
153diagnostics.haveHTTP = function(callback) {
154 var isFired = false;
155 var requestOptions = {
156 hostname: testURL,
157 port: 80,
158 method: "GET"
159 };
160 var httpRequest = http.request(requestOptions, function(response){
161 var responseTotal = "";
162 response.on("data", function(section){
163 responseTotal += section;
164 });
165 response.on("end", function(){
166 if (isFired == false) {
167 isFired = true;
168 callback(true);
169 }
170 });
171 response.on("error", function(){
172 if (isFired == false) {
173 isFired = true;
174 callback(false);
175 }
176 });
177 });
178 httpRequest.on("socket", function (socket){
179 socket.setTimeout(10000);
180 socket.on("timeout", function(){
181 if (isFired == false) {
182 isFired = true;
183 callback(false);
184 }
185 });
186 });
187 httpRequest.on("error", function(failure) {
188 if (isFired == false) {
189 isFired = true;
190 callback(false);
191 }
192 });
193 httpRequest.end();
194 return;
195};
196diagnostics.haveHTTPS = function(callback) {
197 var isFired = false;
198 var requestOptions = {
199 hostname: testURL,
200 port: 443,
201 method: "GET"
202 };
203 var httpRequest = https.request(requestOptions, function(response){
204 var responseTotal = "";
205 response.on("data", function(section){
206 responseTotal += section;
207 });
208 response.on("end", function(){
209 if (isFired == false) {
210 isFired = true;
211 callback(true);
212 }
213 });
214 response.on("error", function(){
215 if (isFired == false) {
216 isFired = true;
217 callback(false);
218 }
219 });
220 });
221 httpRequest.on("socket", function (socket){
222 socket.setTimeout(10000);
223 socket.on("timeout", function(){
224 if (isFired == false) {
225 isFired = true;
226 callback(false);
227 }
228 });
229 });
230 httpRequest.on("error", function(failure) {
231 if (isFired == false) {
232 isFired = true;
233 callback(false);
234 }
235 });
236 httpRequest.end();
237 return;
238
239};
240diagnostics.havePing = function(callback) {
241 ping.sys.probe(testURL, function(reached) {
242 callback(reached);
243 });
244};
245diagnostics.getError = function(id) {
246 switch(id) {
247 case 0: return "NormalNetworkActivity";
248 case 1: return "NoConnection";
249 case 4: return "NoIPv4Connection";
250 case 6: return "NoIPv6Connection";
251 case 7: return "DiagnosticsScriptFailure";
252 case 8: return "PingNotUsable";
253 case 53: return "NoDNS";
254 case 80: return "NoHTTPconnection";
255 case 110: return "NoInsecurePOP3";
256 case 123: return "NoNTP";
257 case 143: return "NoInsecureIMAP";
258 case 443: return "NoHTTPSconnection";
259 case 993: return "NoSecureIMAP";
260 case 995: return "NoSecurePOP3";
261 default: return "Unknown";
262 /*
263 case 20: return "FTPFailure";
264 case 22: return "SSHfailure";
265 case 23: return "TelnetFailure";
266 case 25: return "SMTPfailure";
267 case 37: return "TimeProtocolFailure";
268 case 70: return "NoGopher" ;
269 case 81: return "NoTor";
270 case 88: return "NoKerberos" ;
271 case 161: return "NoSNMP";
272 case 194: return "NoIRC" ;
273 case 5222: return "NoXMPP" ;
274 */
275 }
276}; /*You could use a tree for this, but that would be work.*/
277diagnostics.diagnose = function(callback) {
278 var errors = diagnostics.checkInterfaces();
279 var finalSteps = function(errorSet) {
280 if (errorSet.length < 1) {
281 errorSet[0] = 0;
282 }
283 callback(errorSet);
284 };
285 var checkHTTPS = function(errorSet){
286 diagnostics.haveHTTPS(function(httpWorking){
287 if (httpWorking == false) {
288 errorSet[errorSet.length] = 443;
289 }
290 finalSteps(errorSet);
291 return;
292 });
293 return;
294 };
295 var checkHTTP = function(errorSet){
296 diagnostics.haveHTTP(function(httpWorking){
297 if (httpWorking == false) {
298 errorSet[errorSet.length] = 80;
299 }
300 checkHTTPS(errorSet);
301 return;
302 });
303 return;
304 };
305 var checkPing = function(errorSet) {
306 diagnostics.havePing(function(pingWorking){
307 if (pingWorking == false) {
308 errorSet[errorSet.length] = 8;
309 }
310 checkHTTP(errorSet);
311 });
312 };
313 diagnostics.haveDNS(function(haveDNS){
314 if (haveDNS == false) {
315 errors[errors.length] = 53;
316 }
317 checkPing(errors);
318 });
319};
320var email = require("./emailTester.js");
321diagnostics.haveInsecureImap = email.insecureImapWorks;
322diagnostics.haveSecureImap = email.secureImapWorks;
323diagnostics.haveInsecurePop = email.insecurePopTest;
324diagnostics.haveSecurePop = email.securePopTest;
325var times = require("./times.js");
326diagnostics.haveNTP = times.ntpTester;
327module.exports = exports = diagnostics;