Browse Source

fix some issues

pull/1345/head
Bernd Storath 2 years ago
parent
commit
e55ea79463
  1. 3
      src/server/middleware/setup.ts
  2. 1
      src/server/utils/Database.ts
  3. 59
      src/server/utils/WireGuard.ts
  4. 13
      src/services/database/lowdb.ts
  5. 1
      src/services/database/migrations/1.ts

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

1
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;

59
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;

13
src/services/database/lowdb.ts

@ -19,6 +19,7 @@ const DEBUG = debug('LowDB');
export default class LowDB extends DatabaseProvider {
#db!: Low<Database>;
#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');

1
src/services/database/migrations/1.ts

@ -12,6 +12,7 @@ export async function run1(db: Low<Database>) {
const database: Database = {
migrations: [],
system: {
// TODO: move to var, no need for database
release: packageJson.release.version,
interface: {
privateKey: privateKey,

Loading…
Cancel
Save