mirror of
https://github.com/wireadmin/wireadmin
synced 2025-03-09 13:20:39 +00:00
fix the issue with healthcheck
and silence the annoying warning form sveltekit-superforms
This commit is contained in:
parent
479c4093fc
commit
523cc04105
@ -131,6 +131,32 @@ export class WGServer {
|
||||
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> {
|
||||
const server = await findServer(serverId);
|
||||
if (!server) {
|
||||
@ -154,6 +180,13 @@ export class WGServer {
|
||||
}
|
||||
}
|
||||
|
||||
export type WgUsage = Map<string, PeerUsage>;
|
||||
|
||||
export type PeerUsage = {
|
||||
rx: number;
|
||||
tx: number;
|
||||
};
|
||||
|
||||
class WGPeers {
|
||||
private readonly server: WGServer;
|
||||
|
||||
|
@ -1,18 +1,22 @@
|
||||
import type { RequestHandler } from '@sveltejs/kit';
|
||||
import { getConfigHash, getServers, WGServer } from '$lib/wireguard';
|
||||
import { getServers, WGServer } from '$lib/wireguard';
|
||||
import logger from '$lib/logger';
|
||||
|
||||
export const GET: RequestHandler = async () => {
|
||||
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) {
|
||||
const wg = new WGServer(s.id);
|
||||
|
||||
// Start server
|
||||
if (s.status === 'up') {
|
||||
// If the server is up and the interface doesn't exist, start it
|
||||
if (server.status === 'up' && !hasInterface) {
|
||||
await wg.start();
|
||||
}
|
||||
|
||||
if (server.status === 'down' && hasInterface) {
|
||||
await wg.stop();
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
logger.error('APIFailed: HealthCheck:', e);
|
||||
|
@ -18,6 +18,9 @@
|
||||
|
||||
const options: FormOptions<typeof formSchema> = {
|
||||
validators: formSchema,
|
||||
warnings: {
|
||||
noValidationAndConstraints: false,
|
||||
},
|
||||
onResult: ({ result }) => {
|
||||
if (result.type === 'success') {
|
||||
goto('/');
|
||||
|
Loading…
Reference in New Issue
Block a user