Do SSH stuff, and make reloading do fwconsole reload :)

This commit is contained in:
Christopher Cookman 2024-03-23 17:14:05 -06:00
parent b0de188666
commit 3515d107f3
Signed by: ChrisChrome
GPG key ID: A023A26E42C33A42
4 changed files with 126 additions and 38 deletions

3
.gitignore vendored
View file

@ -135,4 +135,5 @@ config.json.disabled
test.js test.js
embeds.json embeds.json
pageGroups.json pageGroups.json
.ssh/

View file

@ -5,6 +5,18 @@ const colors = require("colors");
const embeds = require("./embeds.json") const embeds = require("./embeds.json")
const axios = require('axios'); const axios = require('axios');
const ping = require("ping") const ping = require("ping")
const ssh2 = require('ssh2')
const sshConn = new ssh2.Client();
// find first file in .ssh local to the script
const fs = require('fs');
const path = require('path');
// get the first file in the .ssh directory
const keyPath = path.join(__dirname, '.ssh');
const keyFiles = fs.readdirSync(keyPath);
const keyFile = keyFiles[0];
// read the key file
const privateKey = fs.readFileSync(".ssh/" + keyFile, 'utf8');
// FreePBX GraphQL Client // FreePBX GraphQL Client
const { const {
FreepbxGqlClient, FreepbxGqlClient,
@ -38,29 +50,25 @@ const cdrPool = mariadb.createPool(config.cdrdb);
const reload = () => { const reload = () => {
// We're gonna start converting all the old gql commands to using mysql `system fwconsole reload` query // We're gonna start converting all the old gql commands to using mysql `system fwconsole reload` query
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// DB connection sshConn.exec('fwconsole reload', (err, stream) => {
var conn = pool.getConnection(); if (err) {
conn.then((conn) => { reject(err);
conn.query("system fwconsole reload").then((result) => { }
resolve(result); stream.on('data', (data) => {
}).catch((error) => { // is there a way to send this data without resolving the promise?
reject(error); console.log(data.toString());
}); });
conn.end(); stream.on('close', (code, signal) => {
}).catch((error) => { if (code == 0) {
reject(error); resolve(code);
}) } else {
reject("Error reloading FreePBX");
}
})
});
}); });
} }
console.log("Reloading PBX")
reload().then((result) => {
console.log("Reloaded PBX")
}).catch((error) => {
console.log("Error reloading PBX")
console.log(error)
});
const getExtCount = () => { const getExtCount = () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
pbxClient.request(funcs.minifyQuery(funcs.generateQuery('list', {}))).then((result) => { pbxClient.request(funcs.minifyQuery(funcs.generateQuery('list', {}))).then((result) => {
@ -89,9 +97,7 @@ const createExtension = (ext, name, uid) => {
name: name, name: name,
uid: uid uid: uid
}))).then((result) => { }))).then((result) => {
pbxClient.request(funcs.minifyQuery(funcs.generateQuery('reload', { reload().then((result) => {
id: "CreateExt"
}))).then((result) => {
pbxClient.request(funcs.minifyQuery(funcs.generateQuery('lookup', { pbxClient.request(funcs.minifyQuery(funcs.generateQuery('lookup', {
ext: ext ext: ext
}))).then((result) => { }))).then((result) => {
@ -153,9 +159,7 @@ const deleteExtension = (ext) => {
pbxClient.request(funcs.minifyQuery(funcs.generateQuery('delete', { pbxClient.request(funcs.minifyQuery(funcs.generateQuery('delete', {
ext: ext ext: ext
}))).then((result) => { }))).then((result) => {
pbxClient.request(funcs.minifyQuery(funcs.generateQuery('reload', { reload().then((result) => {
id: "DeleteExt"
}))).then((result) => {
res = { res = {
"status": "deleted", "status": "deleted",
"result": result "result": result
@ -179,9 +183,7 @@ const updateName = (ext, name) => {
ext: ext, ext: ext,
name: name name: name
}))).then((result) => { }))).then((result) => {
pbxClient.request(funcs.minifyQuery(funcs.generateQuery('reload', { reload().then((result) => {
id: "UpdateName"
}))).then((result) => {
res = { res = {
"status": "updated", "status": "updated",
"result": result "result": result
@ -266,7 +268,7 @@ const generateExtensionListEmbed = async () => {
let field = ""; let field = "";
let embeds = []; let embeds = [];
let count = 0; let count = 0;
// put for loop in function and await it // put for loop in function and await it
embeds.push({ embeds.push({
"title": "Extension List", "title": "Extension List",
@ -783,6 +785,26 @@ dcClient.on('ready', async () => {
sendLog(`${colors.red("[ERROR]")} Error sending ping ${error}`); sendLog(`${colors.red("[ERROR]")} Error sending ping ${error}`);
}); });
}) })
// Start doing SSH stuff
sendLog(`${colors.cyan("[INFO]")} Starting SSH connection`);
await sshConn.connect({
host: config.freepbx.server,
username: "root", // Will make config later
privateKey: privateKey
})
});
sshConn.on('ready', () => {
sendLog(`${colors.cyan("[INFO]")} SSH connection established`);
console.log("Reloading PBX")
reload().then((result) => {
console.log("Reloaded PBX")
}).catch((error) => {
console.log("Error reloading PBX")
console.log(error)
});
}); });
dcClient.on("guildMemberRemove", (member) => { dcClient.on("guildMemberRemove", (member) => {
@ -1045,9 +1067,7 @@ dcClient.on('interactionCreate', async interaction => {
if (result.length == 0) { if (result.length == 0) {
// They're not in the group, add them // They're not in the group, add them
conn.query(`INSERT INTO paging_groups (\`ext\`, \`page_number\`) VALUES (${ext}, ${group})`).then((result) => { conn.query(`INSERT INTO paging_groups (\`ext\`, \`page_number\`) VALUES (${ext}, ${group})`).then((result) => {
pbxClient.request(funcs.minifyQuery(funcs.generateQuery('reload', { reload().then(() => {
id: "UpdatePaging"
}))).then(() => {
interaction.editReply({ interaction.editReply({
content: "Added you to the paging group!", content: "Added you to the paging group!",
ephemeral: true ephemeral: true
@ -1082,9 +1102,7 @@ dcClient.on('interactionCreate', async interaction => {
} else { } else {
// They're in the group, remove them // They're in the group, remove them
conn.query(`DELETE FROM paging_groups WHERE ext = ${ext} AND \`page_number\` = ${group}`).then((result) => { conn.query(`DELETE FROM paging_groups WHERE ext = ${ext} AND \`page_number\` = ${group}`).then((result) => {
pbxClient.request(funcs.minifyQuery(funcs.generateQuery('reload', { reload().then(() => {
id: "UpdatePaging"
}))).then(() => {
interaction.editReply({ interaction.editReply({
content: "Removed you from the paging group!", content: "Removed you from the paging group!",
ephemeral: true ephemeral: true

70
package-lock.json generated
View file

@ -15,7 +15,8 @@
"freepbx-graphql-client": "^0.1.1", "freepbx-graphql-client": "^0.1.1",
"mariadb": "^3.2.0", "mariadb": "^3.2.0",
"ping": "^0.4.4", "ping": "^0.4.4",
"sqlite3": "^5.1.4" "sqlite3": "^5.1.4",
"ssh2": "^1.15.0"
} }
}, },
"node_modules/@discordjs/builders": { "node_modules/@discordjs/builders": {
@ -282,6 +283,14 @@
"node": ">=10" "node": ">=10"
} }
}, },
"node_modules/asn1": {
"version": "0.2.6",
"resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.6.tgz",
"integrity": "sha512-ix/FxPn0MDjeyJ7i/yoHGFt/EX6LyNbxSEhPPXODPL+KB0VPk86UYfL0lMdy+KCnv+fmvIzySwaK5COwqVbWTQ==",
"dependencies": {
"safer-buffer": "~2.1.0"
}
},
"node_modules/asynckit": { "node_modules/asynckit": {
"version": "0.4.0", "version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
@ -315,6 +324,14 @@
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="
}, },
"node_modules/bcrypt-pbkdf": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz",
"integrity": "sha512-qeFIXtP4MSoi6NLqO12WfqARWWuCKi2Rn/9hJLEmtB5yTNr9DqFWkJRCf2qShWzPeAMRnOgCrq0sg/KLv5ES9w==",
"dependencies": {
"tweetnacl": "^0.14.3"
}
},
"node_modules/brace-expansion": { "node_modules/brace-expansion": {
"version": "1.1.11", "version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
@ -324,6 +341,15 @@
"concat-map": "0.0.1" "concat-map": "0.0.1"
} }
}, },
"node_modules/buildcheck": {
"version": "0.0.6",
"resolved": "https://registry.npmjs.org/buildcheck/-/buildcheck-0.0.6.tgz",
"integrity": "sha512-8f9ZJCUXyT1M35Jx7MkBgmBMo3oHTTBIPLiY9xyL0pl3T5RwcPEY8cUHr5LBNfu/fk6c2T4DJZuVM/8ZZT2D2A==",
"optional": true,
"engines": {
"node": ">=10.0.0"
}
},
"node_modules/busboy": { "node_modules/busboy": {
"version": "1.6.0", "version": "1.6.0",
"resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz",
@ -418,6 +444,20 @@
"resolved": "https://registry.npmjs.org/console-control-strings/-/console-control-strings-1.1.0.tgz", "resolved": "https://registry.npmjs.org/console-control-strings/-/console-control-strings-1.1.0.tgz",
"integrity": "sha512-ty/fTekppD2fIwRvnZAVdeOiGd1c7YXEixbgJTNzqcxJWKQnjJ/V1bNEEE6hygpM3WjwHFUVK6HTjWSzV4a8sQ==" "integrity": "sha512-ty/fTekppD2fIwRvnZAVdeOiGd1c7YXEixbgJTNzqcxJWKQnjJ/V1bNEEE6hygpM3WjwHFUVK6HTjWSzV4a8sQ=="
}, },
"node_modules/cpu-features": {
"version": "0.0.9",
"resolved": "https://registry.npmjs.org/cpu-features/-/cpu-features-0.0.9.tgz",
"integrity": "sha512-AKjgn2rP2yJyfbepsmLfiYcmtNn/2eUvocUyM/09yB0YDiz39HteK/5/T4Onf0pmdYDMgkBoGvRLvEguzyL7wQ==",
"hasInstallScript": true,
"optional": true,
"dependencies": {
"buildcheck": "~0.0.6",
"nan": "^2.17.0"
},
"engines": {
"node": ">=10.0.0"
}
},
"node_modules/cross-fetch": { "node_modules/cross-fetch": {
"version": "3.1.8", "version": "3.1.8",
"resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.8.tgz", "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.8.tgz",
@ -1025,6 +1065,12 @@
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
}, },
"node_modules/nan": {
"version": "2.19.0",
"resolved": "https://registry.npmjs.org/nan/-/nan-2.19.0.tgz",
"integrity": "sha512-nO1xXxfh/RWNxfd/XPfbIfFk5vgLsAxUR9y5O0cHMJu/AW9U95JLXqthYHjEp+8gQ5p96K9jUp8nbVOxCdRbtw==",
"optional": true
},
"node_modules/negotiator": { "node_modules/negotiator": {
"version": "0.6.3", "version": "0.6.3",
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz", "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz",
@ -1369,6 +1415,23 @@
} }
} }
}, },
"node_modules/ssh2": {
"version": "1.15.0",
"resolved": "https://registry.npmjs.org/ssh2/-/ssh2-1.15.0.tgz",
"integrity": "sha512-C0PHgX4h6lBxYx7hcXwu3QWdh4tg6tZZsTfXcdvc5caW/EMxaB4H9dWsl7qk+F7LAW762hp8VbXOX7x4xUYvEw==",
"hasInstallScript": true,
"dependencies": {
"asn1": "^0.2.6",
"bcrypt-pbkdf": "^1.0.2"
},
"engines": {
"node": ">=10.16.0"
},
"optionalDependencies": {
"cpu-features": "~0.0.9",
"nan": "^2.18.0"
}
},
"node_modules/ssri": { "node_modules/ssri": {
"version": "8.0.1", "version": "8.0.1",
"resolved": "https://registry.npmjs.org/ssri/-/ssri-8.0.1.tgz", "resolved": "https://registry.npmjs.org/ssri/-/ssri-8.0.1.tgz",
@ -1460,6 +1523,11 @@
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz",
"integrity": "sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==" "integrity": "sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q=="
}, },
"node_modules/tweetnacl": {
"version": "0.14.5",
"resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz",
"integrity": "sha512-KXXFFdAbFXY4geFIwoyNK+f5Z1b7swfXABfL7HXCmoIWMKU3dmS26672A4EeQtDzLKy7SXmfBu51JolvEKwtGA=="
},
"node_modules/undici": { "node_modules/undici": {
"version": "5.22.1", "version": "5.22.1",
"resolved": "https://registry.npmjs.org/undici/-/undici-5.22.1.tgz", "resolved": "https://registry.npmjs.org/undici/-/undici-5.22.1.tgz",

View file

@ -15,6 +15,7 @@
"freepbx-graphql-client": "^0.1.1", "freepbx-graphql-client": "^0.1.1",
"mariadb": "^3.2.0", "mariadb": "^3.2.0",
"ping": "^0.4.4", "ping": "^0.4.4",
"sqlite3": "^5.1.4" "sqlite3": "^5.1.4",
"ssh2": "^1.15.0"
} }
} }