Skip to content

Commit

Permalink
fix: result IPs was empty.
Browse files Browse the repository at this point in the history
  • Loading branch information
BruceWind committed Sep 8, 2024
1 parent 671edbd commit d8962c3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 14 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ const { localRanges } = readJsonFile("./gcore_cdn_ip_ranges.json")
import { Netmask } from 'netmask';

// In case script can not find any available IPs, You should try to increase {THRESHOLD} to 140.
const THRESHOLD = 500;
const THRESHOLD = 200;


const PING_THREADS = 100;
const PING_THREADS = 50;
let countOfBeingProcess = 0;

function execPromise(command) {
Expand Down Expand Up @@ -196,7 +196,7 @@ async function queryTCPLatency(ip) {
const start = process.hrtime.bigint();

try {
await new Promise((resolve, reject) => {
const elapsed = await new Promise((resolve, reject) => {
const socket = new net.Socket();
const timeout = setTimeout(() => {
socket.destroy();
Expand All @@ -206,7 +206,7 @@ async function queryTCPLatency(ip) {
socket.on('connect', () => {
clearTimeout(timeout);
const end = process.hrtime.bigint();
const elapsed = Number(end - start) / 1000000; // to ms
const elapsed = Math.floor(Number(end - start) / 1000000); // to ms
socket.end();
resolve(elapsed);
});
Expand All @@ -218,8 +218,10 @@ async function queryTCPLatency(ip) {

socket.connect(port, ip);
});

return elapsed;
} catch (err) {
console.error(`Failed to connect to ${ip}:${port}: ${err.message}`);
console.log(`TCP connection failed for ${ip}: ${err.message}`);
return 1000;
}
}
Expand All @@ -228,12 +230,18 @@ async function queryTCPLatency(ip) {
async function queryAvgLatency(ip) {
try {
await queryTCPLatency(ip); // this line looks like useless, but In my opinion, this can make connection reliable.
const pingLatency = await queryLatency(ip);
if (pingLatency > THRESHOLD + 50) return latency1;
const latency1 = await queryTCPLatency(ip);
if (latency1 > THRESHOLD + 50) return latency1;
const latency2 = await queryTCPLatency(ip);
if (latency2 > THRESHOLD + 50) return latency2;
if(latency1=== undefined || latency2 === undefined) throw new Error('latencies are undefined');
const latency3 = await queryTCPLatency(ip);
return Math.round((latency1 + latency2 + latency3) / 3);

const result = Math.round((latency1 + latency2) / 2);

return result;
}
catch (e) {
console.log(`${ip} is not reachable.`, e.message);
Expand Down
2 changes: 1 addition & 1 deletion result.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"ip":"92.223.63.23","latency":42},{"ip":"92.223.63.9","latency":43},{"ip":"92.223.63.6","latency":64},{"ip":"92.223.63.7","latency":64},{"ip":"92.223.63.25","latency":69},{"ip":"62.112.216.10","latency":95},{"ip":"92.223.61.25","latency":95},{"ip":"62.112.216.8","latency":96}]
[{"ip":"139.28.7.4","latency":182},{"ip":"213.156.156.4","latency":185},{"ip":"90.84.153.19","latency":188},{"ip":"90.84.153.129","latency":188},{"ip":"80.15.252.63","latency":189},{"ip":"80.15.252.67","latency":191},{"ip":"213.156.156.5","latency":192},{"ip":"78.111.103.4","latency":192},{"ip":"80.15.252.1","latency":193},{"ip":"90.84.153.130","latency":193},{"ip":"80.15.252.47","latency":194},{"ip":"90.84.153.18","latency":194},{"ip":"80.15.228.3","latency":195},{"ip":"80.15.252.41","latency":195},{"ip":"90.84.153.194","latency":195},{"ip":"80.15.252.43","latency":197},{"ip":"80.15.252.9","latency":197},{"ip":"80.15.252.25","latency":197},{"ip":"90.84.153.98","latency":197},{"ip":"90.84.153.131","latency":197},{"ip":"90.84.153.97","latency":197},{"ip":"90.84.153.227","latency":197},{"ip":"80.15.228.1","latency":198},{"ip":"90.84.153.161","latency":198},{"ip":"90.84.153.195","latency":198},{"ip":"90.84.153.225","latency":198},{"ip":"90.84.153.163","latency":198},{"ip":"92.38.170.6","latency":199},{"ip":"80.15.252.33","latency":199},{"ip":"90.84.153.99","latency":199},{"ip":"90.84.153.193","latency":199},{"ip":"80.15.252.209","latency":199}]

0 comments on commit d8962c3

Please sign in to comment.