diff --git a/lib/routes/twitter/api/web-api/utils.ts b/lib/routes/twitter/api/web-api/utils.ts index 3b0cc5a630a9a4..6f38cf09524f9a 100644 --- a/lib/routes/twitter/api/web-api/utils.ts +++ b/lib/routes/twitter/api/web-api/utils.ts @@ -4,11 +4,13 @@ import { config } from '@/config'; import got from '@/utils/got'; import queryString from 'query-string'; import { Cookie, CookieJar } from 'tough-cookie'; -import { CookieAgent } from 'http-cookie-agent/undici'; +import { CookieAgent, CookieClient } from 'http-cookie-agent/undici'; +import { ProxyAgent } from 'undici'; import cache from '@/utils/cache'; import logger from '@/utils/logger'; import { RateLimiterMemory, RateLimiterRedis, RateLimiterQueue } from 'rate-limiter-flexible'; import ofetch from '@/utils/ofetch'; +import proxy from '@/utils/proxy'; const dispatchers = {}; let authTokenIndex = 0; @@ -33,8 +35,14 @@ const token2Cookie = (token) => const jar = new CookieJar(); jar.setCookieSync(`auth_token=${token}`, 'https://x.com'); try { + const agent = proxy.proxyUri + ? new ProxyAgent({ + factory: (origin, opts) => new CookieClient(origin as string, { ...opts, cookies: { jar } }), + uri: proxy.proxyUri, + }) + : new CookieAgent({ cookies: { jar } }); await got('https://x.com', { - dispatcher: new CookieAgent({ cookies: { jar } }), + dispatcher: agent, }); return JSON.stringify(jar.serializeSync()); } catch { @@ -56,9 +64,15 @@ export const twitterGot = async (url, params) => { cookie = JSON.parse(cookie); } const jar = CookieJar.deserializeSync(cookie as any); + const agent = proxy.proxyUri + ? new ProxyAgent({ + factory: (origin, opts) => new CookieClient(origin as string, { ...opts, cookies: { jar } }), + uri: proxy.proxyUri, + }) + : new CookieAgent({ cookies: { jar } }); dispatchers[token] = { jar, - agent: new CookieAgent({ cookies: { jar } }), + agent, }; } else { throw new ConfigNotFoundError(`Twitter cookie for token ${token} is not valid`); diff --git a/lib/routes/twitter/home.ts b/lib/routes/twitter/home.ts index 523bb709df04c5..072edab95db0e3 100644 --- a/lib/routes/twitter/home.ts +++ b/lib/routes/twitter/home.ts @@ -28,7 +28,7 @@ export const route: Route = { supportScihub: false, }, name: 'Home timeline', - maintainers: ['DIYgod'], + maintainers: ['DIYgod', 'CaoMeiYouRen'], handler, radar: [ { diff --git a/lib/routes/twitter/user.ts b/lib/routes/twitter/user.ts index 025de9a1b469af..e6652666f0ed7c 100644 --- a/lib/routes/twitter/user.ts +++ b/lib/routes/twitter/user.ts @@ -39,7 +39,7 @@ export const route: Route = { supportScihub: false, }, name: 'User timeline', - maintainers: ['DIYgod', 'yindaheng98', 'Rongronggg9'], + maintainers: ['DIYgod', 'yindaheng98', 'Rongronggg9', 'CaoMeiYouRen'], handler, radar: [ {