fix(route): fix bug where X (Twitter) router cannot go through proxy configuration (#16298)

fix #16193
This commit is contained in:
CaoMeiYouRen 2024-07-30 09:51:05 +08:00 committed by GitHub
parent 557f4ed661
commit 646d223835
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 5 deletions

View File

@ -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`);

View File

@ -28,7 +28,7 @@ export const route: Route = {
supportScihub: false,
},
name: 'Home timeline',
maintainers: ['DIYgod'],
maintainers: ['DIYgod', 'CaoMeiYouRen'],
handler,
radar: [
{

View File

@ -39,7 +39,7 @@ export const route: Route = {
supportScihub: false,
},
name: 'User timeline',
maintainers: ['DIYgod', 'yindaheng98', 'Rongronggg9'],
maintainers: ['DIYgod', 'yindaheng98', 'Rongronggg9', 'CaoMeiYouRen'],
handler,
radar: [
{