Browse Source

Fix: Database Date type (#1349)

pull/1648/head
Bernd Storath 8 months ago
committed by Bernd Storath
parent
commit
a34bd0e5f5
  1. 15
      src/server/utils/WireGuard.ts
  2. 6
      src/services/database/lowdb.ts
  3. 14
      src/services/database/repositories/client.ts
  4. 2
      src/services/database/repositories/database.ts
  5. 6
      src/services/database/repositories/user.ts

15
src/server/utils/WireGuard.ts

@ -224,7 +224,7 @@ Endpoint = ${system.wgHost}:${system.wgConfigPort}`;
date.setHours(23); date.setHours(23);
date.setMinutes(59); date.setMinutes(59);
date.setSeconds(59); date.setSeconds(59);
client.expiresAt = date; client.expiresAt = date.toISOString();
} }
await Database.createClient(client); await Database.createClient(client);
@ -248,7 +248,7 @@ Endpoint = ${system.wgHost}:${system.wgConfigPort}`;
async generateOneTimeLink({ clientId }: { clientId: string }) { async generateOneTimeLink({ clientId }: { clientId: string }) {
const key = `${clientId}-${Math.floor(Math.random() * 1000)}`; const key = `${clientId}-${Math.floor(Math.random() * 1000)}`;
const oneTimeLink = Math.abs(CRC32.str(key)).toString(16); 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, { await Database.createOneTimeLink(clientId, {
oneTimeLink, oneTimeLink,
expiresAt, expiresAt,
@ -305,14 +305,14 @@ Endpoint = ${system.wgHost}:${system.wgConfigPort}`;
clientId: string; clientId: string;
expireDate: string | null; expireDate: string | null;
}) { }) {
let updatedDate: Date | null = null; let updatedDate: string | null = null;
if (expireDate) { if (expireDate) {
const date = new Date(expireDate); const date = new Date(expireDate);
date.setHours(23); date.setHours(23);
date.setMinutes(59); date.setMinutes(59);
date.setSeconds(59); date.setSeconds(59);
updatedDate = date; updatedDate = date.toISOString();
} }
await Database.updateClientExpirationDate(clientId, updatedDate); await Database.updateClientExpirationDate(clientId, updatedDate);
@ -394,7 +394,10 @@ Endpoint = ${system.wgHost}:${system.wgConfigPort}`;
if (system.clientExpiration.enabled) { if (system.clientExpiration.enabled) {
for (const client of Object.values(clients)) { for (const client of Object.values(clients)) {
if (client.enabled !== true) continue; 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.`); DEBUG(`Client ${client.id} expired.`);
await Database.toggleClient(client.id, false); await Database.toggleClient(client.id, false);
} }
@ -405,7 +408,7 @@ Endpoint = ${system.wgHost}:${system.wgConfigPort}`;
for (const client of Object.values(clients)) { for (const client of Object.values(clients)) {
if ( if (
client.oneTimeLink !== null && client.oneTimeLink !== null &&
new Date() > client.oneTimeLink.expiresAt new Date() > new Date(client.oneTimeLink.expiresAt)
) { ) {
DEBUG(`Client ${client.id} One Time Link expired.`); DEBUG(`Client ${client.id} One Time Link expired.`);
await Database.deleteOneTimeLink(client.id); await Database.deleteOneTimeLink(client.id);

6
src/services/database/lowdb.ts

@ -95,7 +95,7 @@ export default class LowDB extends DatabaseProvider {
throw new DatabaseError(DatabaseError.ERROR_USER_EXIST); 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 isUserEmpty = this.#db.data.users.length === 0;
const newUser: User = { const newUser: User = {
@ -141,7 +141,7 @@ export default class LowDB extends DatabaseProvider {
async createClient(client: NewClient) { async createClient(client: NewClient) {
DEBUG('Create Client'); DEBUG('Create Client');
const now = new Date(); const now = new Date().toISOString();
const newClient: Client = { ...client, createdAt: now, updatedAt: now }; const newClient: Client = { ...client, createdAt: now, updatedAt: now };
await this.#db.update((data) => { await this.#db.update((data) => {
data.clients[client.id] = newClient; 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'); DEBUG('Update Client Address');
await this.#db.update((data) => { await this.#db.update((data) => {
if (data.clients[id]) { if (data.clients[id]) {

14
src/services/database/repositories/client.ts

@ -1,6 +1,7 @@
export type OneTimeLink = { export type OneTimeLink = {
oneTimeLink: string; oneTimeLink: string;
expiresAt: Date; /** ISO String */
expiresAt: string;
}; };
export type Client = { export type Client = {
@ -10,12 +11,15 @@ export type Client = {
privateKey: string; privateKey: string;
publicKey: string; publicKey: string;
preSharedKey: string; preSharedKey: string;
expiresAt: Date | null; /** ISO String */
expiresAt: string | null;
endpoint: string | null; endpoint: string | null;
allowedIPs: string[]; allowedIPs: string[];
oneTimeLink: OneTimeLink | null; oneTimeLink: OneTimeLink | null;
createdAt: Date; /** ISO String */
updatedAt: Date; createdAt: string;
/** ISO String */
updatedAt: string;
enabled: boolean; enabled: boolean;
persistentKeepalive: number; persistentKeepalive: number;
}; };
@ -36,7 +40,7 @@ export interface ClientRepository {
updateClientAddress(id: string, address: string): Promise<void>; updateClientAddress(id: string, address: string): Promise<void>;
updateClientExpirationDate( updateClientExpirationDate(
id: string, id: string,
expirationDate: Date | null expirationDate: string | null
): Promise<void>; ): Promise<void>;
deleteOneTimeLink(id: string): Promise<void>; deleteOneTimeLink(id: string): Promise<void>;
createOneTimeLink(id: string, oneTimeLink: OneTimeLink): Promise<void>; createOneTimeLink(id: string, oneTimeLink: OneTimeLink): Promise<void>;

2
src/services/database/repositories/database.ts

@ -62,7 +62,7 @@ export abstract class DatabaseProvider
abstract updateClientAddress(id: string, address: string): Promise<void>; abstract updateClientAddress(id: string, address: string): Promise<void>;
abstract updateClientExpirationDate( abstract updateClientExpirationDate(
id: string, id: string,
expirationDate: Date | null expirationDate: string | null
): Promise<void>; ): Promise<void>;
abstract deleteOneTimeLink(id: string): Promise<void>; abstract deleteOneTimeLink(id: string): Promise<void>;
abstract createOneTimeLink( abstract createOneTimeLink(

6
src/services/database/repositories/user.ts

@ -21,8 +21,10 @@ export type User = {
privateKey?: string; privateKey?: string;
publicKey?: string; publicKey?: string;
preSharedKey?: string; preSharedKey?: string;
createdAt: Date; /** ISO String */
updatedAt: Date; createdAt: string;
/** ISO String */
updatedAt: string;
enabled: boolean; enabled: boolean;
}; };

Loading…
Cancel
Save