-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
236 lines (212 loc) · 7.36 KB
/
index.js
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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
*/
class WGEasyWrapper {
/**
* Create a new WGEasyWrapper object
* @param {string} baseURL - Url of the API request. It may look like https://website.com or http://127.0.0.1:51820
* @param {string} password - Password to log in to the Web UI
* @constructor
*/
constructor(baseURL, password) {
if (!baseURL) throw new Error('baseUrl not specified');
if (!password) throw new Error('password not specified');
const api = axios.create({
baseURL,
headers: { 'Content-Type': 'application/json' }
});
let authRetryCount = 0;
const maxAuthRetries = 3;
const handleAuthorizationAndRetry = async (originalRequest) => {
try {
const auth = await api.post('/api/session', { password });
api.defaults.headers.common['Cookie'] = auth.headers['set-cookie'][0];
return api.request(originalRequest);
} catch (e) {
throw new Error('Authorization Error: ' + e.message);
}
};
api.interceptors.response.use((config) => { return config; }, async (error) => {
const { status } = error.response;
const originalRequest = error.config;
if (status !== 401) return Promise.reject(error);
try {
if (authRetryCount < maxAuthRetries) {
authRetryCount++;
return handleAuthorizationAndRetry(originalRequest);
} else {
throw new Error('Authorization retries limit exceeded.');
}
} catch (e) {
throw new Error('Intercept Error: ' + e.message);
}
});
this.api = api;
}
/**
* Getting the release version
* @returns {Promise<Object|Number>}
* @async
*/
async getRelease() {
return await this.api.get('/api/release').then(res => {
return res.data;
}).catch(err => {
return err.response.data;
});
}
/**
* Getting a session
* @returns {Promise<Object>}
* @async
*/
async getSession() {
return await this.api.get('/api/session').then(res => {
return res.data;
}).catch(err => {
return err.response.data;
});
}
/**
* Getting wg-easy clients
* @returns {Promise<Array|Object>}
* @async
*/
async getClients() {
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<Object|String>}
* @async
*/
async getConfig(clientId) {
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<Object|String>}
* @async
*/
async getQRCode(clientId) {
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<Object>}
* @async
*/
async enable(clientId) {
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<Object>}
* @async
*/
async disable(clientId) {
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<Object>}
* @async
*/
async rename(clientId, newName) {
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<Object>}
* @async
*/
async updateAddress(clientId, address) {
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 or client address
* @returns {Promise<Object>}
* @async
*/
async find(client) {
const clients = await this.getClients();
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;
}
/**
* Creating a wg-easy client
* @param {string} name - name for a new client
* @returns {Promise<String|Object>}
* @async
*/
async create(name) {
const check = 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<Object|String>}
* @async
*/
async delete(clientId) {
return await this.api.delete(`/api/wireguard/client/${clientId}`).then(() => {
return { message: 'Deleted' };
}).catch(err => {
return err.response.data;
});
}
}
module.exports = WGEasyWrapper;