Browse Source

Allowed IPs now uses ENV

pull/238/head
joshuakraitberg 4 years ago
parent
commit
d6beae9a0c
  1. 9
      src/lib/Server.js
  2. 18
      src/lib/WireGuard.js
  3. 6
      src/www/index.html
  4. 7
      src/www/js/api.js
  5. 4
      src/www/js/app.js

9
src/lib/Server.js

@ -62,7 +62,7 @@ module.exports = class Server {
req.session.authenticated = true;
req.session.save();
console.log(`New Session: ${req.session.id})`);
debug(`New Session: ${req.session.id})`);
}))
// WireGuard
@ -84,7 +84,7 @@ module.exports = class Server {
req.session.destroy();
console.log(`Deleted Session: ${sessionId}`);
debug(`Deleted Session: ${sessionId}`);
}))
.get('/api/wireguard/hardened', Util.promisify(async req => {
return WireGuard.areClientsHardened();
@ -92,6 +92,9 @@ module.exports = class Server {
.get('/api/wireguard/dns', Util.promisify(async req => {
return WireGuard.getDns();
}))
.get('/api/wireguard/default-allowed-ips', Util.promisify(async req => {
return WireGuard.getDefaultAllowedIPs();
}))
.get('/api/wireguard/client', Util.promisify(async req => {
return WireGuard.getClients();
}))
@ -138,7 +141,7 @@ module.exports = class Server {
}))
.listen(PORT, () => {
console.log(`Listening on http://0.0.0.0:${PORT}`);
debug(`Listening on http://0.0.0.0:${PORT}`);
});
}

18
src/lib/WireGuard.js

@ -33,12 +33,12 @@ module.exports = class WireGuard {
throw new Error('WG_HOST Environment Variable Not Set!');
}
console.log('Loading configuration...');
debug('Loading configuration...');
let config;
try {
config = await fs.readFile(path.join(WG_PATH, 'wg0.json'), 'utf8');
config = JSON.parse(config);
console.log('Configuration loaded.');
debug('Configuration loaded.');
} catch (err) {
const privateKey = await Util.exec('wg genkey');
const publicKey = await Util.exec(`echo ${privateKey} | wg pubkey`, privateKey);
@ -52,7 +52,7 @@ module.exports = class WireGuard {
},
clients: {},
};
console.log('Configuration generated.');
debug('Configuration generated.');
}
await this.__saveConfig(config);
@ -110,26 +110,30 @@ PresharedKey = ${client.preSharedKey}
AllowedIPs = ${client.address}/32`;
}
console.log('Config saving...');
debug('Config saving...');
await fs.writeFile(path.join(WG_PATH, 'wg0.json'), JSON.stringify(config, false, 2), {
mode: 0o660,
});
await fs.writeFile(path.join(WG_PATH, 'wg0.conf'), result, {
mode: 0o600,
});
console.log('Config saved.');
debug('Config saved.');
}
async __syncConfig() {
console.log('Config syncing...');
debug('Config syncing...');
await Util.exec('wg syncconf wg0 <(wg-quick strip wg0)');
console.log('Config synced.');
debug('Config synced.');
}
async getDns() {
return WG_DEFAULT_DNS;
}
async getDefaultAllowedIPs() {
return WG_ALLOWED_IPS;
}
async areClientsHardened() {
return WG_HARDEN_CLIENTS;
}

6
src/www/index.html

@ -54,7 +54,7 @@
<p class="text-2xl font-medium">Clients</p>
</div>
<div class="flex-shrink-0">
<button @click="clientCreate = true; clientCreateName = ''; clientCreateAllowedIPs = clientCreateAllowedIPsDefault;"
<button @click="clientCreate = true; clientCreateName = ''; clientCreateAllowedIPs = getDefaultAllowedIPs();"
class="hover:bg-red-800 hover:border-red-800 hover:text-white text-gray-700 border-2 border-gray-100 py-2 px-4 rounded inline-flex items-center transition">
<svg class="w-4 mr-2" inline xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
stroke="currentColor">
@ -235,7 +235,7 @@
</div>
<div v-if="clients && clients.length === 0">
<p class="text-center m-10 text-gray-400 text-sm">There are no clients yet.<br /><br />
<button @click="clientCreate = true; clientCreateName = ''; clientCreateAllowedIPs = clientCreateAllowedIPsDefault;"
<button @click="clientCreate = true; clientCreateName = ''; clientCreateAllowedIPs = getDefaultAllowedIPs();"
class="bg-red-800 text-white hover:bg-red-700 border-2 border-none py-2 px-4 rounded inline-flex items-center transition">
<svg class="w-4 mr-2" inline xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
stroke="currentColor">
@ -325,7 +325,7 @@
type="text" v-model.trim="clientCreateName" placeholder="Name" />
<input class="rounded p-2 border-2 border-gray-100 focus:border-gray-200 outline-none w-full"
type="text" v-model.trim="clientCreateAllowedIPs" placeholder="AllowedIPs" />
<button type="button" @click="clientCreateAllowedIPs = clientCreateAllowedIPsDefault"
<button type="button" @click="clientCreateAllowedIPs = getDefaultAllowedIPs()"
class="hover:bg-red-800 hover:border-red-800 hover:text-white text-gray-700 border-2 border-gray-100 py-2 px-4 rounded inline-flex items-center transition">
Allow all IPs
</button>

7
src/www/js/api.js

@ -72,6 +72,13 @@ class API {
});
}
async getDefaultAllowedIPs() {
return this.call({
method: 'get',
path: '/wireguard/default-allowed-ips',
});
}
async getClients() {
return this.call({
method: 'get',

4
src/www/js/app.js

@ -42,7 +42,6 @@ new Vue({
clientConfigDownload: null,
clientCreateName: '',
clientCreateAllowedIPs: '',
clientCreateAllowedIPsDefault: '0.0.0.0/0, ::0/0',
clientCreateAllowedIPsExclude: (
'::/0, 1.0.0.0/8, 2.0.0.0/8, 3.0.0.0/8, '
+ '4.0.0.0/6, 8.0.0.0/7, 11.0.0.0/8, 12.0.0.0/6, '
@ -231,6 +230,9 @@ new Vue({
getDns() {
return this.api.getDns();
},
getDefaultAllowedIPs() {
return this.api.getDefaultAllowedIPs();
},
createClient() {
const name = this.clientCreateName;
const allowedIPs = this.clientCreateAllowedIPs;

Loading…
Cancel
Save