This commit is contained in:
Emile Nijssen 2021-05-23 14:28:22 +02:00
parent 01f2724518
commit 0c3217def0
3 changed files with 31 additions and 19 deletions

View File

@ -1,21 +1,19 @@
FROM node:16-buster FROM node:16-alpine
# Install Linux packages # Install Linux packages
RUN apt-get clean RUN apk add -U wireguard-tools
RUN echo "deb http://deb.debian.org/debian buster-backports main" > /etc/apt/sources.list.d/backports.list
RUN apt-get update
RUN apt-get install -y wireguard iproute2 openresolv curl
# Install Node.js
# RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
# RUN apt-get install -y nodejs
# Copy Web UI
COPY src/ /app/ COPY src/ /app/
WORKDIR /app WORKDIR /app
RUN npm ci --production RUN npm ci --production
# Expose Ports
EXPOSE 51820/udp EXPOSE 51820/udp
EXPOSE 80/tcp EXPOSE 80/tcp
# Set Environment
ENV DEBUG=Server,WireGuard ENV DEBUG=Server,WireGuard
# Run Web UI
CMD ["node", "server.js"] CMD ["node", "server.js"]

View File

@ -44,7 +44,9 @@ module.exports = class Util {
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
childProcess.exec(cmd, (err, stdout) => { childProcess.exec(cmd, {
shell: 'bash',
}, (err, stdout) => {
if (err) return reject(err); if (err) return reject(err);
return resolve(String(stdout).trim()); return resolve(String(stdout).trim());
}); });

View File

@ -32,23 +32,25 @@ module.exports = class WireGuard {
try { try {
config = await fs.readFile(path.join(WG_PATH, 'wg0.json'), 'utf8'); config = await fs.readFile(path.join(WG_PATH, 'wg0.json'), 'utf8');
config = JSON.parse(config); config = JSON.parse(config);
debug('Configuration loaded'); debug('Configuration loaded.');
} catch (err) { } catch (err) {
const privateKey = await Util.exec('wg genkey');
const publicKey = await Util.exec(`echo ${privateKey} | wg pubkey`);
const address = WG_DEFAULT_ADDRESS.replace('x', '1');
config = { config = {
server: { server: {
privateKey: await Util.exec('wg genkey'), privateKey,
address: `${WG_DEFAULT_ADDRESS.replace('x', '1')}/24`, publicKey,
address,
}, },
clients: {}, clients: {},
}; };
debug('New configuration saved');
} }
await this.__saveConfig(config); await this.__saveConfig(config);
debug('Starting...');
await Util.exec('wg-quick up wg0'); await Util.exec('wg-quick up wg0');
debug('Started'); await this.__syncConfig();
return config; return config;
}); });
@ -60,6 +62,7 @@ module.exports = class WireGuard {
async saveConfig() { async saveConfig() {
const config = await this.getConfig(); const config = await this.getConfig();
await this.__saveConfig(config); await this.__saveConfig(config);
await this.__syncConfig();
} }
async __saveConfig(config) { async __saveConfig(config) {
@ -85,8 +88,16 @@ PresharedKey = ${client.preSharedKey}
AllowedIPs = ${client.address}/32`; AllowedIPs = ${client.address}/32`;
} }
debug('Saving config...');
await fs.writeFile(path.join(WG_PATH, 'wg0.json'), JSON.stringify(config, false, 2)); await fs.writeFile(path.join(WG_PATH, 'wg0.json'), JSON.stringify(config, false, 2));
await fs.writeFile(path.join(WG_PATH, 'wg0.conf'), result); await fs.writeFile(path.join(WG_PATH, 'wg0.conf'), result);
debug('Config saved.');
}
async __syncConfig() {
debug('Syncing config...');
await Util.exec('wg syncconf wg0 <(wg-quick strip wg0)');
debug('Config synced.');
} }
async getClients() { async getClients() {
@ -150,6 +161,7 @@ AllowedIPs = ${client.address}/32`;
} }
async getClientConfiguration({ clientId }) { async getClientConfiguration({ clientId }) {
const config = await this.getConfig();
const client = await this.getClient({ clientId }); const client = await this.getClient({ clientId });
return ` return `
@ -159,7 +171,7 @@ Address = ${client.address}/24
DNS = ${WG_DEFAULT_DNS} DNS = ${WG_DEFAULT_DNS}
[Peer] [Peer]
PublicKey = ${client.publicKey} PublicKey = ${config.server.publicKey}
PresharedKey = ${client.preSharedKey} PresharedKey = ${client.preSharedKey}
AllowedIPs = 0.0.0.0/0, ::/0 AllowedIPs = 0.0.0.0/0, ::/0
Endpoint = ${WG_HOST}:${WG_PORT}`; Endpoint = ${WG_HOST}:${WG_PORT}`;