Feat expiration date (#1296)
Closes #1287 Co-authored-by: Vadim Babadzhanyan <vadim.babadzhanyan@my.games>
This commit is contained in:
committed by
GitHub
parent
40af030266
commit
8145809e22
+24
-1
@@ -35,6 +35,7 @@ const {
|
||||
UI_CHART_TYPE,
|
||||
UI_SHOW_LINKS,
|
||||
UI_ENABLE_SORT_CLIENTS,
|
||||
WG_ENABLE_EXPIRES_TIME,
|
||||
} = require('../config');
|
||||
|
||||
const requiresPassword = !!PASSWORD_HASH;
|
||||
@@ -59,6 +60,11 @@ const isPasswordValid = (password) => {
|
||||
return false;
|
||||
};
|
||||
|
||||
const cronJobEveryMinute = async () => {
|
||||
await WireGuard.cronJobEveryMinute();
|
||||
setTimeout(cronJobEveryMinute, 60 * 1000);
|
||||
};
|
||||
|
||||
module.exports = class Server {
|
||||
|
||||
constructor() {
|
||||
@@ -110,6 +116,11 @@ module.exports = class Server {
|
||||
return `${UI_ENABLE_SORT_CLIENTS}`;
|
||||
}))
|
||||
|
||||
.get('/api/wg-enable-expire-time', defineEventHandler((event) => {
|
||||
setHeader(event, 'Content-Type', 'application/json');
|
||||
return `${WG_ENABLE_EXPIRES_TIME}`;
|
||||
}))
|
||||
|
||||
// Authentication
|
||||
.get('/api/session', defineEventHandler((event) => {
|
||||
const authenticated = requiresPassword
|
||||
@@ -224,7 +235,8 @@ module.exports = class Server {
|
||||
}))
|
||||
.post('/api/wireguard/client', defineEventHandler(async (event) => {
|
||||
const { name } = await readBody(event);
|
||||
await WireGuard.createClient({ name });
|
||||
const { expiredDate } = await readBody(event);
|
||||
await WireGuard.createClient({ name, expiredDate });
|
||||
return { success: true };
|
||||
}))
|
||||
.delete('/api/wireguard/client/:clientId', defineEventHandler(async (event) => {
|
||||
@@ -265,6 +277,15 @@ module.exports = class Server {
|
||||
const { address } = await readBody(event);
|
||||
await WireGuard.updateClientAddress({ clientId, address });
|
||||
return { success: true };
|
||||
}))
|
||||
.put('/api/wireguard/client/:clientId/expireDate', defineEventHandler(async (event) => {
|
||||
const clientId = getRouterParam(event, 'clientId');
|
||||
if (clientId === '__proto__' || clientId === 'constructor' || clientId === 'prototype') {
|
||||
throw createError({ status: 403 });
|
||||
}
|
||||
const { expireDate } = await readBody(event);
|
||||
await WireGuard.updateClientExpireDate({ clientId, expireDate });
|
||||
return { success: true };
|
||||
}));
|
||||
|
||||
const safePathJoin = (base, target) => {
|
||||
@@ -340,6 +361,8 @@ module.exports = class Server {
|
||||
|
||||
createServer(toNodeListener(app)).listen(PORT, WEBUI_HOST);
|
||||
debug(`Listening on http://${WEBUI_HOST}:${PORT}`);
|
||||
|
||||
cronJobEveryMinute();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
+47
-4
@@ -24,6 +24,7 @@ const {
|
||||
WG_POST_UP,
|
||||
WG_PRE_DOWN,
|
||||
WG_POST_DOWN,
|
||||
WG_ENABLE_EXPIRES_TIME,
|
||||
} = require('../config');
|
||||
|
||||
module.exports = class WireGuard {
|
||||
@@ -147,6 +148,9 @@ ${client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : ''
|
||||
publicKey: client.publicKey,
|
||||
createdAt: new Date(client.createdAt),
|
||||
updatedAt: new Date(client.updatedAt),
|
||||
expiredAt: client.expiredAt !== null
|
||||
? new Date(client.expiredAt)
|
||||
: null,
|
||||
allowedIPs: client.allowedIPs,
|
||||
hash: Math.abs(CRC32.str(clientId)).toString(16),
|
||||
downloadableConfig: 'privateKey' in client,
|
||||
@@ -227,7 +231,7 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`;
|
||||
});
|
||||
}
|
||||
|
||||
async createClient({ name }) {
|
||||
async createClient({ name, expiredDate }) {
|
||||
if (!name) {
|
||||
throw new Error('Missing: Name');
|
||||
}
|
||||
@@ -256,7 +260,6 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`;
|
||||
if (!address) {
|
||||
throw new Error('Maximum number of clients reached.');
|
||||
}
|
||||
|
||||
// Create Client
|
||||
const id = crypto.randomUUID();
|
||||
const client = {
|
||||
@@ -269,10 +272,15 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`;
|
||||
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
|
||||
expiredAt: null,
|
||||
enabled: true,
|
||||
};
|
||||
|
||||
if (expiredDate) {
|
||||
client.expiredAt = new Date(expiredDate);
|
||||
client.expiredAt.setHours(23);
|
||||
client.expiredAt.setMinutes(59);
|
||||
client.expiredAt.setSeconds(59);
|
||||
}
|
||||
config.clients[id] = client;
|
||||
|
||||
await this.saveConfig();
|
||||
@@ -329,6 +337,22 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`;
|
||||
await this.saveConfig();
|
||||
}
|
||||
|
||||
async updateClientExpireDate({ clientId, expireDate }) {
|
||||
const client = await this.getClient({ clientId });
|
||||
|
||||
if (expireDate) {
|
||||
client.expiredAt = new Date(expireDate);
|
||||
client.expiredAt.setHours(23);
|
||||
client.expiredAt.setMinutes(59);
|
||||
client.expiredAt.setSeconds(59);
|
||||
} else {
|
||||
client.expiredAt = null;
|
||||
}
|
||||
client.updatedAt = new Date();
|
||||
|
||||
await this.saveConfig();
|
||||
}
|
||||
|
||||
async __reloadConfig() {
|
||||
await this.__buildConfig();
|
||||
await this.__syncConfig();
|
||||
@@ -355,4 +379,23 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`;
|
||||
await Util.exec('wg-quick down wg0').catch(() => {});
|
||||
}
|
||||
|
||||
async cronJobEveryMinute() {
|
||||
const config = await this.getConfig();
|
||||
if (WG_ENABLE_EXPIRES_TIME === 'true') {
|
||||
let needSaveConfig = false;
|
||||
for (const client of Object.values(config.clients)) {
|
||||
if (client.enabled !== true) continue;
|
||||
if (client.expiredAt !== null && new Date() > new Date(client.expiredAt)) {
|
||||
debug(`Client ${client.id} expired.`);
|
||||
needSaveConfig = true;
|
||||
client.enabled = false;
|
||||
client.updatedAt = new Date();
|
||||
}
|
||||
}
|
||||
if (needSaveConfig) {
|
||||
await this.saveConfig();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user