-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
108 lines (103 loc) · 2.69 KB
/
index.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
#!/usr/bin/env node
const axios = require("axios").default
const program = require("commander")
const Conf = require("conf")
const config = new Conf()
import chalk from "chalk"
program.version("1.0.7")
program
.option("-i, --country-id <country-id>", "Get NordVPN server for country ID")
.option("-s, --set-default-id <country-id>", "Set default country ID")
.option(
"-r, --remove-default-id",
"Removes default country ID. Will get closest server instead."
)
.option("-v, --verbose", "Verbose")
program.parse(process.argv)
const options = program.opts()
;(async () => {
if (options.setDefaultId) {
config.set("countryId", options.setDefaultId)
console.log(`Default country ID set to: ${options.setDefaultId}`)
} else if (options.removeDefaultId) {
config.delete("countryId")
console.log("Removed country ID. Will now get closest server instead. ")
} else {
try {
let nordvpnResult
let countryId = config.get("countryId")
countryId = options.countryId
if (!countryId) {
countryId = "fi"
}
let countryCodeNumber = getCountryCodeNumber(countryId)
let url = `https://nordvpn.com/wp-admin/admin-ajax.php?action=servers_recommendations&filters={%22country_id%22:${countryCodeNumber},%22servers_groups%22:[15]}`
nordvpnResult = await axios.get(url)
if (nordvpnResult.data[0].hostname) {
let { hostname, name, load, status } = nordvpnResult
.data[0] as ServerData
console.log(`Hostname: ${chalk.blue(hostname)}`)
console.log(`Name: ${chalk.magenta(name)}`)
console.log(`Load: ${chalk.green(load)}`)
console.log(`Status: ${chalk.green(status)}`)
} else {
console.log("Cannot get NordVPN address. Try again later. Sorry :(")
}
} catch (e) {
console.log(e)
console.log(
"Connection error. Cannot get NordVPN address. Try again later. Sorry :("
)
}
}
})()
interface ServerData {
hostname: string
name: string
load: number
status: string
}
function getCountryCodeNumber(countryId) {
switch (countryId) {
case "fi":
return 73
case "us":
return 228
case "se":
return 208
case "no":
return 163
case "dk":
return 58
case "uk":
return 227
case "nl":
return 153
case "de":
return 81
case "it":
return 106
case "es":
return 202
case "pt":
return 175
case "fr":
return 74
case "za":
return 200
case "au":
return 13
case "br":
return 30
case "ca":
return 38
case "hk":
return 97
case "jp":
return 108
case "tr":
return 220
default:
return 73
}
}