Skip to content

Commit

Permalink
Added support HCaptcha for AntiCaptcha
Browse files Browse the repository at this point in the history
  • Loading branch information
Tagliatti committed Jul 22, 2021
1 parent 21eb0d6 commit fb18861
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 0 deletions.
95 changes: 95 additions & 0 deletions src/Provider/AntiCaptcha/HCaptcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php


namespace Crawly\CaptchaBreaker\Provider\AntiCaptcha;

use Crawly\CaptchaBreaker\Provider\ProviderInterface;
use Psr\Log\LoggerInterface;

class HCaptcha extends AntiCaptcha implements ProviderInterface
{
protected $websiteURL;
protected $websiteKey;
protected $userAgent;
protected $logger;
protected $proxyAddress;
protected $proxyPort;
protected $proxyLogin;
protected $proxyPassword;
protected $proxyType;

/**
* @SuppressWarnings("PHPMD.ExcessiveParameterList")
*/
public function __construct(
string $clientKey,
string $websiteURL,
string $websiteKey,
string $userAgent,
LoggerInterface $logger = null,
string $proxyAddress = '',
string $proxyPort = '',
string $proxyLogin = '',
string $proxyPassword = '',
string $proxyType = 'http'
) {
$this->clientKey = $clientKey;
$this->websiteURL = $websiteURL;
$this->websiteKey = $websiteKey;
$this->userAgent = $userAgent;

$this->logger = $logger;
$this->proxyAddress = $proxyAddress;
$this->proxyPort = $proxyPort;
$this->proxyLogin = $proxyLogin;
$this->proxyPassword = $proxyPassword;
$this->proxyType = $proxyType;

parent::__construct();
}

protected function getPostData()
{
return [
'type' => empty($this->proxyAddress) ? 'HCaptchaTaskProxyless' : 'HCaptchaTask',
'websiteURL' => $this->websiteURL,
'websiteKey' => $this->websiteKey,
'userAgent' => $this->userAgent,
'proxyType' => $this->proxyType,
'proxyAddress' => $this->proxyAddress,
'proxyPort' => $this->proxyPort,
'proxyLogin' => $this->proxyLogin,
'proxyPassword' => $this->proxyPassword,
];
}

/**
* @return string
* @codeCoverageIgnore
*/
protected function getTaskSolution(): string
{
return $this->taskInfo->solution->gRecaptchaResponse;
}

/**
* {@inheritDoc}
* @codeCoverageIgnore
*/
public function solve(): string
{
$this->createTask();
$this->waitForResult();

return $this->getTaskSolution();
}

/**
* {@inheritDoc}
* @codeCoverageIgnore
*/
public function balance()
{
return $this->getBalance();
}
}
78 changes: 78 additions & 0 deletions tests/AntiCaptcha/HCaptchaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php


namespace Crawly\CaptchaBreaker\Test\AntiCaptcha;


use Crawly\CaptchaBreaker\Provider\AntiCaptcha\HCaptcha;
use PHPUnit\Framework\TestCase;
use ReflectionClass;

class HCaptchaTest extends TestCase
{
public function testGetPostData()
{
$noCaptcha = $this->getMockBuilder(HCaptcha::class)->setConstructorArgs(['123', 'url', 'key', 'user-agent'])->getMock();

$stub = $this->getNoCaptchaReflection();

$getPostData = $this->getPostDataMethod($stub);

$postData = $getPostData->invoke($noCaptcha);

$this->assertEquals('HCaptchaTaskProxyless', $postData['type']);
$this->assertEquals('url', $postData['websiteURL']);
$this->assertEquals('key', $postData['websiteKey']);
$this->assertEquals('user-agent', $postData['userAgent']);
$this->assertEquals('http', $postData['proxyType']);
$this->assertEquals('', $postData['proxyAddress']);
$this->assertEquals('', $postData['proxyPort']);
$this->assertEquals('', $postData['proxyLogin']);
$this->assertEquals('', $postData['proxyPassword']);
}

public function testGetPostDataWithAllParams()
{
$noCaptcha = $this->getMockBuilder(HCaptcha::class)->setConstructorArgs([
'123',
'url',
'key',
'user-agent',
null,
'proxy-address',
'proxy-port',
'proxy-login',
'proxy-password',
'proxy-type',
])->getMock();

$stub = $this->getNoCaptchaReflection();

$getPostData = $this->getPostDataMethod($stub);

$postData = $getPostData->invoke($noCaptcha);

$this->assertEquals('HCaptchaTask', $postData['type']);
$this->assertEquals('url', $postData['websiteURL']);
$this->assertEquals('key', $postData['websiteKey']);
$this->assertEquals('user-agent', $postData['userAgent']);
$this->assertEquals('proxy-type', $postData['proxyType']);
$this->assertEquals('proxy-address', $postData['proxyAddress']);
$this->assertEquals('proxy-port', $postData['proxyPort']);
$this->assertEquals('proxy-login', $postData['proxyLogin']);
$this->assertEquals('proxy-password', $postData['proxyPassword']);
}

protected function getNoCaptchaReflection(): ReflectionClass
{
return new ReflectionClass(HCaptcha::class);
}

protected function getPostDataMethod(ReflectionClass $stub): \ReflectionMethod
{
$getPostData = $stub->getMethod('getPostData');
$getPostData->setAccessible(true);

return $getPostData;
}
}

0 comments on commit fb18861

Please sign in to comment.