forked from ipfs/public-gateway-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
318 lines (271 loc) Β· 8.95 KB
/
app.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
/*
This program will check IPFS gateways status using 2 methods
1) By asking for a script through a <script src=""> tag, which when loaded, it executes some code
2) By asking data through ajax requests to verify gateway's CORS configuration
*/
const HASH_TO_TEST = 'bafybeifx7yeb55armcsxwwitkymga5xf53dxiarykms3ygqic223w5sk3m';
const SCRIPT_HASH = 'bafybeietzsezxbgeeyrmwicylb5tpvf7yutrm3bxrfaoulaituhbi7q6yi';
const HASH_STRING = 'Hello from IPFS Gateway Checker';
const ipfs_http_client = window.IpfsHttpClient({
host: 'ipfs.io',
port: 443,
protocol: 'https'
});
let checker = document.getElementById('checker');
checker.nodes = [];
checker.checkGateways = function(gateways) {
gateways.forEach((gateway) => {
let node = new Node(this.results, gateway, this.nodes.length);
this.nodes.push(node);
this.results.append(node.tag);
node.check();
});
};
checker.updateStats = function() {
this.stats.update();
};
let Stats = function(parent) {
this.parent = parent;
this.tag = document.getElementById('checker.stats');//document.createElement("div"); // TODO:: ugly i know, WIP
this.tag.className = "Stats";
this.gateways = document.createElement("div");
this.gateways.textContent = `0/0 tested`;
this.gateways.className = "Gateways";
this.tag.append(this.gateways);
this.totals = document.createElement("div");
this.totals.textContent = "0 online";
this.totals.className = "Totals";
this.tag.append(this.totals);
};
Stats.prototype.update = function() {
let up = 0, down = 0;
for (let savedNode of this.parent.nodes) {
if ("up" in savedNode.status) {
savedNode.status.up ? ++up : ++down;
}
}
this.gateways.textContent = `${up+down}/${this.parent.nodes.length} tested`;
this.totals.textContent = `${up} online`;
};
checker.stats = new Stats(checker);
checker.results = document.getElementById('checker.results');
checker.results.parent = checker;
checker.results.checked = function(node) {
this.parent.updateStats(node);
};
checker.results.failed = function(node) {
this.parent.updateStats(node);
};
let Status = function(parent, index) {
this.parent = parent;
this.tag = document.createElement("div");
this.tag.className = "Status";
this.tag.textContent = 'π';
};
Status.prototype.check = function() {
let gatewayAndScriptHash = this.parent.gateway.replace(":hash", SCRIPT_HASH);
// we set a unused number as a url parameter, to try to prevent content caching
// is it right ? ... do you know a better way ? ... does it always work ?
let now = Date.now();
// 3 important things here
// 1) we add #x-ipfs-companion-no-redirect to the final url (self explanatory)
// 2) we add ?filename=anyname.js as a parameter to let the gateway guess Content-Type header
// to be sent in headers in order to prevent CORB
// 3) parameter 'i' is the one used to identify the gateway once the script executes
let src = `${gatewayAndScriptHash}?i=${this.parent.index}&now=${now}&filename=anyname.js#x-ipfs-companion-no-redirect`;
let script = document.createElement('script');
script.src = src;
document.body.append(script);
script.onerror = () => {
// we check this because the gateway could be already checked by CORS before onerror executes
// and, even though it is failing here, we know it is UP
if (!this.up) {
this.up = false;
this.tag.textContent = 'β';
this.parent.failed();
}
};
};
Status.prototype.checked = function() {
this.up = true;
this.tag.innerHTML = 'β
';
};
// this function is executed from that previously loaded script
// it only contains the following: OnScriptloaded(document.currentScript ? document.currentScript.src : '');
function OnScriptloaded(src) {
try {
let url = new URL(src);
let index = url.searchParams.get("i");
let node = checker.nodes[index];
if (node) {
node.checked();
}
} catch(e) {
// this is a URL exception, we can do nothing, user is probably using Internet Explorer
}
}
let Cors = function(parent) {
this.parent = parent;
this.tag = document.createElement("div");
this.tag.className = "Cors";
this.tag.textContent = 'π';
};
Cors.prototype.check = function() {
const gatewayAndHash = this.parent.gateway.replace(':hash', HASH_TO_TEST);
const now = Date.now();
const testUrl = `${gatewayAndHash}?now=${now}#x-ipfs-companion-no-redirect`;
fetch(testUrl).then((res) => {
return res.text();
}).then((text) => {
const matched = (HASH_STRING === text.trim());
if (matched) {
this.parent.checked();
this.tag.textContent = 'β
';
} else {
this.onerror();
}
}).catch((err) => {
this.onerror();
});
};
Cors.prototype.onerror = function() {
this.tag.textContent = 'β';
};
let Origin = function(parent) {
this.parent = parent;
this.tag = document.createElement("div");
this.tag.className = "Origin";
this.tag.textContent = 'π';
};
Origin.prototype.check = function() {
const cidInSubdomain = this.parent.gateway.startsWith('https://:hash.ipfs.');
if (cidInSubdomain) {
this.tag.textContent = 'β
';
} else {
this.onerror();
}
};
Origin.prototype.onerror = function() {
this.tag.textContent = 'β';
};
let Flag = function(parent, hostname) {
this.parent = parent;
this.tag = document.createElement("div");
this.tag.className = "Flag";
this.tag.textContent = '';
let ask = true;
try{
let savedSTR = localStorage.getItem(hostname);
if (savedSTR) {
let saved = JSON.parse(savedSTR);
let now = Date.now();
let savedTime = saved.time;
let elapsed = now - savedTime;
let expiration = 7 * 24 * 60 * 60 * 1000; // 7 days
if (elapsed < expiration) {
ask = false;
this.onResponse(saved);
}
}
} catch(e) {
console.error(`error while getting savedSTR for ${hostname}`, e)
}
if (ask) {
setTimeout(() => {
let request = new XMLHttpRequest();
request.open('GET', `https://cloudflare-dns.com/dns-query?name=${hostname}&type=A`);
request.setRequestHeader("accept", "application/dns-json");
request.onreadystatechange = async () => {
if (4 == request.readyState) {
if (200 == request.status) {
try {
let response = JSON.parse(request.responseText);
let ip = null;
for (let i = 0; i < response.Answer.length && !ip; i++) {
let answer = response.Answer[i];
if (1 == answer.type) {
ip = answer.data;
}
}
if (ip) {
let geoipResponse = await window.IpfsGeoip.lookup(ipfs_http_client, ip);
if (geoipResponse && geoipResponse.country_code) {
this.onResponse(geoipResponse);
geoipResponse.time = Date.now();
let resposeSTR = JSON.stringify(geoipResponse);
localStorage.setItem(hostname, resposeSTR);
}
}
} catch(e) {
console.error(`error while getting DNS A record for ${hostname}`, e)
}
}
}
};
request.onerror = (e) => {};
request.send();
}, 500 * Flag.requests++); // 2 / second, request limit
}
};
Flag.prototype.onResponse = function(response) {
this.tag.style["background-image"] = `url('https://ipfs.io/ipfs/QmaYjj5BHGAWfopTdE8ESzypbuthsZqTeqz9rEuh3EJZi6/${response.country_code.toLowerCase()}.svg')`;
this.tag.title = response.country_name;
};
Flag.requests = 0;
let Node = function(parent, gateway, index) {
this.parent = parent;
this.tag = document.createElement("div");
this.tag.className = "Node";
this.tag.style["order"] = Date.now();
this.status = new Status(this);
this.tag.append(this.status.tag);
this.cors = new Cors(this);
this.tag.append(this.cors.tag);
this.origin = new Origin(this);
this.tag.append(this.origin.tag);
this.link = document.createElement("div");
let gatewayAndHash = gateway.replace(':hash', HASH_TO_TEST);
this.link.url = new URL(gatewayAndHash);
this.link.textContent = gatewayHostname(this.link.url);
this.link.className = "Link";
this.flag = new Flag(this, this.link.textContent);
this.tag.append(this.flag.tag);
this.tag.append(this.link);
this.took = document.createElement("div");
this.took.className = "Took";
this.tag.append(this.took);
this.gateway = gateway;
this.index = index;
this.checkingTime = 0;
};
Node.prototype.check = function() {
this.checkingTime = Date.now();
this.status.check();
this.cors.check();
this.origin.check();
};
Node.prototype.checked = function() {
// we care only about the fatest method
if (!this.status.up) {
this.status.checked();
this.parent.checked(this);
let url = this.link.url;
let host = gatewayHostname(url);
this.link.innerHTML = `<a title="${host}" href="${url}#x-ipfs-companion-no-redirect" target="_blank">${host}</a>`;
let ms = Date.now() - this.checkingTime;
this.tag.style["order"] = ms;
let s = (ms / 1000).toFixed(2);
this.took.textContent = `${s}s`;
}
};
Node.prototype.failed = function() {
this.parent.failed(this);
};
function gatewayHostname (url) {
if (url && url.hostname) url = url.hostname.toString()
return url.replace(`${HASH_TO_TEST}.ipfs.`, "") // skip .ipfs. in subdomain gateways
.replace(`${HASH_TO_TEST}.`, "") // path-based
}
fetch('./gateways.json')
.then(res => res.json())
.then(gateways => checker.checkGateways(gateways));