WIP: implement wg-quick as a provider
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful

This commit is contained in:
2025-05-11 00:40:32 -07:00
parent bb80776776
commit 0491189850
7 changed files with 176 additions and 5 deletions

View File

@@ -1,12 +1,32 @@
import { WgProviderOpnsense } from '$lib/server/wg-providers/opnsense';
import { assertGuard } from 'typia';
import { env } from '$env/dynamic/private';
import type { IWgProvider } from '$lib/server/types';
import { WgProviderOpnsense } from '$lib/server/wg-providers/opnsense';
import { WgProviderWgQuick } from '$lib/server/wg-providers/wg-quick';
const wgProvider: IWgProvider = new WgProviderOpnsense({
const opnsense: IWgProvider = new WgProviderOpnsense({
opnsenseUrl: env.OPNSENSE_API_URL,
opnsenseApiKey: env.OPNSENSE_API_KEY,
opnsenseApiSecret: env.OPNSENSE_API_SECRET,
opnsenseWgIfname: env.OPNSENSE_WG_IFNAME,
});
const wgQuick: IWgProvider = new WgProviderWgQuick({
filename: env.WG_QUICK_FILENAME,
address: env.WG_QUICK_ADDRESS,
privateKey: env.WG_QUICK_PRIVATE_KEY,
listenPort: parseInt(env.WG_QUICK_LISTEN_PORT),
});
const providers = {
opnsense,
'wg-quick': wgQuick,
};
const chosenProvider = env.WG_PROVIDER?? 'wg-quick';
assertGuard<keyof typeof providers>(chosenProvider, () =>
Error(`WG_PROVIDER must be one of ${Object.keys(providers).join(', ')}`),
);
const wgProvider = providers[chosenProvider];
export default wgProvider;

View File

@@ -0,0 +1,76 @@
import assert from 'node:assert';
import { appendFile } from 'node:fs/promises';
import type { ClientConnection, CreateClientParams, IWgProvider, WgKeys } from '$lib/server/types';
import { err, ok, type Result } from '$lib/types';
import {
type IWgQuickInterfaceConfig,
wgGenPrivKey,
wgGenPsk,
wgGenPubKey,
wgPeerConfig,
wgQuickInterfaceConfig, wgQuickUp
} from './snippets';
import type { User } from '$lib/server/db/schema';
export class WgProviderWgQuick implements IWgProvider {
private filename: string;
private publicKey?: string;
private config: IWgQuickInterfaceConfig;
constructor(params: WgQuickParams) {
this.filename = params.filename;
this.config = {
address: params.address,
privateKey: params.privateKey,
listenPort: params.listenPort,
};
}
async init(): Promise<Result<null, Error>> {
const file = Bun.file(this.filename);
this.publicKey = await wgGenPubKey(this.config.privateKey);
console.log(`wg-quick: running with public key: ${this.publicKey}`);
if (await file.exists()) {
// TODO: Check if the file is a valid WireGuard config file and our settings match
return ok(null);
}
await Bun.write(this.filename, wgQuickInterfaceConfig(this.config) + '\n');
console.log('created wg-quick config file', this.filename);
return wgQuickUp(this.filename);
}
getServerPublicKey(): string {
assert(this.publicKey, 'WgQuick public key not set, init() must be called first');
return this.publicKey;
}
async generateKeys(): Promise<Result<WgKeys, Error>> {
const privateKey = await wgGenPrivKey();
const publicKey = await wgGenPubKey(privateKey);
const preSharedKey = await wgGenPsk();
return ok({
publicKey,
privateKey,
preSharedKey,
});
}
async createClient(params: CreateClientParams): Promise<Result<null, Error>> {
const peerConfig = wgPeerConfig(params);
await appendFile(this.filename, peerConfig + `\n`);
return ok(null);
}
async findConnections(user: User): Promise<Result<ClientConnection[], Error>> {
return err(Error('WgProviderWgQuick: listing connection information is not yet supported'));
}
async deleteClient(publicKey: string): Promise<Result<null, Error>> {
return err(Error('WgProviderWgQuick: deleting client is not yet supported'));
}
}
interface WgQuickParams extends IWgQuickInterfaceConfig {
filename: string;
}

View File

@@ -0,0 +1,59 @@
import { $ } from 'bun';
import type { CreateClientParams } from '$lib/server/types';
import { err, ok, type Result } from '$lib/types';
export type IWgQuickInterfaceConfig = {
address: string;
privateKey: string;
listenPort: number;
}
export function wgQuickInterfaceConfig(params: IWgQuickInterfaceConfig): string {
return`\
[Interface]
Address = ${params.address}
PrivateKey = ${params.privateKey}
ListenPort = ${params.listenPort}
`;
}
export function wgPeerConfig(params: CreateClientParams): string {
return`\
[Peer]
PublicKey = ${params.publicKey}
PresharedKey = ${params.preSharedKey}
AllowedIPs = ${params.allowedIps}
# vpgen-user = ${params.user.username}
`;
}
export async function runCommand(command: string): Promise<Result<null, Error>> {
const result = await $`${command}`;
if (result.exitCode !== 0) return err(Error(`'${command}' failed with exit code ${result.exitCode}\n${result.stderr.toString()}`));
return ok(null);
}
export async function wgQuickUp(ifname: string): Promise<Result<null, Error>> {
return runCommand(`wg-quick up ${ifname}`);
}
export async function wgQuickDown(ifname: string): Promise<Result<null, Error>> {
return runCommand(`wg-quick down ${ifname}`);
}
export async function wgReload(ifname: string): Promise<Result<null, Error>> {
return runCommand(`wg-quick strip ${ifname} | wg syncconf ${ifname} /dev/stdin`);
// return runCommand(`wg syncconf ${ifname} <(wg-quick strip ${ifname})`);
}
export async function wgGenPubKey(privateKey: string) {
return (await $`echo ${privateKey} | wg pubkey`.text()).trim();
}
export async function wgGenPrivKey() {
return (await $`wg genkey`.text()).trim();
}
export async function wgGenPsk() {
return (await $`wg genpsk`.text()).trim();
}