Skip to content

Commit

Permalink
feat: add provider and owned flags (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
grant0417 authored Mar 24, 2023
1 parent d964217 commit e90320f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
/.vscode
mullvad-ping*
Makefile
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"deno.enable": true,
"editor.defaultFormatter": "denoland.vscode-deno"
}
54 changes: 37 additions & 17 deletions script.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -37,46 +37,64 @@ const runTypes = ["all", "ram", "disk"];
const args = parse(Deno.args);
if (args.help == true) {
console.log(`Usage: script [OPTION]
--country <code> the country you want to query (eg. us, gb, de)
--list-countries lists the available countries
--type <type> the type of server to query (${serverTypes.join(", ")})
--count <n> the number of pings to the server (default 3)`);
--country <code> the country you want to query (eg. us, gb, de)
--list-countries lists the available countries
--type <type> the type of server to query (${
serverTypes.join(", ")
})
--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)`,
` --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)
--port-speed <n> only show servers with at least n Gigabit port speed
--run-mode <type> only show servers running from (${runTypes.join(", ")})
--help usage information`,
` --top <n> the number of top servers to show, (0=all)
--port-speed <n> only show servers with at least n Gigabit port speed
--provider <name> only show servers from the given provider
--owned <true|false> only show servers owned by Mullvad
--run-mode <type> only show servers running from (${
runTypes.join(", ")
})
--help usage information`,
);
Deno.exit(0);
}

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;
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}/`,
Expand All @@ -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") {
Expand Down

0 comments on commit e90320f

Please sign in to comment.