Skip to content

Commit a7ac927

Browse files
author
windka
committed
added proxy support fixed #2
1 parent fd0b413 commit a7ac927

4 files changed

Lines changed: 160 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
# [0.1.3] - 2023-10-12
5+
### added proxy support - [#2](https://github.com/windkh/node-red-node-telegrambot/issues/2)
6+
47
# [0.1.2] - 2023-10-09
58
### added raw events
69

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-node-telegrambot",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"description": "Telegram userbot nodes for Node-RED",
55
"dependencies": {
66
"telegram": "^2.18.38"

telegrambot/telegrambot.html

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99
botname: { value: "", required: true },
1010
verboselogging: { value: false, required: false },
1111
loginmode: { value: "user", required: true },
12+
13+
useproxy: { value: false, required: false },
14+
usewss: { value: false, required: false },
15+
host: { value: "", required: false },
16+
sockstype: { value: "5", required: false },
17+
port: { value: 6667, required: false, validate:function(v) { return ((v === "") || (RED.validators.number(v) && (v >= 0) && (v <= 2147483))) }},
18+
username: { value: "anonymous", required: false },
19+
password: { value: "", required: false },
20+
secret: { value: "", required: false },
21+
mtproxy: { value: false, required: false },
22+
timeout: { value: 2, required: false, validate:function(v) { return ((v === "") || (RED.validators.number(v) && (v >= 0) && (v <= 2147483))) }},
1223
},
1324
credentials: {
1425
apiid: { value: 0, required: true, validate:function(v) { return ((v === "") || (RED.validators.number(v) && (v >= 0) && (v <= 4294967295))) }},
@@ -22,7 +33,7 @@
2233
return this.botname;
2334
},
2435
oneditprepare: function() {
25-
// polling or webhook
36+
// polling or webhook
2637
var updateLoginMode = function() {
2738
var mode = $("#node-config-input-loginmode").val();
2839
if (mode == "user") {
@@ -38,14 +49,43 @@
3849
updateLoginMode();
3950
$("#node-config-input-loginmode").change(updateLoginMode);
4051

52+
53+
// proxy on / off
54+
var useproxy = function() {
55+
var mode = $("#node-config-input-useproxy").prop('checked');
56+
if (mode === false) {
57+
$("#useproxy").hide();
58+
} else {
59+
$("#useproxy").show();
60+
}
61+
};
62+
useproxy();
63+
$("#node-config-input-useproxy").change(useproxy);
64+
65+
4166
var login = function() {
42-
4367
let apiId = $("#node-config-input-apiid").val();
4468
let apiHash = $("#node-config-input-apihash").val();
4569
let phoneNumber = $("#node-config-input-phonenumber").val();
4670
let password = $("#node-config-input-password").val();
4771
let botToken = $("#node-config-input-bottoken").val();
4872
let loginMode = $("#node-config-input-loginmode").val();
73+
let useProxy = $("#node-config-input-useproxy").val();
74+
let useWSS = $("#node-config-input-usewss").val();
75+
let proxy;
76+
77+
if (useProxy) {
78+
proxy = {
79+
ip: $("#node-config-input-host").val(),
80+
socksType: Number($("#node-config-input-sockstype").val()),
81+
port: Number($("#node-config-input-port").val()),
82+
username: $("#node-config-input-username").val(),
83+
password: $("#node-config-input-password").val(),
84+
secret: $("#node-config-input-secret").val(),
85+
MTProxy: $("#node-config-input-mtproxy").val(),
86+
timeout: Number($("#node-config-input-timeout").val()),
87+
};
88+
}
4989

5090
if (apiId !== '' && apiHash) {
5191
let parameters = {
@@ -54,7 +94,9 @@
5494
phoneNumber : phoneNumber,
5595
password : password,
5696
botToken : botToken,
57-
loginMode : loginMode
97+
loginMode : loginMode,
98+
proxy : proxy,
99+
useWSS : useWSS,
58100
}
59101

60102
$("#loginbuttontip").text("Login started: enter code in field below.");
@@ -199,6 +241,60 @@
199241

200242
<hr align="middle"/>
201243

244+
<div class="form-row">
245+
<label for="node-config-input-useproxy"><i class="fa fa-lock"></i> Use Proxy</label>
246+
<input type="checkbox" id="node-config-input-useproxy" style="display: inline-block; width: auto; vertical-align: top;">
247+
</div>
248+
<div class="form-row hidden" id="useproxy" style="background: var(--red-ui-tertiary-background)">
249+
<label style="width: auto"><i class="fa fa-cogs"></i> Proxy Options:</label>
250+
251+
<div class="form-row" style="background: #fff; margin-left: 20px">
252+
<div class="form-row">
253+
<label for="node-config-input-mtproxy"><i class="fa fa-telegram"></i> MT-Proxy</label>
254+
<input type="checkbox" id="node-config-input-mtproxy" style="display: inline-block; width: auto; vertical-align: top;">
255+
</div>
256+
<div class="form-row">
257+
<label for="node-config-input-sockstype"><i class="fa fa-user-secret"></i> Socks Type</label>
258+
<select id="node-config-input-sockstype">
259+
<option value="4">socks4</option>
260+
<option value="5">socks5</option>
261+
</select>
262+
</div>
263+
<div class="form-row">
264+
<label for="node-config-input-host"><i class="fa fa-desktop"></i> Host</label>
265+
<input type="text" id="node-config-input-host" placeholder="(IP or hostname of the proxy.)">
266+
</div>
267+
<div class="form-row">
268+
<label for="node-config-input-port"><i class="fa fa-random"></i> Port</label>
269+
<input type="text" id="node-config-input-port" placeholder="(Port of the proxy.)">
270+
</div>
271+
<div class="form-row">
272+
<label for="node-config-input-username"><i class="fa fa-user"></i> Username</label>
273+
<input type="text" id="node-config-input-username" placeholder="(Username of the socks proxy.)">
274+
</div>
275+
<div class="form-row">
276+
<label for="node-config-input-password"><i class="fa fa-key"></i> Password</label>
277+
<input type="text" id="node-config-input-password" placeholder="(Password of the socks proxy.)">
278+
</div>
279+
<div class="form-row">
280+
<label for="node-config-input-secret"><i class="fa fa-user-secret"></i> Secret</label>
281+
<input type="text" id="node-config-input-secret" placeholder="(Secret of the mtproxy.)">
282+
</div>
283+
<div class="form-row">
284+
<label for="node-config-input-timeout"><i class="fa fa-clock-o"></i> Timeout s</label>
285+
<input type="text" id="node-config-input-timeout" placeholder="(Connect timeout in seconds e.g.: 2)">
286+
</div>
287+
<div class="form-row">
288+
<label for="node-config-input-usewss"><i class="fa fa-lock"></i> Use WSS</label>
289+
<input type="checkbox" id="node-config-input-usewss" style="display: inline-block; width: auto; vertical-align: top;">
290+
</div>
291+
</div>
292+
293+
<div class="form-tips" style="width: auto"><b>Tip:</b> SOCKS proxy support is optional can can only be used with a valid proxy server, port, username and password.</div>
294+
</div>
295+
296+
<hr align="middle"/>
297+
202298
<div class="form-row">
203299
<label for="node-config-input-verboselogging"><i class="fa fa-search"></i> Verbose Logging</label>
204300
<input type="checkbox" id="node-config-input-verboselogging" style="display: inline-block; width: auto; vertical-align: top;">

telegrambot/telegrambot.js

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,25 @@ module.exports = function (RED) {
3939
let botToken = parameters.botToken;
4040
let password = parameters.password;
4141
let loginMode = parameters.loginMode;
42+
let proxy = parameters.proxy;
43+
let useWSS = parameters.useWSS;
4244

4345
if (password === undefined || password === '') {
4446
password = async () => await getPassword;
4547
}
4648

4749
if (apiId !== undefined && apiHash !== undefined && phoneNumber !== undefined) {
4850
const stringSession = new StringSession('');
49-
const client = new TelegramClient(stringSession, apiId, apiHash, {
51+
52+
let clientParams = {
5053
connectionRetries: 5,
51-
});
54+
proxy: proxy,
55+
useWSS: useWSS,
56+
};
57+
58+
const client = new TelegramClient(stringSession, apiId, apiHash, clientParams);
5259

53-
// client.setLogLevel('debug');
60+
client.setLogLevel('warn');
5461

5562
let authParams;
5663
if (loginMode === 'user') {
@@ -162,6 +169,23 @@ module.exports = function (RED) {
162169
this.client = null;
163170
this.logLevel = 'warn'; // 'none', 'error', 'warn','info', 'debug'
164171
this.verbose = n.verboselogging;
172+
this.useProxy = n.useproxy || false;
173+
this.useWSS = n.usewss || false;
174+
this.proxy;
175+
176+
if (this.useProxy) {
177+
this.proxy = {
178+
ip: n.host,
179+
socksType: Number(n.sockstype),
180+
port: Number(n.port),
181+
username: n.username,
182+
password: n.password,
183+
secret: n.secret,
184+
MTProxy: n.mtproxy,
185+
timeout: Number(n.timeout),
186+
};
187+
}
188+
165189
this.loginMode = n.loginmode;
166190
if (!this.loginMode) {
167191
this.loginMode = 'user';
@@ -179,15 +203,19 @@ module.exports = function (RED) {
179203
this.phoneNumber = this.credentials.phonenumber || '';
180204
}
181205

182-
this.createTelegramClient = async function createTelegramClient(node, apiId, apiHash, session, phoneNumber, botToken, logLevel) {
206+
this.createTelegramClient = async function createTelegramClient(node, apiId, apiHash, session, phoneNumber, botToken, logLevel, proxy, useWSS) {
183207
let client;
184208
try {
185209
if (apiId !== undefined && apiId !== '' && session !== undefined && session !== '') {
186210
const stringSession = new StringSession(session);
187211
const ID = Number(apiId);
188-
client = new TelegramClient(stringSession, ID, apiHash, {
212+
213+
let clientParams = {
189214
connectionRetries: 5,
190-
});
215+
proxy: proxy,
216+
useWSS: useWSS,
217+
};
218+
client = new TelegramClient(stringSession, ID, apiHash, clientParams);
191219

192220
client.setLogLevel(logLevel);
193221

@@ -221,7 +249,17 @@ module.exports = function (RED) {
221249
// Activates the client or returns the already activated bot.
222250
this.getTelegramClient = async function (node) {
223251
if (!this.client) {
224-
this.client = await this.createTelegramClient(node, this.apiId, this.apiHash, this.session, this.phoneNumber, this.botToken, this.logLevel);
252+
this.client = await this.createTelegramClient(
253+
node,
254+
this.apiId,
255+
this.apiHash,
256+
this.session,
257+
this.phoneNumber,
258+
this.botToken,
259+
this.logLevel,
260+
this.proxy,
261+
this.useWSS
262+
);
225263
}
226264

227265
return this.client;
@@ -292,7 +330,7 @@ module.exports = function (RED) {
292330
}
293331

294332
node.status({
295-
fill: 'green',
333+
fill: 'red',
296334
shape: 'ring',
297335
text: 'disconnected',
298336
});
@@ -324,7 +362,7 @@ module.exports = function (RED) {
324362

325363
this.on('close', function (removed, done) {
326364
node.stop();
327-
node.status({});
365+
// node.status({});
328366
done();
329367
});
330368
}
@@ -354,6 +392,14 @@ module.exports = function (RED) {
354392
};
355393
this.start();
356394

395+
this.stop = async () => {
396+
node.status({
397+
fill: 'red',
398+
shape: 'ring',
399+
text: 'disconnected',
400+
});
401+
};
402+
357403
this.processMessage = function (client, msg, nodeSend, nodeDone) {
358404
if (msg.payload !== undefined) {
359405
let api = msg.payload.api;
@@ -398,7 +444,8 @@ module.exports = function (RED) {
398444
});
399445

400446
this.on('close', function (removed, done) {
401-
node.status({});
447+
node.stop();
448+
// node.status({});
402449
done();
403450
});
404451
}

0 commit comments

Comments
 (0)