Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import express from 'express';
|
| 2 |
-
import { solveCloudflare } from './solve.js';
|
| 3 |
|
| 4 |
const app = express();
|
| 5 |
app.use(express.json());
|
|
@@ -30,6 +30,17 @@ app.post('/solve', async (req, res) => {
|
|
| 30 |
}
|
| 31 |
});
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
app.listen(PORT, '0.0.0.0', () => {
|
| 34 |
console.log(`Server listening on port ${PORT}`);
|
| 35 |
});
|
|
|
|
| 1 |
import express from 'express';
|
| 2 |
+
import { solveCloudflare, getSakanaCookie } from './solve.js';
|
| 3 |
|
| 4 |
const app = express();
|
| 5 |
app.use(express.json());
|
|
|
|
| 30 |
}
|
| 31 |
});
|
| 32 |
|
| 33 |
+
app.post('/solve_sakana', async (req, res) => {
|
| 34 |
+
try {
|
| 35 |
+
const result = await getSakanaCookie({
|
| 36 |
+
timeout: 60
|
| 37 |
+
});
|
| 38 |
+
res.json(result);
|
| 39 |
+
} catch (error) {
|
| 40 |
+
res.status(500).json({ error: error.message });
|
| 41 |
+
}
|
| 42 |
+
});
|
| 43 |
+
|
| 44 |
app.listen(PORT, '0.0.0.0', () => {
|
| 45 |
console.log(`Server listening on port ${PORT}`);
|
| 46 |
});
|
solve.js
CHANGED
|
@@ -272,4 +272,89 @@ async function solveCloudflare({ url, headless = false, proxy = null, proxyFile
|
|
| 272 |
throw err;
|
| 273 |
}
|
| 274 |
}
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
throw err;
|
| 273 |
}
|
| 274 |
}
|
| 275 |
+
|
| 276 |
+
async function getSakanaCookie({ timeout = 60 } = {}) {
|
| 277 |
+
const userAgent = getChromeUA();
|
| 278 |
+
const args = [
|
| 279 |
+
'--no-sandbox',
|
| 280 |
+
'--disable-setuid-sandbox',
|
| 281 |
+
'--disable-quic',
|
| 282 |
+
`--window-size=${WIDTH},${HEIGHT}`,
|
| 283 |
+
];
|
| 284 |
+
|
| 285 |
+
const { browser, page } = await connect({
|
| 286 |
+
headless: 'auto',
|
| 287 |
+
args,
|
| 288 |
+
turnstile: true,
|
| 289 |
+
connectOption: {},
|
| 290 |
+
disableXvfb: false,
|
| 291 |
+
ignoreAllFlags: false,
|
| 292 |
+
});
|
| 293 |
+
|
| 294 |
+
try {
|
| 295 |
+
await page.setViewport({ width: WIDTH, height: HEIGHT });
|
| 296 |
+
await page.setUserAgent(userAgent);
|
| 297 |
+
|
| 298 |
+
console.log('[Sakana] Navigating to chat.sakana.ai...');
|
| 299 |
+
await page.goto('https://chat.sakana.ai', {
|
| 300 |
+
waitUntil: 'domcontentloaded',
|
| 301 |
+
timeout: timeout * 1000
|
| 302 |
+
});
|
| 303 |
+
|
| 304 |
+
console.log('[Sakana] Waiting for Turnstile and page stabilization...');
|
| 305 |
+
await sleep(5000);
|
| 306 |
+
|
| 307 |
+
// Try to trigger the chat by typing
|
| 308 |
+
try {
|
| 309 |
+
console.log('[Sakana] Trying to type in textarea to trigger cookie...');
|
| 310 |
+
await page.waitForSelector('textarea', { timeout: 10000 });
|
| 311 |
+
await page.type('textarea', 'hai');
|
| 312 |
+
await page.keyboard.press('Enter');
|
| 313 |
+
await sleep(3000); // Wait for response/cookie
|
| 314 |
+
} catch (e) {
|
| 315 |
+
console.log('[Sakana] Textarea not found or typing failed, waiting for cookie anyway...');
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
let cookies = [];
|
| 319 |
+
let sakanaCookie = null;
|
| 320 |
+
let cfClearance = null;
|
| 321 |
+
|
| 322 |
+
const deadline = Date.now() + (timeout * 1000);
|
| 323 |
+
|
| 324 |
+
while (Date.now() < deadline) {
|
| 325 |
+
cookies = await page.cookies().catch(() => []);
|
| 326 |
+
sakanaCookie = cookies.find(c => c.name === 'sakana-chat');
|
| 327 |
+
cfClearance = cookies.find(c => c.name === 'cf_clearance');
|
| 328 |
+
|
| 329 |
+
if (sakanaCookie && cfClearance) {
|
| 330 |
+
console.log('[Sakana] Successfully obtained both cookies!');
|
| 331 |
+
break;
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
await sleep(1000);
|
| 335 |
+
}
|
| 336 |
+
|
| 337 |
+
await browser.close();
|
| 338 |
+
|
| 339 |
+
if (!sakanaCookie) {
|
| 340 |
+
return {
|
| 341 |
+
success: false,
|
| 342 |
+
error: 'Failed to obtain sakana-chat cookie within timeout'
|
| 343 |
+
};
|
| 344 |
+
}
|
| 345 |
+
|
| 346 |
+
return {
|
| 347 |
+
success: true,
|
| 348 |
+
sakana_chat: sakanaCookie.value,
|
| 349 |
+
cf_clearance: cfClearance ? cfClearance.value : null,
|
| 350 |
+
cookie_string: buildCookieString(cookies),
|
| 351 |
+
user_agent: userAgent
|
| 352 |
+
};
|
| 353 |
+
|
| 354 |
+
} catch (err) {
|
| 355 |
+
await browser.close().catch(() => {});
|
| 356 |
+
return { success: false, error: err.message };
|
| 357 |
+
}
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
export { solveCloudflare, getSakanaCookie };
|