Browse Source

Add preUp preDown postUp postDown for client

pull/1714/head
yanghuanglin 5 months ago
parent
commit
d96e8256d8
  1. 7
      src/app/pages/clients/[id].vue
  2. 3
      src/i18n/locales/en.json
  3. 8
      src/server/api/setup/migrate.post.ts
  4. 4
      src/server/database/migrations/0000_short_skin.sql
  5. 28
      src/server/database/migrations/meta/0000_snapshot.json
  6. 28
      src/server/database/migrations/meta/0001_snapshot.json
  7. 4
      src/server/database/repositories/client/schema.ts
  8. 16
      src/server/database/repositories/client/service.ts
  9. 28
      src/server/database/repositories/client/types.ts
  10. 2
      src/server/database/repositories/userConfig/types.ts
  11. 16
      src/server/utils/wgHelper.ts

7
src/app/pages/clients/[id].vue

@ -71,6 +71,13 @@
:label="$t('general.persistentKeepalive')" :label="$t('general.persistentKeepalive')"
/> />
</FormGroup> </FormGroup>
<FormGroup>
<FormHeading>{{ $t('form.hooks') }}</FormHeading>
<FormTextField id="PreUp" v-model="data.preUp" label="PreUp" />
<FormTextField id="PostUp" v-model="data.postUp" label="PostUp" />
<FormTextField id="PreDown" v-model="data.preDown" label="PreDown" />
<FormTextField id="PostDown" v-model="data.postDown" label="PostDown" />
</FormGroup>
<FormGroup> <FormGroup>
<FormHeading>{{ $t('form.actions') }}</FormHeading> <FormHeading>{{ $t('form.actions') }}</FormHeading>
<FormActionField type="submit" :label="$t('form.save')" /> <FormActionField type="submit" :label="$t('form.save')" />

3
src/i18n/locales/en.json

@ -118,7 +118,8 @@
"sectionGeneral": "General", "sectionGeneral": "General",
"sectionAdvanced": "Advanced", "sectionAdvanced": "Advanced",
"noItems": "No items", "noItems": "No items",
"add": "Add" "add": "Add",
"hooks": "Hooks"
}, },
"admin": { "admin": {
"general": { "general": {

8
src/server/api/setup/migrate.post.ts

@ -28,6 +28,10 @@ export default defineSetupEventHandler('migrate', async ({ event }) => {
createdAt: z.string(), createdAt: z.string(),
updatedAt: z.string(), updatedAt: z.string(),
enabled: z.boolean(), enabled: z.boolean(),
preUp: z.string(),
postUp: z.string(),
preDown: z.string(),
postDown: z.string(),
}) })
), ),
}); });
@ -68,6 +72,10 @@ export default defineSetupEventHandler('migrate', async ({ event }) => {
...clientConfig, ...clientConfig,
ipv4Address: clientConfig.address, ipv4Address: clientConfig.address,
ipv6Address, ipv6Address,
preUp: clientConfig.preUp,
postUp: clientConfig.postUp,
preDown: clientConfig.preDown,
postDown: clientConfig.postDown
}); });
} }

4
src/server/database/migrations/0000_short_skin.sql

@ -12,6 +12,10 @@ CREATE TABLE `clients_table` (
`server_allowed_ips` text NOT NULL, `server_allowed_ips` text NOT NULL,
`persistent_keepalive` integer NOT NULL, `persistent_keepalive` integer NOT NULL,
`mtu` integer NOT NULL, `mtu` integer NOT NULL,
`pre_up` text,
`post_up` text,
`pre_down` text,
`post_down` text,
`dns` text NOT NULL, `dns` text NOT NULL,
`enabled` integer NOT NULL, `enabled` integer NOT NULL,
`created_at` text DEFAULT (CURRENT_TIMESTAMP) NOT NULL, `created_at` text DEFAULT (CURRENT_TIMESTAMP) NOT NULL,

28
src/server/database/migrations/meta/0000_snapshot.json

@ -98,6 +98,34 @@
"notNull": true, "notNull": true,
"autoincrement": false "autoincrement": false
}, },
"pre_up": {
"name": "pre_up",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"post_up": {
"name": "post_up",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"pre_down": {
"name": "pre_down",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"post_down": {
"name": "post_down",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"dns": { "dns": {
"name": "dns", "name": "dns",
"type": "text", "type": "text",

28
src/server/database/migrations/meta/0001_snapshot.json

@ -98,6 +98,34 @@
"notNull": true, "notNull": true,
"autoincrement": false "autoincrement": false
}, },
"pre_up": {
"name": "pre_up",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"post_up": {
"name": "post_up",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"pre_down": {
"name": "pre_down",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"post_down": {
"name": "post_down",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"dns": { "dns": {
"name": "dns", "name": "dns",
"type": "text", "type": "text",

4
src/server/database/repositories/client/schema.ts

@ -14,6 +14,10 @@ export const client = sqliteTable('clients_table', {
name: text().notNull(), name: text().notNull(),
ipv4Address: text('ipv4_address').notNull().unique(), ipv4Address: text('ipv4_address').notNull().unique(),
ipv6Address: text('ipv6_address').notNull().unique(), ipv6Address: text('ipv6_address').notNull().unique(),
preUp: text('pre_up'),
postUp: text('post_up'),
preDown: text('pre_down'),
postDown: text('post_down'),
privateKey: text('private_key').notNull(), privateKey: text('private_key').notNull(),
publicKey: text('public_key').notNull(), publicKey: text('public_key').notNull(),
preSharedKey: text('pre_shared_key').notNull(), preSharedKey: text('pre_shared_key').notNull(),

16
src/server/database/repositories/client/service.ts

@ -101,6 +101,10 @@ export class ClientService {
const ipv4Address = nextIP(4, ipv4Cidr, clients); const ipv4Address = nextIP(4, ipv4Cidr, clients);
const ipv6Cidr = parseCidr(clientInterface.ipv6Cidr); const ipv6Cidr = parseCidr(clientInterface.ipv6Cidr);
const ipv6Address = nextIP(6, ipv6Cidr, clients); const ipv6Address = nextIP(6, ipv6Cidr, clients);
const preUp = '';
const postUp = '';
const preDown = '';
const postDown = '';
await tx await tx
.insert(client) .insert(client)
@ -114,6 +118,10 @@ export class ClientService {
preSharedKey, preSharedKey,
ipv4Address, ipv4Address,
ipv6Address, ipv6Address,
preUp,
postUp,
preDown,
postDown,
mtu: clientConfig.defaultMtu, mtu: clientConfig.defaultMtu,
allowedIps: clientConfig.defaultAllowedIps, allowedIps: clientConfig.defaultAllowedIps,
dns: clientConfig.defaultDns, dns: clientConfig.defaultDns,
@ -145,6 +153,10 @@ export class ClientService {
preSharedKey, preSharedKey,
privateKey, privateKey,
publicKey, publicKey,
preUp,
postUp,
preDown,
postDown
}: ClientCreateFromExistingType) { }: ClientCreateFromExistingType) {
const clientConfig = await Database.userConfigs.get(); const clientConfig = await Database.userConfigs.get();
@ -158,6 +170,10 @@ export class ClientService {
preSharedKey, preSharedKey,
ipv4Address, ipv4Address,
ipv6Address, ipv6Address,
preUp,
postUp,
preDown,
postDown,
mtu: clientConfig.defaultMtu, mtu: clientConfig.defaultMtu,
allowedIps: clientConfig.defaultAllowedIps, allowedIps: clientConfig.defaultAllowedIps,
dns: clientConfig.defaultDns, dns: clientConfig.defaultDns,

28
src/server/database/repositories/client/types.ts

@ -39,6 +39,26 @@ const address6 = z
.min(1, { message: t('zod.client.address6') }) .min(1, { message: t('zod.client.address6') })
.pipe(safeStringRefine); .pipe(safeStringRefine);
const preUp = z
.string({ message: t('zod.client.preUp') })
.pipe(safeStringRefine)
.nullable();
const postUp = z
.string({ message: t('zod.client.postUp') })
.pipe(safeStringRefine)
.nullable();
const preDown = z
.string({ message: t('zod.client.preDown') })
.pipe(safeStringRefine)
.nullable();
const postDown = z
.string({ message: t('zod.client.postDown') })
.pipe(safeStringRefine)
.nullable();
const serverAllowedIps = z.array(AddressSchema, { const serverAllowedIps = z.array(AddressSchema, {
message: t('zod.client.serverAllowedIps'), message: t('zod.client.serverAllowedIps'),
}); });
@ -57,6 +77,10 @@ export const ClientUpdateSchema = schemaForType<UpdateClientType>()(
expiresAt: expiresAt, expiresAt: expiresAt,
ipv4Address: address4, ipv4Address: address4,
ipv6Address: address6, ipv6Address: address6,
preUp: preUp,
postUp: postUp,
preDown: preDown,
postDown: postDown,
allowedIps: AllowedIpsSchema, allowedIps: AllowedIpsSchema,
serverAllowedIps: serverAllowedIps, serverAllowedIps: serverAllowedIps,
mtu: MtuSchema, mtu: MtuSchema,
@ -77,6 +101,10 @@ export type ClientCreateFromExistingType = Pick<
| 'name' | 'name'
| 'ipv4Address' | 'ipv4Address'
| 'ipv6Address' | 'ipv6Address'
| 'preUp'
| 'postUp'
| 'preDown'
| 'postDown'
| 'privateKey' | 'privateKey'
| 'preSharedKey' | 'preSharedKey'
| 'publicKey' | 'publicKey'

2
src/server/database/repositories/userConfig/types.ts

@ -26,6 +26,6 @@ export const UserConfigUpdateSchema = schemaForType<UserConfigUpdateType>()(
defaultPersistentKeepalive: PersistentKeepaliveSchema, defaultPersistentKeepalive: PersistentKeepaliveSchema,
defaultDns: DnsSchema, defaultDns: DnsSchema,
defaultAllowedIps: AllowedIpsSchema, defaultAllowedIps: AllowedIpsSchema,
host: host, host: host
}) })
); );

16
src/server/utils/wgHelper.ts

@ -54,6 +54,10 @@ PrivateKey = ${client.privateKey}
Address = ${client.ipv4Address}/${cidr4Block}, ${client.ipv6Address}/${cidr6Block} Address = ${client.ipv4Address}/${cidr4Block}, ${client.ipv6Address}/${cidr6Block}
DNS = ${client.dns.join(', ')} DNS = ${client.dns.join(', ')}
MTU = ${client.mtu} MTU = ${client.mtu}
PreUp = ${client.preUp}
PostUp = ${client.postUp}
PreDown = ${client.preDown}
PostDown = ${client.postDown}
[Peer] [Peer]
PublicKey = ${wgInterface.publicKey} PublicKey = ${wgInterface.publicKey}
@ -103,6 +107,10 @@ Endpoint = ${userConfig.host}:${userConfig.port}`;
string, string,
string, string,
string, string,
string,
string,
string,
string,
]; ];
return rawDump return rawDump
@ -120,6 +128,10 @@ Endpoint = ${userConfig.host}:${userConfig.port}`;
transferRx, transferRx,
transferTx, transferTx,
persistentKeepalive, persistentKeepalive,
preUp,
postUp,
preDown,
postDown
] = splitLines as wgDumpLine; ] = splitLines as wgDumpLine;
return { return {
@ -134,6 +146,10 @@ Endpoint = ${userConfig.host}:${userConfig.port}`;
transferRx: Number.parseInt(transferRx), transferRx: Number.parseInt(transferRx),
transferTx: Number.parseInt(transferTx), transferTx: Number.parseInt(transferTx),
persistentKeepalive: persistentKeepalive, persistentKeepalive: persistentKeepalive,
preUp: preUp,
postUp: postUp,
preDown: preDown,
postDown: postDown
}; };
}); });
}, },

Loading…
Cancel
Save