Added Telegram proxy support (#4697, #4650)

Ylian Saint-Hilaire committed Nov 1, 2022 at 14:51 UTC 1ef585613ec41f3ca76de0b5714ff4109d11a55c
3 files changed +50 -3
docs/docs/messaging/index.md
+22 -1
@@ -71,7 +71,28 @@ MeshCentral HTTPS relay server running on relay1.mesh.meshcentral.com:443.
71 MeshCentral Telegram client is bot connected.
72 ```
73
74 -Note the last line, indicating it's connected as a bot.
74 +Note the last line, indicating it's connected as a bot. If you wish to use Telegram with a proxy, here are the possible Telegram settings. You can use the proxy settings for both user or bot login modes.
75 +
76 +```
77 +{
78 + "messaging": {
79 + "telegram": {
80 + "apiid": 0,
81 + "apihash": "00000000000000000000000",
82 + "session": "aaaaaaaaaaaaaaaaaaaaaaa",
83 + "useWSS": false, // Important. Most proxies cannot use SSL.
84 + "proxy": {
85 + "ip": "123.123.123.123", // Proxy host (IP or hostname)
86 + "port": 123, // Proxy port
87 + "MTProxy": false, // Whether it's an MTProxy or a normal Socks one
88 + "secret": "00000000000000000000000000000000", // If used MTProxy then you need to provide a secret (or zeros).
89 + "socksType": 5, // If used Socks you can choose 4 or 5.
90 + "timeout": 2 // Timeout (in seconds) for connection,
91 + }
92 + }
93 + }
94 +}
95 +```
96
97 ## Discord Setup
98
meshmessaging.js
+26 -2
@@ -33,6 +33,26 @@
33 }
34 }
35
36 +// For Telegram login with proxy settings, add this in config.json
37 +{
38 + "messaging": {
39 + "telegram": {
40 + "apiid": 0,
41 + "apihash": "00000000000000000000000",
42 + "session": "aaaaaaaaaaaaaaaaaaaaaaa",
43 + "useWSS": false, // Important. Most proxies cannot use SSL.
44 + "proxy": {
45 + "ip": "123.123.123.123", // Proxy host (IP or hostname)
46 + "port": 123, // Proxy port
47 + "MTProxy": false, // Whether it's an MTProxy or a normal Socks one
48 + "secret": "00000000000000000000000000000000", // If used MTProxy then you need to provide a secret (or zeros).
49 + "socksType": 5, // If used Socks you can choose 4 or 5.
50 + "timeout": 2 // Timeout (in seconds) for connection,
51 + }
52 + }
53 + }
54 +}
55 +
56 // For Discord login, add this in config.json
57 "messaging": {
58 "discord": {
@@ -120,10 +140,14 @@ module.exports.CreateServer = function (parent) {
140 const logger = new Logger({ LogLevel : 'none' });
141 const input = require('input');
142 var client;
143 + var options = { connectionRetries: 5, baseLogger: logger };
144 + if (parent.config.messaging.telegram.usewss == false) { options.useWSS = false; }
145 + if (typeof parent.config.messaging.telegram.connectionretries == 'number') { options.connectionRetries = parent.config.messaging.telegram.connectionretries; }
146 + if (typeof parent.config.messaging.telegram.proxy == 'object') { options.proxy = parent.config.messaging.telegram.proxy; }
147 if (parent.config.messaging.telegram.bottoken == null) {
148 // User login
149 var stringSession = new StringSession(parent.config.messaging.telegram.session);
126 - const client = new TelegramClient(stringSession, parent.config.messaging.telegram.apiid, parent.config.messaging.telegram.apihash, { connectionRetries: 5, baseLogger: logger });
150 + const client = new TelegramClient(stringSession, parent.config.messaging.telegram.apiid, parent.config.messaging.telegram.apihash, options);
151 await client.start({ onError: function (err) { console.log('Telegram error', err); } });
152 obj.telegramClient = client;
153 obj.providers += 1; // Enable Telegram messaging
@@ -131,7 +155,7 @@ module.exports.CreateServer = function (parent) {
155 } else {
156 // Bot login
157 var stringSession = new StringSession('');
134 - const client = new TelegramClient(stringSession, parent.config.messaging.telegram.apiid, parent.config.messaging.telegram.apihash, { connectionRetries: 5, baseLogger: logger });
158 + const client = new TelegramClient(stringSession, parent.config.messaging.telegram.apiid, parent.config.messaging.telegram.apihash, options);
159 await client.start({ botAuthToken: parent.config.messaging.telegram.bottoken, onError: function (err) { console.log('Telegram error', err); } });
160 obj.telegramClient = client;
161 obj.providers += 1; // Enable Telegram messaging
package.json
+2
@@ -46,10 +46,12 @@
46 "express": "^4.17.0",
47 "express-handlebars": "^5.3.5",
48 "express-ws": "^4.0.0",
49 + "input": "^1.0.1",
50 "ipcheck": "^0.1.0",
51 "minimist": "^1.2.5",
52 "multiparty": "^4.2.1",
53 "node-forge": "^1.0.0",
54 + "telegram": "^2.13.6",
55 "ws": "^5.2.3",
56 "yauzl": "^2.10.0",
57 "zulip": "^0.1.0"