From 73d8ebd77f7d80f0cbda7d4795e8e1c2e0702221 Mon Sep 17 00:00:00 2001 From: Bernd Storath <32197462+kaaax0815@users.noreply.github.com> Date: Thu, 5 Sep 2024 07:37:09 +0200 Subject: [PATCH] Fix: Database Date type (#1349) --- src/server/utils/WireGuard.ts | 15 +++++++++------ src/services/database/lowdb.ts | 6 +++--- src/services/database/repositories/client.ts | 14 +++++++++----- src/services/database/repositories/database.ts | 2 +- src/services/database/repositories/user.ts | 6 ++++-- 5 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/server/utils/WireGuard.ts b/src/server/utils/WireGuard.ts index 7d1be6a1..85f6462f 100644 --- a/src/server/utils/WireGuard.ts +++ b/src/server/utils/WireGuard.ts @@ -224,7 +224,7 @@ Endpoint = ${system.wgHost}:${system.wgConfigPort}`; date.setHours(23); date.setMinutes(59); date.setSeconds(59); - client.expiresAt = date; + client.expiresAt = date.toISOString(); } await Database.createClient(client); @@ -248,7 +248,7 @@ Endpoint = ${system.wgHost}:${system.wgConfigPort}`; async generateOneTimeLink({ clientId }: { clientId: string }) { const key = `${clientId}-${Math.floor(Math.random() * 1000)}`; const oneTimeLink = Math.abs(CRC32.str(key)).toString(16); - const expiresAt = new Date(Date.now() + 5 * 60 * 1000); + const expiresAt = new Date(Date.now() + 5 * 60 * 1000).toISOString(); await Database.createOneTimeLink(clientId, { oneTimeLink, expiresAt, @@ -305,14 +305,14 @@ Endpoint = ${system.wgHost}:${system.wgConfigPort}`; clientId: string; expireDate: string | null; }) { - let updatedDate: Date | null = null; + let updatedDate: string | null = null; if (expireDate) { const date = new Date(expireDate); date.setHours(23); date.setMinutes(59); date.setSeconds(59); - updatedDate = date; + updatedDate = date.toISOString(); } await Database.updateClientExpirationDate(clientId, updatedDate); @@ -394,7 +394,10 @@ Endpoint = ${system.wgHost}:${system.wgConfigPort}`; if (system.clientExpiration.enabled) { for (const client of Object.values(clients)) { if (client.enabled !== true) continue; - if (client.expiresAt !== null && new Date() > client.expiresAt) { + if ( + client.expiresAt !== null && + new Date() > new Date(client.expiresAt) + ) { DEBUG(`Client ${client.id} expired.`); await Database.toggleClient(client.id, false); } @@ -405,7 +408,7 @@ Endpoint = ${system.wgHost}:${system.wgConfigPort}`; for (const client of Object.values(clients)) { if ( client.oneTimeLink !== null && - new Date() > client.oneTimeLink.expiresAt + new Date() > new Date(client.oneTimeLink.expiresAt) ) { DEBUG(`Client ${client.id} One Time Link expired.`); await Database.deleteOneTimeLink(client.id); diff --git a/src/services/database/lowdb.ts b/src/services/database/lowdb.ts index 303c225f..c4ff8ad1 100644 --- a/src/services/database/lowdb.ts +++ b/src/services/database/lowdb.ts @@ -95,7 +95,7 @@ export default class LowDB extends DatabaseProvider { throw new DatabaseError(DatabaseError.ERROR_USER_EXIST); } - const now = new Date(); + const now = new Date().toISOString(); const isUserEmpty = this.#db.data.users.length === 0; const newUser: User = { @@ -141,7 +141,7 @@ export default class LowDB extends DatabaseProvider { async createClient(client: NewClient) { DEBUG('Create Client'); - const now = new Date(); + const now = new Date().toISOString(); const newClient: Client = { ...client, createdAt: now, updatedAt: now }; await this.#db.update((data) => { data.clients[client.id] = newClient; @@ -184,7 +184,7 @@ export default class LowDB extends DatabaseProvider { }); } - async updateClientExpirationDate(id: string, expirationDate: Date | null) { + async updateClientExpirationDate(id: string, expirationDate: string | null) { DEBUG('Update Client Address'); await this.#db.update((data) => { if (data.clients[id]) { diff --git a/src/services/database/repositories/client.ts b/src/services/database/repositories/client.ts index a84faa72..8666f300 100644 --- a/src/services/database/repositories/client.ts +++ b/src/services/database/repositories/client.ts @@ -1,6 +1,7 @@ export type OneTimeLink = { oneTimeLink: string; - expiresAt: Date; + /** ISO String */ + expiresAt: string; }; export type Client = { @@ -10,12 +11,15 @@ export type Client = { privateKey: string; publicKey: string; preSharedKey: string; - expiresAt: Date | null; + /** ISO String */ + expiresAt: string | null; endpoint: string | null; allowedIPs: string[]; oneTimeLink: OneTimeLink | null; - createdAt: Date; - updatedAt: Date; + /** ISO String */ + createdAt: string; + /** ISO String */ + updatedAt: string; enabled: boolean; persistentKeepalive: number; }; @@ -36,7 +40,7 @@ export interface ClientRepository { updateClientAddress(id: string, address: string): Promise; updateClientExpirationDate( id: string, - expirationDate: Date | null + expirationDate: string | null ): Promise; deleteOneTimeLink(id: string): Promise; createOneTimeLink(id: string, oneTimeLink: OneTimeLink): Promise; diff --git a/src/services/database/repositories/database.ts b/src/services/database/repositories/database.ts index 1e7ca39c..3c8c1e5f 100644 --- a/src/services/database/repositories/database.ts +++ b/src/services/database/repositories/database.ts @@ -62,7 +62,7 @@ export abstract class DatabaseProvider abstract updateClientAddress(id: string, address: string): Promise; abstract updateClientExpirationDate( id: string, - expirationDate: Date | null + expirationDate: string | null ): Promise; abstract deleteOneTimeLink(id: string): Promise; abstract createOneTimeLink( diff --git a/src/services/database/repositories/user.ts b/src/services/database/repositories/user.ts index c93c64fc..260b113a 100644 --- a/src/services/database/repositories/user.ts +++ b/src/services/database/repositories/user.ts @@ -21,8 +21,10 @@ export type User = { privateKey?: string; publicKey?: string; preSharedKey?: string; - createdAt: Date; - updatedAt: Date; + /** ISO String */ + createdAt: string; + /** ISO String */ + updatedAt: string; enabled: boolean; };