From e92ee0464ecb6b53f343726635a16ab150af1b6a Mon Sep 17 00:00:00 2001 From: Bernd Storath <32197462+kaaax0815@users.noreply.github.com> Date: Tue, 1 Apr 2025 16:13:51 +0200 Subject: [PATCH] Feat: Server Endpoint (#1785) * add server endpoint to client * be able to update endpoint over api --- src/server/database/migrations/0000_short_skin.sql | 1 + .../database/migrations/meta/0000_snapshot.json | 9 ++++++++- .../database/migrations/meta/0001_snapshot.json | 11 +++++++++-- src/server/database/migrations/meta/_journal.json | 4 ++-- src/server/database/repositories/client/schema.ts | 1 + src/server/database/repositories/client/types.ts | 1 + src/server/utils/WireGuard.ts | 2 ++ src/server/utils/wgHelper.ts | 7 ++++++- 8 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/server/database/migrations/0000_short_skin.sql b/src/server/database/migrations/0000_short_skin.sql index f99d9e5b..5ae181f8 100644 --- a/src/server/database/migrations/0000_short_skin.sql +++ b/src/server/database/migrations/0000_short_skin.sql @@ -17,6 +17,7 @@ CREATE TABLE `clients_table` ( `persistent_keepalive` integer NOT NULL, `mtu` integer NOT NULL, `dns` text, + `server_endpoint` text, `enabled` integer NOT NULL, `created_at` text DEFAULT (CURRENT_TIMESTAMP) NOT NULL, `updated_at` text DEFAULT (CURRENT_TIMESTAMP) NOT NULL, diff --git a/src/server/database/migrations/meta/0000_snapshot.json b/src/server/database/migrations/meta/0000_snapshot.json index 7fc1e71c..c0621f0c 100644 --- a/src/server/database/migrations/meta/0000_snapshot.json +++ b/src/server/database/migrations/meta/0000_snapshot.json @@ -1,7 +1,7 @@ { "version": "6", "dialect": "sqlite", - "id": "91f8ccee-7842-4fd3-bb84-f43e00466b20", + "id": "b812341a-1ec2-49a6-8bc8-0332f5b32df4", "prevId": "00000000-0000-0000-0000-000000000000", "tables": { "clients_table": { @@ -137,6 +137,13 @@ "notNull": false, "autoincrement": false }, + "server_endpoint": { + "name": "server_endpoint", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, "enabled": { "name": "enabled", "type": "integer", diff --git a/src/server/database/migrations/meta/0001_snapshot.json b/src/server/database/migrations/meta/0001_snapshot.json index 1e8fe22f..f19ca3f1 100644 --- a/src/server/database/migrations/meta/0001_snapshot.json +++ b/src/server/database/migrations/meta/0001_snapshot.json @@ -1,6 +1,6 @@ { - "id": "0224c6a5-3456-402d-a40d-0821637015da", - "prevId": "91f8ccee-7842-4fd3-bb84-f43e00466b20", + "id": "c4c5bfb7-a66c-4e6b-a15c-232b16689dcf", + "prevId": "b812341a-1ec2-49a6-8bc8-0332f5b32df4", "version": "6", "dialect": "sqlite", "tables": { @@ -137,6 +137,13 @@ "notNull": false, "autoincrement": false }, + "server_endpoint": { + "name": "server_endpoint", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, "enabled": { "name": "enabled", "type": "integer", diff --git a/src/server/database/migrations/meta/_journal.json b/src/server/database/migrations/meta/_journal.json index 0629bfb4..2fc2534a 100644 --- a/src/server/database/migrations/meta/_journal.json +++ b/src/server/database/migrations/meta/_journal.json @@ -5,14 +5,14 @@ { "idx": 0, "version": "6", - "when": 1743490907551, + "when": 1743515334198, "tag": "0000_short_skin", "breakpoints": true }, { "idx": 1, "version": "6", - "when": 1743490912488, + "when": 1743515338707, "tag": "0001_classy_the_stranger", "breakpoints": true } diff --git a/src/server/database/repositories/client/schema.ts b/src/server/database/repositories/client/schema.ts index 8f5f402e..97c8e8a9 100644 --- a/src/server/database/repositories/client/schema.ts +++ b/src/server/database/repositories/client/schema.ts @@ -31,6 +31,7 @@ export const client = sqliteTable('clients_table', { persistentKeepalive: int('persistent_keepalive').notNull(), mtu: int().notNull(), dns: text({ mode: 'json' }).$type(), + serverEndpoint: text('server_endpoint'), enabled: int({ mode: 'boolean' }).notNull(), createdAt: text('created_at') .notNull() diff --git a/src/server/database/repositories/client/types.ts b/src/server/database/repositories/client/types.ts index f52afabc..6b56abc2 100644 --- a/src/server/database/repositories/client/types.ts +++ b/src/server/database/repositories/client/types.ts @@ -65,6 +65,7 @@ export const ClientUpdateSchema = schemaForType()( serverAllowedIps: serverAllowedIps, mtu: MtuSchema, persistentKeepalive: PersistentKeepaliveSchema, + serverEndpoint: AddressSchema.nullable(), dns: DnsSchema.nullable(), }) ); diff --git a/src/server/utils/WireGuard.ts b/src/server/utils/WireGuard.ts index 1ae62182..e827602a 100644 --- a/src/server/utils/WireGuard.ts +++ b/src/server/utils/WireGuard.ts @@ -34,6 +34,8 @@ class WireGuard { result.push(wg.generateServerPeer(client)); } + result.push(''); + WG_DEBUG('Saving Config...'); await fs.writeFile( `/etc/wireguard/${wgInterface.name}.conf`, diff --git a/src/server/utils/wgHelper.ts b/src/server/utils/wgHelper.ts index ab79109d..7f1c44ec 100644 --- a/src/server/utils/wgHelper.ts +++ b/src/server/utils/wgHelper.ts @@ -13,11 +13,16 @@ export const wg = { ...(client.serverAllowedIps ?? []), ]; + const extraLines = []; + if (client.serverEndpoint) { + extraLines.push(`Endpoint = ${client.serverEndpoint}`); + } + return `# Client: ${client.name} (${client.id}) [Peer] PublicKey = ${client.publicKey} PresharedKey = ${client.preSharedKey} -AllowedIPs = ${allowedIps.join(', ')}`; +AllowedIPs = ${allowedIps.join(', ')}${extraLines.length ? `\n${extraLines.join('\n')}` : ''}`; }, generateServerInterface: (wgInterface: InterfaceType, hooks: HooksType) => {