mirror of https://github.com/wg-easy/wg-easy
14 changed files with 117 additions and 44 deletions
@ -43,6 +43,9 @@ importers: |
|||
vue3-apexcharts: |
|||
specifier: ^1.5.3 |
|||
version: 1.5.3([email protected])([email protected]([email protected])) |
|||
zod: |
|||
specifier: ^3.23.8 |
|||
version: 3.23.8 |
|||
devDependencies: |
|||
'@nuxt/eslint-config': |
|||
specifier: ^0.5.0 |
|||
@ -7405,6 +7408,12 @@ packages: |
|||
} |
|||
engines: { node: '>= 14' } |
|||
|
|||
[email protected]: |
|||
resolution: |
|||
{ |
|||
integrity: sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==, |
|||
} |
|||
|
|||
snapshots: |
|||
'@alloc/[email protected]': {} |
|||
|
|||
@ -12213,3 +12222,5 @@ snapshots: |
|||
archiver-utils: 5.0.2 |
|||
compress-commons: 6.0.2 |
|||
readable-stream: 4.5.2 |
|||
|
|||
[email protected]: {} |
|||
|
@ -1,13 +1,9 @@ |
|||
export default defineEventHandler(async (event) => { |
|||
const clientId = getRouterParam(event, 'clientId'); |
|||
if ( |
|||
clientId === '__proto__' || |
|||
clientId === 'constructor' || |
|||
clientId === 'prototype' |
|||
) { |
|||
throw createError({ statusCode: 403 }); |
|||
} |
|||
const { address } = await readBody(event); |
|||
const { clientId } = await getValidatedRouterParams( |
|||
event, |
|||
validateZod(clientIdType) |
|||
); |
|||
const { address } = await readValidatedBody(event, validateZod(addressType)); |
|||
await WireGuard.updateClientAddress({ clientId, address }); |
|||
return { success: true }; |
|||
}); |
|||
|
@ -1,12 +1,8 @@ |
|||
export default defineEventHandler(async (event) => { |
|||
const clientId = getRouterParam(event, 'clientId'); |
|||
if ( |
|||
clientId === '__proto__' || |
|||
clientId === 'constructor' || |
|||
clientId === 'prototype' |
|||
) { |
|||
throw createError({ statusCode: 403 }); |
|||
} |
|||
const { clientId } = await getValidatedRouterParams( |
|||
event, |
|||
validateZod(clientIdType) |
|||
); |
|||
await WireGuard.disableClient({ clientId }); |
|||
return { success: true }; |
|||
}); |
|||
|
@ -1,12 +1,8 @@ |
|||
export default defineEventHandler(async (event) => { |
|||
const clientId = getRouterParam(event, 'clientId'); |
|||
if ( |
|||
clientId === '__proto__' || |
|||
clientId === 'constructor' || |
|||
clientId === 'prototype' |
|||
) { |
|||
throw createError({ statusCode: 403 }); |
|||
} |
|||
const { clientId } = await getValidatedRouterParams( |
|||
event, |
|||
validateZod(clientIdType) |
|||
); |
|||
await WireGuard.enableClient({ clientId }); |
|||
return { success: true }; |
|||
}); |
|||
|
@ -1,5 +1,8 @@ |
|||
export default defineEventHandler(async (event) => { |
|||
const clientId = getRouterParam(event, 'clientId'); |
|||
const { clientId } = await getValidatedRouterParams( |
|||
event, |
|||
validateZod(clientIdType) |
|||
); |
|||
await WireGuard.deleteClient({ clientId }); |
|||
return { success: true }; |
|||
}); |
|||
|
@ -1,13 +1,9 @@ |
|||
export default defineEventHandler(async (event) => { |
|||
const clientId = getRouterParam(event, 'clientId'); |
|||
if ( |
|||
clientId === '__proto__' || |
|||
clientId === 'constructor' || |
|||
clientId === 'prototype' |
|||
) { |
|||
throw createError({ statusCode: 403 }); |
|||
} |
|||
const { name } = await readBody(event); |
|||
const { clientId } = await getValidatedRouterParams( |
|||
event, |
|||
validateZod(clientIdType) |
|||
); |
|||
const { name } = await readValidatedBody(event, validateZod(nameType)); |
|||
await WireGuard.updateClientName({ clientId, name }); |
|||
return { success: true }; |
|||
}); |
|||
|
@ -1,5 +1,5 @@ |
|||
export default defineEventHandler(async (event) => { |
|||
const { name } = await readBody(event); |
|||
const { name } = await readValidatedBody(event, validateZod(nameType)); |
|||
await WireGuard.createClient({ name }); |
|||
return { success: true }; |
|||
}); |
|||
|
@ -1,5 +1,5 @@ |
|||
export default defineEventHandler(async (event) => { |
|||
const { file } = await readBody(event); |
|||
const { file } = await readValidatedBody(event, validateZod(fileType)); |
|||
await WireGuard.restoreConfiguration(file); |
|||
return { success: true }; |
|||
}); |
|||
|
@ -0,0 +1,65 @@ |
|||
import type { ZodSchema } from 'zod'; |
|||
import { z, ZodError } from 'zod'; |
|||
|
|||
const safeStringRefine = z |
|||
.string() |
|||
.refine( |
|||
(v) => v !== '__proto__' && v !== 'constructor' && v !== 'prototype', |
|||
{ message: 'String is malformed' } |
|||
); |
|||
|
|||
const id = z |
|||
.string() |
|||
.uuid('Client ID must be a valid UUID') |
|||
.and(safeStringRefine); |
|||
|
|||
const address = z |
|||
.string({ message: 'Address must be a valid string' }) |
|||
.and(safeStringRefine); |
|||
|
|||
const name = z |
|||
.string({ message: 'Name must be a valid string' }) |
|||
.min(1, 'Name must be at least 1 Character') |
|||
.and(safeStringRefine); |
|||
|
|||
const file = z |
|||
.string({ message: 'File must be a valid string' }) |
|||
.and(safeStringRefine); |
|||
|
|||
const password = z |
|||
.string({ message: 'Password must be a valid string' }) |
|||
.and(safeStringRefine); |
|||
|
|||
export const clientIdType = z.object({ |
|||
clientId: id, |
|||
}); |
|||
|
|||
export const addressType = z.object({ |
|||
address: address, |
|||
}); |
|||
|
|||
export const nameType = z.object({ |
|||
name: name, |
|||
}); |
|||
|
|||
export const fileType = z.object({ |
|||
file: file, |
|||
}); |
|||
|
|||
export const passwordType = z.object({ |
|||
password: password, |
|||
}); |
|||
|
|||
export function validateZod<T>(schema: ZodSchema<T>) { |
|||
return async (data: unknown) => { |
|||
try { |
|||
return await schema.parseAsync(data); |
|||
} catch (error) { |
|||
let message = 'Unexpected Error'; |
|||
if (error instanceof ZodError) { |
|||
message = error.issues.map((v) => v.message).join('; '); |
|||
} |
|||
throw new Error(message); |
|||
} |
|||
}; |
|||
} |
Loading…
Reference in new issue