From 3761a93961ebb3a12d0fe4745393354a511c806b Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Thu, 27 Oct 2022 10:38:15 -0700 Subject: [PATCH] Completed Discord integration (#4651) --- MeshCentralServer.njsproj | 6 ++++++ docs/docs/messaging/index.md | 23 ++++++++++++++++++++++- meshcentral-config-schema.json | 9 +++++++++ meshmessaging.js | 3 ++- meshuser.js | 5 ++++- package.json | 10 +++++++++- sample-config-advanced.json | 10 +++++++--- views/default.handlebars | 28 ++++++++++++++++++++-------- 8 files changed, 79 insertions(+), 15 deletions(-) diff --git a/MeshCentralServer.njsproj b/MeshCentralServer.njsproj index 3974731b..b4f4a7c6 100644 --- a/MeshCentralServer.njsproj +++ b/MeshCentralServer.njsproj @@ -681,6 +681,7 @@ + @@ -695,6 +696,7 @@ + @@ -711,6 +713,7 @@ + @@ -724,6 +727,7 @@ + @@ -738,6 +742,7 @@ + @@ -754,6 +759,7 @@ + diff --git a/docs/docs/messaging/index.md b/docs/docs/messaging/index.md index 52b736e0..edfde515 100644 --- a/docs/docs/messaging/index.md +++ b/docs/docs/messaging/index.md @@ -6,7 +6,7 @@ MeshCentral supports messaging platforms so that users can register a messaging ## Telegram Setup -Currently only Telegram is supported. You will need to provide MeshCentral with the necessary login information so that MeshCentral can authenticate and connect to the Telegram servers and start sending notifications. For Telegram, both user and bot login is supported with bot login being the more typical way to go. The configuration in the config.json for a bot login looks like this: +For Telegram integration, you will need to provide MeshCentral with the necessary login information so that MeshCentral can authenticate and connect to the Telegram servers and start sending notifications. For Telegram, both user and bot login is supported with bot login being the more typical way to go. The configuration in the config.json for a bot login looks like this: ```json { @@ -59,6 +59,27 @@ MeshCentral Telegram client is bot connected. Note the last line, indicating it's connected as a bot. +## Discord Setup + +For Discord integration, you need to provide MeshCentral with a bot application token so that MeshCentral can login and send notifications to users. The Discord bot will need to be joined to one or more Discord servers and users will need to join at at least one Discord server that is in common with the bot to receive notifications. + +There are many tutorials online on how to create a Discord bot and get the login token. For example follow the [two first sections of this tutorial](https://www.freecodecamp.org/news/create-a-discord-bot-with-javascript-nodejs/). The "How to Create a Discord Bot Account" section will show how to create a bot and get the token, the "How to Invite Your Bot to Join a Server" section shows how to join the bot to a Discord server. + +Once you have the Discord bot login token, the config.json Discord configuration looks like this: + +```json +{ + "messaging": { + "discord": { + "serverurl": "https://discord.gg/xxxxxxxxx", + "token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxx" + } + } +} +``` + +Once users will need to join the same Discord server as the bot, the optional "serverurl" can be used to give the users a URL link to join the server, this can be a server invitation link or some other URL with instructions. + ## User Setup Once a messaging system is setup with MeshCentral, users will be able to register their handle and verify that they own that account by typing in a 6 digit code. diff --git a/meshcentral-config-schema.json b/meshcentral-config-schema.json index 44e36fb7..38526f89 100644 --- a/meshcentral-config-schema.json +++ b/meshcentral-config-schema.json @@ -1370,6 +1370,15 @@ "apihash": { "type": "string" }, "session": { "type": "string" } } + }, + "discord": { + "type": "object", + "description": "Configure Discord messaging system", + "properties": { + "serverurl": { "type": "string", "format": "uri", "description": "An optional HTTP link to the discord server the user must join to get notifications." }, + "token": { "type": "string", "description": "A Discord bot token that MeshCentral will use to login to Discord." } + }, + "required": [ "token" ] } } } diff --git a/meshmessaging.js b/meshmessaging.js index e88eac4c..274d92cd 100644 --- a/meshmessaging.js +++ b/meshmessaging.js @@ -49,6 +49,7 @@ module.exports.CreateServer = function (parent) { obj.providers = 0; // 1 = Telegram, 2 = Signal, 4 = Discord obj.telegramClient = null; obj.discordClient = null; + obj.discordUrl = null; // Telegram client setup if (parent.config.messaging.telegram) { @@ -93,7 +94,6 @@ module.exports.CreateServer = function (parent) { if (parent.config.messaging.discord) { // Validate Discord configuration values var discordOK = true; - if (typeof parent.config.messaging.discord.inviteurl != 'string') { console.log('Invalid or missing Discord invite URL.'); discordOK = false; } if (typeof parent.config.messaging.discord.token != 'string') { console.log('Invalid or missing Discord token.'); discordOK = false; } if (discordOK) { @@ -113,6 +113,7 @@ module.exports.CreateServer = function (parent) { discordClient.on('ready', function() { console.log(`MeshCentral Discord client is connected as ${discordClient.user.tag}!`); obj.discordClient = discordClient; + obj.discordUrl = parent.config.messaging.discord.serverurl; obj.providers += 4; // Enable Discord messaging }); diff --git a/meshuser.js b/meshuser.js index e5b7c9d9..d9eeae08 100644 --- a/meshuser.js +++ b/meshuser.js @@ -575,7 +575,10 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use if (domain.passwordrequirements.lock2factor == true) { serverinfo.lock2factor = true; } // Indicate 2FA change are not allowed if (typeof domain.passwordrequirements.maxfidokeys == 'number') { serverinfo.maxfidokeys = domain.passwordrequirements.maxfidokeys; } } - if (parent.parent.msgserver != null) { serverinfo.userMsgProviders = parent.parent.msgserver.providers; } + if (parent.parent.msgserver != null) { // Setup messaging providers information + serverinfo.userMsgProviders = parent.parent.msgserver.providers; + if (parent.parent.msgserver.discordUrl != null) { serverinfo.discordUrl = parent.parent.msgserver.discordUrl; } + } // Build the mobile agent URL, this is used to connect mobile devices var agentServerName = parent.getWebServerName(domain, req); diff --git a/package.json b/package.json index 014f23e1..0ca78120 100644 --- a/package.json +++ b/package.json @@ -37,19 +37,27 @@ "sample-config-advanced.json" ], "dependencies": { + "@yetzt/nedb": "^1.8.0", "archiver": "^5.3.1", "body-parser": "^1.19.0", "cbor": "~5.2.0", "compression": "^1.7.4", "cookie-session": "^1.4.0", + "discord.js": "^14.6.0", "express": "^4.17.0", "express-handlebars": "^5.3.5", "express-ws": "^4.0.0", + "input": "^1.0.1", "ipcheck": "^0.1.0", + "loadavg-windows": "^1.1.1", "minimist": "^1.2.5", "multiparty": "^4.2.1", - "@yetzt/nedb": "^1.8.0", + "mysql": "^2.18.1", "node-forge": "^1.0.0", + "node-windows": "^0.1.4", + "otplib": "^10.2.3", + "ssh2": "^1.11.0", + "telegram": "^2.13.6", "ws": "^5.2.3", "yauzl": "^2.10.0" }, diff --git a/sample-config-advanced.json b/sample-config-advanced.json index 234eae64..c3ebb79c 100644 --- a/sample-config-advanced.json +++ b/sample-config-advanced.json @@ -616,10 +616,14 @@ "url": "http://example.com/sms.ashx?phone={{phone}}&message={{message}}" }, "_messaging": { - "telegram": { + "_telegram": { "apiid": 0, - "apihash": "hexBalue", - "session": "base64Value" + "apihash": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "session": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + }, + "_discord": { + "serverurl": "https://discord.gg/xxxxxxxxx", + "token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxx" } } } diff --git a/views/default.handlebars b/views/default.handlebars index 4d79383b..e8f85a33 100644 --- a/views/default.handlebars +++ b/views/default.handlebars @@ -11981,7 +11981,7 @@ } else { x = '
'; x += '' + "Enter your messaging service and handle. Once verified, this server can send you login verification and other notifications." + '

'; - var y = ''; if ((serverinfo.userMsgProviders & 1) != 0) { y += ''; } if ((serverinfo.userMsgProviders & 2) != 0) { y += ''; } if ((serverinfo.userMsgProviders & 4) != 0) { y += ''; } @@ -11989,13 +11989,18 @@ x += '
' + "Service" + '' + y; x += '
' + "Handle" + ''; x += '
'; + if (serverinfo.discordUrl) { x += '' } setDialogMode(2, "Messaging Notifications", 3, account_manageMessagingAdd, x, 'verifyMessaging'); Q('d2handleinput').focus(); account_manageMessagingValidate(); } } - function account_manageMessagingValidate(x) { var ok = (Q('d2handleinput').value.length > 0); QE('idx_dlgOkButton', ok); if ((x == 1) && ok) { dialogclose(1); } } + function account_manageMessagingValidate(x) { + if (serverinfo.discordUrl) { QV('d2discordurl', Q('d2serviceselect').value == 4); } + if (Q('d2serviceselect').value == 4) { Q('d2handleinput')['placeholder'] = "Username:0000"; } else { Q('d2handleinput')['placeholder'] = "Username"; } + var ok = (Q('d2handleinput').value.length > 0); QE('idx_dlgOkButton', ok); if ((x == 1) && ok) { dialogclose(1); } + } function account_manageMessagingAdd() { if (Q('d2handleinput').value.length == 0) return; QE('d2handleinput', false); meshserver.send({ action: 'verifyMessaging', service: Q('d2serviceselect').value, handle: Q('d2handleinput').value }); } function account_manageMessagingConfirm(b, tag) { meshserver.send({ action: 'confirmMessaging', code: Q('d2phoneCodeInput').value, cookie: tag }); } function account_manageMessagingRemove() { if (Q('d2delPhone').checked) { meshserver.send({ action: 'removeMessaging' }); } } @@ -15850,9 +15855,8 @@ function p30editMessaging() { if (xxdialogMode) return; var x = '
'; - x += '' + "Messaging account for this user." + '
' + "Leave blank for none."; - - var y = '
' + "Messaging account for this user."; + var y = '
' + "Service" + '' + y; x += '
' + "Handle" + ''; x += '
'; - + if (serverinfo.discordUrl) { x += '' } setDialogMode(2, "Messaging Notifications", 3, p30editMessagingEx, x, 'verifyMessaging'); Q('d2handleinput').focus(); + if (userinfo.msghandle) { + if (userinfo.msghandle.startsWith('telegram:') && ((serverinfo.userMsgProviders & 1) != 0)) { Q('d2serviceselect').value = 1; Q('d2handleinput').value = userinfo.msghandle.substring(10); } + if (userinfo.msghandle.startsWith('discord:') && ((serverinfo.userMsgProviders & 4) != 0)) { Q('d2serviceselect').value = 4; Q('d2handleinput').value = userinfo.msghandle.substring(8); } + } p30editMessagingValidate(); } - function p30editMessagingValidate(x) { if (x == 1) { dialogclose(1); } } + function p30editMessagingValidate(x) { + QE('d2handleinput', Q('d2serviceselect').value != 0); + if (serverinfo.discordUrl) { QV('d2discordurl', Q('d2serviceselect').value == 4); } + if (x == 1) { dialogclose(1); } + } // Send to the server the user's messaging account function p30editMessagingEx() { var handle = null; - if (Q('d2handleinput').value == '') { handle = ''; } + if ((Q('d2handleinput').value == '') || (Q('d2serviceselect').value == 0)) { handle = ''; } else if (Q('d2serviceselect').value == 1) { handle = 'telegram:@' + Q('d2handleinput').value; } else if (Q('d2serviceselect').value == 4) { handle = 'discord:' + Q('d2handleinput').value; } if (handle != null) { meshserver.send({ action: 'edituser', id: currentUser._id, msghandle: handle }); }