This commit is contained in:
Shahrad Elahi 2023-10-08 15:20:57 +03:30
parent 43e5d9a5c8
commit 4e3b93b2ef
4 changed files with 32 additions and 16 deletions

View File

@ -30,4 +30,9 @@ export default class Network {
return await Shell.exec(`ip route list default | awk '{print $5}'`) 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() !== '')
}
}; };

View File

@ -13,14 +13,11 @@ export default class Shell {
{ shell: 'bash' }, { shell: 'bash' },
(err, stdout, stderr) => { (err, stdout, stderr) => {
if (err) { if (err) {
console.error('Shell Command Failed:'); console.error(
console.error({ `${safe ? 'Ignored::' : 'CRITICAL::'} Shell Command Failed:`,
command: cmd, JSON.stringify({ cmd, code: err.code, killed: err.killed, stderr })
code: err.code, );
killed: err.killed, return safe ? resolve(stderr) : reject(err);
stderr
})
return safe ? resolve('') : reject(err);
} }
return resolve(String(stdout).trim()); return resolve(String(stdout).trim());
} }

View File

@ -8,7 +8,7 @@ import { dynaJoin, isJson } from "@lib/utils";
import deepmerge from "deepmerge"; import deepmerge from "deepmerge";
import { getPeerConf } from "@lib/wireguard-utils"; import { getPeerConf } from "@lib/wireguard-utils";
import Network from "@lib/network"; import Network from "@lib/network";
import { SHA256 } from "crypto-js"; import { enc, SHA256 } from "crypto-js";
export class WGServer { export class WGServer {
@ -19,7 +19,9 @@ export class WGServer {
return false 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' }) await this.update(id, { status: 'down' })
return true return true
@ -32,7 +34,16 @@ export class WGServer {
return false 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 Shell.exec(`wg-quick up wg${server.confId}`)
await this.update(id, { status: 'up' }) await this.update(id, { status: 'up' })
@ -484,9 +495,10 @@ export async function getConfigHash(confId: number): Promise<string | undefined>
if (!await wgConfExists(confId)) { if (!await wgConfExists(confId)) {
return undefined return undefined
} }
const confPath = path.join(WG_PATH, `wg${confId}.conf`) const confPath = path.join(WG_PATH, `wg${confId}.conf`)
const conf = await fs.readFile(confPath, 'utf-8') 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> { export async function writeConfigFile(wg: WgServer): Promise<void> {

View File

@ -1,6 +1,6 @@
import type { NextApiRequest, NextApiResponse } from 'next' import type { NextApiRequest, NextApiResponse } from 'next'
import safeServe from "@lib/safe-serve"; 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) { export default async function handler(req: NextApiRequest, res: NextApiResponse) {
return safeServe(res, async () => { return safeServe(res, async () => {
@ -10,13 +10,15 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
for (const s of servers) { for (const s of servers) {
const HASH = await getConfigHash(s.confId); 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 // Skip, due to no changes on the config
continue; continue;
} }
await writeConfigFile(s); // Start server
await WGServer.start(s.id) if (s.status === 'up') {
await WGServer.start(s.id);
}
} }
return res return res