@mytmpvpn/mytmpvpn-cli 1.5.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,335 @@
1
+
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ // Use NODE_NO_WARNINGS=1 to get rid of the fetch node deprecated API
5
+ // https://github.com/netlify/cli/issues/4608 -- but it hangs the process: https://github.com/nodejs/node/issues/21960
6
+ const fs = require("fs");
7
+ const path = require("path");
8
+ const commander_1 = require("commander");
9
+ const log = require("loglevel");
10
+ log.setDefaultLevel("info");
11
+ const vpnlib = require("@mytmpvpn/mytmpvpn-common/models/vpn");
12
+ const peanuts = require("@mytmpvpn/mytmpvpn-common/models/peanuts");
13
+ const appconfig_1 = require("@mytmpvpn/mytmpvpn-client/appconfig");
14
+ const userconfig_1 = require("@mytmpvpn/mytmpvpn-client/userconfig");
15
+ const auth = require("@mytmpvpn/mytmpvpn-client/auth");
16
+ const client_1 = require("@mytmpvpn/mytmpvpn-client/client");
17
+ const program = new commander_1.Command();
18
+ function handleError(error, verbose = false) {
19
+ if (error.response) {
20
+ // The request was made and the server responded with a status code
21
+ // that falls out of the range of 2xx
22
+ try {
23
+ log.error(`[${error.response.status}] - ${JSON.stringify(error.response.data)}`);
24
+ }
25
+ catch (Error) {
26
+ log.error(`[${error.response.status}] - ${JSON.stringify(error.message)}`);
27
+ }
28
+ log.debug(error.response.headers);
29
+ }
30
+ else if (error.request) {
31
+ // The request was made but no response was received
32
+ // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
33
+ // http.ClientRequest in node.js
34
+ log.error(`No reponse received. Check your profile and your appConfig`);
35
+ }
36
+ else {
37
+ // Something happened in setting up the request that triggered an Error
38
+ log.error(`Error while setting up the request ${JSON.stringify(error)}`);
39
+ }
40
+ log.trace(`Stack trace: ${error}`);
41
+ }
42
+ program
43
+ .name('mytmpvpn-cli')
44
+ .description('MyTmpVpn CLI')
45
+ .version('0.0.1')
46
+ .option('--verbose', 'Produce more logs')
47
+ .option('--appConfig <file>', 'Path to the application config file', (0, appconfig_1.getDefaultAppConfigFile)())
48
+ .option('--userConfig <file>', 'Path to the user config file', (0, userconfig_1.getDefaultUserConfigFile)())
49
+ .option('--profile <name>', 'Name of the profile in the user config file to use', (0, userconfig_1.getDefaultUserProfile)());
50
+ program.on('option:verbose', function () {
51
+ log.setDefaultLevel("trace");
52
+ });
53
+ program.command('list-peanuts-packs')
54
+ .description(`Returns the list of peanuts packs you can purchase. A peanuts pack contains a given number of peanuts. You need a minimum of ${peanuts.PEANUTS_CONFIG.min} peanuts to create a vpn`)
55
+ .action((_, command) => {
56
+ const options = command.optsWithGlobals();
57
+ const appConfig = (0, appconfig_1.loadAppConfig)(options.appConfig);
58
+ // We don't need authenticated user to call this API
59
+ const client = new client_1.MyTmpVpnClient(appConfig.apiUrl);
60
+ client.listPeanutsPacks()
61
+ .then(((packs) => {
62
+ log.info(JSON.stringify(packs, null, 2));
63
+ }))
64
+ .catch((err) => handleError(err));
65
+ });
66
+ program.command('get-peanuts-balance')
67
+ .description('Get the current peanuts balance')
68
+ .action((_, command) => {
69
+ const options = command.optsWithGlobals();
70
+ log.debug(`Get peanuts balance`);
71
+ auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
72
+ if (err) {
73
+ handleError(err, options.verbose);
74
+ return;
75
+ }
76
+ client.getPeanutsBalance().then(balance => {
77
+ log.info(balance);
78
+ }).catch(err => {
79
+ handleError(err);
80
+ });
81
+ });
82
+ });
83
+ program.command('list-regions')
84
+ .description('Returns the list of all regions where vpn can be created')
85
+ .action((_, command) => {
86
+ const options = command.optsWithGlobals();
87
+ const appConfig = (0, appconfig_1.loadAppConfig)(options.appConfig);
88
+ // We don't need authenticated user to call this API
89
+ const client = new client_1.MyTmpVpnClient(appConfig.apiUrl);
90
+ client.listRegions()
91
+ .then(((regions) => {
92
+ log.info(JSON.stringify(regions, null, 2));
93
+ }))
94
+ .catch((err) => handleError(err));
95
+ });
96
+ program.command('create')
97
+ .description('Create a new vpn')
98
+ .argument('<region>', 'region where the vpn should be created, as returned by list-regions')
99
+ .option('--sync', 'wait for vpn creation completion')
100
+ .addOption(new commander_1.Option('--type <type>', 'Type of VPN')
101
+ .choices(vpnlib.getVpnConfigTypes())
102
+ .default(vpnlib.VpnType.WireGuard))
103
+ .option('--peanuts <nb>', 'Max number of peanuts to use (specify -1 for maximum)', '-1')
104
+ .action((region, _, command) => {
105
+ const options = command.optsWithGlobals();
106
+ const syncStr = options.sync ? "synchronously" : "asynchronously";
107
+ log.debug(`Creating new ${options.type} vpn into ${region} ${syncStr}`);
108
+ auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
109
+ if (err) {
110
+ handleError(err, options.verbose);
111
+ return;
112
+ }
113
+ client.createVpn(region, { type: options.type, maxPeanuts: options.peanuts }).then(vpn => {
114
+ if (!options.sync) {
115
+ log.info(vpn);
116
+ return;
117
+ }
118
+ client.waitUntilVpnStateIs(vpn.vpnId, vpnlib.VpnState.Running).then(updatedVpn => {
119
+ log.info(updatedVpn);
120
+ }).catch(err => {
121
+ handleError(err);
122
+ });
123
+ }).catch(err => {
124
+ handleError(err);
125
+ });
126
+ });
127
+ });
128
+ program.command('delete')
129
+ .description('Delete a vpn')
130
+ .argument('<vpnId>', 'vpnId to delete')
131
+ .option('--sync', 'wait for vpn deletion completion')
132
+ .action((vpnId, _, command) => {
133
+ const options = command.optsWithGlobals();
134
+ const syncStr = options.sync ? "synchronously" : "asynchronously";
135
+ log.debug(`Deleting vpn ${vpnId} ${syncStr}`);
136
+ auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
137
+ if (err) {
138
+ handleError(err.message || JSON.stringify(err));
139
+ return;
140
+ }
141
+ client.deleteVpn(vpnId).then(vpn => {
142
+ if (!options.sync) {
143
+ log.info(JSON.stringify(vpn));
144
+ return;
145
+ }
146
+ client.waitUntilVpnStateIs(vpnId, vpnlib.VpnState.Deleted).then(updatedVpn => {
147
+ log.info(updatedVpn);
148
+ return;
149
+ }).catch(err => {
150
+ handleError(err.response?.data || JSON.stringify(err));
151
+ });
152
+ }).catch(err => {
153
+ handleError(err.response?.data || JSON.stringify(err));
154
+ });
155
+ });
156
+ });
157
+ program.command('get')
158
+ .description('Get information on a vpn')
159
+ .argument('<vpnId>', 'vpnId to get information from')
160
+ .action((vpnId, _, command) => {
161
+ const options = command.optsWithGlobals();
162
+ auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
163
+ if (err) {
164
+ handleError(err);
165
+ return;
166
+ }
167
+ client.getVpn(vpnId)
168
+ .then(result => log.info(result))
169
+ .catch(err => handleError(err));
170
+ });
171
+ });
172
+ program.command('download-config')
173
+ .description('Download configuration of the given vpn to the given file')
174
+ .argument('<vpnId>', 'vpnId to get config file from')
175
+ .option('--file <file>', 'file where the config should be downloaded to. Default is <vpnId>.tar.gz')
176
+ .option('--path <path>', 'path where the config file should be written to', (0, userconfig_1.getDefaultUserConfigDir)())
177
+ .action((vpnId, _, command) => {
178
+ const options = command.optsWithGlobals();
179
+ const file = options.file ? options.file : `${vpnId}.tar.gz`;
180
+ const fullpath = path.join(options.path, file);
181
+ auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
182
+ if (err) {
183
+ handleError(err);
184
+ return;
185
+ }
186
+ client.writeVpnConfigTo(vpnId, fs.createWriteStream(fullpath))
187
+ .then(() => log.info(fullpath))
188
+ .catch(err => handleError(err));
189
+ });
190
+ });
191
+ program.command('list')
192
+ .description('List all vpns')
193
+ .option('--region <region>', 'region to list vpns from')
194
+ .option('--exclude-state <state>', 'state to exclude from the list', 'DELETED')
195
+ .action((_, command) => {
196
+ const options = command.optsWithGlobals();
197
+ auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
198
+ if (err) {
199
+ handleError(err);
200
+ return;
201
+ }
202
+ client.listVpns(options.state)
203
+ .then(vpns => {
204
+ log.info(JSON.stringify(vpns, null, 2));
205
+ })
206
+ .catch(err => handleError(err));
207
+ });
208
+ });
209
+ program.command('wait')
210
+ .description('Wait for vpn operation completion')
211
+ .argument('<vpnId>', 'the vpnId to wait a status change for')
212
+ .argument('<state>', 'the state to wait for')
213
+ .action((vpnId, state, _, command) => {
214
+ const options = command.optsWithGlobals();
215
+ const actualState = vpnlib.VpnState[state];
216
+ if (!actualState) {
217
+ handleError(`Unknown state: ${state}. Valid states: ${Object.values(vpnlib.VpnState)}`);
218
+ return;
219
+ }
220
+ log.debug(`Waiting for ${vpnId} state to be (at least) ${state}`);
221
+ auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
222
+ if (err) {
223
+ handleError(err);
224
+ return;
225
+ }
226
+ client.waitUntilVpnStateIs(vpnId, actualState)
227
+ .then(vpn => {
228
+ log.info(vpn);
229
+ })
230
+ .catch(err => handleError(err));
231
+ });
232
+ });
233
+ program.command('register')
234
+ .description('Register a new user to the MyTmpVpn application')
235
+ .argument('<username>', 'the email/phone that will identify your account')
236
+ .argument('<password>', 'a password')
237
+ .action((username, password, _, command) => {
238
+ const options = command.optsWithGlobals();
239
+ var userConfig;
240
+ if (fs.existsSync(options.userConfig)) {
241
+ userConfig = (0, userconfig_1.loadUserConfig)(options.userConfig);
242
+ if (userConfig.profiles[options.profile]) {
243
+ handleError(`Profile ${options.profile} already exists in ${options.userConfig}, specify another profile name using --profile`);
244
+ return;
245
+ }
246
+ userConfig.profiles[options.profile] = {
247
+ username: username,
248
+ password: password
249
+ };
250
+ }
251
+ else {
252
+ userConfig = {
253
+ version: 1,
254
+ profiles: {
255
+ default: {
256
+ username: username,
257
+ password: password
258
+ }
259
+ }
260
+ };
261
+ }
262
+ auth.registerUserFromFiles(options.appConfig, username, password, (err, result) => {
263
+ if (err) {
264
+ handleError(err);
265
+ return;
266
+ }
267
+ log.info(`Please confirm your identity with the code sent to ${username}`);
268
+ fs.mkdir(path.dirname(options.userConfig), { recursive: true }, (err, path) => {
269
+ if (err) {
270
+ handleError(err);
271
+ return;
272
+ }
273
+ log.debug(`Directory: ${path} created`);
274
+ fs.writeFile(options.userConfig, JSON.stringify(userConfig, null, 2), (err) => {
275
+ if (err) {
276
+ handleError(err);
277
+ return;
278
+ }
279
+ });
280
+ log.info(`A new profile has been created for ${options.profile} in ${options.userConfig}`);
281
+ });
282
+ });
283
+ });
284
+ program.command('confirm-registration')
285
+ .description('Confirm registration to the MyTmpVpn application using the code that was sent to email/phone')
286
+ .argument('<username>', 'the email/phone that identify your account')
287
+ .argument('<code>', 'the code received')
288
+ .action((username, code, _, command) => {
289
+ const options = command.optsWithGlobals();
290
+ auth.confirmUserFromFiles(options.appConfig, username, code, (err, result) => {
291
+ if (err) {
292
+ handleError(err);
293
+ return;
294
+ }
295
+ log.debug(result);
296
+ log.info(`User ${username} confirmed!`);
297
+ });
298
+ });
299
+ program.addHelpText('after', `
300
+
301
+ Examples:
302
+
303
+ # Before doing anything, you need to register to the MyTmpVpn service:
304
+
305
+ $ mytmpvpn register 'your@email.com' 'StrongPassword' # Min: 8 characters with digit, lower, upper and symbols
306
+ $ mytmpvpn confirm-registration 'your@email.com' 'code' # The code you've received by email
307
+
308
+ # You then need to buy some peanuts, the list of peanuts packs can be fetched using:
309
+
310
+ $ mytmpvpn list-peanuts-packs
311
+
312
+ # Visit one of those URLs returned, and complete the purchase.
313
+ # There is no CLI for purchasing peanuts-packs.
314
+ # Once you have purchased some peanuts, your peanuts balance should show it:
315
+
316
+ $ mytmpvpn get-peanuts-balance
317
+
318
+ # Then you can use the following command at will:
319
+
320
+ # List available regions:
321
+ $ mytmpvpn list-regions
322
+
323
+ # Create a new vpn in Paris:
324
+ $ mytmpvpn create 'paris' --sync # This takes several minutes, please be patient or remove --sync
325
+
326
+ # List all my vpns:
327
+ $ mytmpvpn list
328
+
329
+ # Fetch the configuration of a given vpn
330
+ $ mytmpvpn download-config 'vpn-id' # The vpn-id is provided by the list command
331
+
332
+ # Delete a given vpn
333
+ $ mytmpvpn delete 'vpn-id' # The vpn-id is provided by the list command
334
+ `);
335
+ program.parse();
package/dist/mytmpvpn.js CHANGED
@@ -93,19 +93,6 @@ program.command('list-regions')
93
93
  }))
94
94
  .catch((err) => handleError(err));
95
95
  });
96
- program.command('list-regions-detailed')
97
- .description('Returns the detailed list of all regions where vpn can be created')
98
- .action((_, command) => {
99
- const options = command.optsWithGlobals();
100
- const appConfig = (0, appconfig_1.loadAppConfig)(options.appConfig);
101
- // We don't need authenticated user to call this API
102
- const client = new client_1.MyTmpVpnClient(appConfig.apiUrl);
103
- client.listRegionsDetailed()
104
- .then(((regions) => {
105
- log.info(JSON.stringify(regions, null, 2));
106
- }))
107
- .catch((err) => handleError(err));
108
- });
109
96
  program.command('create')
110
97
  .description('Create a new vpn')
111
98
  .argument('<region>', 'region where the vpn should be created, as returned by list-regions')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mytmpvpn/mytmpvpn-cli",
3
- "version": "1.5.0",
3
+ "version": "1.7.0",
4
4
  "description": "MyTmpVpn CLI",
5
5
  "main": "./dist/mytmpvpn.js",
6
6
  "bin": {
@@ -27,8 +27,8 @@
27
27
  "dependencies": {
28
28
  "commander": "^9.4.1",
29
29
  "loglevel": "^1.8.1",
30
- "@mytmpvpn/mytmpvpn-client": "^1.6.2",
31
- "@mytmpvpn/mytmpvpn-common": "^1.4.0"
30
+ "@mytmpvpn/mytmpvpn-client": "^2.2.0",
31
+ "@mytmpvpn/mytmpvpn-common": "^2.1.0"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/jest": "^29.2.2",