From b7f8e5238bef0767fbd1da2f8554619754628852 Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Sat, 22 Oct 2022 14:51:16 -0700 Subject: [PATCH] Added Telegram setup system with --setuptelegram (#4650) --- meshcentral.js | 5 ++++- meshmessaging.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/meshcentral.js b/meshcentral.js index b4bf7d9c71..126eefc8d5 100644 --- a/meshcentral.js +++ b/meshcentral.js @@ -139,7 +139,7 @@ function CreateMeshCentralServer(config, args) { try { require('./pass').hash('test', function () { }, 0); } catch (ex) { console.log('Old version of node, must upgrade.'); return; } // TODO: Not sure if this test works or not. // Check for invalid arguments - const validArguments = ['_', 'user', 'port', 'aliasport', 'mpsport', 'mpsaliasport', 'redirport', 'rediraliasport', 'cert', 'mpscert', 'deletedomain', 'deletedefaultdomain', 'showall', 'showusers', 'showitem', 'listuserids', 'showusergroups', 'shownodes', 'showallmeshes', 'showmeshes', 'showevents', 'showsmbios', 'showpower', 'clearpower', 'showiplocations', 'help', 'exactports', 'xinstall', 'xuninstall', 'install', 'uninstall', 'start', 'stop', 'restart', 'debug', 'filespath', 'datapath', 'noagentupdate', 'launch', 'noserverbackup', 'mongodb', 'mongodbcol', 'wanonly', 'lanonly', 'nousers', 'mpspass', 'ciralocalfqdn', 'dbexport', 'dbexportmin', 'dbimport', 'dbmerge', 'dbfix', 'dbencryptkey', 'selfupdate', 'tlsoffload', 'userallowedip', 'userblockedip', 'swarmallowedip', 'agentallowedip', 'agentblockedip', 'fastcert', 'swarmport', 'logintoken', 'logintokenkey', 'logintokengen', 'mailtokengen', 'admin', 'unadmin', 'sessionkey', 'sessiontime', 'minify', 'minifycore', 'dblistconfigfiles', 'dbshowconfigfile', 'dbpushconfigfiles', 'dbpullconfigfiles', 'dbdeleteconfigfiles', 'vaultpushconfigfiles', 'vaultpullconfigfiles', 'vaultdeleteconfigfiles', 'configkey', 'loadconfigfromdb', 'npmpath', 'serverid', 'recordencryptionrecode', 'vault', 'token', 'unsealkey', 'name', 'log', 'dbstats', 'translate', 'createaccount', 'resetaccount', 'pass', 'removesubdomain', 'adminaccount', 'domain', 'email', 'configfile', 'maintenancemode', 'nedbtodb', 'removetestagents', 'agentupdatetest', 'hashpassword', 'hashpass', 'indexmcrec', 'mpsdebug', 'dumpcores']; + const validArguments = ['_', 'user', 'port', 'aliasport', 'mpsport', 'mpsaliasport', 'redirport', 'rediraliasport', 'cert', 'mpscert', 'deletedomain', 'deletedefaultdomain', 'showall', 'showusers', 'showitem', 'listuserids', 'showusergroups', 'shownodes', 'showallmeshes', 'showmeshes', 'showevents', 'showsmbios', 'showpower', 'clearpower', 'showiplocations', 'help', 'exactports', 'xinstall', 'xuninstall', 'install', 'uninstall', 'start', 'stop', 'restart', 'debug', 'filespath', 'datapath', 'noagentupdate', 'launch', 'noserverbackup', 'mongodb', 'mongodbcol', 'wanonly', 'lanonly', 'nousers', 'mpspass', 'ciralocalfqdn', 'dbexport', 'dbexportmin', 'dbimport', 'dbmerge', 'dbfix', 'dbencryptkey', 'selfupdate', 'tlsoffload', 'userallowedip', 'userblockedip', 'swarmallowedip', 'agentallowedip', 'agentblockedip', 'fastcert', 'swarmport', 'logintoken', 'logintokenkey', 'logintokengen', 'mailtokengen', 'admin', 'unadmin', 'sessionkey', 'sessiontime', 'minify', 'minifycore', 'dblistconfigfiles', 'dbshowconfigfile', 'dbpushconfigfiles', 'dbpullconfigfiles', 'dbdeleteconfigfiles', 'vaultpushconfigfiles', 'vaultpullconfigfiles', 'vaultdeleteconfigfiles', 'configkey', 'loadconfigfromdb', 'npmpath', 'serverid', 'recordencryptionrecode', 'vault', 'token', 'unsealkey', 'name', 'log', 'dbstats', 'translate', 'createaccount', 'setuptelegram', 'resetaccount', 'pass', 'removesubdomain', 'adminaccount', 'domain', 'email', 'configfile', 'maintenancemode', 'nedbtodb', 'removetestagents', 'agentupdatetest', 'hashpassword', 'hashpass', 'indexmcrec', 'mpsdebug', 'dumpcores']; for (var arg in obj.args) { obj.args[arg.toLocaleLowerCase()] = obj.args[arg]; if (validArguments.indexOf(arg.toLocaleLowerCase()) == -1) { console.log('Invalid argument "' + arg + '", use --help.'); return; } } if (obj.args.mongodb == true) { console.log('Must specify: --mongodb [connectionstring] \r\nSee https://docs.mongodb.com/manual/reference/connection-string/ for MongoDB connection string.'); return; } for (i in obj.config.settings) { obj.args[i] = obj.config.settings[i]; } // Place all settings into arguments, arguments have already been placed into settings so arguments take precedence. @@ -195,6 +195,9 @@ function CreateMeshCentralServer(config, args) { // Dump to mesh cores if (obj.args.dumpcores) { obj.updateMeshCore(function () { console.log('Done.'); }, true); return; } + // Setup Telegram + if (obj.args.setuptelegram) { require('./meshmessaging.js').SetupTelegram(obj); return; } + // Perform web site translations into different languages if (obj.args.translate) { // Check NodeJS version diff --git a/meshmessaging.js b/meshmessaging.js index e5580bf410..b52eb7cb90 100644 --- a/meshmessaging.js +++ b/meshmessaging.js @@ -25,7 +25,7 @@ } */ -// Construct a SMS server object +// Construct a messaging server object module.exports.CreateServer = function (parent) { var obj = {}; obj.parent = parent; @@ -149,3 +149,55 @@ module.exports.CreateServer = function (parent) { return obj; }; + +// Called to setup the Telegram session key +module.exports.SetupTelegram = async function (parent) { + // If basic telegram values are not setup, instruct the user on how to get them. + if ((typeof parent.config.messaging != 'object') || (typeof parent.config.messaging.telegram != 'object') || (typeof parent.config.messaging.telegram.apiid != 'number') || (typeof parent.config.messaging.telegram.apihash != 'string')) { + console.log('Login to your Telegram account at this URL: https://my.telegram.org/.'); + console.log('Click "API development tools" and fill your application details (only app title and short name required).'); + console.log('Click "Create application"'); + console.log('Set this apiid and apihash values in the messaging section of the config.json like this:'); + console.log('{'); + console.log(' "messaging": {'); + console.log(' "telegram": {'); + console.log(' "apiid": 123456,'); + console.log(' "apihash": "123456abcdfg"'); + console.log(' }'); + console.log(' }'); + console.log('}'); + console.log('Then, run --setuptelegram again to continue.'); + process.exit(); + return; + } + + // If the session value is missing, perform the process to get it + if ((parent.config.messaging.telegram.session == null) || (parent.config.messaging.telegram.session == '') || (typeof parent.config.messaging.telegram.session != 'string')) { + const { TelegramClient } = require('telegram'); + const { StringSession } = require('telegram/sessions'); + const input = require('input'); + const stringSession = new StringSession(''); + const client = new TelegramClient(stringSession, parent.config.messaging.telegram.apiid, parent.config.messaging.telegram.apihash, { connectionRetries: 5 }); + await client.start({ + phoneNumber: async function () { return await input.text("Please enter your number (+1-111-222-3333): "); }, + password: async function () { return await input.text("Please enter your password: "); }, + phoneCode: async function () { return await input.text("Please enter the code you received: "); }, + onError: function (err) { console.log('Telegram error', err); } + }); + console.log('Set this session value in the messaging section of the config.json like this:'); + console.log('{'); + console.log(' "messaging": {'); + console.log(' "telegram": {'); + console.log(' "apiid": ' + parent.config.messaging.telegram.apiid + ','); + console.log(' "apihash": "' + parent.config.messaging.telegram.apihash + '",'); + console.log(' "session": "' + client.session.save() + '"'); + console.log(' }'); + console.log(' }'); + console.log('}'); + process.exit(); + } + + // All Telegram values seem ok + console.log('Telegram seems to be configured correctly in the config.json, no need to run --setuptelegram.'); + process.exit(); +};