@the-convocation/twitter-scraper 0.21.1 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -0
- package/dist/cycletls/cjs/index.cjs +48 -3
- package/dist/cycletls/cjs/index.cjs.map +1 -1
- package/dist/cycletls/esm/index.mjs +48 -3
- package/dist/cycletls/esm/index.mjs.map +1 -1
- package/dist/default/cjs/index.js +1320 -154
- package/dist/default/cjs/index.js.map +1 -1
- package/dist/default/esm/index.mjs +1320 -155
- package/dist/default/esm/index.mjs.map +1 -1
- package/dist/node/cjs/index.cjs +1320 -154
- package/dist/node/cjs/index.cjs.map +1 -1
- package/dist/node/esm/index.mjs +1320 -155
- package/dist/node/esm/index.mjs.map +1 -1
- package/dist/types/index.d.ts +70 -1
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -212,6 +212,64 @@ cycleTLSExit();
|
|
|
212
212
|
|
|
213
213
|
See the [cycletls example](./examples/cycletls/) for a complete working example.
|
|
214
214
|
|
|
215
|
+
### Cookie-based authentication
|
|
216
|
+
|
|
217
|
+
If you're encountering `error 399` ("Incorrect. Please try again") or Twitter's suspicious activity detection during login, you can use cookies exported from an already-authenticated browser session instead. This approach:
|
|
218
|
+
|
|
219
|
+
- Avoids Twitter's anti-bot protection that blocks automated logins
|
|
220
|
+
- No need to store or handle passwords in code
|
|
221
|
+
- Uses your established browser session
|
|
222
|
+
- Bypasses rate limiting on authentication endpoints
|
|
223
|
+
|
|
224
|
+
**Step 1: Export cookies from your browser**
|
|
225
|
+
|
|
226
|
+
Using Chrome/Edge:
|
|
227
|
+
|
|
228
|
+
1. Log in to X.com in your browser
|
|
229
|
+
2. Open DevTools (F12) → Application tab → Cookies
|
|
230
|
+
3. Click the URL bar that says "Filter cookies" and press Ctrl+A to select all cookies
|
|
231
|
+
4. Copy all cookies (they'll be in format: `name1=value1; name2=value2; ...`)
|
|
232
|
+
|
|
233
|
+
Using Firefox:
|
|
234
|
+
|
|
235
|
+
1. Log in to X.com in your browser
|
|
236
|
+
2. Open DevTools (F12) → Storage tab → Cookies → `https://x.com`
|
|
237
|
+
3. Find the `ct0` cookie and copy its value
|
|
238
|
+
4. Find the `auth_token` cookie and copy its value
|
|
239
|
+
5. Construct the cookie string: `ct0=<value>; auth_token=<value>`
|
|
240
|
+
|
|
241
|
+
> **Tip:** You can use the [Cookie-Editor](https://addons.mozilla.org/en-US/firefox/addon/cookie-editor/) extension to export cookies in a convenient format.
|
|
242
|
+
|
|
243
|
+
**Step 2: Use cookies in your code**
|
|
244
|
+
|
|
245
|
+
```ts
|
|
246
|
+
import { Cookie } from 'tough-cookie';
|
|
247
|
+
import { Scraper } from '@the-convocation/twitter-scraper';
|
|
248
|
+
|
|
249
|
+
// Your cookie string from browser (name=value; name2=value2; ...)
|
|
250
|
+
const cookieString = 'ct0=abc123; auth_token=xyz789; lang=en; ...';
|
|
251
|
+
|
|
252
|
+
// Parse the cookie string
|
|
253
|
+
const cookies = cookieString
|
|
254
|
+
.split(';')
|
|
255
|
+
.map((c) => Cookie.parse(c))
|
|
256
|
+
.filter(Boolean);
|
|
257
|
+
|
|
258
|
+
// Create scraper and set cookies
|
|
259
|
+
const scraper = new Scraper();
|
|
260
|
+
await scraper.setCookies(cookies);
|
|
261
|
+
|
|
262
|
+
// Verify authentication works
|
|
263
|
+
const isLoggedIn = await scraper.isLoggedIn();
|
|
264
|
+
if (isLoggedIn) {
|
|
265
|
+
console.log('✓ Successfully authenticated with cookies!');
|
|
266
|
+
// Now you can use authenticated features
|
|
267
|
+
const profile = await scraper.getProfile('username');
|
|
268
|
+
}
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
Cookies expire over time. If authentication fails, you may need to export fresh cookies from your browser.
|
|
272
|
+
|
|
215
273
|
### Rate limiting
|
|
216
274
|
|
|
217
275
|
The Twitter API heavily rate-limits clients, requiring that the scraper has its own
|
|
@@ -4,6 +4,47 @@ var initCycleTLS = require('cycletls');
|
|
|
4
4
|
var headersPolyfill = require('headers-polyfill');
|
|
5
5
|
var debug = require('debug');
|
|
6
6
|
|
|
7
|
+
const CHROME_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36";
|
|
8
|
+
const CHROME_JA3 = "771,4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,0-10-35-16-11-51-27-65037-43-45-18-23-5-65281-13-17613,4588-29-23-24,0";
|
|
9
|
+
const CHROME_JA4R = "t13d1516h2_002f,0035,009c,009d,1301,1302,1303,c013,c014,c02b,c02c,c02f,c030,cca8,cca9_0005,000a,000b,000d,0012,0017,001b,0023,002b,002d,0033,44cd,fe0d,ff01_0403,0804,0401,0503,0805,0501,0806,0601";
|
|
10
|
+
const CHROME_HTTP2_FINGERPRINT = "1:65536;2:0;4:6291456;6:262144|15663105|0|m,a,s,p";
|
|
11
|
+
const CHROME_HEADER_ORDER = [
|
|
12
|
+
// HTTP/2 pseudo-headers (Chrome 144 order: method, authority, scheme, path)
|
|
13
|
+
":method",
|
|
14
|
+
":authority",
|
|
15
|
+
":scheme",
|
|
16
|
+
":path",
|
|
17
|
+
// Chrome Client Hints (mandatory for modern detection bypass)
|
|
18
|
+
"sec-ch-ua",
|
|
19
|
+
"sec-ch-ua-mobile",
|
|
20
|
+
"sec-ch-ua-platform",
|
|
21
|
+
// Standard browser headers
|
|
22
|
+
"upgrade-insecure-requests",
|
|
23
|
+
"user-agent",
|
|
24
|
+
"accept",
|
|
25
|
+
"origin",
|
|
26
|
+
"sec-fetch-site",
|
|
27
|
+
"sec-fetch-mode",
|
|
28
|
+
"sec-fetch-user",
|
|
29
|
+
"sec-fetch-dest",
|
|
30
|
+
"referer",
|
|
31
|
+
"accept-encoding",
|
|
32
|
+
"accept-language",
|
|
33
|
+
"priority",
|
|
34
|
+
// Authentication headers
|
|
35
|
+
"authorization",
|
|
36
|
+
"x-csrf-token",
|
|
37
|
+
"x-guest-token",
|
|
38
|
+
"x-twitter-auth-type",
|
|
39
|
+
"x-twitter-active-user",
|
|
40
|
+
"x-twitter-client-language",
|
|
41
|
+
"x-client-transaction-id",
|
|
42
|
+
"x-xp-forwarded-for",
|
|
43
|
+
// POST-specific
|
|
44
|
+
"content-type",
|
|
45
|
+
"cookie"
|
|
46
|
+
];
|
|
47
|
+
|
|
7
48
|
const log = debug("twitter-scraper:cycletls");
|
|
8
49
|
let cycleTLSInstance = null;
|
|
9
50
|
async function initCycleTLSFetch() {
|
|
@@ -53,9 +94,13 @@ async function cycleTLSFetch(input, init) {
|
|
|
53
94
|
const options = {
|
|
54
95
|
body,
|
|
55
96
|
headers,
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
97
|
+
ja3: CHROME_JA3,
|
|
98
|
+
ja4r: CHROME_JA4R,
|
|
99
|
+
http2Fingerprint: CHROME_HTTP2_FINGERPRINT,
|
|
100
|
+
headerOrder: CHROME_HEADER_ORDER,
|
|
101
|
+
orderAsProvided: true,
|
|
102
|
+
disableGrease: false,
|
|
103
|
+
userAgent: headers["user-agent"] || CHROME_USER_AGENT
|
|
59
104
|
};
|
|
60
105
|
try {
|
|
61
106
|
const response = await instance(
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../../src/cycletls-fetch.ts"],"sourcesContent":["import initCycleTLS from 'cycletls';\nimport { Headers } from 'headers-polyfill';\nimport debug from 'debug';\n\nconst log = debug('twitter-scraper:cycletls');\n\nlet cycleTLSInstance: Awaited<ReturnType<typeof initCycleTLS>> | null = null;\n\n/**\n * Initialize the CycleTLS instance. This should be called once before using the fetch wrapper.\n */\nexport async function initCycleTLSFetch() {\n if (!cycleTLSInstance) {\n log('Initializing CycleTLS...');\n cycleTLSInstance = await initCycleTLS();\n log('CycleTLS initialized successfully');\n }\n return cycleTLSInstance;\n}\n\n/**\n * Cleanup the CycleTLS instance. Call this when you're done making requests.\n */\nexport function cycleTLSExit() {\n if (cycleTLSInstance) {\n log('Exiting CycleTLS...');\n cycleTLSInstance.exit();\n cycleTLSInstance = null;\n }\n}\n\n/**\n * A fetch-compatible wrapper around CycleTLS that mimics Chrome's TLS fingerprint\n * to bypass Cloudflare and other bot detection systems.\n */\nexport async function cycleTLSFetch(\n input: RequestInfo | URL,\n init?: RequestInit,\n): Promise<Response> {\n const instance = await initCycleTLSFetch();\n\n const url =\n typeof input === 'string'\n ? input\n : input instanceof URL\n ? input.toString()\n : input.url;\n const method = (init?.method || 'GET').toUpperCase();\n\n log(`Making ${method} request to ${url}`);\n\n // Extract headers from RequestInit\n const headers: Record<string, string> = {};\n if (init?.headers) {\n if (init.headers instanceof Headers) {\n init.headers.forEach((value, key) => {\n headers[key] = value;\n });\n } else if (Array.isArray(init.headers)) {\n init.headers.forEach(([key, value]) => {\n headers[key] = value;\n });\n } else {\n Object.assign(headers, init.headers);\n }\n }\n\n // Convert body to string if needed\n let body: string | undefined;\n if (init?.body) {\n if (typeof init.body === 'string') {\n body = init.body;\n } else if (init.body instanceof URLSearchParams) {\n body = init.body.toString();\n } else {\n body = init.body.toString();\n }\n }\n\n // Use Chrome 120 JA3 fingerprint for maximum compatibility\n const options = {\n body,\n headers,\n // Chrome 120 on Windows 10\n ja3: '771,4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,0-23-65281-10-11-35-16-5-13-18-51-45-43-27-17513,29-23-24,0',\n userAgent:\n headers['user-agent'] ||\n 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',\n };\n\n try {\n const response = await instance(\n url,\n options,\n method.toLowerCase() as\n | 'get'\n | 'post'\n | 'put'\n | 'delete'\n | 'patch'\n | 'head'\n | 'options',\n );\n\n // Convert CycleTLS response to fetch Response\n // CycleTLS returns headers as an object\n const responseHeaders = new Headers();\n if (response.headers) {\n Object.entries(response.headers).forEach(([key, value]) => {\n if (Array.isArray(value)) {\n value.forEach((v) => {\n responseHeaders.append(key, v);\n });\n } else if (typeof value === 'string') {\n responseHeaders.set(key, value);\n }\n });\n }\n\n // Get response body - cycletls provides helper methods, but we need the raw text\n // The response object has a text() method that returns the body as text\n let responseBody = '';\n if (typeof response.text === 'function') {\n responseBody = await response.text();\n } else if ((response as any).body) {\n responseBody = (response as any).body;\n }\n\n // Create a proper Response object using standard Response constructor\n const fetchResponse = new Response(responseBody, {\n status: response.status,\n statusText: '', // CycleTLS doesn't provide status text\n headers: responseHeaders,\n });\n\n return fetchResponse;\n } catch (error) {\n log(`CycleTLS request failed: ${error}`);\n throw error;\n }\n}\n"],"names":["Headers"],"mappings":";;;;;;AAIA,MAAM,GAAA,GAAM,MAAM,0BAA0B,CAAA,CAAA;AAE5C,IAAI,gBAAoE,GAAA,IAAA,CAAA;AAKxE,eAAsB,iBAAoB,GAAA;AACxC,EAAA,IAAI,CAAC,gBAAkB,EAAA;AACrB,IAAA,GAAA,CAAI,0BAA0B,CAAA,CAAA;AAC9B,IAAA,gBAAA,GAAmB,MAAM,YAAa,EAAA,CAAA;AACtC,IAAA,GAAA,CAAI,mCAAmC,CAAA,CAAA;AAAA,GACzC;AACA,EAAO,OAAA,gBAAA,CAAA;AACT,CAAA;AAKO,SAAS,YAAe,GAAA;AAC7B,EAAA,IAAI,gBAAkB,EAAA;AACpB,IAAA,GAAA,CAAI,qBAAqB,CAAA,CAAA;AACzB,IAAA,gBAAA,CAAiB,IAAK,EAAA,CAAA;AACtB,IAAmB,gBAAA,GAAA,IAAA,CAAA;AAAA,GACrB;AACF,CAAA;AAMsB,eAAA,aAAA,CACpB,OACA,IACmB,EAAA;AACnB,EAAM,MAAA,QAAA,GAAW,MAAM,iBAAkB,EAAA,CAAA;AAEzC,EAAM,MAAA,GAAA,GACJ,OAAO,KAAA,KAAU,QACb,GAAA,KAAA,GACA,iBAAiB,GACjB,GAAA,KAAA,CAAM,QAAS,EAAA,GACf,KAAM,CAAA,GAAA,CAAA;AACZ,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CAAA,MAAA;AAAA,MACjB,UAAY,EAAA,EAAA;AAAA;AAAA,MACZ,OAAS,EAAA,eAAA;AAAA,KACV,CAAA,CAAA;AAED,IAAO,OAAA,aAAA,CAAA;AAAA,WACA,KAAO,EAAA;AACd,IAAI,GAAA,CAAA,CAAA,yBAAA,EAA4B,KAAK,CAAE,CAAA,CAAA,CAAA;AACvC,IAAM,MAAA,KAAA,CAAA;AAAA,GACR;AACF;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../../src/chrome-fingerprint.ts","../../../src/cycletls-fetch.ts"],"sourcesContent":["/**\n * Chrome version-dependent fingerprint constants.\n *\n * IMPORTANT: All constants in this file are tied to a specific Chrome version\n * (currently Chrome 144 on Windows 10). When bumping the Chrome version:\n * 1. Update CHROME_USER_AGENT with the new version string\n * 2. Update CHROME_SEC_CH_UA with the matching Client Hints\n * 3. Update CHROME_JA3 fingerprint (capture via tls.peet.ws)\n * 4. Update CHROME_JA4R fingerprint\n * 5. Update CHROME_HTTP2_FINGERPRINT settings frame\n * 6. Review CHROME_HEADER_ORDER if Chrome changes header ordering\n * 7. Update castle.ts DEFAULT_PROFILE if Chrome version affects fingerprint fields\n *\n * All values must be consistent with each other and match a real Chrome release.\n */\n\n/**\n * User-Agent string matching Chrome 144 on Windows 10.\n * Must be consistent across all requests and match the TLS fingerprint.\n */\nexport const CHROME_USER_AGENT =\n 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36';\n\n/**\n * Chrome Client Hints header matching the Chrome 144 user-agent.\n */\nexport const CHROME_SEC_CH_UA =\n '\"Not(A:Brand\";v=\"8\", \"Chromium\";v=\"144\", \"Google Chrome\";v=\"144\"';\n\n/**\n * JA3 TLS fingerprint for Chrome 144.\n * Captured from a real Chrome 144 browser session via tls.peet.ws.\n */\nexport const CHROME_JA3 =\n '771,4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,0-10-35-16-11-51-27-65037-43-45-18-23-5-65281-13-17613,4588-29-23-24,0';\n\n/**\n * JA4r fingerprint for Chrome 144.\n * Format: t13d1516h2_CIPHERS_EXTENSIONS_SIG_ALGS\n */\nexport const CHROME_JA4R =\n 't13d1516h2_002f,0035,009c,009d,1301,1302,1303,c013,c014,c02b,c02c,c02f,c030,cca8,cca9_0005,000a,000b,000d,0012,0017,001b,0023,002b,002d,0033,44cd,fe0d,ff01_0403,0804,0401,0503,0805,0501,0806,0601';\n\n/**\n * Chrome 144 HTTP/2 fingerprint - mimics exact HTTP/2 SETTINGS frame.\n * Format: SETTINGS|window_size|unknown|priority_order\n */\nexport const CHROME_HTTP2_FINGERPRINT =\n '1:65536;2:0;4:6291456;6:262144|15663105|0|m,a,s,p';\n\n/**\n * Exact header order that Chrome 144 uses.\n * Header ordering is critical for HTTP/2 fingerprint evasion — servers can detect\n * non-browser clients by checking if headers arrive in a non-standard order.\n */\nexport const CHROME_HEADER_ORDER = [\n // HTTP/2 pseudo-headers (Chrome 144 order: method, authority, scheme, path)\n ':method',\n ':authority',\n ':scheme',\n ':path',\n // Chrome Client Hints (mandatory for modern detection bypass)\n 'sec-ch-ua',\n 'sec-ch-ua-mobile',\n 'sec-ch-ua-platform',\n // Standard browser headers\n 'upgrade-insecure-requests',\n 'user-agent',\n 'accept',\n 'origin',\n 'sec-fetch-site',\n 'sec-fetch-mode',\n 'sec-fetch-user',\n 'sec-fetch-dest',\n 'referer',\n 'accept-encoding',\n 'accept-language',\n 'priority',\n // Authentication headers\n 'authorization',\n 'x-csrf-token',\n 'x-guest-token',\n 'x-twitter-auth-type',\n 'x-twitter-active-user',\n 'x-twitter-client-language',\n 'x-client-transaction-id',\n 'x-xp-forwarded-for',\n // POST-specific\n 'content-type',\n 'cookie',\n];\n","import initCycleTLS from 'cycletls';\nimport { Headers } from 'headers-polyfill';\nimport debug from 'debug';\nimport {\n CHROME_USER_AGENT,\n CHROME_JA3,\n CHROME_JA4R,\n CHROME_HTTP2_FINGERPRINT,\n CHROME_HEADER_ORDER,\n} from './chrome-fingerprint';\n\nconst log = debug('twitter-scraper:cycletls');\n\nlet cycleTLSInstance: Awaited<ReturnType<typeof initCycleTLS>> | null = null;\n\n/**\n * Initialize the CycleTLS instance. This should be called once before using the fetch wrapper.\n */\nexport async function initCycleTLSFetch() {\n if (!cycleTLSInstance) {\n log('Initializing CycleTLS...');\n cycleTLSInstance = await initCycleTLS();\n log('CycleTLS initialized successfully');\n }\n return cycleTLSInstance;\n}\n\n/**\n * Cleanup the CycleTLS instance. Call this when you're done making requests.\n */\nexport function cycleTLSExit() {\n if (cycleTLSInstance) {\n log('Exiting CycleTLS...');\n cycleTLSInstance.exit();\n cycleTLSInstance = null;\n }\n}\n\n/**\n * A fetch-compatible wrapper around CycleTLS that mimics Chrome's TLS fingerprint\n * to bypass Cloudflare and other bot detection systems.\n */\nexport async function cycleTLSFetch(\n input: RequestInfo | URL,\n init?: RequestInit,\n): Promise<Response> {\n const instance = await initCycleTLSFetch();\n\n const url =\n typeof input === 'string'\n ? input\n : input instanceof URL\n ? input.toString()\n : input.url;\n const method = (init?.method || 'GET').toUpperCase();\n\n log(`Making ${method} request to ${url}`);\n\n // Extract headers from RequestInit\n const headers: Record<string, string> = {};\n if (init?.headers) {\n if (init.headers instanceof Headers) {\n init.headers.forEach((value, key) => {\n headers[key] = value;\n });\n } else if (Array.isArray(init.headers)) {\n init.headers.forEach(([key, value]) => {\n headers[key] = value;\n });\n } else {\n Object.assign(headers, init.headers);\n }\n }\n\n // Convert body to string if needed\n let body: string | undefined;\n if (init?.body) {\n if (typeof init.body === 'string') {\n body = init.body;\n } else if (init.body instanceof URLSearchParams) {\n body = init.body.toString();\n } else {\n body = init.body.toString();\n }\n }\n\n // All Chrome fingerprint constants imported from chrome-fingerprint.ts\n const options = {\n body,\n headers,\n ja3: CHROME_JA3,\n ja4r: CHROME_JA4R,\n http2Fingerprint: CHROME_HTTP2_FINGERPRINT,\n headerOrder: CHROME_HEADER_ORDER,\n orderAsProvided: true,\n disableGrease: false,\n userAgent: headers['user-agent'] || CHROME_USER_AGENT,\n };\n\n try {\n const response = await instance(\n url,\n options,\n method.toLowerCase() as\n | 'get'\n | 'post'\n | 'put'\n | 'delete'\n | 'patch'\n | 'head'\n | 'options',\n );\n\n // Convert CycleTLS response to fetch Response\n // CycleTLS returns headers as an object\n const responseHeaders = new Headers();\n if (response.headers) {\n Object.entries(response.headers).forEach(([key, value]) => {\n if (Array.isArray(value)) {\n value.forEach((v) => {\n responseHeaders.append(key, v);\n });\n } else if (typeof value === 'string') {\n responseHeaders.set(key, value);\n }\n });\n }\n\n // Get response body - cycletls provides helper methods, but we need the raw text\n // The response object has a text() method that returns the body as text\n let responseBody = '';\n if (typeof response.text === 'function') {\n responseBody = await response.text();\n } else if ((response as any).body) {\n responseBody = (response as any).body;\n }\n\n // Create a proper Response object using standard Response constructor\n const fetchResponse = new Response(responseBody, {\n status: response.status,\n statusText: '', // CycleTLS doesn't provide status text\n headers: responseHeaders,\n });\n\n return fetchResponse;\n } catch (error) {\n log(`CycleTLS request failed: ${error}`);\n throw error;\n }\n}\n"],"names":["Headers"],"mappings":";;;;;;AAoBO,MAAM,iBACX,GAAA,iHAAA,CAAA;AAYK,MAAM,UACX,GAAA,yJAAA,CAAA;AAMK,MAAM,WACX,GAAA,qMAAA,CAAA;AAMK,MAAM,wBACX,GAAA,mDAAA,CAAA;AAOK,MAAM,mBAAsB,GAAA;AAAA;AAAA,EAEjC,SAAA;AAAA,EACA,YAAA;AAAA,EACA,SAAA;AAAA,EACA,OAAA;AAAA;AAAA,EAEA,WAAA;AAAA,EACA,kBAAA;AAAA,EACA,oBAAA;AAAA;AAAA,EAEA,2BAAA;AAAA,EACA,YAAA;AAAA,EACA,QAAA;AAAA,EACA,QAAA;AAAA,EACA,gBAAA;AAAA,EACA,gBAAA;AAAA,EACA,gBAAA;AAAA,EACA,gBAAA;AAAA,EACA,SAAA;AAAA,EACA,iBAAA;AAAA,EACA,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A,MACZ,OAAS,EAAA,eAAA;AAAA,KACV,CAAA,CAAA;AAED,IAAO,OAAA,aAAA,CAAA;AAAA,WACA,KAAO,EAAA;AACd,IAAI,GAAA,CAAA,CAAA,yBAAA,EAA4B,KAAK,CAAE,CAAA,CAAA,CAAA;AACvC,IAAM,MAAA,KAAA,CAAA;AAAA,GACR;AACF;;;;;"}
|
|
@@ -2,6 +2,47 @@ import initCycleTLS from 'cycletls';
|
|
|
2
2
|
import { Headers } from 'headers-polyfill';
|
|
3
3
|
import debug from 'debug';
|
|
4
4
|
|
|
5
|
+
const CHROME_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36";
|
|
6
|
+
const CHROME_JA3 = "771,4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,0-10-35-16-11-51-27-65037-43-45-18-23-5-65281-13-17613,4588-29-23-24,0";
|
|
7
|
+
const CHROME_JA4R = "t13d1516h2_002f,0035,009c,009d,1301,1302,1303,c013,c014,c02b,c02c,c02f,c030,cca8,cca9_0005,000a,000b,000d,0012,0017,001b,0023,002b,002d,0033,44cd,fe0d,ff01_0403,0804,0401,0503,0805,0501,0806,0601";
|
|
8
|
+
const CHROME_HTTP2_FINGERPRINT = "1:65536;2:0;4:6291456;6:262144|15663105|0|m,a,s,p";
|
|
9
|
+
const CHROME_HEADER_ORDER = [
|
|
10
|
+
// HTTP/2 pseudo-headers (Chrome 144 order: method, authority, scheme, path)
|
|
11
|
+
":method",
|
|
12
|
+
":authority",
|
|
13
|
+
":scheme",
|
|
14
|
+
":path",
|
|
15
|
+
// Chrome Client Hints (mandatory for modern detection bypass)
|
|
16
|
+
"sec-ch-ua",
|
|
17
|
+
"sec-ch-ua-mobile",
|
|
18
|
+
"sec-ch-ua-platform",
|
|
19
|
+
// Standard browser headers
|
|
20
|
+
"upgrade-insecure-requests",
|
|
21
|
+
"user-agent",
|
|
22
|
+
"accept",
|
|
23
|
+
"origin",
|
|
24
|
+
"sec-fetch-site",
|
|
25
|
+
"sec-fetch-mode",
|
|
26
|
+
"sec-fetch-user",
|
|
27
|
+
"sec-fetch-dest",
|
|
28
|
+
"referer",
|
|
29
|
+
"accept-encoding",
|
|
30
|
+
"accept-language",
|
|
31
|
+
"priority",
|
|
32
|
+
// Authentication headers
|
|
33
|
+
"authorization",
|
|
34
|
+
"x-csrf-token",
|
|
35
|
+
"x-guest-token",
|
|
36
|
+
"x-twitter-auth-type",
|
|
37
|
+
"x-twitter-active-user",
|
|
38
|
+
"x-twitter-client-language",
|
|
39
|
+
"x-client-transaction-id",
|
|
40
|
+
"x-xp-forwarded-for",
|
|
41
|
+
// POST-specific
|
|
42
|
+
"content-type",
|
|
43
|
+
"cookie"
|
|
44
|
+
];
|
|
45
|
+
|
|
5
46
|
const log = debug("twitter-scraper:cycletls");
|
|
6
47
|
let cycleTLSInstance = null;
|
|
7
48
|
async function initCycleTLSFetch() {
|
|
@@ -51,9 +92,13 @@ async function cycleTLSFetch(input, init) {
|
|
|
51
92
|
const options = {
|
|
52
93
|
body,
|
|
53
94
|
headers,
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
95
|
+
ja3: CHROME_JA3,
|
|
96
|
+
ja4r: CHROME_JA4R,
|
|
97
|
+
http2Fingerprint: CHROME_HTTP2_FINGERPRINT,
|
|
98
|
+
headerOrder: CHROME_HEADER_ORDER,
|
|
99
|
+
orderAsProvided: true,
|
|
100
|
+
disableGrease: false,
|
|
101
|
+
userAgent: headers["user-agent"] || CHROME_USER_AGENT
|
|
57
102
|
};
|
|
58
103
|
try {
|
|
59
104
|
const response = await instance(
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../../../src/cycletls-fetch.ts"],"sourcesContent":["import initCycleTLS from 'cycletls';\nimport { Headers } from 'headers-polyfill';\nimport debug from 'debug';\n\nconst log = debug('twitter-scraper:cycletls');\n\nlet cycleTLSInstance: Awaited<ReturnType<typeof initCycleTLS>> | null = null;\n\n/**\n * Initialize the CycleTLS instance. This should be called once before using the fetch wrapper.\n */\nexport async function initCycleTLSFetch() {\n if (!cycleTLSInstance) {\n log('Initializing CycleTLS...');\n cycleTLSInstance = await initCycleTLS();\n log('CycleTLS initialized successfully');\n }\n return cycleTLSInstance;\n}\n\n/**\n * Cleanup the CycleTLS instance. Call this when you're done making requests.\n */\nexport function cycleTLSExit() {\n if (cycleTLSInstance) {\n log('Exiting CycleTLS...');\n cycleTLSInstance.exit();\n cycleTLSInstance = null;\n }\n}\n\n/**\n * A fetch-compatible wrapper around CycleTLS that mimics Chrome's TLS fingerprint\n * to bypass Cloudflare and other bot detection systems.\n */\nexport async function cycleTLSFetch(\n input: RequestInfo | URL,\n init?: RequestInit,\n): Promise<Response> {\n const instance = await initCycleTLSFetch();\n\n const url =\n typeof input === 'string'\n ? input\n : input instanceof URL\n ? input.toString()\n : input.url;\n const method = (init?.method || 'GET').toUpperCase();\n\n log(`Making ${method} request to ${url}`);\n\n // Extract headers from RequestInit\n const headers: Record<string, string> = {};\n if (init?.headers) {\n if (init.headers instanceof Headers) {\n init.headers.forEach((value, key) => {\n headers[key] = value;\n });\n } else if (Array.isArray(init.headers)) {\n init.headers.forEach(([key, value]) => {\n headers[key] = value;\n });\n } else {\n Object.assign(headers, init.headers);\n }\n }\n\n // Convert body to string if needed\n let body: string | undefined;\n if (init?.body) {\n if (typeof init.body === 'string') {\n body = init.body;\n } else if (init.body instanceof URLSearchParams) {\n body = init.body.toString();\n } else {\n body = init.body.toString();\n }\n }\n\n // Use Chrome 120 JA3 fingerprint for maximum compatibility\n const options = {\n body,\n headers,\n // Chrome 120 on Windows 10\n ja3: '771,4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,0-23-65281-10-11-35-16-5-13-18-51-45-43-27-17513,29-23-24,0',\n userAgent:\n headers['user-agent'] ||\n 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',\n };\n\n try {\n const response = await instance(\n url,\n options,\n method.toLowerCase() as\n | 'get'\n | 'post'\n | 'put'\n | 'delete'\n | 'patch'\n | 'head'\n | 'options',\n );\n\n // Convert CycleTLS response to fetch Response\n // CycleTLS returns headers as an object\n const responseHeaders = new Headers();\n if (response.headers) {\n Object.entries(response.headers).forEach(([key, value]) => {\n if (Array.isArray(value)) {\n value.forEach((v) => {\n responseHeaders.append(key, v);\n });\n } else if (typeof value === 'string') {\n responseHeaders.set(key, value);\n }\n });\n }\n\n // Get response body - cycletls provides helper methods, but we need the raw text\n // The response object has a text() method that returns the body as text\n let responseBody = '';\n if (typeof response.text === 'function') {\n responseBody = await response.text();\n } else if ((response as any).body) {\n responseBody = (response as any).body;\n }\n\n // Create a proper Response object using standard Response constructor\n const fetchResponse = new Response(responseBody, {\n status: response.status,\n statusText: '', // CycleTLS doesn't provide status text\n headers: responseHeaders,\n });\n\n return fetchResponse;\n } catch (error) {\n log(`CycleTLS request failed: ${error}`);\n throw error;\n }\n}\n"],"names":[],"mappings":";;;;AAIA,MAAM,GAAA,GAAM,MAAM,0BAA0B,CAAA,CAAA;AAE5C,IAAI,gBAAoE,GAAA,IAAA,CAAA;AAKxE,eAAsB,iBAAoB,GAAA;AACxC,EAAA,IAAI,CAAC,gBAAkB,EAAA;AACrB,IAAA,GAAA,CAAI,0BAA0B,CAAA,CAAA;AAC9B,IAAA,gBAAA,GAAmB,MAAM,YAAa,EAAA,CAAA;AACtC,IAAA,GAAA,CAAI,mCAAmC,CAAA,CAAA;AAAA,GACzC;AACA,EAAO,OAAA,gBAAA,CAAA;AACT,CAAA;AAKO,SAAS,YAAe,GAAA;AAC7B,EAAA,IAAI,gBAAkB,EAAA;AACpB,IAAA,GAAA,CAAI,qBAAqB,CAAA,CAAA;AACzB,IAAA,gBAAA,CAAiB,IAAK,EAAA,CAAA;AACtB,IAAmB,gBAAA,GAAA,IAAA,CAAA;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,KAAS,UAAY,EAAA;AACvC,MAAe,YAAA,GAAA,MAAM,SAAS,IAAK,EAAA,CAAA;AAAA,KACrC,MAAA,IAAY,SAAiB,IAAM,EAAA;AACjC,MAAA,YAAA,GAAgB,QAAiB,CAAA,IAAA,CAAA;AAAA,KACnC;AAGA,IAAM,MAAA,aAAA,GAAgB,IAAI,QAAA,CAAS,YAAc,EAAA;AAAA,MAC/C,QAAQ,QAAS,CAAA,MAAA;AAAA,MACjB,UAAY,EAAA,EAAA;AAAA;AAAA,MACZ,OAAS,EAAA,eAAA;AAAA,KACV,CAAA,CAAA;AAED,IAAO,OAAA,aAAA,CAAA;AAAA,WACA,KAAO,EAAA;AACd,IAAI,GAAA,CAAA,CAAA,yBAAA,EAA4B,KAAK,CAAE,CAAA,CAAA,CAAA;AACvC,IAAM,MAAA,KAAA,CAAA;AAAA,GACR;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../../../src/chrome-fingerprint.ts","../../../src/cycletls-fetch.ts"],"sourcesContent":["/**\n * Chrome version-dependent fingerprint constants.\n *\n * IMPORTANT: All constants in this file are tied to a specific Chrome version\n * (currently Chrome 144 on Windows 10). When bumping the Chrome version:\n * 1. Update CHROME_USER_AGENT with the new version string\n * 2. Update CHROME_SEC_CH_UA with the matching Client Hints\n * 3. Update CHROME_JA3 fingerprint (capture via tls.peet.ws)\n * 4. Update CHROME_JA4R fingerprint\n * 5. Update CHROME_HTTP2_FINGERPRINT settings frame\n * 6. Review CHROME_HEADER_ORDER if Chrome changes header ordering\n * 7. Update castle.ts DEFAULT_PROFILE if Chrome version affects fingerprint fields\n *\n * All values must be consistent with each other and match a real Chrome release.\n */\n\n/**\n * User-Agent string matching Chrome 144 on Windows 10.\n * Must be consistent across all requests and match the TLS fingerprint.\n */\nexport const CHROME_USER_AGENT =\n 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36';\n\n/**\n * Chrome Client Hints header matching the Chrome 144 user-agent.\n */\nexport const CHROME_SEC_CH_UA =\n '\"Not(A:Brand\";v=\"8\", \"Chromium\";v=\"144\", \"Google Chrome\";v=\"144\"';\n\n/**\n * JA3 TLS fingerprint for Chrome 144.\n * Captured from a real Chrome 144 browser session via tls.peet.ws.\n */\nexport const CHROME_JA3 =\n '771,4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,0-10-35-16-11-51-27-65037-43-45-18-23-5-65281-13-17613,4588-29-23-24,0';\n\n/**\n * JA4r fingerprint for Chrome 144.\n * Format: t13d1516h2_CIPHERS_EXTENSIONS_SIG_ALGS\n */\nexport const CHROME_JA4R =\n 't13d1516h2_002f,0035,009c,009d,1301,1302,1303,c013,c014,c02b,c02c,c02f,c030,cca8,cca9_0005,000a,000b,000d,0012,0017,001b,0023,002b,002d,0033,44cd,fe0d,ff01_0403,0804,0401,0503,0805,0501,0806,0601';\n\n/**\n * Chrome 144 HTTP/2 fingerprint - mimics exact HTTP/2 SETTINGS frame.\n * Format: SETTINGS|window_size|unknown|priority_order\n */\nexport const CHROME_HTTP2_FINGERPRINT =\n '1:65536;2:0;4:6291456;6:262144|15663105|0|m,a,s,p';\n\n/**\n * Exact header order that Chrome 144 uses.\n * Header ordering is critical for HTTP/2 fingerprint evasion — servers can detect\n * non-browser clients by checking if headers arrive in a non-standard order.\n */\nexport const CHROME_HEADER_ORDER = [\n // HTTP/2 pseudo-headers (Chrome 144 order: method, authority, scheme, path)\n ':method',\n ':authority',\n ':scheme',\n ':path',\n // Chrome Client Hints (mandatory for modern detection bypass)\n 'sec-ch-ua',\n 'sec-ch-ua-mobile',\n 'sec-ch-ua-platform',\n // Standard browser headers\n 'upgrade-insecure-requests',\n 'user-agent',\n 'accept',\n 'origin',\n 'sec-fetch-site',\n 'sec-fetch-mode',\n 'sec-fetch-user',\n 'sec-fetch-dest',\n 'referer',\n 'accept-encoding',\n 'accept-language',\n 'priority',\n // Authentication headers\n 'authorization',\n 'x-csrf-token',\n 'x-guest-token',\n 'x-twitter-auth-type',\n 'x-twitter-active-user',\n 'x-twitter-client-language',\n 'x-client-transaction-id',\n 'x-xp-forwarded-for',\n // POST-specific\n 'content-type',\n 'cookie',\n];\n","import initCycleTLS from 'cycletls';\nimport { Headers } from 'headers-polyfill';\nimport debug from 'debug';\nimport {\n CHROME_USER_AGENT,\n CHROME_JA3,\n CHROME_JA4R,\n CHROME_HTTP2_FINGERPRINT,\n CHROME_HEADER_ORDER,\n} from './chrome-fingerprint';\n\nconst log = debug('twitter-scraper:cycletls');\n\nlet cycleTLSInstance: Awaited<ReturnType<typeof initCycleTLS>> | null = null;\n\n/**\n * Initialize the CycleTLS instance. This should be called once before using the fetch wrapper.\n */\nexport async function initCycleTLSFetch() {\n if (!cycleTLSInstance) {\n log('Initializing CycleTLS...');\n cycleTLSInstance = await initCycleTLS();\n log('CycleTLS initialized successfully');\n }\n return cycleTLSInstance;\n}\n\n/**\n * Cleanup the CycleTLS instance. Call this when you're done making requests.\n */\nexport function cycleTLSExit() {\n if (cycleTLSInstance) {\n log('Exiting CycleTLS...');\n cycleTLSInstance.exit();\n cycleTLSInstance = null;\n }\n}\n\n/**\n * A fetch-compatible wrapper around CycleTLS that mimics Chrome's TLS fingerprint\n * to bypass Cloudflare and other bot detection systems.\n */\nexport async function cycleTLSFetch(\n input: RequestInfo | URL,\n init?: RequestInit,\n): Promise<Response> {\n const instance = await initCycleTLSFetch();\n\n const url =\n typeof input === 'string'\n ? input\n : input instanceof URL\n ? input.toString()\n : input.url;\n const method = (init?.method || 'GET').toUpperCase();\n\n log(`Making ${method} request to ${url}`);\n\n // Extract headers from RequestInit\n const headers: Record<string, string> = {};\n if (init?.headers) {\n if (init.headers instanceof Headers) {\n init.headers.forEach((value, key) => {\n headers[key] = value;\n });\n } else if (Array.isArray(init.headers)) {\n init.headers.forEach(([key, value]) => {\n headers[key] = value;\n });\n } else {\n Object.assign(headers, init.headers);\n }\n }\n\n // Convert body to string if needed\n let body: string | undefined;\n if (init?.body) {\n if (typeof init.body === 'string') {\n body = init.body;\n } else if (init.body instanceof URLSearchParams) {\n body = init.body.toString();\n } else {\n body = init.body.toString();\n }\n }\n\n // All Chrome fingerprint constants imported from chrome-fingerprint.ts\n const options = {\n body,\n headers,\n ja3: CHROME_JA3,\n ja4r: CHROME_JA4R,\n http2Fingerprint: CHROME_HTTP2_FINGERPRINT,\n headerOrder: CHROME_HEADER_ORDER,\n orderAsProvided: true,\n disableGrease: false,\n userAgent: headers['user-agent'] || CHROME_USER_AGENT,\n };\n\n try {\n const response = await instance(\n url,\n options,\n method.toLowerCase() as\n | 'get'\n | 'post'\n | 'put'\n | 'delete'\n | 'patch'\n | 'head'\n | 'options',\n );\n\n // Convert CycleTLS response to fetch Response\n // CycleTLS returns headers as an object\n const responseHeaders = new Headers();\n if (response.headers) {\n Object.entries(response.headers).forEach(([key, value]) => {\n if (Array.isArray(value)) {\n value.forEach((v) => {\n responseHeaders.append(key, v);\n });\n } else if (typeof value === 'string') {\n responseHeaders.set(key, value);\n }\n });\n }\n\n // Get response body - cycletls provides helper methods, but we need the raw text\n // The response object has a text() method that returns the body as text\n let responseBody = '';\n if (typeof response.text === 'function') {\n responseBody = await response.text();\n } else if ((response as any).body) {\n responseBody = (response as any).body;\n }\n\n // Create a proper Response object using standard Response constructor\n const fetchResponse = new Response(responseBody, {\n status: response.status,\n statusText: '', // CycleTLS doesn't provide status text\n headers: responseHeaders,\n });\n\n return fetchResponse;\n } catch (error) {\n log(`CycleTLS request failed: ${error}`);\n throw error;\n }\n}\n"],"names":[],"mappings":";;;;AAoBO,MAAM,iBACX,GAAA,iHAAA,CAAA;AAYK,MAAM,UACX,GAAA,yJAAA,CAAA;AAMK,MAAM,WACX,GAAA,qMAAA,CAAA;AAMK,MAAM,wBACX,GAAA,mDAAA,CAAA;AAOK,MAAM,mBAAsB,GAAA;AAAA;AAAA,EAEjC,SAAA;AAAA,EACA,YAAA;AAAA,EACA,SAAA;AAAA,EACA,O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