Skip to content

Commit

Permalink
Added Telegram setup system with --setuptelegram (#4650)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ylianst committed Oct 22, 2022
1 parent e2cf723 commit b7f8e52
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
5 changes: 4 additions & 1 deletion meshcentral.js
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
54 changes: 53 additions & 1 deletion meshmessaging.js
Expand Up @@ -25,7 +25,7 @@
}
*/

// Construct a SMS server object
// Construct a messaging server object
module.exports.CreateServer = function (parent) {
var obj = {};
obj.parent = parent;
Expand Down Expand Up @@ -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();
};

0 comments on commit b7f8e52

Please sign in to comment.