From d8962c3abb24dfc7efae2ed92a18d5d7fbada1dc Mon Sep 17 00:00:00 2001 From: Bruce Date: Sun, 8 Sep 2024 19:46:39 +0800 Subject: [PATCH] fix: result IPs was empty. --- index.js | 20 ++++++++++++++------ result.txt | 2 +- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index c27faa9..2a36a9a 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -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(); @@ -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); }); @@ -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; } } @@ -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); diff --git a/result.txt b/result.txt index 54ea118..74b36bb 100644 --- a/result.txt +++ b/result.txt @@ -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}] \ No newline at end of file +[{"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}] \ No newline at end of file