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 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
const express = require('express');
const { chromium } = require('playwright');
const tf = require('@tensorflow/tfjs-node');
const axios = require('axios');
const sharp = require('sharp');

const app = express();
app.use(express.json());

const HCAPTCHA_FRAME = "iframe[src*='hcaptcha.com/captcha']";
const HCAPTCHA_CHECKBOX = "iframe[src*='hcaptcha.com/checkbox']";
const CHALLENGE_CONTAINER = ".challenge-container";

let model = null;

function log(...args) {
  console.log('[hCaptcha API]', new Date().toISOString(), ...args);
}

function delay(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

function randomDelay(min, max) {
  return delay(Math.floor(Math.random() * (max - min + 1)) + min);
}

async function loadModel() {
  if (!model) {
    try {
      log('Loading ML model...');
      model = await tf.loadLayersModel('https://tfhub.dev/google/tfjs-model/imagenet/mobilenet_v2_100_224/classification/3/default/1', {
        fromTFHub: true
      });
      log('ML model loaded successfully');
    } catch (error) {
      log('Failed to load model, using fallback:', error.message);
      model = null;
    }
  }
  return model;
}

async function preprocessImage(imageBuffer) {
  const processedImage = await sharp(imageBuffer)
    .resize(224, 224)
    .toBuffer();
  
  const tensor = tf.node.decodeImage(processedImage, 3)
    .toFloat()
    .div(255.0)
    .expandDims(0);
  
  return tensor;
}

async function analyzeImageWithML(imageUrl) {
  try {
    const response = await axios.get(imageUrl, { responseType: 'arraybuffer' });
    const imageBuffer = Buffer.from(response.data);
    
    const tensor = await preprocessImage(imageBuffer);
    
    if (model) {
      const predictions = await model.predict(tensor);
      const predArray = await predictions.data();
      const topPredictions = Array.from(predArray)
        .map((prob, idx) => ({ class: idx, probability: prob }))
        .sort((a, b) => b.probability - a.probability)
        .slice(0, 5);
      
      tensor.dispose();
      predictions.dispose();
      
      return topPredictions;
    }
    
    tensor.dispose();
    return null;
  } catch (error) {
    log('ML analysis error:', error.message);
    return null;
  }
}

async function analyzeImageFeatures(frame, imageElement) {
  return await frame.evaluate((img) => {
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');
    canvas.width = img.width;
    canvas.height = img.height;
    ctx.drawImage(img, 0, 0);
    
    const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
    let brightness = 0;
    let edges = 0;
    const colors = new Set();
    let redDominance = 0;
    let blueDominance = 0;
    let greenDominance = 0;
    
    for (let i = 0; i < imageData.data.length; i += 4) {
      const r = imageData.data[i];
      const g = imageData.data[i + 1];
      const b = imageData.data[i + 2];
      brightness += (r + g + b) / 3;
      
      if (r > g && r > b) redDominance++;
      if (b > r && b > g) blueDominance++;
      if (g > r && g > b) greenDominance++;
      
      const colorKey = `${Math.floor(r/50)*50},${Math.floor(g/50)*50},${Math.floor(b/50)*50}`;
      colors.add(colorKey);
      
      if (i + canvas.width * 4 < imageData.data.length) {
        if (Math.abs(r - imageData.data[i + canvas.width * 4]) > 30) edges++;
      }
    }
    
    const totalPixels = imageData.data.length / 4;
    
    return {
      brightness: brightness / totalPixels,
      edges: edges,
      colorDiversity: colors.size,
      redDominance: redDominance / totalPixels,
      blueDominance: blueDominance / totalPixels,
      greenDominance: greenDominance / totalPixels
    };
  }, imageElement);
}

async function classifyImage(imageUrl, challengeText, features) {
  const mlPredictions = await analyzeImageWithML(imageUrl);
  
  let score = 0;
  const challenge = challengeText.toLowerCase();
  
  if (mlPredictions && mlPredictions.length > 0) {
    const topClass = mlPredictions[0].class;
    
    if (challenge.includes('vehicle') || challenge.includes('car') || challenge.includes('bus') || challenge.includes('truck')) {
      if ((topClass >= 400 && topClass <= 900) || features.edges > 1000) {
        score += 3;
      }
    } else if (challenge.includes('animal') || challenge.includes('cat') || challenge.includes('dog') || challenge.includes('bird')) {
      if ((topClass >= 0 && topClass <= 400) || features.colorDiversity > 80) {
        score += 3;
      }
    } else if (challenge.includes('plant') || challenge.includes('tree') || challenge.includes('flower')) {
      if (features.greenDominance > 0.3 || (topClass >= 900 && topClass <= 1000)) {
        score += 3;
      }
    } else if (challenge.includes('sky') || challenge.includes('cloud')) {
      if (features.blueDominance > 0.4 || features.brightness > 150) {
        score += 3;
      }
    } else if (challenge.includes('water') || challenge.includes('ocean') || challenge.includes('lake')) {
      if (features.blueDominance > 0.3 || features.brightness > 100) {
        score += 3;
      }
    }
  }
  
  if (challenge.includes('vehicle') || challenge.includes('car') || challenge.includes('bus')) {
    if (features.edges > 1200) score += 2;
    if (features.colorDiversity > 70) score += 1;
  } else if (challenge.includes('animal') || challenge.includes('cat') || challenge.includes('dog')) {
    if (features.colorDiversity > 90) score += 2;
    if (features.edges > 800) score += 1;
  } else if (challenge.includes('plant') || challenge.includes('tree')) {
    if (features.greenDominance > 0.25) score += 2;
    if (features.colorDiversity > 60) score += 1;
  } else if (challenge.includes('sky') || challenge.includes('water')) {
    if (features.brightness > 140) score += 2;
    if (features.blueDominance > 0.3) score += 1;
  } else if (challenge.includes('building') || challenge.includes('house')) {
    if (features.edges > 1500) score += 2;
    if (features.colorDiversity < 70) score += 1;
  }
  
  return score;
}

async function findPuzzleMatch(frame) {
  log("Analyzing puzzle piece position with ML");
  
  const puzzleBox = await frame.$eval('.challenge-image', el => {
    const rect = el.getBoundingClientRect();
    return { x: rect.x, y: rect.y, width: rect.width, height: rect.height };
  });
  
  const pieceBox = await frame.$eval('.draggable-piece', el => {
    const rect = el.getBoundingClientRect();
    return { x: rect.x, y: rect.y, width: rect.width, height: rect.height };
  });
  
  const gapX = await frame.evaluate(() => {
    const canvas = document.querySelector('.challenge-image');
    const ctx = canvas.getContext('2d');
    const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
    
    let darkestX = 0;
    let minBrightness = 999;
    let maxEdgeCount = 0;
    let edgeX = 0;
    
    for (let x = 0; x < canvas.width; x += 3) {
      let columnBrightness = 0;
      let edgeCount = 0;
      
      for (let y = 0; y < canvas.height; y++) {
        const i = (y * canvas.width + x) * 4;
        const brightness = (imageData.data[i] + imageData.data[i+1] + imageData.data[i+2]) / 3;
        columnBrightness += brightness;
        
        if (y > 0) {
          const prevI = ((y-1) * canvas.width + x) * 4;
          const diff = Math.abs(brightness - (imageData.data[prevI] + imageData.data[prevI+1] + imageData.data[prevI+2]) / 3);
          if (diff > 40) edgeCount++;
        }
      }
      
      columnBrightness /= canvas.height;
      
      if (columnBrightness < minBrightness) {
        minBrightness = columnBrightness;
        darkestX = x;
      }
      
      if (edgeCount > maxEdgeCount) {
        maxEdgeCount = edgeCount;
        edgeX = x;
      }
    }
    
    return (darkestX + edgeX) / 2;
  });
  
  return {
    targetX: puzzleBox.x + gapX,
    targetY: puzzleBox.y + puzzleBox.height / 2,
    startX: pieceBox.x + pieceBox.width / 2,
    startY: pieceBox.y + pieceBox.height / 2
  };
}

async function humanMouseMove(page, startX, startY, endX, endY) {
  await page.mouse.move(startX, startY);
  await randomDelay(50, 150);
  await page.mouse.down();
  await randomDelay(100, 200);
  
  const steps = 15 + Math.floor(Math.random() * 10);
  const controlX = (startX + endX) / 2 + (Math.random() - 0.5) * 50;
  const controlY = (startY + endY) / 2 + (Math.random() - 0.5) * 50;
  
  for (let i = 1; i <= steps; i++) {
    const t = i / steps;
    const x = Math.pow(1 - t, 2) * startX + 2 * (1 - t) * t * controlX + Math.pow(t, 2) * endX;
    const y = Math.pow(1 - t, 2) * startY + 2 * (1 - t) * t * controlY + Math.pow(t, 2) * endY;
    
    await page.mouse.move(x + (Math.random() - 0.5) * 2, y + (Math.random() - 0.5) * 2);
    await randomDelay(30, 80);
  }
  
  await randomDelay(150, 300);
  await page.mouse.up();
  await randomDelay(200, 400);
}

async function solveDragDropChallenge(page, challengeFrame) {
  log("Solving drag & drop challenge");
  
  const position = await findPuzzleMatch(challengeFrame);
  await humanMouseMove(page, position.startX, position.startY, position.targetX, position.targetY);
  
  await randomDelay(500, 1000);
  return true;
}

async function solveImageChallenge(challengeFrame, challengeText) {
  log("Solving image selection challenge with ML:", challengeText);
  
  const images = await challengeFrame.$$('.task-image');
  log(`Found ${images.length} images`);
  
  const imageData = [];
  
  for (let i = 0; i < images.length; i++) {
    const img = images[i];
    const features = await analyzeImageFeatures(challengeFrame, img);
    const imageUrl = await img.evaluate(el => el.src);
    const score = await classifyImage(imageUrl, challengeText, features);
    
    imageData.push({ index: i, score, features });
    await randomDelay(100, 200);
  }
  
  imageData.sort((a, b) => b.score - a.score);
  
  const threshold = imageData[0].score * 0.7;
  const selectedIndices = imageData
    .filter(data => data.score >= threshold)
    .map(data => data.index);
  
  if (selectedIndices.length === 0) {
    const randomCount = Math.min(3, Math.floor(Math.random() * 3) + 1);
    for (let i = 0; i < randomCount; i++) {
      selectedIndices.push(imageData[i].index);
    }
  }
  
  log(`Selecting ${selectedIndices.length} images based on ML:`, selectedIndices);
  
  for (const index of selectedIndices) {
    await randomDelay(300, 700);
    await images[index].click();
    await randomDelay(200, 400);
  }
  
  await randomDelay(500, 1000);
  
  const submitButton = await challengeFrame.$('.button-submit');
  if (submitButton) {
    await submitButton.click();
    await randomDelay(1500, 2500);
  }
  
  return true;
}

async function solveCaptcha(url, options = {}) {
  const {
    waitTimeout = 30000,
    maxRetries = 3
  } = options;
  
  await loadModel();
  
  const browser = await chromium.launch({
    headless: true,
    args: [
      '--no-sandbox',
      '--disable-setuid-sandbox',
      '--disable-dev-shm-usage',
      '--disable-accelerated-2d-canvas',
      '--disable-gpu'
    ]
  });
  
  const context = await browser.newContext({
    viewport: { width: 1280, height: 720 },
    userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
  });
  
  const page = await context.newPage();
  
  try {
    log(`Navigating to: ${url}`);
    await page.goto(url, { waitUntil: 'networkidle', timeout: waitTimeout });
    
    await page.waitForSelector(HCAPTCHA_CHECKBOX, { state: "attached", timeout: waitTimeout });
    
    const checkboxElement = await page.$(HCAPTCHA_CHECKBOX);
    if (!checkboxElement) {
      throw new Error("Could not find hCaptcha checkbox iframe");
    }
    
    const checkboxFrame = await checkboxElement.contentFrame();
    if (!checkboxFrame) {
      throw new Error("Could not find hCaptcha checkbox iframe content");
    }
    
    const checkboxRect = await checkboxElement.boundingBox();
    if (!checkboxRect) {
      throw new Error("Could not get checkbox position");
    }
    
    log("Clicking checkbox with mouse");
    const checkboxX = checkboxRect.x + checkboxRect.width / 2;
    const checkboxY = checkboxRect.y + checkboxRect.height / 2;
    
    await page.mouse.move(checkboxX, checkboxY);
    await randomDelay(100, 300);
    await page.mouse.click(checkboxX, checkboxY);
    await randomDelay(2000, 3000);
    
    const challengeElement = await page.$(HCAPTCHA_FRAME);
    if (!challengeElement) {
      log("No challenge appeared - likely passed");
      
      const cookies = await context.cookies();
      await browser.close();
      
      return {
        success: true,
        message: "No challenge required",
        cookies: cookies
      };
    }
    
    const challengeFrame = await challengeElement.contentFrame();
    if (!challengeFrame) {
      throw new Error("Could not find hCaptcha challenge iframe content");
    }
    
    await challengeFrame.waitForSelector(CHALLENGE_CONTAINER, { timeout: waitTimeout });
    
    let attempts = 0;
    let solved = false;
    
    while (!solved && attempts < maxRetries) {
      attempts++;
      log(`Attempt ${attempts}/${maxRetries}`);
      
      const challengeText = await challengeFrame.$eval('.prompt-text', el => el.textContent).catch(() => '');
      log("Challenge prompt:", challengeText);
      
      const isDragDrop = await challengeFrame.$('.draggable-piece') !== null;
      
      try {
        if (isDragDrop) {
          await solveDragDropChallenge(page, challengeFrame);
        } else {
          await solveImageChallenge(challengeFrame, challengeText);
        }
        
        await randomDelay(2000, 3000);
        
        const stillHasChallenge = await page.$(HCAPTCHA_FRAME) !== null;
        if (!stillHasChallenge) {
          solved = true;
          log("Challenge solved successfully with ML!");
        }
      } catch (error) {
        log(`Attempt ${attempts} failed:`, error.message);
        await randomDelay(1000, 2000);
      }
    }
    
    if (!solved) {
      await browser.close();
      return {
        success: false,
        message: "Could not solve hCaptcha after maximum retries"
      };
    }
    
    const cookies = await context.cookies();
    const token = await page.evaluate(() => {
      const responseInput = document.querySelector('[name="h-captcha-response"]');
      return responseInput ? responseInput.value : null;
    });
    
    await browser.close();
    
    return {
      success: true,
      message: "hCaptcha solved successfully with ML",
      token: token,
      cookies: cookies
    };
    
  } catch (error) {
    await browser.close();
    throw error;
  }
}

app.post('/solve', async (req, res) => {
  try {
    const { url, options } = req.body;
    
    if (!url) {
      return res.status(400).json({
        success: false,
        error: 'URL is required'
      });
    }
    
    log(`Received solve request for: ${url}`);
    
    const result = await solveCaptcha(url, options);
    
    res.json(result);
    
  } catch (error) {
    log('Error:', error.message);
    res.status(500).json({
      success: false,
      error: error.message
    });
  }
});

app.get('/health', (req, res) => {
  res.json({ status: 'ok', model: model ? 'loaded' : 'fallback', timestamp: new Date().toISOString() });
});

const PORT = process.env.PORT || 7860;

app.listen(PORT, '0.0.0.0', () => {
  log(`hCaptcha Solver API with ML running on port ${PORT}`);
});