From 1ee9ebb8cdd572adddf2fdc2910f0c7579b06ee7 Mon Sep 17 00:00:00 2001 From: soffee Date: Tue, 10 Feb 2026 20:43:45 +0300 Subject: [PATCH] add socks proxy support --- package.json | 2 +- src/env.ts | 4 +++- src/main.ts | 24 +++++++++++++++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index a1a34a8..2452a50 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "mtproto_exporter", "type": "module", - "version": "1.5.2", + "version": "1.5.3", "packageManager": "pnpm@10.6.5", "license": "MIT", "scripts": { diff --git a/src/env.ts b/src/env.ts index 36a7e38..5cc1bcb 100644 --- a/src/env.ts +++ b/src/env.ts @@ -7,8 +7,10 @@ const USERBOT_PHONE = process.env.USERBOT_PHONE; const USERBOT_2FACODE = process.env.USERBOT_2FACODE; const USERBOT_PASSWORD = process.env.USERBOT_PASSWORD; +const SOCKS_PROXY = process.env.SOCKS_PROXY; + if (Number.isNaN(API_ID) || !API_HASH) { 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 }; diff --git a/src/main.ts b/src/main.ts index f494e41..3c5fef5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,6 @@ import fs from "node:fs"; import { Dispatcher } from "@mtcute/dispatcher"; -import { TelegramClient } from "@mtcute/node"; +import { SocksProxyTcpTransport, TcpTransport, TelegramClient } from "@mtcute/node"; import { collectDefaultMetrics, Registry } from "prom-client"; import { config, readKeywords } from "./config.js"; @@ -16,10 +16,32 @@ collectDefaultMetrics({ register: registry }); const server = new MetricsServer(registry); 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({ apiId: env.API_ID, apiHash: env.API_HASH, storage: "bot-data/session", + transport, }); const dp = Dispatcher.for(tg);