rezaharis commited on
Commit
74b1e26
·
verified ·
1 Parent(s): 5f99bb7

Update endpoints/cloudflare-cookies.js

Browse files
Files changed (1) hide show
  1. endpoints/cloudflare-cookies.js +76 -60
endpoints/cloudflare-cookies.js CHANGED
@@ -25,49 +25,47 @@ async function cloudflareCookies({ domain, cookies = [], proxy, mobile = false }
25
  await page.setViewport({ width: 980, height: 1743 });
26
  }
27
 
28
- // Handle cookies - bisa string panjang seperti contoh Anda
29
- if (cookies && cookies.length > 0) {
30
- const cookieObjects = [];
 
 
 
 
 
 
 
 
 
 
31
 
32
- for (const cookie of cookies) {
33
  if (typeof cookie === 'string') {
34
- // Parse string cookies seperti: "name=value; name2=value2"
35
- const cookiePairs = cookie.split(';').map(pair => pair.trim());
36
 
37
- for (const pair of cookiePairs) {
38
- if (pair.includes('=')) {
39
- const [name, ...valueParts] = pair.split('=');
40
- const value = valueParts.join('=');
41
-
42
- if (name && value) {
43
- cookieObjects.push({
44
- name: name.trim(),
45
- value: value.trim(),
46
- domain: new URL(domain).hostname,
47
- path: '/',
48
- secure: true,
49
- httpOnly: name.startsWith('_') || name.includes('token'),
50
- sameSite: 'Lax'
51
- });
52
- }
53
- }
54
  }
55
  } else {
56
- // Jika sudah object, langsung push
57
- cookieObjects.push({
58
  ...cookie,
59
  domain: cookie.domain || new URL(domain).hostname
60
  });
61
  }
62
  }
63
 
64
- // Set cookies ke browser
65
- if (cookieObjects.length > 0) {
66
- for (const cookie of cookieObjects) {
67
  try {
68
  await page.setCookie(cookie);
69
  } catch (e) {
70
- console.log('Failed to set cookie:', cookie.name);
71
  }
72
  }
73
  }
@@ -79,69 +77,87 @@ async function cloudflareCookies({ domain, cookies = [], proxy, mobile = false }
79
  timeout: timeout
80
  });
81
 
82
- // Cloudflare challenge detection and solving logic
83
- let challengeDetected = false;
84
- let clearanceCookie = null;
85
  const startTime = Date.now();
86
 
87
- while (Date.now() - startTime < timeout && !clearanceCookie) {
88
  // Check for Cloudflare challenge
89
- const hasChallenge = await page.evaluate(() => {
90
- return document.querySelector('#challenge-form') !== null ||
91
- document.querySelector('.cf-browser-verification') !== null ||
92
- document.body.innerHTML.includes('Checking if the site connection is secure') ||
93
- document.body.innerHTML.includes('cf-browser-verification');
 
 
 
 
 
94
  });
95
 
96
- if (hasChallenge && !challengeDetected) {
97
- challengeDetected = true;
98
  console.log('Cloudflare challenge detected, solving...');
99
 
100
  // Try to click challenge button if exists
101
- try {
102
- const submitButton = await page.$('input[type="submit"]');
103
- if (submitButton) {
104
- await submitButton.click();
105
  await page.waitForTimeout(5000);
 
 
106
  }
107
- } catch (e) {
108
- // Continue if button click fails
109
  }
 
 
110
  }
111
 
112
- // Check for clearance cookie
113
  const currentCookies = await page.cookies();
114
- clearanceCookie = currentCookies.find(cookie => cookie.name === 'cf_clearance');
115
-
116
- if (clearanceCookie) break;
117
 
 
 
 
 
 
118
  await page.waitForTimeout(1000);
119
  }
120
 
121
- // Get final results
122
  const finalCookies = await page.cookies();
123
  const finalUrl = page.url();
124
  const userAgent = await page.evaluate(() => navigator.userAgent);
 
 
 
125
 
126
  isResolved = true;
127
  clearTimeout(cl);
128
 
129
- if (!clearanceCookie) {
130
- throw new Error("Failed to obtain Cloudflare clearance cookie");
131
  }
132
 
133
- // Format cookies to string like your example
134
- const cookieString = finalCookies.map(cookie =>
135
- `${cookie.name}=${cookie.value}`
136
- ).join('; ');
 
 
 
 
 
 
 
137
 
138
  return {
139
  url: finalUrl,
140
  cookies: finalCookies,
141
- cookie_string: cookieString, // Format seperti contoh panjang Anda
142
- clearance: clearanceCookie,
143
  user_agent: userAgent,
144
- cf_clearance: clearanceCookie.value,
145
  success: true
146
  };
147
 
 
25
  await page.setViewport({ width: 980, height: 1743 });
26
  }
27
 
28
+ // Simpan cookies input asli untuk nanti
29
+ const originalCookiesString = typeof cookies === 'string' ? cookies : cookies.join('; ');
30
+
31
+ // Parse cookies input untuk diset ke browser
32
+ let cookiesToSet = [];
33
+ let cookiesArray = [];
34
+
35
+ if (cookies) {
36
+ if (typeof cookies === 'string') {
37
+ cookiesArray = cookies.split(';').map(cookie => cookie.trim()).filter(cookie => cookie);
38
+ } else {
39
+ cookiesArray = cookies;
40
+ }
41
 
42
+ for (const cookie of cookiesArray) {
43
  if (typeof cookie === 'string') {
44
+ const [name, ...valueParts] = cookie.split('=');
45
+ const value = valueParts.join('=');
46
 
47
+ if (name && value) {
48
+ cookiesToSet.push({
49
+ name: name.trim(),
50
+ value: value.trim(),
51
+ domain: new URL(domain).hostname
52
+ });
 
 
 
 
 
 
 
 
 
 
 
53
  }
54
  } else {
55
+ cookiesToSet.push({
 
56
  ...cookie,
57
  domain: cookie.domain || new URL(domain).hostname
58
  });
59
  }
60
  }
61
 
62
+ // Set cookies ke browser (termasuk cf_clearance lama)
63
+ if (cookiesToSet.length > 0) {
64
+ for (const cookie of cookiesToSet) {
65
  try {
66
  await page.setCookie(cookie);
67
  } catch (e) {
68
+ // Ignore cookie errors
69
  }
70
  }
71
  }
 
77
  timeout: timeout
78
  });
79
 
80
+ // Cloudflare challenge detection and solving
81
+ let challengeSolved = false;
82
+ let newClearanceCookie = null;
83
  const startTime = Date.now();
84
 
85
+ while (Date.now() - startTime < timeout && !challengeSolved) {
86
  // Check for Cloudflare challenge
87
+ const challengeState = await page.evaluate(() => {
88
+ const hasChallenge = document.querySelector('#challenge-form') !== null ||
89
+ document.querySelector('.cf-browser-verification') !== null ||
90
+ document.body.innerHTML.includes('Checking if the site connection is secure') ||
91
+ document.body.innerHTML.includes('Just a moment...');
92
+
93
+ const challengeButton = document.querySelector('input[type="submit"]') ||
94
+ document.querySelector('button[type="submit"]');
95
+
96
+ return { hasChallenge, challengeButton: !!challengeButton };
97
  });
98
 
99
+ if (challengeState.hasChallenge && !challengeSolved) {
 
100
  console.log('Cloudflare challenge detected, solving...');
101
 
102
  // Try to click challenge button if exists
103
+ if (challengeState.challengeButton) {
104
+ try {
105
+ await page.click('input[type="submit"], button[type="submit"]');
 
106
  await page.waitForTimeout(5000);
107
+ } catch (e) {
108
+ // Continue if button click fails
109
  }
 
 
110
  }
111
+
112
+ await page.waitForTimeout(3000);
113
  }
114
 
115
+ // Check for new clearance cookie
116
  const currentCookies = await page.cookies();
117
+ newClearanceCookie = currentCookies.find(cookie => cookie.name === 'cf_clearance');
 
 
118
 
119
+ if (newClearanceCookie) {
120
+ challengeSolved = true;
121
+ break;
122
+ }
123
+
124
  await page.waitForTimeout(1000);
125
  }
126
 
127
+ // Get final cookies
128
  const finalCookies = await page.cookies();
129
  const finalUrl = page.url();
130
  const userAgent = await page.evaluate(() => navigator.userAgent);
131
+
132
+ // Find the new clearance cookie
133
+ newClearanceCookie = finalCookies.find(cookie => cookie.name === 'cf_clearance');
134
 
135
  isResolved = true;
136
  clearTimeout(cl);
137
 
138
+ if (!newClearanceCookie) {
139
+ throw new Error("Failed to get new Cloudflare clearance cookie");
140
  }
141
 
142
+ // BUAT COOKIE_STRING BARU: Gunakan cookies input asli, tapi ganti cf_clearance dengan yang baru
143
+ let updatedCookieString = originalCookiesString;
144
+
145
+ // Replace cf_clearance lama dengan yang baru
146
+ if (originalCookiesString.includes('cf_clearance=')) {
147
+ const oldClearanceRegex = /cf_clearance=[^;]+/;
148
+ updatedCookieString = originalCookiesString.replace(oldClearanceRegex, `cf_clearance=${newClearanceCookie.value}`);
149
+ } else {
150
+ // Jika tidak ada cf_clearance di input, tambahkan
151
+ updatedCookieString = originalCookiesString + `; cf_clearance=${newClearanceCookie.value}`;
152
+ }
153
 
154
  return {
155
  url: finalUrl,
156
  cookies: finalCookies,
157
+ cookie_string: updatedCookieString, // String panjang SAMA dengan input, tapi cf_clearance DIUPDATE
158
+ clearance: newClearanceCookie,
159
  user_agent: userAgent,
160
+ cf_clearance: newClearanceCookie.value,
161
  success: true
162
  };
163