-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
502 lines (431 loc) · 14.5 KB
/
script.js
File metadata and controls
502 lines (431 loc) · 14.5 KB
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
var urlprefix = ".ip.suysker.top"
var imgUrls = ["/img/s.jpg", "/img/m.jpg", "/img/l.png"]
var imgBytes = [118221, 1531677, 10830957]
var imgi = 1
var concurrency = parseInt($("#concurrency-input").val());
var respondTimeout = parseInt($("#ping-timeout-input").val());
var speedTimeout = parseInt($("#http-timeout-input").val());
var page = 50
var idn = 0
var database = {}
var previousRegions = [];
var currentProvider = 'Cloudflare'
var filename = 'simple_reachable_ips'
function isIPv6(ip) {
return ip.includes(":");
}
$(".ip-button").click(function () {
$(".ip-button").css("background-color", "grey");
$(this).css("background-color", "green");
});
$(".provider-button").click(function () {
$(".provider-button").css("background-color", "grey");
$(this).css("background-color", "green");
});
// Main Table
options = {
selectable: true,
layout: "fitDataTable",
downloadRowRange: "selected",
rowSelected: function (row) {
select_1()
},
rowDeselected: function (row) {
var selectedRows = table.getSelectedRows()
if (selectedRows.length == 0)
select_0()
},
columns: [
{ title: "IP address", field: "ip" },
{ title: "Region", field: "region" },
{
title: "Mean Respond Time", field: "time", sorter: "number", sorterParams: {
alignEmptyValues: "bottom",
}
},
{
title: "Mean Download Speed", field: "speed", sorter: "number", sorterParams: {
alignEmptyValues: "bottom",
}
},
],
}
if (typeof (page) != 'undefined' && page) {
options.pagination = "local" // pagination may cause problem in mobile devices
options.paginationSize = 100
}
table = new Tabulator("#main-table", options)
// Panel
function select_0() {
$("#select-all").attr("data-status", 0)
$("#select-all").text("Select All")
}
function select_1() {
$("#select-all").attr("data-status", 1)
$("#select-all").text("Deselect All")
}
$("#select-all").click(function () {
if ($("#select-all").attr("data-status") == 0) {
table.selectRow()
select_1()
} else {
table.deselectRow()
select_0()
}
})
$("#select-random").click(function () {
table.deselectRow()
var idList = []
var cList = []
var sn = $("#select-number").val()
table.getRows().forEach(function (one) {
idList.push(one.getData().id)
})
for (var i = 0; i < sn; i++) {
var s = Math.floor(Math.random() * idList.length)
cList.push(idList.splice(s, 1)[0])
}
table.selectRow(cList.sort())
})
$("#download").click(function () {
table.download("csv", "test_result.csv", { bom: true })
// include BOM to ensure that UTF-8 characters can be correctly interpereted
})
// Respond time test
function tcpingCallback(time, id) {
database[id].time.push(time)
var alln = database[id].time.length
var validset = []
var sum = 0
database[id].time.forEach(function (one) {
if (one > 0) {
validset.push(one)
sum += one
}
})
var validn = validset.length
var mean = sum / validn
var str = ""
if (validn > 1) {
str = " (" + validn + "/" + alln + ")"
var sumsq = 0
validset.forEach(function (one) {
sumsq += Math.pow(one - mean, 2)
})
var std = Math.sqrt(sumsq / validn)
str = mean.toFixed(1) + "ms" + " σ=" + std.toFixed(1) + str
}
else if (validn == 1) {
str = mean.toFixed(1) + "ms" + str
}
else {
str = "Timeout" + str
}
table.updateData([{ id: id, time: str }])
}
function tcping(addr, callback, id, resolve) {
var started = window.performance.now();
var http = new XMLHttpRequest();
http.open("GET", addr, true);
http.onreadystatechange = function () {
if (http.readyState == 2) {
var ended = window.performance.now();
var milliseconds = ended - started;
callback(milliseconds, id);
resolve();
}
};
http.onerror = function () {
var ended = window.performance.now();
var milliseconds = ended - started;
if (currentProvider === "CloudFront" || currentProvider === "Gcore") {
// 认为连接断开是成功的,返回估算的响应时间
callback(milliseconds, id);
} else {
callback(-1, id);
}
resolve();
};
http.onload = function () {
var resp = http.responseText;
var loc = resp.split("\n")[6].split("=")[1];
table.updateData([{ id: id, region: loc }]);
};
http.timeout = respondTimeout;
http.ontimeout = function () {
callback(-1, id); // Indicate timeout with -1
resolve();
};
http.send(null);
}
var positionSort = function (a, b) {
return a.getPosition(true) - b.getPosition(true)
}
$("#test-respond").click(async function () {
respondTimeout = parseInt($("#ping-timeout-input").val());
concurrency = parseInt($("#concurrency-input").val());
var selectedRows = table.getSelectedRows();
var sn = selectedRows.length;
// 根据currentProvider判断是否使用http或https
var protocol = (currentProvider === "Gcore") ? "https" : "http";
var pingUrl = (currentProvider === "Cloudflare") ? "/cdn-cgi/trace" : "";
if (sn > 0) {
$("#test-respond").prop("disabled", true);
selectedRows.sort(positionSort);
for (let i = 0; i < sn; i += concurrency) {
let batch = selectedRows.slice(i, i + concurrency);
await Promise.all(batch.map(row => {
return new Promise(resolve => {
var one = row.getData();
addr = protocol + "://" + (isIPv6(one.ip) ? "[" + one.ip + "]" : one.ip) + pingUrl + "?" + Math.random();
tcping(addr, tcpingCallback, one.id, resolve); // 注意这里,传递resolve作为参数
});
}));
}
table.redraw(true);
$("#test-respond").prop("disabled", false);
}
});
// Speed test
function speedProgressCallback(rbytes, time, id) {
var rate = rbytes / imgBytes[imgi] * 100
var speed = (rbytes / 1024) / (time / 1000)
var str = speed.toFixed(1) + " KB/s " + rate.toFixed(1) + " %"
table.updateData([{ id: id, speed: str }])
}
function speedEndCallback(rbytes, time, id) {
var speed = (rbytes / 1024) / (time / 1000)
database[id].speed.push(speed)
var alln = database[id].speed.length
var validset = []
var sum = 0
database[id].speed.forEach(function (one) {
if (one > 0 && rbytes / imgBytes[imgi] > 0.05) { // in case 403
validset.push(one)
sum += one
}
})
var validn = validset.length
var mean = sum / validn
var str = ""
if (validn > 1) {
str = " (" + validn + "/" + alln + ")"
var sumsq = 0
validset.forEach(function (one) {
sumsq += Math.pow(one - mean, 2)
})
var std = Math.sqrt(sumsq / validn)
str = mean.toFixed(1) + " KB/s" + " σ=" + std.toFixed(1) + str
}
else if (validn == 1) {
str = mean.toFixed(1) + " KB/s" + str
}
else {
str = "Error" + str
}
table.updateData([{ id: id, speed: str }])
}
function speedRecur(list, i, resolve) {
if (i >= list.length) {
table.redraw(true);
$("#test-speed").prop("disabled", false);
$("#img-select").prop("disabled", false);
return;
}
var one = list[i];
var id = one.id;
var addr = one.addr;
var started = window.performance.now();
var http = new XMLHttpRequest();
http.open("GET", addr, true);
http.onreadystatechange = function () {
// ... (keep the existing code here unchanged)
};
http.loadr = 0;
http.onloadend = function (e) {
var rbytes = (e.loaded == 0) ? http.loadr : e.loaded;
var ended = window.performance.now();
var milliseconds = ended - started;
speedEndCallback(rbytes, milliseconds, id);
// Move on to the next IP in the list
if (i === list.length - 1) {
resolve();
} else {
// Otherwise, move on to the next IP in the list
speedRecur(list, i + 1, resolve);
}
};
http.onprogress = function (e) {
var rbytes = e.loaded;
http.loadr = rbytes;
var ended = window.performance.now();
var milliseconds = ended - started;
if (milliseconds > 100)
speedProgressCallback(rbytes, milliseconds, id);
};
http.timeout = speedTimeout;
http.ontimeout = function () {
if (callback != null) {
callback(-1, id);
callback = null;
}
};
http.send();
}
$("#test-speed").click(async function () {
speedTimeout = parseInt($("#http-timeout-input").val());
concurrency = parseInt($("#concurrency-input").val());
imgi = parseInt($("#img-select").val());
var selectedRows = table.getSelectedRows();
var sn = selectedRows.length;
if (sn > 0) {
$("#test-speed").prop("disabled", true);
$("#img-select").prop("disabled", true);
selectedRows.sort(positionSort);
var sList = [];
selectedRows.forEach(function (row) {
var one = row.getData();
var ip = one.ip.endsWith("::") ? one.ip.slice(0, -2) : one.ip;
sList.push({
id: one.id,
addr: "//" + (isIPv6(ip) ? ip.replace("::", "-").replace(/:/g, "-") : ip.replace(/\./g, "-")) + urlprefix + imgUrls[imgi] + "?" + Math.random()
});
});
for (let i = 0; i < sn; i += concurrency) {
let batch = sList.slice(i, i + concurrency);
// Wait for all items in the current batch to complete
await Promise.all(batch.map(item => {
return new Promise(resolve => {
// Modified speedRecur to accept a resolve function, which will be called at the end
speedRecur([item], 0, resolve);
});
}));
}
table.redraw(true);
$("#test-speed").prop("disabled", false);
$("#img-select").prop("disabled", false);
}
});
function setupButtons(buttonClass, updateFunction) {
if (typeof updateFunction !== "function") {
return; // 如果不是函数,就退出
}
document.querySelectorAll(buttonClass).forEach(function (button) {
button.addEventListener('click', function () {
updateFunction(this.id);
fetchData();
});
});
}
function updateCurrentProvider(value) {
currentProvider = value;
// 当选择Gcore或CloudFront时,禁用test-speed按钮
if (currentProvider === "Gcore" || currentProvider === "CloudFront") {
console.log("Disabling test-speed button"); // 输出禁用信息
$("#test-speed").prop("disabled", true);
} else {
console.log("Enabling test-speed button"); // 输出启用信息
$("#test-speed").prop("disabled", false);
}
}
function updateFilename(value) {
filename = value;
}
function fetchData() {
let url = `https://raw.githubusercontent.com/Suysker/IP-Check/main/${currentProvider}/${filename}.txt`;
if (currentProvider === "CloudFront") {
url = `https://raw.githubusercontent.com/Suysker/IP-Check/main/${currentProvider}/geo_${filename}.txt`;
}
$.get(url, function (data) {
tablemake(data);
}).fail(function () {
if (currentProvider === "CloudFront") {
$.get(`/${currentProvider}/geo_${filename}.txt`, tablemake);
} else {
$.get(`/${currentProvider}/${filename}.txt`, tablemake);
}
});
}
document.addEventListener("DOMContentLoaded", function () {
setupButtons('.provider-button', updateCurrentProvider);
setupButtons('.ip-button', updateFilename);
// 在页面加载完成后立即执行初始化
fetchData();
});
// Entry
function tablemake(data) {
var initData = [];
ip_list = data.split("\n");
// 过滤空字符串
ip_list = ip_list.filter(item => item.trim() !== "");
ip_list.forEach(function (line) {
let parts = line.split(/\s+/); // Split by one or more spaces
let one_ip, region;
if (parts.length > 1) {
// Format: IP region (for CloudFront)
one_ip = parts[0];
region = parts[1];
} else {
// Format: IP
one_ip = parts[0];
region = "";
}
initData.push({
id: idn,
ip: one_ip,
region: region,
time: "",
speed: ""
});
database[idn] = {
time: [],
speed: []
};
idn += 1;
});
table.replaceData(initData);
$("#select-number").attr("max", idn);
}
$("#region-select").on('focus touchstart click', function() {
var regions = [];
// Get all rows from the table
var rows = table.getRows();
rows.forEach(function(row) {
var region = row.getData().region; // Assuming the column key for regions is "region"
if (region && regions.indexOf(region) === -1) { // Ensure the region is not empty
regions.push(region);
}
});
// Sort regions alphabetically
regions.sort();
// Add "All" to the beginning of the sorted regions array
regions.unshift("All");
// Check if regions have changed
if (JSON.stringify(regions) !== JSON.stringify(previousRegions)) {
var selectDropdown = $(this); // Using 'this' since we are inside the click event of the dropdown
selectDropdown.empty(); // Using jQuery's .empty() to clear the dropdown
// Add new options to the dropdown
regions.forEach(function(region) {
var option = $("<option>").val(region).text(region);
selectDropdown.append(option);
});
// Save the new regions as the previous ones for the next check
previousRegions = regions.slice();
}
});
$("#test-region-button").click(function () {
var selectedRegion = document.getElementById("region-select").value;
if(selectedRegion) {
// Deselect all rows first
table.deselectRow();
// Select rows based on the selected region
var rows = table.getRows();
rows.forEach(function(row) {
if (selectedRegion === "All" && row.getData().region) {
row.select();
} else if (row.getData().region === selectedRegion) {
row.select();
}
});
}
});