fpbx-anti-forward/index.js
2025-10-25 10:08:39 -06:00

55 lines
1.9 KiB
JavaScript

require("dotenv").config({quiet: true});
const exec = require("child_process").exec;
const main = async () => {
const runAsterisk = (command) =>
new Promise((resolve) =>
exec(command, (error, stdout, stderr) => resolve({ error, stdout, stderr }))
);
try {
const commands = [
'asterisk -x "database show CF"',
'asterisk -x "database show CFB"',
'asterisk -x "database show CFU"',
];
for (const command of commands) {
const { error, stdout, stderr } = await runAsterisk(command);
if (error) {
console.error(`Error executing command "${command}": ${error}`);
continue;
}
console.log(`Output for command "${command}":\n${stdout}`);
const lines = stdout.split('\n');
for (const line of lines) {
const match = line.match(/^\/(CF|CFB|CFU)\/(\d+)\s+:\s+(.+)$/);
if (match) {
const type = match[1];
const extension = match[2];
const target = match[3];
console.log(`Parsed - Type: ${type}, Extension: ${extension}, Target: ${target}`);
}
}
}
} catch (err) {
console.error(`Unexpected error: ${err}`);
}
};
const startup = async () => {
// Run `asterisk -x "core show version"` to get the Asterisk version. If it fails, wait 10 seconds before running main again
exec('asterisk -x "core show version"', (error, stdout, stderr) => {
if (error) {
console.error(`Error executing command: ${error}`);
console.log("Asterisk is not running. Retrying in 10 seconds...");
setTimeout(startup, 10000);
return;
}
const version = stdout.trim();
console.log(`Asterisk version: ${version}`);
main();
});
}
startup();