fpbx-anti-forward/index.js

57 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 {
var forwards = {};
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;
}
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];
forwards[`${type}/${extension}`] = target;
}
}
}
console.log("Call forwards:", forwards);
} 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();