diff --git a/.gitignore b/.gitignore index 88f7fae..98d6135 100644 --- a/.gitignore +++ b/.gitignore @@ -135,4 +135,5 @@ config.json.disabled test.js embeds.json -pageGroups.json \ No newline at end of file +pageGroups.json +.ssh/ \ No newline at end of file diff --git a/index.js b/index.js index e8de4da..c96d22c 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,18 @@ const colors = require("colors"); const embeds = require("./embeds.json") const axios = require('axios'); 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 const { FreepbxGqlClient, @@ -38,29 +50,25 @@ const cdrPool = mariadb.createPool(config.cdrdb); const reload = () => { // We're gonna start converting all the old gql commands to using mysql `system fwconsole reload` query return new Promise((resolve, reject) => { - // DB connection - var conn = pool.getConnection(); - conn.then((conn) => { - conn.query("system fwconsole reload").then((result) => { - resolve(result); - }).catch((error) => { - reject(error); + sshConn.exec('fwconsole reload', (err, stream) => { + if (err) { + reject(err); + } + stream.on('data', (data) => { + // is there a way to send this data without resolving the promise? + console.log(data.toString()); }); - conn.end(); - }).catch((error) => { - reject(error); - }) + stream.on('close', (code, signal) => { + if (code == 0) { + 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 = () => { return new Promise((resolve, reject) => { pbxClient.request(funcs.minifyQuery(funcs.generateQuery('list', {}))).then((result) => { @@ -89,9 +97,7 @@ const createExtension = (ext, name, uid) => { name: name, uid: uid }))).then((result) => { - pbxClient.request(funcs.minifyQuery(funcs.generateQuery('reload', { - id: "CreateExt" - }))).then((result) => { + reload().then((result) => { pbxClient.request(funcs.minifyQuery(funcs.generateQuery('lookup', { ext: ext }))).then((result) => { @@ -153,9 +159,7 @@ const deleteExtension = (ext) => { pbxClient.request(funcs.minifyQuery(funcs.generateQuery('delete', { ext: ext }))).then((result) => { - pbxClient.request(funcs.minifyQuery(funcs.generateQuery('reload', { - id: "DeleteExt" - }))).then((result) => { + reload().then((result) => { res = { "status": "deleted", "result": result @@ -179,9 +183,7 @@ const updateName = (ext, name) => { ext: ext, name: name }))).then((result) => { - pbxClient.request(funcs.minifyQuery(funcs.generateQuery('reload', { - id: "UpdateName" - }))).then((result) => { + reload().then((result) => { res = { "status": "updated", "result": result @@ -266,7 +268,7 @@ const generateExtensionListEmbed = async () => { let field = ""; let embeds = []; let count = 0; - + // put for loop in function and await it embeds.push({ "title": "Extension List", @@ -783,6 +785,26 @@ dcClient.on('ready', async () => { 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) => { @@ -1045,9 +1067,7 @@ dcClient.on('interactionCreate', async interaction => { if (result.length == 0) { // They're not in the group, add them conn.query(`INSERT INTO paging_groups (\`ext\`, \`page_number\`) VALUES (${ext}, ${group})`).then((result) => { - pbxClient.request(funcs.minifyQuery(funcs.generateQuery('reload', { - id: "UpdatePaging" - }))).then(() => { + reload().then(() => { interaction.editReply({ content: "Added you to the paging group!", ephemeral: true @@ -1082,9 +1102,7 @@ dcClient.on('interactionCreate', async interaction => { } else { // They're in the group, remove them conn.query(`DELETE FROM paging_groups WHERE ext = ${ext} AND \`page_number\` = ${group}`).then((result) => { - pbxClient.request(funcs.minifyQuery(funcs.generateQuery('reload', { - id: "UpdatePaging" - }))).then(() => { + reload().then(() => { interaction.editReply({ content: "Removed you from the paging group!", ephemeral: true diff --git a/package-lock.json b/package-lock.json index beae983..de567f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,8 @@ "freepbx-graphql-client": "^0.1.1", "mariadb": "^3.2.0", "ping": "^0.4.4", - "sqlite3": "^5.1.4" + "sqlite3": "^5.1.4", + "ssh2": "^1.15.0" } }, "node_modules/@discordjs/builders": { @@ -282,6 +283,14 @@ "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": { "version": "0.4.0", "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", "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": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -324,6 +341,15 @@ "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": { "version": "1.6.0", "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", "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": { "version": "3.1.8", "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", "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": { "version": "0.6.3", "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": { "version": "8.0.1", "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", "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": { "version": "5.22.1", "resolved": "https://registry.npmjs.org/undici/-/undici-5.22.1.tgz", diff --git a/package.json b/package.json index 5212796..715f7af 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "freepbx-graphql-client": "^0.1.1", "mariadb": "^3.2.0", "ping": "^0.4.4", - "sqlite3": "^5.1.4" + "sqlite3": "^5.1.4", + "ssh2": "^1.15.0" } }