mirror of https://github.com/wg-easy/wg-easy
35 changed files with 401 additions and 201 deletions
@ -1,10 +1,13 @@ |
|||||
import { GeneralUpdateSchema } from '#db/repositories/general/types'; |
import { GeneralUpdateSchema } from '#db/repositories/general/types'; |
||||
|
|
||||
export default defineEventHandler(async (event) => { |
export default definePermissionEventHandler( |
||||
|
actions.ADMIN, |
||||
|
async ({ event }) => { |
||||
const data = await readValidatedBody( |
const data = await readValidatedBody( |
||||
event, |
event, |
||||
validateZod(GeneralUpdateSchema, event) |
validateZod(GeneralUpdateSchema, event) |
||||
); |
); |
||||
await Database.general.update(data); |
await Database.general.update(data); |
||||
return { success: true }; |
return { success: true }; |
||||
}); |
} |
||||
|
); |
||||
|
@ -1,9 +1,14 @@ |
|||||
export default defineEventHandler(async (event) => { |
import { HooksUpdateSchema } from '#db/repositories/hooks/types'; |
||||
|
|
||||
|
export default definePermissionEventHandler( |
||||
|
actions.ADMIN, |
||||
|
async ({ event }) => { |
||||
const data = await readValidatedBody( |
const data = await readValidatedBody( |
||||
event, |
event, |
||||
validateZod(hooksUpdateType, event) |
validateZod(HooksUpdateSchema, event) |
||||
); |
); |
||||
await Database.hooks.update(data); |
await Database.hooks.update('wg0', data); |
||||
await WireGuard.saveConfig(); |
await WireGuard.saveConfig(); |
||||
return { success: true }; |
return { success: true }; |
||||
}); |
} |
||||
|
); |
||||
|
@ -1,7 +0,0 @@ |
|||||
export default defineEventHandler(async () => { |
|
||||
const wgInterface = await Database.interfaces.get('wg0'); |
|
||||
return { |
|
||||
...wgInterface, |
|
||||
privateKey: undefined, |
|
||||
}; |
|
||||
}); |
|
@ -1,9 +0,0 @@ |
|||||
export default defineEventHandler(async (event) => { |
|
||||
const data = await readValidatedBody( |
|
||||
event, |
|
||||
validateZod(interfaceUpdateType, event) |
|
||||
); |
|
||||
await Database.system.updateInterface(data); |
|
||||
await WireGuard.saveConfig(); |
|
||||
return { success: true }; |
|
||||
}); |
|
@ -0,0 +1,15 @@ |
|||||
|
import { InterfaceCidrUpdateSchema } from '#db/repositories/interface/types'; |
||||
|
|
||||
|
export default definePermissionEventHandler( |
||||
|
actions.ADMIN, |
||||
|
async ({ event }) => { |
||||
|
const data = await readValidatedBody( |
||||
|
event, |
||||
|
validateZod(InterfaceCidrUpdateSchema, event) |
||||
|
); |
||||
|
|
||||
|
await Database.interfaces.updateCidr('wg0', data); |
||||
|
await WireGuard.saveConfig(); |
||||
|
return { success: true }; |
||||
|
} |
||||
|
); |
@ -0,0 +1,12 @@ |
|||||
|
export default definePermissionEventHandler(actions.ADMIN, async () => { |
||||
|
const wgInterface = await Database.interfaces.get('wg0'); |
||||
|
|
||||
|
if (!wgInterface) { |
||||
|
throw new Error('Interface not found'); |
||||
|
} |
||||
|
|
||||
|
return { |
||||
|
...wgInterface, |
||||
|
privateKey: undefined, |
||||
|
}; |
||||
|
}); |
@ -0,0 +1,14 @@ |
|||||
|
import { InterfaceUpdateSchema } from '#db/repositories/interface/types'; |
||||
|
|
||||
|
export default definePermissionEventHandler( |
||||
|
actions.ADMIN, |
||||
|
async ({ event }) => { |
||||
|
const data = await readValidatedBody( |
||||
|
event, |
||||
|
validateZod(InterfaceUpdateSchema, event) |
||||
|
); |
||||
|
await Database.interfaces.update('wg0', data); |
||||
|
await WireGuard.saveConfig(); |
||||
|
return { success: true }; |
||||
|
} |
||||
|
); |
@ -0,0 +1,7 @@ |
|||||
|
export default definePermissionEventHandler(actions.ADMIN, async () => { |
||||
|
const userConfig = await Database.userConfigs.get('wg0'); |
||||
|
if (!userConfig) { |
||||
|
throw new Error('User config not found'); |
||||
|
} |
||||
|
return userConfig; |
||||
|
}); |
@ -0,0 +1,14 @@ |
|||||
|
import { UserConfigUpdateSchema } from '#db/repositories/userConfig/types'; |
||||
|
|
||||
|
export default definePermissionEventHandler( |
||||
|
actions.ADMIN, |
||||
|
async ({ event }) => { |
||||
|
const data = await readValidatedBody( |
||||
|
event, |
||||
|
validateZod(UserConfigUpdateSchema, event) |
||||
|
); |
||||
|
await Database.userConfigs.update('wg0', data); |
||||
|
await WireGuard.saveConfig(); |
||||
|
return { success: true }; |
||||
|
} |
||||
|
); |
@ -1,9 +0,0 @@ |
|||||
export default defineEventHandler(async (event) => { |
|
||||
const data = await readValidatedBody( |
|
||||
event, |
|
||||
validateZod(cidrUpdateType, event) |
|
||||
); |
|
||||
|
|
||||
await WireGuard.updateAddressRange(data); |
|
||||
return { success: true }; |
|
||||
}); |
|
@ -1,4 +0,0 @@ |
|||||
export default defineEventHandler(async () => { |
|
||||
const system = await Database.system.get(); |
|
||||
return system.userConfig; |
|
||||
}); |
|
@ -1,9 +0,0 @@ |
|||||
export default defineEventHandler(async (event) => { |
|
||||
const data = await readValidatedBody( |
|
||||
event, |
|
||||
validateZod(userConfigUpdateType, event) |
|
||||
); |
|
||||
await Database.system.updateUserConfig(data); |
|
||||
await WireGuard.saveConfig(); |
|
||||
return { success: true }; |
|
||||
}); |
|
@ -1,4 +1,18 @@ |
|||||
import type { InferSelectModel } from 'drizzle-orm'; |
import type { InferSelectModel } from 'drizzle-orm'; |
||||
import type { hooks } from './schema'; |
import type { hooks } from './schema'; |
||||
|
import z from 'zod'; |
||||
|
|
||||
export type HooksType = InferSelectModel<typeof hooks>; |
export type HooksType = InferSelectModel<typeof hooks>; |
||||
|
|
||||
|
export type HooksUpdateType = Omit<HooksType, 'id' | 'createdAt' | 'updatedAt'>; |
||||
|
|
||||
|
const hook = z.string({ message: 'zod.hook' }).pipe(safeStringRefine); |
||||
|
|
||||
|
export const HooksUpdateSchema = schemaForType<HooksUpdateType>()( |
||||
|
z.object({ |
||||
|
preUp: hook, |
||||
|
postUp: hook, |
||||
|
preDown: hook, |
||||
|
postDown: hook, |
||||
|
}) |
||||
|
); |
||||
|
@ -1,4 +1,49 @@ |
|||||
import type { InferSelectModel } from 'drizzle-orm'; |
import type { InferSelectModel } from 'drizzle-orm'; |
||||
import type { wgInterface } from './schema'; |
import type { wgInterface } from './schema'; |
||||
|
import z from 'zod'; |
||||
|
|
||||
export type InterfaceType = InferSelectModel<typeof wgInterface>; |
export type InterfaceType = InferSelectModel<typeof wgInterface>; |
||||
|
|
||||
|
export type InterfaceCreateType = Omit< |
||||
|
InterfaceType, |
||||
|
'createdAt' | 'updatedAt' |
||||
|
>; |
||||
|
|
||||
|
export type InterfaceUpdateType = Omit< |
||||
|
InterfaceCreateType, |
||||
|
'name' | 'createdAt' | 'updatedAt' | 'privateKey' | 'publicKey' |
||||
|
>; |
||||
|
|
||||
|
const device = z |
||||
|
.string({ message: 'zod.device' }) |
||||
|
.min(1, 'zod.deviceMin') |
||||
|
.pipe(safeStringRefine); |
||||
|
|
||||
|
const cidr = z |
||||
|
.string({ message: 'zod.interface.cidr' }) |
||||
|
.min(1, { message: 'zod.interface.cidrMin' }) |
||||
|
.pipe(safeStringRefine); |
||||
|
|
||||
|
export const InterfaceUpdateSchema = schemaForType<InterfaceUpdateType>()( |
||||
|
z.object({ |
||||
|
ipv4Cidr: cidr, |
||||
|
ipv6Cidr: cidr, |
||||
|
mtu: MtuSchema, |
||||
|
port: PortSchema, |
||||
|
device: device, |
||||
|
enabled: EnabledSchema, |
||||
|
}) |
||||
|
); |
||||
|
|
||||
|
export type InterfaceCidrUpdateType = { |
||||
|
ipv4Cidr: string; |
||||
|
ipv6Cidr: string; |
||||
|
}; |
||||
|
|
||||
|
export const InterfaceCidrUpdateSchema = |
||||
|
schemaForType<InterfaceCidrUpdateType>()( |
||||
|
z.object({ |
||||
|
ipv4Cidr: cidr, |
||||
|
ipv6Cidr: cidr, |
||||
|
}) |
||||
|
); |
||||
|
Loading…
Reference in new issue