1 |
|
2 |
|
3 |
|
4 |
|
5 | var os = require("os");
|
6 | var dns = require("native-dns");
|
7 | var http = require("http");
|
8 | var https = require("https");
|
9 | var diagnostics = {};
|
10 | var testURL = "google.com";
|
11 | diagnostics.getTestURL = function(){ return testURL; };
|
12 | diagnostics.setTestURL = function(replacement) {
|
13 | testURL = replacement;
|
14 | return;
|
15 | };
|
16 | diagnostics.checkInterfaces = function() {
|
17 | var errors = [];
|
18 | var outside = false;
|
19 | var v4 = false;
|
20 | var v6 = false;
|
21 | var interfaces = os.networkInterfaces();
|
22 | for (var interfaceSet in interfaces) {
|
23 | for (var interface in interfaces[interfaceSet]) {
|
24 | if (interfaces[interfaceSet][interface].internal == false) {
|
25 | outside = true;
|
26 | if (interfaces[interfaceSet][interface].family == "IPv4") {
|
27 | v4 = true;
|
28 | }
|
29 | if (interfaces[interfaceSet][interface].family == "IPv6") {
|
30 | v6 = true;
|
31 | }
|
32 | }
|
33 | }
|
34 | }
|
35 | if (outside != true) {
|
36 | errors[errors.length] = 1;
|
37 | }
|
38 | if (v4 != true) {
|
39 | errors[errors.length] = 4;
|
40 | }
|
41 | if (v6 != true) {
|
42 | errors[errors.length] = 6;
|
43 | }
|
44 | return errors;
|
45 | };
|
46 | diagnostics.haveIPv4 = function(){
|
47 | var errors = diagnostics.checkInterfaces();
|
48 | if(errors.indexOf(4) > -1) {
|
49 | return false;
|
50 | }
|
51 | return true;
|
52 | };
|
53 | diagnostics.haveIPv6 = function(){
|
54 | var errors = diagnostics.checkInterfaces();
|
55 | if(errors.indexOf(6) > -1) {
|
56 | return false;
|
57 | }
|
58 | return true;
|
59 | };
|
60 | diagnostics.haveConnection = function(){
|
61 | var errors = diagnostics.checkInterfaces();
|
62 | if(errors.indexOf(1) > -1) {
|
63 | return false;
|
64 | }
|
65 | return true;
|
66 | };
|
67 | diagnostics.haveDNS = function(callback) {
|
68 | var checkIPv4DNS = function(){
|
69 | var DNSanswer = "";
|
70 | var dns4Connection = dns.Request({
|
71 | question: dns.Question({
|
72 | name: testURL
|
73 | }),
|
74 | server: {
|
75 | address: '8.8.8.8'
|
76 | }
|
77 | });
|
78 | var deactivate4 = false;
|
79 | dns4Connection.on("timeout", function(){
|
80 | if (!deactivate4) {
|
81 | deactivate4 = true;
|
82 | callback(false);
|
83 | return;
|
84 | }
|
85 | });
|
86 | dns4Connection.on("message", function(DNSerr, section){
|
87 | if (DNSerr) {
|
88 | if (!deactivate4) {
|
89 | deactivate4 = true;
|
90 | callback(false);
|
91 | }
|
92 | return;
|
93 | }
|
94 | DNSanswer += section;
|
95 | });
|
96 | dns4Connection.on("end", function(){
|
97 | if (!deactivate4) {
|
98 | deactivate4 = true;
|
99 | callback(true);
|
100 | }
|
101 | });
|
102 | dns4Connection.send();
|
103 | };
|
104 | var DNSsegments = "";
|
105 | var deactivate6 = false;
|
106 | var dnsConnection = dns.Request({
|
107 | question: dns.Question({
|
108 | name: testURL
|
109 | }),
|
110 | server: {
|
111 | address: '2001:4860:4860::8888'
|
112 | }
|
113 | });
|
114 | dnsConnection.on("timeout", function(){
|
115 | if (!deactivate6) {
|
116 | deactivate6 = true;
|
117 | checkIPv4DNS();
|
118 | }
|
119 | });
|
120 | dnsConnection.on("message", function(DNSerr, section){
|
121 | if (DNSerr) {
|
122 | if (!deactivate6) {
|
123 | deactivate6 = true;
|
124 | checkIPv4DNS();
|
125 | }
|
126 | return;
|
127 | }
|
128 | DNSsegments += section;
|
129 | });
|
130 | dnsConnection.on("end", function(){
|
131 | if (!deactivate6) {
|
132 | deactivate6 = true;
|
133 | callback(true);
|
134 | }
|
135 | return;
|
136 | });
|
137 | dnsConnection.send();
|
138 | return;
|
139 | };
|
140 | diagnostics.haveHTTP = function(callback) {
|
141 | var isFired = false;
|
142 | var requestOptions = {
|
143 | hostname: testURL,
|
144 | port: 80,
|
145 | method: "GET"
|
146 | };
|
147 | var httpRequest = http.request(requestOptions, function(response){
|
148 | var responseTotal = "";
|
149 | response.on("data", function(section){
|
150 | responseTotal += section;
|
151 | });
|
152 | response.on("end", function(){
|
153 | if (isFired == false) {
|
154 | isFired = true;
|
155 | callback(true);
|
156 | }
|
157 | });
|
158 | response.on("error", function(){
|
159 | if (isFired == false) {
|
160 | isFired = true;
|
161 | callback(false);
|
162 | }
|
163 | });
|
164 | });
|
165 | httpRequest.on("socket", function (socket){
|
166 | socket.setTimeout(10000);
|
167 | socket.on("timeout", function(){
|
168 | if (isFired == false) {
|
169 | isFired = true;
|
170 | callback(false);
|
171 | }
|
172 | });
|
173 | });
|
174 | httpRequest.on("error", function(failure) {
|
175 | if (isFired == false) {
|
176 | isFired = true;
|
177 | callback(false);
|
178 | }
|
179 | });
|
180 | httpRequest.end();
|
181 | return;
|
182 | };
|
183 | diagnostics.haveHTTPS = function(callback) {
|
184 | var isFired = false;
|
185 | var requestOptions = {
|
186 | hostname: testURL,
|
187 | port: 443,
|
188 | method: "GET"
|
189 | };
|
190 | var httpRequest = https.request(requestOptions, function(response){
|
191 | var responseTotal = "";
|
192 | response.on("data", function(section){
|
193 | responseTotal += section;
|
194 | });
|
195 | response.on("end", function(){
|
196 | if (isFired == false) {
|
197 | isFired = true;
|
198 | callback(true);
|
199 | }
|
200 | });
|
201 | response.on("error", function(){
|
202 | if (isFired == false) {
|
203 | isFired = true;
|
204 | callback(false);
|
205 | }
|
206 | });
|
207 | });
|
208 | httpRequest.on("socket", function (socket){
|
209 | socket.setTimeout(10000);
|
210 | socket.on("timeout", function(){
|
211 | if (isFired == false) {
|
212 | isFired = true;
|
213 | callback(false);
|
214 | }
|
215 | });
|
216 | });
|
217 | httpRequest.on("error", function(failure) {
|
218 | if (isFired == false) {
|
219 | isFired = true;
|
220 | callback(false);
|
221 | }
|
222 | });
|
223 | httpRequest.end();
|
224 | return;
|
225 |
|
226 | };
|
227 | diagnostics.getError = function(id) {
|
228 | switch(id) {
|
229 | case 0: return "NormalNetworkActivity";
|
230 | case 1: return "NoConnection";
|
231 | case 4: return "NoIPv4Connection";
|
232 | case 6: return "NoIPv6Connection";
|
233 | case 7: return "DiagnosticsScriptFailure";
|
234 | case 53: return "NoDNS"
|
235 | case 80: return "NoHTTPconnection";
|
236 | case 443: return "NoHTTPSconnection";
|
237 | default: return "Unknown";
|
238 | |
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | }
|
255 | };
|
256 | diagnostics.diagnose = function(callback) {
|
257 | var errors = diagnostics.checkInterfaces();
|
258 | var finalSteps = function(errorSet) {
|
259 | if (errorSet.length < 1) {
|
260 | errorSet[0] = 0;
|
261 | }
|
262 | callback(errorSet);
|
263 | };
|
264 | var checkHTTPS = function(errorSet){
|
265 | diagnostics.haveHTTPS(function(httpWorking){
|
266 | if (httpWorking == false) {
|
267 | errorSet[errorSet.length] = 443;
|
268 | }
|
269 | finalSteps(errorSet);
|
270 | return;
|
271 | });
|
272 | return;
|
273 | };
|
274 | var checkHTTP = function(errorSet){
|
275 | diagnostics.haveHTTP(function(httpWorking){
|
276 | if (httpWorking == false) {
|
277 | errorSet[errorSet.length] = 80;
|
278 | }
|
279 | checkHTTPS(errorSet);
|
280 | return;
|
281 | });
|
282 | return;
|
283 | };
|
284 | diagnostics.haveDNS(function(haveDNS){
|
285 | if (haveDNS == false) {
|
286 | errors[errors.length] = 53;
|
287 | }
|
288 | checkHTTP(errors);
|
289 | });
|
290 | };
|
291 | module.exports = exports = diagnostics; |
\ | No newline at end of file |