Browse Source

Fix: Database Date type (#1349)

pull/1648/head
Bernd Storath 7 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.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);

6
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]) {

14
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<void>;
updateClientExpirationDate(
id: string,
expirationDate: Date | null
expirationDate: string | null
): Promise<void>;
deleteOneTimeLink(id: string): 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 updateClientExpirationDate(
id: string,
expirationDate: Date | null
expirationDate: string | null
): Promise<void>;
abstract deleteOneTimeLink(id: string): Promise<void>;
abstract createOneTimeLink(

6
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;
};

Loading…
Cancel
Save