fix the issue with healthcheck and silence the annoying warning form sveltekit-superforms

This commit is contained in:
Shahrad Elahi 2023-12-21 15:42:26 +03:30
parent 479c4093fc
commit 523cc04105
3 changed files with 47 additions and 7 deletions

View File

@ -131,6 +131,32 @@ export class WGServer {
await this.update({ confHash: getConfigHash(wg.confId) }); await this.update({ confHash: getConfigHash(wg.confId) });
} }
async hasInterface(): Promise<boolean> {
const server = await this.get();
return await Network.checkInterfaceExists(`wg${server.confId}`);
}
async getUsage(): Promise<WgUsage> {
const server = await this.get();
const hasInterface = await this.hasInterface();
if (!hasInterface) {
logger.error('GetUsage: interface does not exists');
return new Map();
}
const res = await Shell.exec(`wg show wg${server.confId} transfer`);
const lines = res.split('\n');
const usages: WgUsage = new Map();
for (const line of lines) {
const [peer, rx, tx] = line.split('\t');
if (!peer) continue;
usages.set(peer, { rx: Number(rx), tx: Number(tx) });
}
return usages;
}
static async getFreePeerIp(serverId: string): Promise<string | undefined> { static async getFreePeerIp(serverId: string): Promise<string | undefined> {
const server = await findServer(serverId); const server = await findServer(serverId);
if (!server) { if (!server) {
@ -154,6 +180,13 @@ export class WGServer {
} }
} }
export type WgUsage = Map<string, PeerUsage>;
export type PeerUsage = {
rx: number;
tx: number;
};
class WGPeers { class WGPeers {
private readonly server: WGServer; private readonly server: WGServer;

View File

@ -1,18 +1,22 @@
import type { RequestHandler } from '@sveltejs/kit'; import type { RequestHandler } from '@sveltejs/kit';
import { getConfigHash, getServers, WGServer } from '$lib/wireguard'; import { getServers, WGServer } from '$lib/wireguard';
import logger from '$lib/logger'; import logger from '$lib/logger';
export const GET: RequestHandler = async () => { export const GET: RequestHandler = async () => {
try { try {
const servers = await getServers(); for (const { id } of await getServers()) {
const wg = new WGServer(id);
const server = await wg.get();
const hasInterface = await wg.hasInterface();
for (const s of servers) { // If the server is up and the interface doesn't exist, start it
const wg = new WGServer(s.id); if (server.status === 'up' && !hasInterface) {
// Start server
if (s.status === 'up') {
await wg.start(); await wg.start();
} }
if (server.status === 'down' && hasInterface) {
await wg.stop();
}
} }
} catch (e) { } catch (e) {
logger.error('APIFailed: HealthCheck:', e); logger.error('APIFailed: HealthCheck:', e);

View File

@ -18,6 +18,9 @@
const options: FormOptions<typeof formSchema> = { const options: FormOptions<typeof formSchema> = {
validators: formSchema, validators: formSchema,
warnings: {
noValidationAndConstraints: false,
},
onResult: ({ result }) => { onResult: ({ result }) => {
if (result.type === 'success') { if (result.type === 'success') {
goto('/'); goto('/');