Feat: Initial Setup through env vars (#1736)
* initial support for initial setup * improve setup * improve mobile view * move base admin route * admin panel mobile view * set initial host and port * add docs * properly setup everything, use for dev env * change userconfig and interface port on setup, note users afterwards
This commit is contained in:
@@ -2,11 +2,10 @@ export default defineEventHandler(async (event) => {
|
||||
const session = await useWGSession(event);
|
||||
|
||||
if (!session.data.userId) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: 'Not logged in',
|
||||
});
|
||||
// not logged in
|
||||
return null;
|
||||
}
|
||||
|
||||
const user = await Database.users.get(session.data.userId);
|
||||
if (!user) {
|
||||
throw createError({
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { eq, sql } from 'drizzle-orm';
|
||||
import { userConfig } from './schema';
|
||||
import type { UserConfigUpdateType } from './types';
|
||||
import { wgInterface } from '#db/schema';
|
||||
import type { DBType } from '#db/sqlite';
|
||||
|
||||
function createPreparedStatement(db: DBType) {
|
||||
@@ -8,14 +9,6 @@ function createPreparedStatement(db: DBType) {
|
||||
get: db.query.userConfig
|
||||
.findFirst({ where: eq(userConfig.id, sql.placeholder('interface')) })
|
||||
.prepare(),
|
||||
updateHostPort: db
|
||||
.update(userConfig)
|
||||
.set({
|
||||
host: sql.placeholder('host') as never as string,
|
||||
port: sql.placeholder('port') as never as number,
|
||||
})
|
||||
.where(eq(userConfig.id, sql.placeholder('interface')))
|
||||
.prepare(),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -38,11 +31,26 @@ export class UserConfigService {
|
||||
return userConfig;
|
||||
}
|
||||
|
||||
// TODO: wrap ipv6 host in square brackets
|
||||
|
||||
/**
|
||||
* sets host of user config
|
||||
*
|
||||
* sets port of user config and interface
|
||||
*/
|
||||
updateHostPort(host: string, port: number) {
|
||||
return this.#statements.updateHostPort.execute({
|
||||
interface: 'wg0',
|
||||
host,
|
||||
port,
|
||||
return this.#db.transaction(async (tx) => {
|
||||
await tx
|
||||
.update(userConfig)
|
||||
.set({ host, port })
|
||||
.where(eq(userConfig.id, 'wg0'))
|
||||
.execute();
|
||||
|
||||
await tx
|
||||
.update(wgInterface)
|
||||
.set({ port })
|
||||
.where(eq(wgInterface.name, 'wg0'))
|
||||
.execute();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,13 @@ const db = drizzle({ client, schema });
|
||||
|
||||
export async function connect() {
|
||||
await migrate();
|
||||
return new DBService(db);
|
||||
const dbService = new DBService(db);
|
||||
|
||||
if (WG_INITIAL_ENV.ENABLED) {
|
||||
await initialSetup(dbService);
|
||||
}
|
||||
|
||||
return dbService;
|
||||
}
|
||||
|
||||
class DBService {
|
||||
@@ -58,3 +64,47 @@ async function migrate() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function initialSetup(db: DBServiceType) {
|
||||
const setup = await db.general.getSetupStep();
|
||||
|
||||
if (setup.done) {
|
||||
DB_DEBUG('Setup already done. Skiping initial setup.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (WG_INITIAL_ENV.IPV4_CIDR && WG_INITIAL_ENV.IPV6_CIDR) {
|
||||
DB_DEBUG('Setting initial CIDR...');
|
||||
await db.interfaces.updateCidr({
|
||||
ipv4Cidr: WG_INITIAL_ENV.IPV4_CIDR,
|
||||
ipv6Cidr: WG_INITIAL_ENV.IPV6_CIDR,
|
||||
});
|
||||
}
|
||||
|
||||
if (WG_INITIAL_ENV.DNS) {
|
||||
DB_DEBUG('Setting initial DNS...');
|
||||
const userConfig = await db.userConfigs.get();
|
||||
await db.userConfigs.update({
|
||||
...userConfig,
|
||||
defaultDns: WG_INITIAL_ENV.DNS,
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
WG_INITIAL_ENV.USERNAME &&
|
||||
WG_INITIAL_ENV.PASSWORD &&
|
||||
WG_INITIAL_ENV.HOST &&
|
||||
WG_INITIAL_ENV.PORT
|
||||
) {
|
||||
DB_DEBUG('Creating initial user...');
|
||||
await db.users.create(WG_INITIAL_ENV.USERNAME, WG_INITIAL_ENV.PASSWORD);
|
||||
|
||||
DB_DEBUG('Setting initial host and port...');
|
||||
await db.userConfigs.updateHostPort(
|
||||
WG_INITIAL_ENV.HOST,
|
||||
WG_INITIAL_ENV.PORT
|
||||
);
|
||||
|
||||
await db.general.setSetupStep(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,19 @@ export const WG_ENV = {
|
||||
PORT: assertEnv('PORT'),
|
||||
};
|
||||
|
||||
export const WG_INITIAL_ENV = {
|
||||
ENABLED: process.env.INIT_ENABLED === 'true',
|
||||
USERNAME: process.env.INIT_USERNAME,
|
||||
PASSWORD: process.env.INIT_PASSWORD,
|
||||
DNS: process.env.INIT_DNS?.split(',').map((x) => x.trim()),
|
||||
IPV4_CIDR: process.env.INIT_IPV4_CIDR,
|
||||
IPV6_CIDR: process.env.INIT_IPV6_CIDR,
|
||||
HOST: process.env.INIT_HOST,
|
||||
PORT: process.env.INIT_PORT
|
||||
? Number.parseInt(process.env.INIT_PORT, 10)
|
||||
: undefined,
|
||||
};
|
||||
|
||||
function assertEnv<T extends string>(env: T) {
|
||||
const val = process.env[env];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user