45 lines
No EOL
1.4 KiB
JavaScript
45 lines
No EOL
1.4 KiB
JavaScript
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;
|
|
}
|
|
} |