Browse Source

initial support for initial setup

pull/1736/head
Bernd Storath 3 weeks ago
parent
commit
c4bbadfed7
  1. 1
      Dockerfile
  2. 3
      Dockerfile.dev
  3. 40
      src/server/database/sqlite.ts
  4. 9
      src/server/utils/config.ts

1
Dockerfile

@ -47,6 +47,7 @@ ENV DEBUG=Server,WireGuard,Database,CMD
ENV PORT=51821
ENV HOST=0.0.0.0
ENV INSECURE=false
ENV INIT_ENABLED=false
LABEL org.opencontainers.image.source=https://github.com/wg-easy/wg-easy

3
Dockerfile.dev

@ -26,7 +26,8 @@ RUN update-alternatives --install /usr/sbin/ip6tables ip6tables /usr/sbin/ip6tab
ENV DEBUG=Server,WireGuard,Database,CMD
ENV PORT=51821
ENV HOST=0.0.0.0
ENV INSECURE=false
ENV INSECURE=true
ENV INIT_ENABLED=false
# Install Dependencies
COPY src/package.json src/pnpm-lock.yaml ./

40
src/server/database/sqlite.ts

@ -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,35 @@ async function migrate() {
}
}
}
async function initialSetup(db: DBServiceType) {
const setup = await db.general.getSetupStep();
if (setup.done) {
DB_DEBUG('Warning: Setup already done. Skiping initial setup.');
return;
}
if (WG_INITIAL_ENV.USERNAME && WG_INITIAL_ENV.PASSWORD) {
await db.users.create(WG_INITIAL_ENV.USERNAME, WG_INITIAL_ENV.PASSWORD);
}
if (WG_INITIAL_ENV.IPV4_CIDR && WG_INITIAL_ENV.IPV6_CIDR) {
await db.interfaces.updateCidr({
ipv4Cidr: WG_INITIAL_ENV.IPV4_CIDR,
ipv6Cidr: WG_INITIAL_ENV.IPV6_CIDR,
});
}
if (WG_INITIAL_ENV.DNS) {
const userConfig = await db.userConfigs.get();
await db.userConfigs.update({
...userConfig,
defaultDns: WG_INITIAL_ENV.DNS,
});
}
// TODO: set host, port
await db.general.setSetupStep(0);
}

9
src/server/utils/config.ts

@ -19,6 +19,15 @@ 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,
};
function assertEnv<T extends string>(env: T) {
const val = process.env[env];

Loading…
Cancel
Save