diff --git a/src/server/middleware/setup.ts b/src/server/middleware/setup.ts index d0e3a3e9..ede7f88e 100644 --- a/src/server/middleware/setup.ts +++ b/src/server/middleware/setup.ts @@ -4,7 +4,8 @@ export default defineEventHandler(async (event) => { if ( url.pathname.startsWith('/setup') || - url.pathname === '/api/account/new' + url.pathname === '/api/account/new' || + url.pathname === '/api/features' ) { return; } diff --git a/src/server/utils/Database.ts b/src/server/utils/Database.ts index b240f6f8..3de346d9 100644 --- a/src/server/utils/Database.ts +++ b/src/server/utils/Database.ts @@ -13,5 +13,4 @@ provider.connect().catch((err) => { }); // TODO: check if old config exists and tell user about migration path - export default provider; diff --git a/src/server/utils/WireGuard.ts b/src/server/utils/WireGuard.ts index d6ceaa05..0715de20 100644 --- a/src/server/utils/WireGuard.ts +++ b/src/server/utils/WireGuard.ts @@ -334,12 +334,55 @@ Endpoint = ${system.wgHost}:${system.wgConfigPort}`; return backup; */ } + async Startup() { + // TODO: improve this + await new Promise((res) => { + function wait() { + if (Database.connected) { + return res(true); + } + } + setTimeout(wait, 1000); + }); + DEBUG('Starting Wireguard'); + await this.#saveWireguardConfig(); + await exec('wg-quick down wg0').catch(() => {}); + await exec('wg-quick up wg0').catch((err) => { + if ( + err && + err.message && + err.message.includes('Cannot find device "wg0"') + ) { + throw new Error( + 'WireGuard exited with the error: Cannot find device "wg0"\nThis usually means that your host\'s kernel does not support WireGuard!' + ); + } + + throw err; + }); + await this.#syncWireguardConfig(); + DEBUG('Wireguard started successfully'); + + DEBUG('Starting Cron Job'); + await this.startCronJob(); + } + + async startCronJob() { + await this.cronJob().catch((err) => { + DEBUG('Running Cron Job failed.'); + console.error(err); + }); + setTimeout(() => { + this.startCronJob(); + }, 60 * 1000); + } + // Shutdown wireguard async Shutdown() { await exec('wg-quick down wg0').catch(() => {}); } - async cronJobEveryMinute() { + async cronJob() { const clients = await Database.getClients(); const system = await Database.getSystem(); // Expires Feature @@ -441,15 +484,9 @@ Endpoint = ${system.wgHost}:${system.wgConfigPort}`; } const inst = new WireGuard(); - -// This also has to also start the WireGuard Server -async function cronJobEveryMinute() { - await inst.cronJobEveryMinute().catch((err) => { - DEBUG('Running Cron Job failed.'); - console.error(err); - }); - setTimeout(cronJobEveryMinute, 60 * 1000); -} -cronJobEveryMinute(); +inst.Startup().catch((v) => { + console.error(v); + process.exit(1); +}); export default inst; diff --git a/src/services/database/lowdb.ts b/src/services/database/lowdb.ts index f127d423..a9c9ffd5 100644 --- a/src/services/database/lowdb.ts +++ b/src/services/database/lowdb.ts @@ -19,6 +19,7 @@ const DEBUG = debug('LowDB'); export default class LowDB extends DatabaseProvider { #db!: Low; + #connected = false; // is this really needed? private async __init() { @@ -30,6 +31,9 @@ export default class LowDB extends DatabaseProvider { * @throws */ async connect() { + if (this.#connected) { + return; + } try { await this.__init(); DEBUG('Running Migrations'); @@ -39,11 +43,16 @@ export default class LowDB extends DatabaseProvider { DEBUG(e); throw new DatabaseError(DatabaseError.ERROR_INIT); } - + this.#connected = true; DEBUG('Connected successfully'); } + get connected() { + return this.#connected; + } + async disconnect() { + this.#connected = false; DEBUG('Disconnected successfully'); } @@ -104,7 +113,7 @@ export default class LowDB extends DatabaseProvider { } async updateUser(user: User) { - // TODO: avoid mutation, prefer .update + // TODO: avoid mutation, prefer .update, updatedAt let oldUser = await this.getUser(user.id); if (oldUser) { DEBUG('Update User'); diff --git a/src/services/database/migrations/1.ts b/src/services/database/migrations/1.ts index fc000e49..e36300cc 100644 --- a/src/services/database/migrations/1.ts +++ b/src/services/database/migrations/1.ts @@ -12,6 +12,7 @@ export async function run1(db: Low) { const database: Database = { migrations: [], system: { + // TODO: move to var, no need for database release: packageJson.release.version, interface: { privateKey: privateKey,