mirror of
https://github.com/wireadmin/wireadmin
synced 2025-01-23 04:17:00 +00:00
fix
This commit is contained in:
parent
43e5d9a5c8
commit
4e3b93b2ef
@ -30,4 +30,9 @@ export default class Network {
|
||||
return await Shell.exec(`ip route list default | awk '{print $5}'`)
|
||||
}
|
||||
|
||||
public static async checkInterfaceExists(inet: string): Promise<boolean> {
|
||||
return await Shell.exec(`ip link show | grep ${inet}`, true)
|
||||
.then((o) => o.trim() !== '')
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -13,14 +13,11 @@ export default class Shell {
|
||||
{ shell: 'bash' },
|
||||
(err, stdout, stderr) => {
|
||||
if (err) {
|
||||
console.error('Shell Command Failed:');
|
||||
console.error({
|
||||
command: cmd,
|
||||
code: err.code,
|
||||
killed: err.killed,
|
||||
stderr
|
||||
})
|
||||
return safe ? resolve('') : reject(err);
|
||||
console.error(
|
||||
`${safe ? 'Ignored::' : 'CRITICAL::'} Shell Command Failed:`,
|
||||
JSON.stringify({ cmd, code: err.code, killed: err.killed, stderr })
|
||||
);
|
||||
return safe ? resolve(stderr) : reject(err);
|
||||
}
|
||||
return resolve(String(stdout).trim());
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import { dynaJoin, isJson } from "@lib/utils";
|
||||
import deepmerge from "deepmerge";
|
||||
import { getPeerConf } from "@lib/wireguard-utils";
|
||||
import Network from "@lib/network";
|
||||
import { SHA256 } from "crypto-js";
|
||||
import { enc, SHA256 } from "crypto-js";
|
||||
|
||||
export class WGServer {
|
||||
|
||||
@ -19,7 +19,9 @@ export class WGServer {
|
||||
return false
|
||||
}
|
||||
|
||||
await Shell.exec(`wg-quick down wg${server.confId}`, true)
|
||||
if (await Network.checkInterfaceExists(`wg${server.confId}`)) {
|
||||
await Shell.exec(`wg-quick down wg${server.confId}`, true)
|
||||
}
|
||||
|
||||
await this.update(id, { status: 'down' })
|
||||
return true
|
||||
@ -32,7 +34,16 @@ export class WGServer {
|
||||
return false
|
||||
}
|
||||
|
||||
await Shell.exec(`wg-quick down wg${server.confId}`, true)
|
||||
const HASH = await getConfigHash(server.confId);
|
||||
if (!HASH || server.confHash !== HASH) {
|
||||
await writeConfigFile(server);
|
||||
await WGServer.update(id, { confHash: await getConfigHash(server.confId) });
|
||||
}
|
||||
|
||||
if (await Network.checkInterfaceExists(`wg${server.confId}`)) {
|
||||
await Shell.exec(`wg-quick down wg${server.confId}`, true)
|
||||
}
|
||||
|
||||
await Shell.exec(`wg-quick up wg${server.confId}`)
|
||||
|
||||
await this.update(id, { status: 'up' })
|
||||
@ -484,9 +495,10 @@ export async function getConfigHash(confId: number): Promise<string | undefined>
|
||||
if (!await wgConfExists(confId)) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const confPath = path.join(WG_PATH, `wg${confId}.conf`)
|
||||
const conf = await fs.readFile(confPath, 'utf-8')
|
||||
return CryptoJS.enc.Hex.stringify(SHA256(conf));
|
||||
return enc.Hex.stringify(SHA256(conf));
|
||||
}
|
||||
|
||||
export async function writeConfigFile(wg: WgServer): Promise<void> {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next'
|
||||
import safeServe from "@lib/safe-serve";
|
||||
import { getConfigHash, getServers, WGServer, writeConfigFile } from "@lib/wireguard";
|
||||
import { getConfigHash, getServers, WGServer } from "@lib/wireguard";
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
return safeServe(res, async () => {
|
||||
@ -10,13 +10,15 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
for (const s of servers) {
|
||||
|
||||
const HASH = await getConfigHash(s.confId);
|
||||
if (s.confId && s.confHash === HASH) {
|
||||
if (s.confId && HASH && s.confHash === HASH) {
|
||||
// Skip, due to no changes on the config
|
||||
continue;
|
||||
}
|
||||
|
||||
await writeConfigFile(s);
|
||||
await WGServer.start(s.id)
|
||||
// Start server
|
||||
if (s.status === 'up') {
|
||||
await WGServer.start(s.id);
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
|
Loading…
Reference in New Issue
Block a user