mirror of https://github.com/wg-easy/wg-easy
6 changed files with 118 additions and 23 deletions
@ -1,30 +1,33 @@ |
|||||
export default defineEventHandler(async (event) => { |
export default defineEventHandler(async (event) => { |
||||
const system = await Database.getSystem(); |
const system = await Database.getSystem(); |
||||
if (!system) |
|
||||
throw createError({ |
|
||||
statusCode: 500, |
|
||||
statusMessage: 'Invalid', |
|
||||
}); |
|
||||
if (!system.oneTimeLinks.enabled) { |
if (!system.oneTimeLinks.enabled) { |
||||
throw createError({ |
throw createError({ |
||||
status: 404, |
statusCode: 404, |
||||
message: 'Invalid state', |
statusMessage: 'Invalid state', |
||||
}); |
}); |
||||
} |
} |
||||
// TODO: validate with zod
|
// TODO: validate with zod
|
||||
const clientOneTimeLink = getRouterParam(event, 'clientOneTimeLink'); |
const { oneTimeLink } = await getValidatedRouterParams( |
||||
|
event, |
||||
|
validateZod(oneTimeLinkType) |
||||
|
); |
||||
const clients = await WireGuard.getClients(); |
const clients = await WireGuard.getClients(); |
||||
const client = clients.find( |
const client = clients.find( |
||||
(client) => client.oneTimeLink === clientOneTimeLink |
(client) => client.oneTimeLink?.oneTimeLink === oneTimeLink |
||||
); |
); |
||||
if (!client) return; |
if (!client) { |
||||
|
throw createError({ |
||||
|
statusCode: 404, |
||||
|
statusMessage: 'Invalid One Time Link', |
||||
|
}); |
||||
|
} |
||||
const clientId = client.id; |
const clientId = client.id; |
||||
const config = await WireGuard.getClientConfiguration({ clientId }); |
const config = await WireGuard.getClientConfiguration({ clientId }); |
||||
await WireGuard.eraseOneTimeLink({ clientId }); |
await WireGuard.eraseOneTimeLink({ clientId }); |
||||
setHeader( |
setHeader( |
||||
event, |
event, |
||||
'Content-Disposition', |
'Content-Disposition', |
||||
`attachment; filename="${clientOneTimeLink}.conf"` |
`attachment; filename="${client.name}.conf"` |
||||
); |
); |
||||
setHeader(event, 'Content-Type', 'text/plain'); |
setHeader(event, 'Content-Type', 'text/plain'); |
||||
return config; |
return config; |
Loading…
Reference in new issue