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",
|
||||
"type": "module",
|
||||
"version": "1.5.2",
|
||||
"version": "1.5.3",
|
||||
"packageManager": "pnpm@10.6.5",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
|
|
|
|||
|
|
@ -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 };
|
||||
|
|
|
|||
24
src/main.ts
24
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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue