-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support HCaptcha for AntiCaptcha
- Loading branch information
Showing
2 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |