fix(route/twitter): Prevent storing empty cookie if obtaining such from `auth_token` failed. (#17232)

This commit is contained in:
Andvari 2024-10-31 23:56:54 +08:00 committed by GitHub
parent a8b11c68e1
commit 2eef201d16
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 32 additions and 28 deletions

View File

@ -13,36 +13,40 @@ import login from './login';
let authTokenIndex = 0;
const token2Cookie = (token) =>
cache.tryGet(`twitter:cookie:${token}`, async () => {
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 } });
if (token) {
await ofetch('https://x.com', {
dispatcher: agent,
});
} else {
const data = await ofetch('https://x.com/narendramodi?mx=2', {
dispatcher: agent,
});
const gt = data.match(/document\.cookie="gt=(\d+)/)?.[1];
if (gt) {
jar.setCookieSync(`gt=${gt}`, 'https://x.com');
}
const token2Cookie = async (token) => {
if (cache.get(`twitter:cookie:${token}`)) {
return cache.get(`twitter:cookie:${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 } });
if (token) {
await ofetch('https://x.com', {
dispatcher: agent,
});
} else {
const data = await ofetch('https://x.com/narendramodi?mx=2', {
dispatcher: agent,
});
const gt = data.match(/document\.cookie="gt=(\d+)/)?.[1];
if (gt) {
jar.setCookieSync(`gt=${gt}`, 'https://x.com');
}
return JSON.stringify(jar.serializeSync());
} catch {
// ignore
return '';
}
});
const cookie = JSON.stringify(jar.serializeSync());
cache.set(`twitter:cookie:${token}`, cookie);
return cookie;
} catch {
// ignore
return '';
}
};
const lockPrefix = 'twitter:lock-token1:';