Skip to content

Commit

Permalink
Added Windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
grant0417 committed Jun 8, 2021
1 parent 32545ba commit f82f0fe
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.vscode
mullvad-ping
mullvad-ping*
Makefile
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Mullvad Ping

Gets the list of Mullvad servers with the best latency according to `ping`.
To use either download the Linux executable or run using Deno
`deno run --allow-net --allow-run script.ts`.
Gets the list of Mullvad servers with the best latency according to `ping`. To
use either download the executable or run using Deno
`deno run --allow-net --allow-run script.ts`. To generate an executable run
`deno compile --allow-net --allow-run -o mullvad-ping script.ts`.

To generate an executable run `deno compile --allow-net --allow-run -o mullvad-ping script.ts`.
Note: The Windows version of `ping` is somewhat more limited than that of Linux or
Mac so the times are less precice and the script will take ~5x longer.

## Usage

Expand Down
102 changes: 72 additions & 30 deletions script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ if (args.help == true) {
--country <code> the country you want to quary (eg. us, gb, de)
--list-countries lists the avaiable countries
--type <type> the type of server to quary (${serverTypes.join(", ")})
--count <n> the number of pings to the server (default 3)
--interval <i> the interval between pings in seconds (default/min 0.2)
--top <n> the number of top servers to show, (0=all)
--help usage infromation`);
--count <n> the number of pings to the server (default 3)`);
if (Deno.build.os != "windows") {
console.log(
` --interval <i> the interval between pings in seconds (default/min 0.2)`,
);
}
console.log(
` --top <n> the number of top servers to show, (0=all)
--help usage infromation`,
);
Deno.exit(0);
}

Expand Down Expand Up @@ -69,43 +75,79 @@ if (args["list-countries"]) {
if (
(country == null || country == server.country_code)
) {
const p = Deno.run({
cmd: [
let cmd = [];
if (Deno.build.os == "windows") {
cmd = [
"ping",
"-n",
count.toString(),
server.ipv4_addr_in,
];
} else {
cmd = [
"ping",
"-c",
count.toString(),
"-i",
interval.toString(),
server.ipv4_addr_in,
],
];
}

const p = Deno.run({
cmd,
stdout: "piped",
});

const output = new TextDecoder().decode(await p.output());

// [all, min, avg, max, mdev]
const regex =
/(?<min>\d+(?:.\d+)?)\/(?<avg>\d+(?:.\d+)?)\/(?<max>\d+(?:.\d+)?)\/(?<mdev>\d+(?:.\d+)?)/;

const values = output.match(regex);
if (values) {
console.log(
`Pinged ${server.hostname}.mullvad.net, min/avg/max/mdev ${
values[0]
}`,
);

results.push({
hostname: server.hostname,
city: server.city_name,
country: server.country_name,
type: server.type,
ip: server.ipv4_addr_in,
avg: parseFloat(values[2]) || 0,
});
if (Deno.build.os == "windows") {
// [all, min, avg, max, mdev]
const regex = /Average = (\d*)ms/
const avg = output.match(regex);
if (avg) {
console.log(
`Pinged ${server.hostname}.mullvad.net, avg ${
avg[1]
}ms`,
);

results.push({
hostname: server.hostname,
city: server.city_name,
country: server.country_name,
type: server.type,
ip: server.ipv4_addr_in,
avg: parseFloat(avg[1]) || 0,
});
}

await sleep(200);
} else {
// [all, min, avg, max, mdev]
const regex =
/(?<min>\d+(?:.\d+)?)\/(?<avg>\d+(?:.\d+)?)\/(?<max>\d+(?:.\d+)?)\/(?<mdev>\d+(?:.\d+)?)/;

const values = output.match(regex);
if (values) {
console.log(
`Pinged ${server.hostname}.mullvad.net, min/avg/max/mdev ${
values[0]
}`,
);

results.push({
hostname: server.hostname,
city: server.city_name,
country: server.country_name,
type: server.type,
ip: server.ipv4_addr_in,
avg: parseFloat(values[2]) || 0,
});
}

await sleep(interval * 1000);
}

await sleep(interval * 1000);
}
}

Expand All @@ -125,7 +167,7 @@ if (args["list-countries"]) {
}ms) ${e.type} ${e.city}, ${e.country}`,
);
}
console.table()
console.table();
} else {
console.error("No servers found");
}
Expand Down

0 comments on commit f82f0fe

Please sign in to comment.