Fix short link. Generate One Time Link (#1301)
Co-authored-by: Vadim Babadzhanyan <vadim.babadzhanyan@my.games>
This commit is contained in:
committed by
Vadim Babadzhanyan
parent
b97b66462b
commit
91da296740
+13
-4
@@ -133,14 +133,15 @@ module.exports = class Server {
|
||||
authenticated,
|
||||
};
|
||||
}))
|
||||
.get('/:clientHash', defineEventHandler(async (event) => {
|
||||
const clientHash = getRouterParam(event, 'clientHash');
|
||||
.get('/cnf/:clientOneTimeLink', defineEventHandler(async (event) => {
|
||||
const clientOneTimeLink = getRouterParam(event, 'clientOneTimeLink');
|
||||
const clients = await WireGuard.getClients();
|
||||
const client = clients.find((client) => client.hash === clientHash);
|
||||
const client = clients.find((client) => client.oneTimeLink === clientOneTimeLink);
|
||||
if (!client) return;
|
||||
const clientId = client.id;
|
||||
const config = await WireGuard.getClientConfiguration({ clientId });
|
||||
setHeader(event, 'Content-Disposition', `attachment; filename="${clientHash}.conf"`);
|
||||
await WireGuard.eraseOneTimeLink({ clientId });
|
||||
setHeader(event, 'Content-Disposition', `attachment; filename="${clientOneTimeLink}.conf"`);
|
||||
setHeader(event, 'Content-Type', 'text/plain');
|
||||
return config;
|
||||
}))
|
||||
@@ -253,6 +254,14 @@ module.exports = class Server {
|
||||
await WireGuard.enableClient({ clientId });
|
||||
return { success: true };
|
||||
}))
|
||||
.post('/api/wireguard/client/:clientId/generateOneTimeLink', defineEventHandler(async (event) => {
|
||||
const clientId = getRouterParam(event, 'clientId');
|
||||
if (clientId === '__proto__' || clientId === 'constructor' || clientId === 'prototype') {
|
||||
throw createError({ status: 403 });
|
||||
}
|
||||
await WireGuard.generateOneTimeLink({ clientId });
|
||||
return { success: true };
|
||||
}))
|
||||
.post('/api/wireguard/client/:clientId/disable', defineEventHandler(async (event) => {
|
||||
const clientId = getRouterParam(event, 'clientId');
|
||||
if (clientId === '__proto__' || clientId === 'constructor' || clientId === 'prototype') {
|
||||
|
||||
+16
-1
@@ -152,7 +152,7 @@ ${client.preSharedKey ? `PresharedKey = ${client.preSharedKey}\n` : ''
|
||||
? new Date(client.expiredAt)
|
||||
: null,
|
||||
allowedIPs: client.allowedIPs,
|
||||
hash: Math.abs(CRC32.str(clientId)).toString(16),
|
||||
oneTimeLink: client.oneTimeLink ? client.oneTimeLink : null,
|
||||
downloadableConfig: 'privateKey' in client,
|
||||
persistentKeepalive: null,
|
||||
latestHandshakeAt: null,
|
||||
@@ -308,6 +308,21 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`;
|
||||
await this.saveConfig();
|
||||
}
|
||||
|
||||
async generateOneTimeLink({ clientId }) {
|
||||
const client = await this.getClient({ clientId });
|
||||
const key = `${clientId}-${Math.floor(Math.random() * 1000)}`;
|
||||
client.oneTimeLink = Math.abs(CRC32.str(key)).toString(16);
|
||||
client.updatedAt = new Date();
|
||||
await this.saveConfig();
|
||||
}
|
||||
|
||||
async eraseOneTimeLink({ clientId }) {
|
||||
const client = await this.getClient({ clientId });
|
||||
client.oneTimeLink = null;
|
||||
client.updatedAt = new Date();
|
||||
await this.saveConfig();
|
||||
}
|
||||
|
||||
async disableClient({ clientId }) {
|
||||
const client = await this.getClient({ clientId });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user