Compare commits

...

21 commits

Author SHA1 Message Date
Christopher Cookman 66931ca735 Merge pull request 'Merge dev to main' (#1) from dev into main
Reviewed-on: #1
2025-10-28 07:58:46 -06:00
Christopher Cookman 62d2f4a7d1 Oops 2025-10-28 07:55:19 -06:00
Christopher Cookman 73d56cc8c3 Bwug 2025-10-28 07:55:01 -06:00
Christopher Cookman e517a259a9 less log 2025-10-28 07:54:14 -06:00
Christopher Cookman 43c81ad433 5 secs 2025-10-28 07:53:34 -06:00
Christopher Cookman b5add30dda UUUU 2025-10-28 07:52:57 -06:00
Christopher Cookman 4e8cd9c23f Bwug 2025-10-28 07:51:37 -06:00
Christopher Cookman 395373d27b I hate js sometimes: 2025-10-28 07:44:26 -06:00
Christopher Cookman dfadb8a468 Grr 2025-10-28 07:42:21 -06:00
Christopher Cookman 13dc274a36 Maybe fix filtering? I suck at this 2025-10-28 07:41:16 -06:00
Christopher Cookman 9fcb753bfa Bwug 2025-10-28 07:40:39 -06:00
Christopher Cookman f1edc11f56 Again 2025-10-28 07:38:01 -06:00
Christopher Cookman f00d5fbde4 Buh 2025-10-28 07:37:15 -06:00
Christopher Cookman b5edd8ee48 Big test? 2025-10-28 07:36:28 -06:00
Christopher Cookman 84c8c5c6e6 Try again? 2025-10-28 07:31:15 -06:00
Christopher Cookman 0a982b0115 Test 2 2025-10-28 07:29:57 -06:00
Christopher Cookman 2a95d6626e Test 2 2025-10-28 07:28:51 -06:00
Christopher Cookman db2e410ab5 Test DBGet 2025-10-28 07:28:06 -06:00
Christopher Cookman 0ddf6dac99 Use import everywhere, goofy 2025-10-28 07:26:25 -06:00
Christopher Cookman 5bbb4bf764 Import instead? 2025-10-28 07:21:22 -06:00
Christopher Cookman 900c1e976e Use ami package 2025-10-28 07:16:00 -06:00
3 changed files with 122 additions and 72 deletions

159
index.js
View file

@ -1,6 +1,25 @@
require("dotenv").config({quiet: true});
const exec = require("child_process").exec;
const Discord = require("discord.js");
const blocked = [
'0',
'0000',
'9998',
'9999'
];
import dotenv from 'dotenv';
dotenv.config({ quiet: true })
import AMISocket from 'ami';
const sock = new AMISocket({
connect: {
host: '127.0.0.1',
port: 5038
},
credentials: {
username: process.env.AMI_USER,
secret: process.env.AMI_SECRET
},
events: true
})
import Discord from 'discord.js';
const hook = new Discord.WebhookClient({ url: process.env.DISCORD_WEBHOOK_URL });
const cfTypes = {
@ -9,72 +28,92 @@ const cfTypes = {
CFU: "Unavailable",
}
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 main = () => {
var respData = [];
console.log('checking')
// getList for CF, CFB, CFU
sock.getList({
action: 'DBGetTree',
family: 'CF'
}).then(async (response) => {
// Responses are an array of objects. The first should always have eventlist: 'start', and last should have eventlist: 'complete'. Remove these, make an array of objects {key, val} based on the rest of the response objects.
let entries = response.filter(r => r.eventlist !== 'start' && r.eventlist !== 'Complete').map(r => ({ key: r.key, val: r.val }));
for (let entry of entries) {
let stuff = entry.key.split('/');
let type = stuff[1];
let ext = stuff[2];
console
respData.push({
extension: ext,
type: type,
target: entry.val
});
}
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.replace(/\s+$/, '');
}).then(() => {
sock.getList({
action: 'DBGetTree',
family: 'CFB'
}).then(async (response) => {
let entries = response.filter(r => r.eventlist !== 'start' && r.eventlist !== 'Complete').map(r => ({ key: r.key, val: r.val }));
for (let entry of entries) {
let stuff = entry.key.split('/');
let type = stuff[1];
let ext = stuff[2];
console
respData.push({
extension: ext,
type: type,
target: entry.val
});
}
}).then(() => {
sock.getList({
action: 'DBGetTree',
family: 'CFU'
}).then(async (response) => {
let entries = response.filter(r => r.eventlist !== 'start' && r.eventlist !== 'Complete').map(r => ({ key: r.key, val: r.val }));
for (let entry of entries) {
let stuff = entry.key.split('/');
let type = stuff[1];
let ext = stuff[2];
console
respData.push({
extension: ext,
type: type,
target: entry.val
});
}
}).then(() => {
console.log(`Found ${respData.length} call forwards.`);
for (let cf of respData) {
// Check if forward target is anything in the 700-800 range, or is a number in the blocked list
if ((cf.target >= 700 && cf.target < 800) || blocked.includes(cf.target)) {
// Violation. Remove call forward from db.
console.log(`---> Violation found on extension ${cf.extension}. Removing call forward to ${cf.target}`);
sock.send({
action: 'DBDel',
family: cf.type,
key: cf.extension
}).then((resp) => {
console.log(`-----> Call forward removed successfully.`);
hook.send(`:no_entry: **Call Forward Removed** :no_entry:\nExtension **${cf.extension}** had a **${cfTypes[cf.type]}** call forward to **${cf.target}** The call forward has been removed.`);
})
}
// Search all forwards for any target that is between 700 and 800. If it exists, log to console and run `asterisk -x "database del CF[U/B] <ext>"` to delete it
for (const [key, target] of Object.entries(forwards)) {
const targetExt = parseInt(target, 10);
if ((targetExt >= 700 && targetExt < 800) || targetExt === 0) { // Also block forwards to 0 (operator line)
console.log(`Deleting forward ${key} to target ${target}`);
await hook.send(`Fuckass with extension ${key.split('/')[1]} tried to forward to ${target}. ${cfTypes[key.split('/')[0]]} Forward has been deleted.`);
const delCommand = `asterisk -x "database del ${key.replace('/', ' ')}"`;
const { error, stdout, stderr } = await runAsterisk(delCommand);
if (error) {
console.error(`Error executing command "${delCommand}": ${error}`);
} else {
console.log(`Successfully deleted forward ${key}`);
}
}
}
} catch (err) {
console.error(`Unexpected error: ${err}`);
}
setTimeout(main, process.env.CHECK_INTERVAL * 1000); // Run every CHECK_INTERVAL seconds
};
setTimeout(main, 5000); // Repeat every 60 seconds
});
});
})
}
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}`);
sock.connect().then(() => {
console.log(sock.amiVersion)
main();
}).catch((err) => {
console.error('Error connecting to AMI:', err);
setTimeout(startup, 10000); // Retry after 10 seconds
});
}

10
package-lock.json generated
View file

@ -9,6 +9,7 @@
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"ami": "^1.0.0",
"discord.js": "^14.24.0",
"dotenv": "^17.2.3"
}
@ -201,6 +202,15 @@
"npm": ">=7.0.0"
}
},
"node_modules/ami": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/ami/-/ami-1.0.0.tgz",
"integrity": "sha512-wkuxJl8ahQ4FYVwXvtJpQ7wuRUAqD30ueVpeJ1t5UyYqjzh+GtmrnaeHrXzzp+KO3ukhHZoMrsZlKB0j91oAlw==",
"license": "MIT",
"engines": {
"node": ">=14.0.0"
}
},
"node_modules/discord-api-types": {
"version": "0.38.31",
"resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.38.31.tgz",

View file

@ -9,8 +9,9 @@
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs",
"type": "module",
"dependencies": {
"ami": "^1.0.0",
"discord.js": "^14.24.0",
"dotenv": "^17.2.3"
}