From e90320fcb5f1e92b5e272d9e113796f20e167e3e Mon Sep 17 00:00:00 2001 From: Grant G Date: Thu, 23 Mar 2023 22:09:04 -0700 Subject: [PATCH] feat: add provider and owned flags (#7) --- .gitignore | 1 - .vscode/settings.json | 4 ++++ script.ts | 54 +++++++++++++++++++++++++++++-------------- 3 files changed, 41 insertions(+), 18 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index d47801d..6614107 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ -/.vscode mullvad-ping* Makefile diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..fdbe9bd --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "deno.enable": true, + "editor.defaultFormatter": "denoland.vscode-deno" +} diff --git a/script.ts b/script.ts index d67295f..a90d27a 100644 --- a/script.ts +++ b/script.ts @@ -1,4 +1,4 @@ -import { parse } from "https://deno.land/std@0.132.0/flags/mod.ts"; +import { parse } from "https://deno.land/std@0.181.0/flags/mod.ts"; type ServerDataJSON = { hostname: string; @@ -37,20 +37,26 @@ const runTypes = ["all", "ram", "disk"]; const args = parse(Deno.args); if (args.help == true) { console.log(`Usage: script [OPTION] - --country the country you want to query (eg. us, gb, de) - --list-countries lists the available countries - --type the type of server to query (${serverTypes.join(", ")}) - --count the number of pings to the server (default 3)`); + --country the country you want to query (eg. us, gb, de) + --list-countries lists the available countries + --type the type of server to query (${ + serverTypes.join(", ") + }) + --count the number of pings to the server (default 3)`); if (Deno.build.os != "windows") { console.log( - ` --interval the interval between pings in seconds (default/min 0.2)`, + ` --interval the interval between pings in seconds (default/min 0.2)`, ); } console.log( - ` --top the number of top servers to show, (0=all) - --port-speed only show servers with at least n Gigabit port speed - --run-mode only show servers running from (${runTypes.join(", ")}) - --help usage information`, + ` --top the number of top servers to show, (0=all) + --port-speed only show servers with at least n Gigabit port speed + --provider only show servers from the given provider + --owned only show servers owned by Mullvad + --run-mode only show servers running from (${ + runTypes.join(", ") + }) + --help usage information`, ); Deno.exit(0); } @@ -58,14 +64,12 @@ if (args.help == true) { const country = args.country; const serverType = args.type ?? "all"; if (!serverTypes.includes(serverType)) { - console.error(`Invalid type, allowed types are: ${serverTypes.join(", ")}`); - Deno.exit(1); + throw new Error(`Invalid type, allowed types are: ${serverTypes.join(", ")}`); } const interval = parseFloat(args.interval ?? 0.2) || 0.2; if (interval < 0.2) { - console.error("Minimum interval value is 0.2"); - Deno.exit(1); + throw new Error("Minimum interval value is 0.2"); } const count = parseInt(args.count ?? 5) || 5; const topN = parseInt(args.top ?? 5) || 5; @@ -73,10 +77,24 @@ const portSpeed = parseInt(args["port-speed"] ?? 0) || 0; const runMode = args["run-mode"] ?? "all"; if (!runTypes.includes(runMode)) { - console.error(`Invalid run-mode, allowed types are: ${runTypes.join(", ")}`); - Deno.exit(1); + throw new Error( + `Invalid run-mode, allowed types are: ${runTypes.join(", ")}`, + ); +} + +let owned: boolean | null = null; +if (args.owned != null) { + if (args.owned == "true") { + owned = true; + } else if (args.owned == "false") { + owned = false; + } else { + throw new Error("Invalid value for owned, must be true or false"); + } } +const provider = args.provider; + console.log("Fetching currently available relays..."); const response = await fetch( `https://api.mullvad.net/www/relays/${serverType}/`, @@ -98,7 +116,9 @@ if (args["list-countries"]) { if ( (country == null || country == server.country_code) && (server.network_port_speed >= portSpeed) && - checkRunMode(server.stboot, runMode) + checkRunMode(server.stboot, runMode) && + (provider == null || provider == server.provider) && + (owned == null || owned == server.owned) ) { let cmd = []; if (Deno.build.os == "windows") {