diff --git a/CHANGELOG.md b/CHANGELOG.md index 7090358..97529af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,4 +16,14 @@ * updateAddress(clientId, address) - Update wg-easy client address ### Fix -* rename(clientId, newName) - now it is not possible to take an existing name \ No newline at end of file +* rename(clientId, newName) - now it is not possible to take an existing name + +# 1.0.3 (2023-07-20) + +### Fix + +* rename(clientId, newName) - Added a missed "await" + +### Update + +* Reworked catching errors. Now, in case of errors, you get an object with the variable "error" \ No newline at end of file diff --git a/README.md b/README.md index c075b3a..6a17a79 100644 --- a/README.md +++ b/README.md @@ -10,47 +10,37 @@ Install it from npm: npm install wg-easy-wrapper ``` +# Error Handling + +When a request encounters an error, the response will contain an object with the variable "**error**" This object will provide relevant information about the error +Example: `{ error: 'The name wg-easy is already taken' }` + # Usage -### Create a new WGEasyWrapper object +### Create a new instance of the WGEasyWrapper class ``` const WGEasyWrapper = require('wg-easy-wrapper'); -const WGEW = new WGEasyWrapper('https://website.com', 'password_example'); +const wgEasy = new WGEasyWrapper('https://website.com', 'password_example'); ``` ### Getting the release version ``` -const response = await WGEW.getRelease(); -console.log(response); -``` - -``` -7 +await wgEasy.getRelease(); +// 7 ``` ### Getting a session ``` -const response = await WGEW.getSession(); -console.log(response); -``` - -``` -{ - requiresPassword: true, - authenticated: true -} +await wgEasy.getSession(); +// { requiresPassword: true, authenticated: true } ``` ### Creating a wg-easy client ``` -const response = await WGEW.create('newClient'); -console.log(response); -``` - -``` -{ +await wgEasy.create('newClient'); +/*{ id: '721f6ac2-ab4f-2fz2-v1b4-2gtc93462099', name: 'newClient', enabled: true, @@ -62,32 +52,20 @@ console.log(response); latestHandshakeAt: null, transferRx: 0, transferTx: 0 -} - -Or - -Сlient with name newClient already exists +}*/ ``` ### Deleting a wg-easy client ``` -const response = await WGEW.delete('721f6ac2-ab4f-2fz2-v1b4-2gtc93462099'); -console.log(response); -``` - -``` -true +await wgEasy.delete('721f6ac2-ab4f-2fz2-v1b4-2gtc93462099'); +// { message: 'Deleted' } ``` ### Getting wg-easy clients ``` -const response = await WGEW.getClients(); -console.log(response); -``` - -``` -[ +await wgEasy.getClients(); +/*[ { id: 'f2t3bdbh-b340-4e7d-62f7-651a0122bc62', name: 'testName', @@ -104,17 +82,14 @@ console.log(response); { ... } -] +]*/ ``` ### Getting wg-easy client configuration ``` -const response = await WGEW.getConfig('f2t3bdbh-b340-4e7d-62f7-651a0122bc62'); -console.log(response); -``` - -``` +await wgEasy.getConfig('f2t3bdbh-b340-4e7d-62f7-651a0122bc62'); +/* [Interface] PrivateKey = ... Address = 10.8.0.0/24 @@ -127,80 +102,51 @@ PresharedKey = ... AllowedIPs = 0.0.0.0/0, ::/0 PersistentKeepalive = 0 Endpoint = website.com:51820 +*/ ``` ### Getting wg-easy client qr-code ``` -const response = await WGEW.getQRCode('f2t3bdbh-b340-4e7d-62f7-651a0122bc62'); -console.log(response); -``` - -``` +await wgEasy.getQRCode('f2t3bdbh-b340-4e7d-62f7-651a0122bc62'); +/* +*/ ``` ### Enable wg-easy client ``` -const response = await WGEW.enable('f2t3bdbh-b340-4e7d-62f7-651a0122bc62'); -console.log(response); -``` - -``` -true +await wgEasy.enable('f2t3bdbh-b340-4e7d-62f7-651a0122bc62'); +// { message: 'Enabled' } ``` ### Disable wg-easy client ``` -const response = await WGEW.disable('f2t3bdbh-b340-4e7d-62f7-651a0122bc62'); -console.log(response); -``` - -``` -true +await wgEasy.disable('f2t3bdbh-b340-4e7d-62f7-651a0122bc62'); +// // { message: 'Disabled' } ``` ### Rename wg-easy client ``` -const response = await WGEW.rename('f2t3bdbh-b340-4e7d-62f7-651a0122bc62', 'newName'); -console.log(response); -``` - -``` -The name newName is already taken - -Or - -true +await wgEasy.rename('f2t3bdbh-b340-4e7d-62f7-651a0122bc62', 'newName'); +// { message: 'Renamed' } ``` ### Update wg-easy client address ``` -const response = await WGEW.updateAddress('f2t3bdbh-b340-4e7d-62f7-651a0122bc62', '10.8.0.1'); -console.log(response); -``` - -``` -Address is already occupied - -Or - -true +await wgEasy.updateAddress('f2t3bdbh-b340-4e7d-62f7-651a0122bc62', '10.8.0.1'); +// { message: 'Address updated' } ``` ### Find wg-easy client ``` -const response = await WGEW.find('newName'); || const response = await WGEW.find('f2t3bdbh-b340-4e7d-62f7-651a0122bc62'); -console.log(response); -``` - -``` -{ +await wgEasy.find('newName'); // or id or addres +/*{ id: 'f2t3bdbh-b340-4e7d-62f7-651a0122bc62', name: 'newName', enabled: true, @@ -212,11 +158,7 @@ console.log(response); latestHandshakeAt: null, transferRx: 0, transferTx: 0 -} - -Or - -Client not found +}*/ ``` # License diff --git a/index.js b/index.js index bb2b0ab..61f1b88 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,8 @@ const axios = require('axios'); +const idRegex = /.+-.{4}-.{4}-.{4}-.+/; +const addressRegex = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/; + /** * WGEasyWrapper class */ @@ -36,7 +39,7 @@ class WGEasyWrapper { const { status } = error.response; const originalRequest = error.config; - if (status !== 401) throw new Error(error); + if (status !== 401) return Promise.reject(error); try { if (authRetryCount < maxAuthRetries) { @@ -55,12 +58,15 @@ class WGEasyWrapper { /** * Getting the release version - * @returns {Promise} + * @returns {Promise} * @async */ async getRelease() { - const { data } = await this.api.get('/api/release'); - return data; + return await this.api.get('/api/release').then(res => { + return res.data; + }).catch(err => { + return err.response.data; + }); } /** @@ -69,104 +75,131 @@ class WGEasyWrapper { * @async */ async getSession() { - const { data } = await this.api.get('/api/session'); - return data; + return await this.api.get('/api/session').then(res => { + return res.data; + }).catch(err => { + return err.response.data; + }); } /** * Getting wg-easy clients - * @returns {Promise} + * @returns {Promise} * @async */ async getClients() { - const { data } = await this.api.get('/api/wireguard/client/'); - return data; + return await this.api.get('/api/wireguard/client/').then(res => { + return res.data; + }).catch(err => { + return err.response.data; + }); } /** * Getting wg-easy client configuration * @param {string} clientId - client id. It may look like f2t3bdbh-b340-4e7d-62f7-651a0122bc62 - * @returns {Promise} + * @returns {Promise} * @async */ async getConfig(clientId) { - const { data } = await this.api.get(`/api/wireguard/client/${clientId}/configuration`); - return data; + return await this.api.get(`/api/wireguard/client/${clientId}/configuration`).then(res => { + return res.data; + }).catch(err => { + return err.response.data; + }); } /** * Getting wg-easy client qr-code * @param {string} clientId - client id. It may look like f2t3bdbh-b340-4e7d-62f7-651a0122bc62 - * @returns {Promise} + * @returns {Promise} * @async */ async getQRCode(clientId) { - const { data } = await this.api.get(`/api/wireguard/client/${clientId}/qrcode.svg`); - return data; + return await this.api.get(`/api/wireguard/client/${clientId}/qrcode.svg`).then(res => { + return res.data; + }).catch(err => { + return err.response.data; + }); } /** * Enable wg-easy client * @param {string} clientId - client id. It may look like f2t3bdbh-b340-4e7d-62f7-651a0122bc62 - * @returns {Promise} + * @returns {Promise} * @async */ async enable(clientId) { - await this.api.post(`/api/wireguard/client/${clientId}/enable`); - return true; + return await this.api.post(`/api/wireguard/client/${clientId}/enable`).then(() => { + return { message: 'Enabled' }; + }).catch(err => { + return err.response.data; + }); } /** * Disable wg-easy client * @param {string} clientId - client id. It may look like f2t3bdbh-b340-4e7d-62f7-651a0122bc62 - * @returns {Promise} + * @returns {Promise} * @async */ async disable(clientId) { - await this.api.post(`/api/wireguard/client/${clientId}/disable`); - return true; + return await this.api.post(`/api/wireguard/client/${clientId}/disable`).then(() => { + return { message: 'Disabled' }; + }).catch(err => { + return err.response.data; + }); } /** * Rename wg-easy client * @param {string} clientId - client id. It may look like f2t3bdbh-b340-4e7d-62f7-651a0122bc62 * @param {string} newName - new name - * @returns {Promise} + * @returns {Promise} * @async */ async rename(clientId, newName) { - const check = this.find(newName); - if (check) return `The name ${newName} is already taken`; - await this.api.put(`/api/wireguard/client/${clientId}/name`, { name: newName }); - return true; + if (addressRegex.test(newName)) return { error: `The name ${newName} is forbidden. Looks like an IP` }; + else if (idRegex.test(newName)) return { error: `The name ${newName} is forbidden. Looks like an ID` }; + + const check = await this.find(newName); + if (!check.error) return { error: `The name ${newName} is already taken` }; + + return await this.api.put(`/api/wireguard/client/${clientId}/name`, { name: newName }).then(() => { + return { message: 'Renamed' }; + }).catch(err => { + return err.response.data; + }); } /** * Update wg-easy client address * @param {string} clientId - client id. It may look like f2t3bdbh-b340-4e7d-62f7-651a0122bc62 * @param {string} address - new address - * @returns {Promise} + * @returns {Promise} * @async */ async updateAddress(clientId, address) { - const clients = await this.getClients(); - const check = clients.find(c => c.address === address); - if (check) return 'Address is already occupied'; - await this.api.put(`/api/wireguard/client/${clientId}/address`, { address }); - return true; + const check = await this.find(address); + if (!check.error) return { error: `The address ${address} is already occupied` }; + return await this.api.put(`/api/wireguard/client/${clientId}/address`, { address }).then(() => { + return { message: 'Address updated' }; + }).catch(err => { + return err.response.data; + }); } /** * Find wg-easy client - * @param {string} client - client id or client name - * @returns {Promise} + * @param {string} client - client id or client name or client address + * @returns {Promise} * @async */ async find(client) { const clients = await this.getClients(); - client = clients.find(c => c.id === client || c.name === client); - if (!client) return 'Client not found'; - else return client; + const search = clients.find(c => c.id === client || c.name === client || c.address === client); + if (!search) return { error: `Client ${client} not found` }; + else return search; } /** @@ -176,22 +209,27 @@ class WGEasyWrapper { * @async */ async create(name) { - name = name.toString(); const check = await this.find(name); - if (typeof check === 'object') return `Сlient with name ${name} already exists`; - await this.api.post('/api/wireguard/client/', { name }); - return await this.find(name); + if (!check.error) return { error: `Сlient ${name} already exists` }; + return await this.api.post('/api/wireguard/client/', { name }).then(res => { + return res.data; + }).catch(err => { + return err.response.data; + }); } /** * Deleting a wg-easy client * @param {string} clientId - client id. It may look like f2t3bdbh-b340-4e7d-62f7-651a0122bc62 - * @returns {Promise} + * @returns {Promise} * @async */ async delete(clientId) { - await this.api.delete(`/api/wireguard/client/${clientId}`); - return true; + return await this.api.delete(`/api/wireguard/client/${clientId}`).then(() => { + return { message: 'Deleted' }; + }).catch(err => { + return err.response.data; + }); } } diff --git a/package.json b/package.json index 8cbea61..7e9c416 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wg-easy-wrapper", - "version": "1.0.2", + "version": "1.0.3", "description": "Convenient Node module for interacting with the wg-easy API", "main": "index.js", "keywords": [