Skip to content

Commit e90320f

Browse files
authored
feat: add provider and owned flags (#7)
1 parent d964217 commit e90320f

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
/.vscode
21
mullvad-ping*
32
Makefile

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"deno.enable": true,
3+
"editor.defaultFormatter": "denoland.vscode-deno"
4+
}

script.ts

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parse } from "https://deno.land/std@0.132.0/flags/mod.ts";
1+
import { parse } from "https://deno.land/std@0.181.0/flags/mod.ts";
22

33
type ServerDataJSON = {
44
hostname: string;
@@ -37,46 +37,64 @@ const runTypes = ["all", "ram", "disk"];
3737
const args = parse(Deno.args);
3838
if (args.help == true) {
3939
console.log(`Usage: script [OPTION]
40-
--country <code> the country you want to query (eg. us, gb, de)
41-
--list-countries lists the available countries
42-
--type <type> the type of server to query (${serverTypes.join(", ")})
43-
--count <n> the number of pings to the server (default 3)`);
40+
--country <code> the country you want to query (eg. us, gb, de)
41+
--list-countries lists the available countries
42+
--type <type> the type of server to query (${
43+
serverTypes.join(", ")
44+
})
45+
--count <n> the number of pings to the server (default 3)`);
4446
if (Deno.build.os != "windows") {
4547
console.log(
46-
` --interval <i> the interval between pings in seconds (default/min 0.2)`,
48+
` --interval <i> the interval between pings in seconds (default/min 0.2)`,
4749
);
4850
}
4951
console.log(
50-
` --top <n> the number of top servers to show, (0=all)
51-
--port-speed <n> only show servers with at least n Gigabit port speed
52-
--run-mode <type> only show servers running from (${runTypes.join(", ")})
53-
--help usage information`,
52+
` --top <n> the number of top servers to show, (0=all)
53+
--port-speed <n> only show servers with at least n Gigabit port speed
54+
--provider <name> only show servers from the given provider
55+
--owned <true|false> only show servers owned by Mullvad
56+
--run-mode <type> only show servers running from (${
57+
runTypes.join(", ")
58+
})
59+
--help usage information`,
5460
);
5561
Deno.exit(0);
5662
}
5763

5864
const country = args.country;
5965
const serverType = args.type ?? "all";
6066
if (!serverTypes.includes(serverType)) {
61-
console.error(`Invalid type, allowed types are: ${serverTypes.join(", ")}`);
62-
Deno.exit(1);
67+
throw new Error(`Invalid type, allowed types are: ${serverTypes.join(", ")}`);
6368
}
6469

6570
const interval = parseFloat(args.interval ?? 0.2) || 0.2;
6671
if (interval < 0.2) {
67-
console.error("Minimum interval value is 0.2");
68-
Deno.exit(1);
72+
throw new Error("Minimum interval value is 0.2");
6973
}
7074
const count = parseInt(args.count ?? 5) || 5;
7175
const topN = parseInt(args.top ?? 5) || 5;
7276
const portSpeed = parseInt(args["port-speed"] ?? 0) || 0;
7377

7478
const runMode = args["run-mode"] ?? "all";
7579
if (!runTypes.includes(runMode)) {
76-
console.error(`Invalid run-mode, allowed types are: ${runTypes.join(", ")}`);
77-
Deno.exit(1);
80+
throw new Error(
81+
`Invalid run-mode, allowed types are: ${runTypes.join(", ")}`,
82+
);
83+
}
84+
85+
let owned: boolean | null = null;
86+
if (args.owned != null) {
87+
if (args.owned == "true") {
88+
owned = true;
89+
} else if (args.owned == "false") {
90+
owned = false;
91+
} else {
92+
throw new Error("Invalid value for owned, must be true or false");
93+
}
7894
}
7995

96+
const provider = args.provider;
97+
8098
console.log("Fetching currently available relays...");
8199
const response = await fetch(
82100
`https://api.mullvad.net/www/relays/${serverType}/`,
@@ -98,7 +116,9 @@ if (args["list-countries"]) {
98116
if (
99117
(country == null || country == server.country_code) &&
100118
(server.network_port_speed >= portSpeed) &&
101-
checkRunMode(server.stboot, runMode)
119+
checkRunMode(server.stboot, runMode) &&
120+
(provider == null || provider == server.provider) &&
121+
(owned == null || owned == server.owned)
102122
) {
103123
let cmd = [];
104124
if (Deno.build.os == "windows") {

0 commit comments

Comments
 (0)