Completed Discord integration (#4651)

Ylian Saint-Hilaire committed Oct 27, 2022 at 10:38 UTC 3761a93961ebb3a12d0fe4745393354a511c806b
8 files changed +79 -15
MeshCentralServer.njsproj
+6
@@ -681,6 +681,7 @@
681 <Folder Include="typings\globals\ajv\" />
682 <Folder Include="typings\globals\async\" />
683 <Folder Include="typings\globals\axios\" />
684 + <Folder Include="typings\globals\big-integer\" />
685 <Folder Include="typings\globals\busboy\" />
686 <Folder Include="typings\globals\connect-redis\" />
687 <Folder Include="typings\globals\cookie-session\" />
@@ -695,6 +696,7 @@
696 <Folder Include="typings\globals\he\" />
697 <Folder Include="typings\globals\hooker\" />
698 <Folder Include="typings\globals\http-errors\" />
699 + <Folder Include="typings\globals\ip\" />
700 <Folder Include="typings\globals\is-plain-object\" />
701 <Folder Include="typings\globals\jsbn\" />
702 <Folder Include="typings\globals\klaw\" />
@@ -711,6 +713,7 @@
713 <Folder Include="typings\globals\once\" />
714 <Folder Include="typings\globals\passport\" />
715 <Folder Include="typings\globals\pg-pool\" />
716 + <Folder Include="typings\globals\rx-lite\" />
717 <Folder Include="typings\globals\split2\" />
718 <Folder Include="typings\globals\sprintf-js\" />
719 <Folder Include="typings\globals\sqlite3\" />
@@ -724,6 +727,7 @@
727 <TypeScriptCompile Include="typings\globals\ajv\index.d.ts" />
728 <TypeScriptCompile Include="typings\globals\async\index.d.ts" />
729 <TypeScriptCompile Include="typings\globals\axios\index.d.ts" />
730 + <TypeScriptCompile Include="typings\globals\big-integer\index.d.ts" />
731 <TypeScriptCompile Include="typings\globals\busboy\index.d.ts" />
732 <TypeScriptCompile Include="typings\globals\connect-redis\index.d.ts" />
733 <TypeScriptCompile Include="typings\globals\cookie-session\index.d.ts" />
@@ -738,6 +742,7 @@
742 <TypeScriptCompile Include="typings\globals\he\index.d.ts" />
743 <TypeScriptCompile Include="typings\globals\hooker\index.d.ts" />
744 <TypeScriptCompile Include="typings\globals\http-errors\index.d.ts" />
745 + <TypeScriptCompile Include="typings\globals\ip\index.d.ts" />
746 <TypeScriptCompile Include="typings\globals\is-plain-object\index.d.ts" />
747 <TypeScriptCompile Include="typings\globals\jsbn\index.d.ts" />
748 <TypeScriptCompile Include="typings\globals\klaw\index.d.ts" />
@@ -754,6 +759,7 @@
759 <TypeScriptCompile Include="typings\globals\once\index.d.ts" />
760 <TypeScriptCompile Include="typings\globals\passport\index.d.ts" />
761 <TypeScriptCompile Include="typings\globals\pg-pool\index.d.ts" />
762 + <TypeScriptCompile Include="typings\globals\rx-lite\index.d.ts" />
763 <TypeScriptCompile Include="typings\globals\split2\index.d.ts" />
764 <TypeScriptCompile Include="typings\globals\sprintf-js\index.d.ts" />
765 <TypeScriptCompile Include="typings\globals\sqlite3\index.d.ts" />
docs/docs/messaging/index.md
+22 -1
@@ -6,7 +6,7 @@ MeshCentral supports messaging platforms so that users can register a messaging
6
7 ## Telegram Setup
8
9 -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:
9 +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:
10
11 ```json
12 {
@@ -59,6 +59,27 @@ MeshCentral Telegram client is bot connected.
59
60 Note the last line, indicating it's connected as a bot.
61
62 +## Discord Setup
63 +
64 +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.
65 +
66 +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.
67 +
68 +Once you have the Discord bot login token, the config.json Discord configuration looks like this:
69 +
70 +```json
71 +{
72 + "messaging": {
73 + "discord": {
74 + "serverurl": "https://discord.gg/xxxxxxxxx",
75 + "token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
76 + }
77 + }
78 +}
79 +```
80 +
81 +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.
82 +
83 ## User Setup
84
85 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.
meshcentral-config-schema.json
+9
@@ -1370,6 +1370,15 @@
1370 "apihash": { "type": "string" },
1371 "session": { "type": "string" }
1372 }
1373 + },
1374 + "discord": {
1375 + "type": "object",
1376 + "description": "Configure Discord messaging system",
1377 + "properties": {
1378 + "serverurl": { "type": "string", "format": "uri", "description": "An optional HTTP link to the discord server the user must join to get notifications." },
1379 + "token": { "type": "string", "description": "A Discord bot token that MeshCentral will use to login to Discord." }
1380 + },
1381 + "required": [ "token" ]
1382 }
1383 }
1384 }
meshmessaging.js
+2 -1
@@ -49,6 +49,7 @@ module.exports.CreateServer = function (parent) {
49 obj.providers = 0; // 1 = Telegram, 2 = Signal, 4 = Discord
50 obj.telegramClient = null;
51 obj.discordClient = null;
52 + obj.discordUrl = null;
53
54 // Telegram client setup
55 if (parent.config.messaging.telegram) {
@@ -93,7 +94,6 @@ module.exports.CreateServer = function (parent) {
94 if (parent.config.messaging.discord) {
95 // Validate Discord configuration values
96 var discordOK = true;
96 - if (typeof parent.config.messaging.discord.inviteurl != 'string') { console.log('Invalid or missing Discord invite URL.'); discordOK = false; }
97 if (typeof parent.config.messaging.discord.token != 'string') { console.log('Invalid or missing Discord token.'); discordOK = false; }
98
99 if (discordOK) {
@@ -113,6 +113,7 @@ module.exports.CreateServer = function (parent) {
113 discordClient.on('ready', function() {
114 console.log(`MeshCentral Discord client is connected as ${discordClient.user.tag}!`);
115 obj.discordClient = discordClient;
116 + obj.discordUrl = parent.config.messaging.discord.serverurl;
117 obj.providers += 4; // Enable Discord messaging
118 });
119
meshuser.js
+4 -1
@@ -575,7 +575,10 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
575 if (domain.passwordrequirements.lock2factor == true) { serverinfo.lock2factor = true; } // Indicate 2FA change are not allowed
576 if (typeof domain.passwordrequirements.maxfidokeys == 'number') { serverinfo.maxfidokeys = domain.passwordrequirements.maxfidokeys; }
577 }
578 - if (parent.parent.msgserver != null) { serverinfo.userMsgProviders = parent.parent.msgserver.providers; }
578 + if (parent.parent.msgserver != null) { // Setup messaging providers information
579 + serverinfo.userMsgProviders = parent.parent.msgserver.providers;
580 + if (parent.parent.msgserver.discordUrl != null) { serverinfo.discordUrl = parent.parent.msgserver.discordUrl; }
581 + }
582
583 // Build the mobile agent URL, this is used to connect mobile devices
584 var agentServerName = parent.getWebServerName(domain, req);
package.json
+9 -1
@@ -37,19 +37,27 @@
37 "sample-config-advanced.json"
38 ],
39 "dependencies": {
40 + "@yetzt/nedb": "^1.8.0",
41 "archiver": "^5.3.1",
42 "body-parser": "^1.19.0",
43 "cbor": "~5.2.0",
44 "compression": "^1.7.4",
45 "cookie-session": "^1.4.0",
46 + "discord.js": "^14.6.0",
47 "express": "^4.17.0",
48 "express-handlebars": "^5.3.5",
49 "express-ws": "^4.0.0",
50 + "input": "^1.0.1",
51 "ipcheck": "^0.1.0",
52 + "loadavg-windows": "^1.1.1",
53 "minimist": "^1.2.5",
54 "multiparty": "^4.2.1",
51 - "@yetzt/nedb": "^1.8.0",
55 + "mysql": "^2.18.1",
56 "node-forge": "^1.0.0",
57 + "node-windows": "^0.1.4",
58 + "otplib": "^10.2.3",
59 + "ssh2": "^1.11.0",
60 + "telegram": "^2.13.6",
61 "ws": "^5.2.3",
62 "yauzl": "^2.10.0"
63 },
sample-config-advanced.json
+7 -3
@@ -616,10 +616,14 @@
616 "url": "http://example.com/sms.ashx?phone={{phone}}&message={{message}}"
617 },
618 "_messaging": {
619 - "telegram": {
619 + "_telegram": {
620 "apiid": 0,
621 - "apihash": "hexBalue",
622 - "session": "base64Value"
621 + "apihash": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
622 + "session": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
623 + },
624 + "_discord": {
625 + "serverurl": "https://discord.gg/xxxxxxxxx",
626 + "token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
627 }
628 }
629 }
views/default.handlebars
+20 -8
@@ -11981,7 +11981,7 @@
11981 } else {
11982 x = '<table style=width:100%><tr><td style=width:56px;vertical-align:top><img src="images/messaging40.png" style=padding:8px>';
11983 x += '<td>' + "Enter your messaging service and handle. Once verified, this server can send you login verification and other notifications." + '<br /><br />';
11984 - var y = '<select id=d2serviceselect style=width:160px;margin-left:8px>';
11984 + var y = '<select id=d2serviceselect style=width:160px;margin-left:8px onchange=account_manageMessagingValidate()>';
11985 if ((serverinfo.userMsgProviders & 1) != 0) { y += '<option value=1>' + "Telegram" + '</option>'; }
11986 if ((serverinfo.userMsgProviders & 2) != 0) { y += '<option value=2>' + "Signal Messenger" + '</option>'; }
11987 if ((serverinfo.userMsgProviders & 4) != 0) { y += '<option value=4>' + "Discord" + '</option>'; }
@@ -11989,13 +11989,18 @@
11989 x += '<table><tr><td>' + "Service" + '<td>' + y;
11990 x += '<tr><td>' + "Handle" + '<td><input maxlength=64 style=width:160px;margin-left:8px id=d2handleinput onKeyUp=account_manageMessagingValidate() onkeypress="if (event.key==\'Enter\') account_manageMessagingValidate(1)">';
11991 x += '</table>';
11992 + if (serverinfo.discordUrl) { x += '<div id=d2discordurl style=display:none><br /><a href=' + serverinfo.discordUrl + ' target="_discord">' + "Join this Discord server to receive notifications." + '</a></div>' }
11993 setDialogMode(2, "Messaging Notifications", 3, account_manageMessagingAdd, x, 'verifyMessaging');
11994 Q('d2handleinput').focus();
11995 account_manageMessagingValidate();
11996 }
11997 }
11998
11998 - function account_manageMessagingValidate(x) { var ok = (Q('d2handleinput').value.length > 0); QE('idx_dlgOkButton', ok); if ((x == 1) && ok) { dialogclose(1); } }
11999 + function account_manageMessagingValidate(x) {
12000 + if (serverinfo.discordUrl) { QV('d2discordurl', Q('d2serviceselect').value == 4); }
12001 + if (Q('d2serviceselect').value == 4) { Q('d2handleinput')['placeholder'] = "Username:0000"; } else { Q('d2handleinput')['placeholder'] = "Username"; }
12002 + var ok = (Q('d2handleinput').value.length > 0); QE('idx_dlgOkButton', ok); if ((x == 1) && ok) { dialogclose(1); }
12003 + }
12004 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 }); }
12005 function account_manageMessagingConfirm(b, tag) { meshserver.send({ action: 'confirmMessaging', code: Q('d2phoneCodeInput').value, cookie: tag }); }
12006 function account_manageMessagingRemove() { if (Q('d2delPhone').checked) { meshserver.send({ action: 'removeMessaging' }); } }
@@ -15850,9 +15855,8 @@
15855 function p30editMessaging() {
15856 if (xxdialogMode) return;
15857 var x = '<table style=width:100%><tr><td style=width:56px;vertical-align:top><img src="images/messaging40.png" style=padding:8px>';
15853 - x += '<td style=width:100%>' + "Messaging account for this user." + '<br />' + "Leave blank for none.";
15854 -
15855 - var y = '<select id=d2serviceselect style=width:160px;margin-left:8px>';
15858 + x += '<td style=width:100%>' + "Messaging account for this user.";
15859 + var y = '<select id=d2serviceselect style=width:160px;margin-left:8px onchange=p30editMessagingValidate()><option value=0>' + "None" + '</option>';
15860 if ((serverinfo.userMsgProviders & 1) != 0) { y += '<option value=1>' + "Telegram" + '</option>'; }
15861 if ((serverinfo.userMsgProviders & 2) != 0) { y += '<option value=2>' + "Signal Messenger" + '</option>'; }
15862 if ((serverinfo.userMsgProviders & 4) != 0) { y += '<option value=4>' + "Discord" + '</option>'; }
@@ -15860,18 +15864,26 @@
15864 x += '<table style=margin-top:12px><tr><td>' + "Service" + '<td>' + y;
15865 x += '<tr><td>' + "Handle" + '<td><input maxlength=64 style=width:160px;margin-left:8px id=d2handleinput onKeyUp=p30editMessagingValidate() onkeypress="if (event.key==\'Enter\') p30editMessagingValidate(1)">';
15866 x += '</table>';
15863 -
15867 + if (serverinfo.discordUrl) { x += '<div id=d2discordurl style=display:none><br /><a href=' + serverinfo.discordUrl + ' target="_discord">' + "Join this Discord server to receive notifications." + '</a></div>' }
15868 setDialogMode(2, "Messaging Notifications", 3, p30editMessagingEx, x, 'verifyMessaging');
15869 Q('d2handleinput').focus();
15870 + if (userinfo.msghandle) {
15871 + if (userinfo.msghandle.startsWith('telegram:') && ((serverinfo.userMsgProviders & 1) != 0)) { Q('d2serviceselect').value = 1; Q('d2handleinput').value = userinfo.msghandle.substring(10); }
15872 + if (userinfo.msghandle.startsWith('discord:') && ((serverinfo.userMsgProviders & 4) != 0)) { Q('d2serviceselect').value = 4; Q('d2handleinput').value = userinfo.msghandle.substring(8); }
15873 + }
15874 p30editMessagingValidate();
15875 }
15876
15869 - function p30editMessagingValidate(x) { if (x == 1) { dialogclose(1); } }
15877 + function p30editMessagingValidate(x) {
15878 + QE('d2handleinput', Q('d2serviceselect').value != 0);
15879 + if (serverinfo.discordUrl) { QV('d2discordurl', Q('d2serviceselect').value == 4); }
15880 + if (x == 1) { dialogclose(1); }
15881 + }
15882
15883 // Send to the server the user's messaging account
15884 function p30editMessagingEx() {
15885 var handle = null;
15874 - if (Q('d2handleinput').value == '') { handle = ''; }
15886 + if ((Q('d2handleinput').value == '') || (Q('d2serviceselect').value == 0)) { handle = ''; }
15887 else if (Q('d2serviceselect').value == 1) { handle = 'telegram:@' + Q('d2handleinput').value; }
15888 else if (Q('d2serviceselect').value == 4) { handle = 'discord:' + Q('d2handleinput').value; }
15889 if (handle != null) { meshserver.send({ action: 'edituser', id: currentUser._id, msghandle: handle }); }