this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Show IP, Country, Data Center

SukkaW db8f2c81 828d3c9f

+84 -5
+1
chalk.js
··· 2 2 exports.bold = (...args) => `\u001b[1m${args.join(' ')}\u001b[0m`; 3 3 exports.yellow = (...args) => `\u001b[33m${args.join(' ')}\u001b[0m`; 4 4 exports.green = (...args) => `\u001b[32m${args.join(' ')}\u001b[0m`; 5 + exports.blue = (...args) => `\u001b[34m${args.join(' ')}\u001b[0m`;
+83 -5
cli.js
··· 2 2 3 3 const { performance } = require('perf_hooks'); 4 4 const https = require('https'); 5 - const { magenta, bold, yellow, green } = require('./chalk.js'); 5 + const { magenta, bold, yellow, green, blue } = require('./chalk.js'); 6 6 const stats = require('./stats.js'); 7 + 8 + async function get(hostname, path) { 9 + return new Promise((resolve, reject) => { 10 + const req = https.request( 11 + { 12 + hostname, 13 + path, 14 + method: 'GET', 15 + }, 16 + (res) => { 17 + const body = []; 18 + res.on('data', (chunk) => { 19 + body.push(chunk); 20 + }); 21 + res.on('end', () => { 22 + try { 23 + resolve(Buffer.concat(body).toString()); 24 + } catch (e) { 25 + reject(e); 26 + } 27 + }); 28 + req.on('error', (err) => { 29 + reject(err); 30 + }); 31 + } 32 + ); 33 + 34 + req.end(); 35 + }); 36 + } 37 + 38 + async function fetchServerLocationData() { 39 + const res = JSON.parse(await get('speed.cloudflare.com', '/locations')); 40 + 41 + return res.reduce((data, { iata, city }) => { 42 + // Bypass prettier "no-assign-param" rules 43 + const data1 = data; 44 + 45 + data1[iata] = city; 46 + return data1; 47 + }, {}); 48 + } 49 + 50 + function fetchCfCdnCgiTrace() { 51 + const parseCfCdnCgiTrace = (text) => 52 + text 53 + .split('\n') 54 + .map((i) => { 55 + const j = i.split('='); 56 + 57 + return [j[0], j[1]]; 58 + }) 59 + .reduce((data, [k, v]) => { 60 + if (v === undefined) return data; 61 + 62 + // Bypass prettier "no-assign-param" rules 63 + const data1 = data; 64 + // Object.fromEntries is only supported by Node.js 12 or newer 65 + data1[k] = v; 66 + 67 + return data1; 68 + }, {}); 69 + 70 + return get('speed.cloudflare.com', '/cdn-cgi/trace').then(parseCfCdnCgiTrace); 71 + } 7 72 8 73 function request(options, data = '') { 9 74 let started; ··· 142 207 return measurements; 143 208 } 144 209 210 + function logInfo(text, data) { 211 + console.log(bold(' '.repeat(15 - text.length), `${text}:`, blue(data))); 212 + } 213 + 145 214 function logSpeedTestResult(size, test) { 146 215 const speed = stats.median(test).toFixed(2); 147 216 console.log( 148 - bold(' '.repeat(7 - size.length), size, 'speed:', yellow(`${speed} Mbps`)) 217 + bold(' '.repeat(9 - size.length), size, 'speed:', yellow(`${speed} Mbps`)) 149 218 ); 150 219 } 151 220 152 221 async function speedTest() { 222 + const serverLocationData = await fetchServerLocationData(); 223 + 224 + const { ip, loc, colo } = await fetchCfCdnCgiTrace(); 225 + 226 + const city = serverLocationData[colo]; 227 + 228 + logInfo('Server location', `${city} (${colo})`); 229 + logInfo('Your IP', `${ip} (${loc})`); 230 + 153 231 const ping = await measureLatency(); 154 232 155 - console.log(bold(' Latency:', magenta(`${ping[3].toFixed(2)} ms`))); 233 + console.log(bold(' Latency:', magenta(`${ping[3].toFixed(2)} ms`))); 156 234 157 235 const test1 = await measureDownload(11000, 10); 158 236 ··· 180 258 181 259 console.log( 182 260 bold( 183 - 'Download speed:', 261 + ' Download speed:', 184 262 green( 185 263 stats 186 264 .quartile( ··· 199 277 200 278 console.log( 201 279 bold( 202 - ' Upload speed:', 280 + ' Upload speed:', 203 281 green( 204 282 stats.quartile([...test7, ...test8, ...test9], 0.9).toFixed(2), 205 283 'Mbps'