From b4fece47ed5e20489b79014c2b60a9263069ee28 Mon Sep 17 00:00:00 2001 From: pseudoyu Date: Tue, 5 Nov 2024 12:58:48 +0700 Subject: [PATCH] feat(route/telegram): add mtproxy support for tglib --- lib/config.ts | 13 +++++++++++++ lib/routes/telegram/channel.ts | 30 +++++++++++++++++++++++++++++ lib/routes/telegram/tglib/client.ts | 18 ++++++++++------- 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/lib/config.ts b/lib/config.ts index 785e7d6ad8e209..1784e1f97f2fc8 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -296,6 +296,14 @@ export type Config = { telegram: { token?: string; session?: string; + apiId?: number; + apiHash?: string; + maxConcurrentDownloads?: number; + proxy?: { + host?: string; + port?: number; + secret?: string; + }; }; tophub: { cookie?: string; @@ -709,6 +717,11 @@ const calculateValue = () => { apiId: envs.TELEGRAM_API_ID, apiHash: envs.TELEGRAM_API_HASH, maxConcurrentDownloads: envs.TELEGRAM_MAX_CONCURRENT_DOWNLOADS, + proxy: { + host: envs.TELEGRAM_PROXY_HOST, + port: envs.TELEGRAM_PROXY_PORT, + secret: envs.TELEGRAM_PROXY_SECRET, + }, }, tophub: { cookie: envs.TOPHUB_COOKIE, diff --git a/lib/routes/telegram/channel.ts b/lib/routes/telegram/channel.ts index b3b19857d5e994..5b7caf6d5e97a7 100644 --- a/lib/routes/telegram/channel.ts +++ b/lib/routes/telegram/channel.ts @@ -98,6 +98,36 @@ For backward compatibility reasons, invalid \`routeParams\` will be treated as \ optional: true, description: 'Telegram API Authentication', }, + { + name: 'TELEGRAM_API_ID', + optional: true, + description: 'Telegram API ID', + }, + { + name: 'TELEGRAM_API_HASH', + optional: true, + description: 'Telegram API Hash', + }, + { + name: 'TELEGRAM_MAX_CONCURRENT_DOWNLOADS', + optional: true, + description: 'Telegram Max Concurrent Downloads', + }, + { + name: 'TELEGRAM_PROXY_HOST', + optional: true, + description: 'Telegram Proxy Host', + }, + { + name: 'TELEGRAM_PROXY_PORT', + optional: true, + description: 'Telegram Proxy Port', + }, + { + name: 'TELEGRAM_PROXY_SECRET', + optional: true, + description: 'Telegram Proxy Secret', + }, ], requirePuppeteer: false, antiCrawler: false, diff --git a/lib/routes/telegram/tglib/client.ts b/lib/routes/telegram/tglib/client.ts index 855c5f887d01db..06ed2538cae8b1 100644 --- a/lib/routes/telegram/tglib/client.ts +++ b/lib/routes/telegram/tglib/client.ts @@ -23,14 +23,18 @@ export async function getClient(authParams?: UserAuthParams, session?: string) { autoReconnect: true, retryDelay: 3000, maxConcurrentDownloads: Number(config.telegram.maxConcurrentDownloads ?? 10), + proxy: + config.telegram.proxy?.host && config.telegram.proxy.port && config.telegram.proxy.secret + ? { + ip: config.telegram.proxy.host, + port: Number(config.telegram.proxy.port), + MTProxy: true, + secret: config.telegram.proxy.secret, + } + : undefined, }); - await client.start( - Object.assign(authParams ?? {}, { - onError: (err) => { - throw new Error('Cannot start TG: ' + err); - }, - }) as any - ); + + await client.connect(); return client; }