-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscript.ts
449 lines (397 loc) · 12 KB
/
script.ts
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
import { parseArgs, Spinner } from "https://deno.land/[email protected]/cli/mod.ts";
import {
bold,
cyan,
green,
red,
} from "https://deno.land/[email protected]/fmt/colors.ts";
const CLI_NAME = "mullvad-ping";
const VERSION = "v0.8.0";
type ServerData = {
hostname: string;
country_code: string;
country_name: string;
city_code: string;
city_name: string;
active: boolean;
owned: boolean;
provider: string;
ipv4_addr_in: string;
ipv6_addr_in: string;
network_port_speed: number;
stboot: boolean;
type: string;
};
type ResultServerData = {
avg: number;
min?: number;
max?: number;
mdev?: number;
} & ServerData;
function error(...message: unknown[]) {
console.error(red(bold("error:")), ...message);
console.error();
console.trace();
Deno.exit(1);
}
const serverTypes = ["openvpn", "bridge", "wireguard", "all"];
const ownerTypes = ["mullvad", "rented", "all"];
const runTypes = ["ram", "disk", "all"];
const INTERVAL_DEFAULT = 0.2;
const COUNT_DEFAULT = 5;
const TOP_DEFAULT = 5;
const PORT_SPEED_DEFAULT = 0;
const RUN_MODE_DEFAULT = "all";
const OWNER_DEFAULT = "all";
const TYPE_DEFAULT = "all";
const args = parseArgs(Deno.args, {
"--": false,
string: [
"country",
"type",
"interval",
"count",
"top",
"owner",
"port-speed",
"provider",
"run-mode",
],
boolean: [
"help",
"list",
"list-countries",
"list-providers",
"include-inactive",
"json",
"version",
],
alias: {
c: "country",
l: "list",
t: "type",
C: "count",
n: "top",
s: "port-speed",
p: "provider",
j: "json",
h: "help",
V: "version",
},
default: {
interval: `${INTERVAL_DEFAULT}`,
count: `${COUNT_DEFAULT}`,
top: `${TOP_DEFAULT}`,
"port-speed": `${PORT_SPEED_DEFAULT}`,
"run-mode": RUN_MODE_DEFAULT,
owner: OWNER_DEFAULT,
"type": TYPE_DEFAULT,
},
unknown: (arg) => {
error(`Unknown argument ${arg}`);
},
});
if (args.help) {
const serverTypesStr = serverTypes.join(", ");
const ownerTypesStr = ownerTypes.join(", ");
const runTypesStr = runTypes.join(", ");
// This help is formatted like cargo's help
console.log(
`Gets the list of Mullvad servers with the best latency according to ping
${green(bold("Usage:"))} ${cyan(bold(CLI_NAME))} ${cyan("[OPTIONS]")}
${green(bold("Options:"))}
-c, --country <CODE> the country you want to query (eg. us, gb, de)
-l, --list lists the available servers
--list-countries lists the available countries
--list-providers lists the available providers
-t, --type <TYPE> the type of server to query (${serverTypesStr})
-C, --count <COUNT> the number of pings to the server (default ${COUNT_DEFAULT})
-n, --top <TOPN> the number of top servers to show, (0=all, default ${TOP_DEFAULT})
-s, --port-speed <SPEED> only show servers with at least n Gbps port speed
-p, --provider <NAME> only show servers from the given provider
--owner <OWNER> only show servers by owner (${ownerTypesStr})
--run-mode <TYPE> only show servers running from (${runTypesStr})
--include-inactive include inactive servers
-j, --json output the results in JSON format
-h, --help display this help and exit
-V, --version display version information and exit`
.replace(/(--?[a-zA-Z-]+)/g, cyan(bold("$1")))
.replace(/(<[^>]+>)/g, cyan("$1")),
);
Deno.exit(0);
}
if (args.version) {
console.log(CLI_NAME, VERSION);
Deno.exit(0);
}
// Color output only if the input and output are terminals and not JSON
const isInteractive = Deno.stdout.isTerminal() && Deno.stdin.isTerminal() &&
!args.json;
const country = args.country?.toLowerCase();
const serverType = args.type.toLowerCase();
if (!serverTypes.includes(serverType)) {
error(`Invalid type, allowed types are: ${serverTypes.join(", ")}`);
}
const interval = parseFloat(args.interval) || INTERVAL_DEFAULT;
if (interval < INTERVAL_DEFAULT) {
error(`Minimum interval value is ${INTERVAL_DEFAULT}`);
}
const count = parseInt(args.count) || COUNT_DEFAULT;
const topN = parseInt(args.top) || TOP_DEFAULT;
const portSpeed = parseInt(args["port-speed"]) || PORT_SPEED_DEFAULT;
const owned = args.owner.toLowerCase();
if (!ownerTypes.includes(owned)) {
error(`Invalid owner, allowed types are: ${ownerTypes.join(", ")}`);
}
const runMode = args["run-mode"].toLowerCase();
if (!runTypes.includes(runMode)) {
error(
`Invalid run-mode, allowed types are: ${runTypes.join(", ")}`,
);
}
// ensure that no more than 1 of list, list-countries, list-providers is set
const listCount = [args.list, args["list-countries"], args["list-providers"]]
.filter((e) => e).length;
if (listCount > 1) {
error("Only one of list, list-countries, list-providers can be set");
}
const provider = args.provider;
let fetchingSpinner: Spinner | undefined;
if (isInteractive) {
fetchingSpinner = new Spinner({
message: "Fetching currently available relays",
color: "cyan",
});
fetchingSpinner.start();
}
const response = await fetch(
`https://api.mullvad.net/www/relays/${serverType}/`,
);
if (fetchingSpinner) {
fetchingSpinner.stop();
console.log(green("✓"), "Fetched available relays");
console.log();
}
function checkRunMode(stboot: boolean, runMode: string) {
if (runMode === "all") {
return true;
} else if (runMode === "ram" && stboot === true) {
return true;
} else if (runMode === "disk" && stboot === false) {
return true;
}
return false;
}
function checkOwnership(owned: string, server: ServerData) {
if (owned === "all") {
return true;
} else if (owned === "mullvad" && server.owned) {
return true;
} else if (owned === "rented" && !server.owned) {
return true;
}
return false;
}
const json: Array<ServerData> = await response.json();
const servers = json.filter((server) =>
(country === undefined || country === server.country_code) &&
(server.network_port_speed >= portSpeed) &&
checkRunMode(server.stboot, runMode) &&
(provider === undefined || provider === server.provider) &&
checkOwnership(owned, server) &&
(args["include-inactive"] || server.active)
);
if (args["list-countries"]) {
const countries = new Map<string, string>();
json.forEach((server) => {
countries.set(server.country_code, server.country_name);
});
if (args.json) {
console.log(JSON.stringify(Object.fromEntries(countries), null, 2));
} else {
// sort by country code
const sortedCountries = Array.from(countries).sort((a, b) =>
a[0].localeCompare(b[0])
);
for (const country of sortedCountries) {
console.log(`${country[0]}: ${country[1]}`);
}
}
} else if (args["list-providers"]) {
const providers = new Set<string>();
json.forEach((server) => {
providers.add(server.provider);
});
if (args.json) {
console.log(JSON.stringify(Array.from(providers), null, 2));
} else {
providers.forEach((provider) => {
console.log(provider);
});
}
} else if (args.list) {
if (args.json) {
console.log(JSON.stringify(servers, null, 2));
} else {
for (const server of servers) {
console.log(
`${server.hostname}.mullvad.net, ${server.city_name}, ${server.country_name} (${server.network_port_speed} Gigabit ${server.type})`,
);
}
}
} else {
const results: ResultServerData[] = [];
for (const server of servers) {
let pingArgs = [];
if (Deno.build.os === "windows") {
pingArgs = ["-n", count.toString(), server.ipv4_addr_in];
} else {
pingArgs = [
"-c",
count.toString(),
"-i",
interval.toString(),
server.ipv4_addr_in,
];
}
const p = new Deno.Command(
"ping",
{
args: pingArgs,
stdout: "piped",
},
);
let pingSpinner: Spinner | undefined;
if (isInteractive) {
pingSpinner = new Spinner({
message: server.hostname,
color: "cyan",
});
pingSpinner.start();
}
const output = await p.output();
if (pingSpinner) {
pingSpinner.stop();
}
if (output.success) {
const stdout = new TextDecoder().decode(output.stdout);
if (Deno.build.os === "windows") {
// [all, min, avg, max, mdev]
const regex = /Average = (\d*)ms/;
const avg = stdout.match(regex);
if (avg) {
if (!args.json) {
console.log(
isInteractive ? green("✓") : "",
`${server.hostname}, avg ${avg[1]}ms`,
);
}
results.push({
...server,
avg: parseFloat(avg[1]) || 0,
});
}
} else {
// [all, min, avg, max, mdev]
const regex =
/(?<min>\d+(?:.\d+)?)\/(?<avg>\d+(?:.\d+)?)\/(?<max>\d+(?:.\d+)?)\/(?<mdev>\d+(?:.\d+)?)/;
const values = stdout.match(regex);
if (values) {
if (!args.json) {
console.log(
isInteractive ? green("✓") : "",
`${server.hostname}, min/avg/max/mdev ${values[0]}`,
);
}
results.push({
...server,
min: parseFloat(values[1]),
avg: parseFloat(values[2]),
max: parseFloat(values[3]),
mdev: parseFloat(values[4]),
});
}
}
} else {
if (!args.json) {
console.log(
isInteractive ? red("✗") : "",
`${server.hostname} (${server.ipv4_addr_in}), failed to ping`,
);
}
}
}
results.sort((a, b) => {
return a.avg - b.avg;
});
if (args.json) {
console.log(JSON.stringify(results, null, 2));
} else {
const top = topN === 0 ? results : results.slice(0, topN);
if (top.length > 0) {
const preMsg = `\n\nTop ${top.length} results (lower avg is better)\n`;
console.log(isInteractive ? bold(cyan(preMsg)) : preMsg);
const hostnames = top.map((e) => e.hostname);
const maxHostnameLength = Math.max(
"Hostname".length,
...hostnames.map((e) => e.length),
);
const avg = top.map((e) => `${e.avg.toFixed(1)}ms`);
const maxAvgLength = Math.max(
"Avg".length,
...avg.map((e) => e.length),
);
const speeds = top.map((e) => `${e.network_port_speed} Gbps`);
const maxSpeedLength = Math.max(
"Speed".length,
...speeds.map((e) => e.length),
);
const countries = top.map((e) => e.country_name);
const maxCountryLength = Math.max(
"Country".length,
...countries.map((e) => e.length),
);
const cities = top.map((e) => e.city_name);
const maxCityLength = Math.max(
"City".length,
...cities.map((e) => e.length),
);
const provider = top.map((e) => e.provider);
const maxProviderLength = Math.max(
"Provider".length,
...provider.map((e) => e.length),
);
const ownership = top.map((e) => e.owned ? "Owned by Mullvad" : "Rented");
const maxOwnershipLength = Math.max(
"Ownership".length,
...ownership.map((e) => e.length),
);
const header = [
"Hostname".padEnd(maxHostnameLength),
"Avg".padEnd(maxAvgLength),
"Speed".padEnd(maxSpeedLength),
"Country".padEnd(maxCountryLength),
"City".padEnd(maxCityLength),
"Provider".padEnd(maxProviderLength),
"Ownership".padEnd(maxOwnershipLength),
].join(" ");
console.log(isInteractive ? bold(header) : header);
console.log("─".repeat(header.length));
for (let i = 0; i < top.length; i++) {
console.log(
[
hostnames[i].padEnd(maxHostnameLength),
avg[i].padEnd(maxAvgLength),
speeds[i].padEnd(maxSpeedLength),
countries[i].padEnd(maxCountryLength),
cities[i].padEnd(maxCityLength),
provider[i].padEnd(maxProviderLength),
ownership[i].padEnd(maxOwnershipLength),
].join(" "),
);
}
} else {
error("No servers found");
}
}
}