-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·204 lines (176 loc) · 6.02 KB
/
index.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
#!/usr/bin/env node
import { load } from 'cheerio'
import axios from 'axios'
import { parse } from 'acorn';
import { simple } from 'acorn-walk'
import { performance } from 'node:perf_hooks'
import ms from 'ms'
import chalk from 'chalk'
import clear from 'console-clear';
import { address, mac, dns } from 'address/promises';
import si from 'systeminformation';
async function systemStats() {
const cpu = await si.cpu();
const os = await si.osInfo();
const wifi = await si.wifiConnections()
const gateway = await si.networkGatewayDefault()
let system = {
manufacturer: cpu.manufacturer,
brand: cpu.brand,
vendor: cpu.vendor,
model: cpu.model,
speed: cpu.speed,
platform: os.platform,
distro: os.distro,
release: os.release,
osName: os.codename,
hostname: os.hostname,
arch: os.arch,
}
let response = {
device: system,
wifi: wifi,
gateway,
}
return response
}
async function parseToken(scriptPath) {
const { data: script } = await axios.get(scriptPath, { responseType: 'text' });
const result = parse(script, {
allowHashBang: true,
ecmaVersion: 2022
});
return new Promise((resolve) => {
let token = null;
simple(result, {
Property(node) {
if (token != null) return;
if (node.key.name === "token") {
token = node.value.value;
}
},
});
resolve(token)
})
}
clear(true)
console.log('')
console.log(chalk.red.bold("ETHGlobal Speed Test"))
console.log('')
console.log(chalk.green('Starting..'));
console.log('')
const { data } = await axios.get("https://fast.com/", { responseType: 'text' });
const $ = load(data);
const token = await parseToken(new URL($("script[src]").first().attr('src'), "https://fast.com/"));
clear(true)
console.log('')
console.log(chalk.red.bold("ETHGlobal Speed Test"))
console.log('')
console.log(chalk.green('Initializing..'));
console.log('')
const { data: { client: _client, targets } } = await axios.get(`https://api.fast.com/netflix/speedtest/v2?https=true&token=${token}&urlCount=5`);
let bytes = 0;
const avg = [];
const controller = new AbortController();
let startedAt = Date.now();
let elapsed = Date.now();
const responseTimeSamples = [];
const payload = {}
const history = []
let tick = 0;
let lastReport = {};
const spinner = '◐◓◑◒'.split('')
await Promise.all(
targets.map(async ({ url }) => {
try {
const { data: stream } = await axios.get(url, { responseType: 'stream', signal: controller.signal });
let responseTime = performance.now();
stream.on('data', buffer => {
bytes += buffer.length;
responseTime = performance.now() - responseTime;
responseTimeSamples.push(responseTime)
responseTime = performance.now()
if (Date.now() - startedAt > 100) {
tick++;
avg.push(bytes * 8 * 10);
const averageBits = avg.reduce((a, b) => a + b, 0) / avg.length;
const latency = ms(Math.round(responseTimeSamples.reduce((a, b) => a + b) / responseTimeSamples.length), { long: true })
const spin = offset => chalk.dim(`${spinner[(tick + offset) % spinner.length]}`);
clear(true)
const timeElapsed = ms(Date.now() - elapsed);
console.log('')
console.log(chalk.red.bold("ETHGlobal Speed Test"))
console.log('')
console.log(spin(4) + chalk.yellow(' Speed \t') + Math.round(averageBits / 1000000) + " Mbps ");
console.log(spin(3) + chalk.yellow(' Latency\t') + latency);
console.log(spin(2) + chalk.yellow(' Elapsed\t') + timeElapsed);
lastReport = {
averageBits,
latency,
timeElapsed,
ts: Math.round((new Date()).getTime()/1000),
}
if (Math.random() > 0.9) {
history.push(lastReport)
}
bytes = 0;
startedAt += 100;
}
});
stream.on('end', async () => {
controller.abort();
const {timeElapsed, latency, averageBits} = lastReport;
const _addr = await address();
const _mac = await mac();
const _dns = await dns();
const stats = await systemStats();
payload["summary"] = {
speed: Math.round(averageBits / 1000)/1000,
isp: _client.isp,
latency,
ip: _addr.ip,
mac: _mac,
duration: timeElapsed,
city: _client.location.city,
country: _client.location.country,
publicIP: _client.ip,
...stats,
ipv6: _addr.ipv6,
bits: averageBits,
asn: _client.asn,
dns: _dns,
time: (new Date()).toISOString(),
timestamp: Math.round((new Date()).getTime()/1000),
}
payload["raw"] = history
const spin = () => chalk.green("✔");
clear(true)
console.log('')
console.log(chalk.red.bold("ETHGlobal Speed Test"))
console.log('')
console.log(spin(4) + chalk.green(' Speed \t') + Math.round(averageBits / 1000000) + " Mbps ");
console.log(spin(3) + chalk.green(' Latency\t') + latency);
console.log(spin(2) + chalk.green(' Elapsed\t') + timeElapsed);
console.log('')
console.log(chalk.magenta(' IP\t') + _addr.ip);
console.log(chalk.magenta(' Mac\t') + _mac);
console.log('')
console.log(chalk.green.bold(' Your internet speed is ' + Math.round(averageBits / 10000)/100 + " Mbps on " + (new Date()).toUTCString() + ' '));
console.log('')
const postUpdate = await fetch('https://speed.ethglobal.com/api/create', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(payload)
})
const postUpdateJson = await postUpdate.json()
console.log(chalk.redBright(' Link: ') + 'https://speed.ethglobal.com/run/' + postUpdateJson.key);
console.log('')
// console.log([history.length, payload])
})
} catch(e) {
if (!axios.isAxiosError(e)) {
throw e;
}
}
})
)