Browse Source

fixup: lint errors

pull/759/head
xwvike 3 years ago
parent
commit
164f608514
  1. 16
      src/lib/Server.js
  2. 18
      src/lib/WireGuard.js
  3. 2
      src/www/js/api.js
  4. 22
      src/www/js/app.js

16
src/lib/Server.js

@ -161,14 +161,14 @@ module.exports = class Server {
const { address } = req.body; const { address } = req.body;
return WireGuard.updateClientAddress({ clientId, address }); return WireGuard.updateClientAddress({ clientId, address });
})) }))
.put('/api/wireguard/client/:clientId/allowedips', Util.promisify(async (req, res) => { .put('/api/wireguard/client/:clientId/allowedips', Util.promisify(async (req, res) => {
const { clientId } = req.params; const { clientId } = req.params;
if (clientId === '__proto__' || clientId === 'constructor' || clientId === 'prototype') { if (clientId === '__proto__' || clientId === 'constructor' || clientId === 'prototype') {
res.end(403); res.end(403);
} }
const { allowedIPs } = req.body; const { allowedIPs } = req.body;
return WireGuard.updateClientAllowedIPs({ clientId, allowedIPs }); return WireGuard.updateClientAllowedIPs({ clientId, allowedIPs });
})) }))
.listen(PORT, WEBUI_HOST, () => { .listen(PORT, WEBUI_HOST, () => {
debug(`Listening on http://${WEBUI_HOST}:${PORT}`); debug(`Listening on http://${WEBUI_HOST}:${PORT}`);

18
src/lib/WireGuard.js

@ -105,12 +105,12 @@ PostDown = ${WG_POST_DOWN}
for (const [clientId, client] of Object.entries(config.clients)) { for (const [clientId, client] of Object.entries(config.clients)) {
if (!client.enabled) continue; if (!client.enabled) continue;
let allowedIPs = ''; let allowedIPs = '';
if(client?.allowedIPs){ if (client?.allowedIPs) {
for (const allowedIP of client.allowedIPs) { for (const allowedIP of client.allowedIPs) {
allowedIPs += `,${allowedIP.address}/${allowedIP.cidr}`; allowedIPs += `,${allowedIP.address}/${allowedIP.cidr}`;
} }
allowedIPs = allowedIPs.substring(1); allowedIPs = allowedIPs.substring(1);
}else { } else {
allowedIPs = client.address; allowedIPs = client.address;
} }
@ -149,7 +149,7 @@ AllowedIPs = ${allowedIPs}`;
publicKey: client.publicKey, publicKey: client.publicKey,
createdAt: new Date(client.createdAt), createdAt: new Date(client.createdAt),
updatedAt: new Date(client.updatedAt), updatedAt: new Date(client.updatedAt),
allowedIPs: client?.allowedIPs?client.allowedIPs:[{type:'ipv4', address:client.address, cidr: 32}], allowedIPs: client?.allowedIPs ? client.allowedIPs : [{ type: 'ipv4', address: client.address, cidr: 32 }],
persistentKeepalive: null, persistentKeepalive: null,
latestHandshakeAt: null, latestHandshakeAt: null,
@ -272,7 +272,7 @@ Endpoint = ${WG_HOST}:${WG_PORT}`;
createdAt: new Date(), createdAt: new Date(),
updatedAt: new Date(), updatedAt: new Date(),
allowedIPs: [{type:'ipv4', address, cidr: 32}], allowedIPs: [{ type: 'ipv4', address, cidr: 32 }],
enabled: true, enabled: true,
}; };
@ -326,8 +326,7 @@ Endpoint = ${WG_HOST}:${WG_PORT}`;
if (!Util.isValidIPv4(address)) { if (!Util.isValidIPv4(address)) {
throw new ServerError(`Invalid Address: ${address}`, 400); throw new ServerError(`Invalid Address: ${address}`, 400);
} }
if(client?.allowedIPs){ if (client?.allowedIPs) {
let allowedIPs = client.allowedIPs
client.allowedIPs[0].address = address; client.allowedIPs[0].address = address;
} }
client.address = address; client.address = address;
@ -335,14 +334,15 @@ Endpoint = ${WG_HOST}:${WG_PORT}`;
await this.saveConfig(); await this.saveConfig();
} }
async updateClientAllowedIPs({ clientId, allowedIPs }) { async updateClientAllowedIPs({ clientId, allowedIPs }) {
const client = await this.getClient({ clientId }); const client = await this.getClient({ clientId });
let ips = allowedIPs.map(item=>item.address) const ips = allowedIPs.map((item) => item.address);
ips.forEach(ip=>{ ips.forEach((ip) => {
if (!Util.isValidIPv4(ip)) { if (!Util.isValidIPv4(ip)) {
throw new ServerError(`Invalid Address: ${ip}`, 400); throw new ServerError(`Invalid Address: ${ip}`, 400);
} }
}) });
client.allowedIPs = allowedIPs; client.allowedIPs = allowedIPs;
client.updatedAt = new Date(); client.updatedAt = new Date();

2
src/www/js/api.js

@ -116,6 +116,7 @@ class API {
body: { address }, body: { address },
}); });
} }
async updateClientAllowedIPs({ clientId, allowedIPs }) { async updateClientAllowedIPs({ clientId, allowedIPs }) {
return this.call({ return this.call({
method: 'put', method: 'put',
@ -124,5 +125,4 @@ class API {
}); });
} }
} }

22
src/www/js/app.js

@ -42,7 +42,7 @@ new Vue({
clientEditAddress: null, clientEditAddress: null,
clientEditAddressId: null, clientEditAddressId: null,
clientEditAllowedIPs: null, clientEditAllowedIPs: null,
userInputIP:[0,0,0,0,0], userInputIP: [0, 0, 0, 0, 0],
qrcode: null, qrcode: null,
currentRelease: null, currentRelease: null,
@ -247,20 +247,20 @@ new Vue({
}, },
updateClientAllowedIPs(client) { updateClientAllowedIPs(client) {
this.api.updateClientAllowedIPs({ clientId: client.id, allowedIPs: client.allowedIPs }) this.api.updateClientAllowedIPs({ clientId: client.id, allowedIPs: client.allowedIPs })
.catch((err) => alert(err.message || err.toString())) .catch((err) => alert(err.message || err.toString()))
.finally(() => this.refresh().catch(console.error)); .finally(() => this.refresh().catch(console.error));
}, },
addNewIP(){ addNewIP() {
let address = this.userInputIP.slice(0,4).join('.'); const address = this.userInputIP.slice(0, 4).join('.');
let allowedIPs = this.clientEditAllowedIPs.allowedIPs; const allowedIPs = [...this.clientEditAllowedIPs.allowedIPs];
let obj = {type:'ipv4', address, cidr: this.userInputIP[4]}; const obj = { type: 'ipv4', address, cidr: this.userInputIP[4] };
allowedIPs.push(obj); allowedIPs.push(obj);
this.clientEditAllowedIPs.allowedIPs = allowedIPs; this.clientEditAllowedIPs.allowedIPs = allowedIPs;
this.userInputIP = [0,0,0,0,0]; this.userInputIP = [0, 0, 0, 0, 0];
}, },
removeIP(index){ removeIP(index) {
let allowedIPs = this.clientEditAllowedIPs.allowedIPs; const allowedIPs = [...this.clientEditAllowedIPs.allowedIPs];
allowedIPs.splice(index,1); allowedIPs.splice(index, 1);
this.clientEditAllowedIPs.allowedIPs = allowedIPs; this.clientEditAllowedIPs.allowedIPs = allowedIPs;
}, },
toggleTheme() { toggleTheme() {

Loading…
Cancel
Save