Skip to content

Commit 2032205

Browse files
committed
✨ Add proxy support ✨
1 parent 6f11bb6 commit 2032205

File tree

6 files changed

+129
-2
lines changed

6 files changed

+129
-2
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ If you want to allow another user to use the bot, use `/token` and give
6161
that user the resulting link. If you want to use this bot on a group,
6262
`/token` will give you a message to forward into the group.
6363

64+
## Proxy server
65+
66+
shell-bot obeys the `https_proxy` or `all_proxy` environment variable
67+
to use a proxy, and supports HTTP/HTTPS/SOCKS4/SOCKS4A/SOCKS5 proxies.
68+
Examples:
69+
70+
~~~ bash
71+
export https_proxy="http://168.63.76.32:3128"
72+
node server
73+
74+
export https_proxy="socks://127.0.0.1:9050"
75+
node server
76+
~~~
77+
6478

6579

6680
[Telegram bot]: https://core.telegram.org/bots

lib/utils.js

+37
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var fs = require("fs");
66
var util = require("util");
77
var mime = require("mime");
88
var crypto = require("crypto");
9+
var url = require("url");
910

1011

1112
/** TIMER **/
@@ -201,6 +202,41 @@ function constructFilename(msg) {
201202
return "upload." + mime.extension(msg.file.mime);
202203
}
203204

205+
/** AGENT **/
206+
207+
function createAgent() {
208+
var proxy = process.env["https_proxy"] || process.env["all_proxy"];
209+
if (!proxy) return;
210+
211+
try {
212+
proxy = url.parse(proxy);
213+
} catch (e) {
214+
console.error("Error parsing proxy URL:", e, "Ignoring proxy.");
215+
return;
216+
}
217+
218+
if ([ "socks:", "socks4:", "socks4a:", "socks5:" ].indexOf(proxy.protocol) !== -1) {
219+
try {
220+
var SocksProxyAgent = require('socks-proxy-agent');
221+
} catch (e) {
222+
console.error("Error loading SOCKS proxy support, verify socks-proxy-agent is correctly installed. Ignoring proxy.");
223+
return;
224+
}
225+
return new SocksProxyAgent(proxy);
226+
}
227+
if ([ "http:", "https:" ].indexOf(proxy.protocol) !== -1) {
228+
try {
229+
var HttpsProxyAgent = require('https-proxy-agent');
230+
} catch (e) {
231+
console.error("Error loading HTTPS proxy support, verify https-proxy-agent is correctly installed. Ignoring proxy.");
232+
return;
233+
}
234+
return new HttpsProxyAgent(proxy);
235+
}
236+
237+
console.error("Unknown proxy protocol:", util.inspect(proxy.protocol), "Ignoring proxy.");
238+
}
239+
204240

205241

206242
exports.Timer = Timer;
@@ -212,3 +248,4 @@ exports.resolveShell = resolveShell;
212248
exports.generateToken = generateToken;
213249
exports.resolveBoolean = resolveBoolean;
214250
exports.constructFilename = constructFilename;
251+
exports.createAgent = createAgent;

lib/wizard.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var readline = require("readline");
22
var botgram = require("botgram");
33
var fs = require("fs");
44
var util = require("util");
5+
var utils = require("./utils");
56

67
// Wizard functions
78

@@ -91,7 +92,7 @@ function writeFile(file, contents) {
9192

9293
function createBot(token) {
9394
return new Promise(function (resolve, reject) {
94-
var bot = botgram(token);
95+
var bot = botgram(token, { agent: utils.createAgent() });
9596
bot.on("error", function (err) {
9697
bot.stop();
9798
reject(err);

package-lock.json

+71
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
"node-pty": "^0.8.1",
1919
"terminal.js": "1.0.9"
2020
},
21+
"optionalDependencies": {
22+
"https-proxy-agent": "^2.2.1",
23+
"socks-proxy-agent": "^4.0.1",
24+
},
2125
"bugs": {
2226
"url": "https://github.com/botgram/shell-bot/issues"
2327
},

server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ try {
2121
return;
2222
}
2323

24-
var bot = botgram(config.authToken);
24+
var bot = botgram(config.authToken, { agent: utils.createAgent() });
2525
var owner = config.owner;
2626
var tokens = {};
2727
var granted = {};

0 commit comments

Comments
 (0)