add socks proxy support
This commit is contained in:
parent
c90c0c6688
commit
1ee9ebb8cd
3 changed files with 27 additions and 3 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "mtproto_exporter",
|
"name": "mtproto_exporter",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"version": "1.5.2",
|
"version": "1.5.3",
|
||||||
"packageManager": "pnpm@10.6.5",
|
"packageManager": "pnpm@10.6.5",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,10 @@ const USERBOT_PHONE = process.env.USERBOT_PHONE;
|
||||||
const USERBOT_2FACODE = process.env.USERBOT_2FACODE;
|
const USERBOT_2FACODE = process.env.USERBOT_2FACODE;
|
||||||
const USERBOT_PASSWORD = process.env.USERBOT_PASSWORD;
|
const USERBOT_PASSWORD = process.env.USERBOT_PASSWORD;
|
||||||
|
|
||||||
|
const SOCKS_PROXY = process.env.SOCKS_PROXY;
|
||||||
|
|
||||||
if (Number.isNaN(API_ID) || !API_HASH) {
|
if (Number.isNaN(API_ID) || !API_HASH) {
|
||||||
throw new Error("API_ID or API_HASH not set!");
|
throw new Error("API_ID or API_HASH not set!");
|
||||||
}
|
}
|
||||||
|
|
||||||
export { API_HASH, API_ID, USERBOT_2FACODE, USERBOT_PASSWORD, USERBOT_PHONE };
|
export { API_HASH, API_ID, USERBOT_2FACODE, USERBOT_PASSWORD, USERBOT_PHONE, SOCKS_PROXY };
|
||||||
|
|
|
||||||
24
src/main.ts
24
src/main.ts
|
|
@ -1,6 +1,6 @@
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import { Dispatcher } from "@mtcute/dispatcher";
|
import { Dispatcher } from "@mtcute/dispatcher";
|
||||||
import { TelegramClient } from "@mtcute/node";
|
import { SocksProxyTcpTransport, TcpTransport, TelegramClient } from "@mtcute/node";
|
||||||
|
|
||||||
import { collectDefaultMetrics, Registry } from "prom-client";
|
import { collectDefaultMetrics, Registry } from "prom-client";
|
||||||
import { config, readKeywords } from "./config.js";
|
import { config, readKeywords } from "./config.js";
|
||||||
|
|
@ -16,10 +16,32 @@ collectDefaultMetrics({ register: registry });
|
||||||
const server = new MetricsServer(registry);
|
const server = new MetricsServer(registry);
|
||||||
server.listen(config.bindHost, config.port);
|
server.listen(config.bindHost, config.port);
|
||||||
|
|
||||||
|
let transport;
|
||||||
|
|
||||||
|
if (env.SOCKS_PROXY) {
|
||||||
|
const parts = env.SOCKS_PROXY!.split(":");
|
||||||
|
if (parts.length !== 2) {
|
||||||
|
throw new Error("Malformed SOCKS_PROXY: " + env.SOCKS_PROXY);
|
||||||
|
}
|
||||||
|
|
||||||
|
const port = parseInt(parts[1]);
|
||||||
|
if (isNaN(port) || port < 1 || port > 65535) {
|
||||||
|
throw new Error("Invalid port in SOCKS_PROXY: " + parts[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
transport = new SocksProxyTcpTransport({
|
||||||
|
host: parts[0],
|
||||||
|
port,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
transport = new TcpTransport();
|
||||||
|
}
|
||||||
|
|
||||||
const tg = new TelegramClient({
|
const tg = new TelegramClient({
|
||||||
apiId: env.API_ID,
|
apiId: env.API_ID,
|
||||||
apiHash: env.API_HASH,
|
apiHash: env.API_HASH,
|
||||||
storage: "bot-data/session",
|
storage: "bot-data/session",
|
||||||
|
transport,
|
||||||
});
|
});
|
||||||
|
|
||||||
const dp = Dispatcher.for(tg);
|
const dp = Dispatcher.for(tg);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue