Skip to content

Commit

Permalink
fix(route): fix bug where X (Twitter) router cannot go through proxy …
Browse files Browse the repository at this point in the history
…configuration (#16298)

fix #16193
  • Loading branch information
CaoMeiYouRen authored Jul 30, 2024
1 parent 557f4ed commit 646d223
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
20 changes: 17 additions & 3 deletions lib/routes/twitter/api/web-api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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`);
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/twitter/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const route: Route = {
supportScihub: false,
},
name: 'Home timeline',
maintainers: ['DIYgod'],
maintainers: ['DIYgod', 'CaoMeiYouRen'],
handler,
radar: [
{
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/twitter/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const route: Route = {
supportScihub: false,
},
name: 'User timeline',
maintainers: ['DIYgod', 'yindaheng98', 'Rongronggg9'],
maintainers: ['DIYgod', 'yindaheng98', 'Rongronggg9', 'CaoMeiYouRen'],
handler,
radar: [
{
Expand Down

0 comments on commit 646d223

Please sign in to comment.