34 lines
No EOL
913 B
JavaScript
34 lines
No EOL
913 B
JavaScript
import {
|
|
wireguardInterface,
|
|
wireguardPeer
|
|
} from "../models.mjs";
|
|
|
|
import path from "node:path";
|
|
import file from "node:fs";
|
|
import ejs from "ejs";
|
|
|
|
|
|
const serverTemplatePath = path.join(process.cwd(), 'templates', 'wg_server.ejs');
|
|
const clientTemplatePath = path.join(process.cwd(), 'templates', 'wg_client.ejs');
|
|
|
|
|
|
export async function generateInterfaceConfig(interfaceId) {
|
|
const ifData = Object.values(wireguardInterface.getAll()).find(fi => fi.id == interfaceId);
|
|
const peerList = Object.values(wireguardPeer.getAll()).filter(fi => fi.interface == interfaceId);
|
|
|
|
let configData = await ejs.renderFile(
|
|
serverTemplatePath,
|
|
{
|
|
interface: ifData,
|
|
peerList
|
|
},
|
|
{
|
|
async: true
|
|
}
|
|
);
|
|
|
|
file.writeFileSync(
|
|
path.join(process.cwd(), 'data', 'wireguard', `${ifData.ifName}.conf`),
|
|
configData
|
|
)
|
|
} |