improved Database & Models
This commit is contained in:
commit
0bbe91bec3
18 changed files with 956 additions and 0 deletions
45
lib/wireguard/process.mjs
Normal file
45
lib/wireguard/process.mjs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import {
|
||||
execSync as execCommand
|
||||
} from "node:child_process";
|
||||
|
||||
import {
|
||||
wireguardInterface
|
||||
} from "../models.mjs";
|
||||
|
||||
|
||||
|
||||
export async function startWireguardInterface(interfaceid) {
|
||||
try {
|
||||
const ifData = wireguardInterface.getById(interfaceid);
|
||||
const ifLink = getInterfaceLink(ifData.ifName);
|
||||
|
||||
if (ifLink.operstate == 'UP') {
|
||||
console.log(`Reloading interface ${ifData.ifName}`);
|
||||
console.log(`» reloading interface ${ifData.ifName}`);
|
||||
execCommand(`wg-quick up ${ifData.ifName}`, { stdio: 'inherit' });
|
||||
console.log(`✓ reloaded interface ${ifData.ifName}`);
|
||||
} else {
|
||||
console.log(`» starting interface ${ifData.ifName}`);
|
||||
execCommand(`wg-quick up ${ifData.ifName}`, { stdio: 'inherit' });
|
||||
console.log(`✓ started interface ${ifData.ifName}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`✕ failed to start interface ${iface}:`, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export async function getInterfaceLink(ifName) {
|
||||
try {
|
||||
let result = JSON.parse(
|
||||
execCommand(`ip -j link show`, { stdio: 'inherit' })
|
||||
).find(linkResult => linkResult.ifname == ifName);
|
||||
|
||||
if (!result) { return undefined; }
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error(`failed to get interface link for ${ifName}:`, error.message);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue