From 1dd37f6161958a8e312a10cf9110bfe44d316b79 Mon Sep 17 00:00:00 2001 From: sikofitt Date: Fri, 23 Dec 2016 14:50:39 -0800 Subject: [PATCH] TorClient --- composer.lock | 2 +- src/Sikofitt/Tor/TorClient.php | 210 +++++++++++++++++++++++---------- 2 files changed, 147 insertions(+), 65 deletions(-) diff --git a/composer.lock b/composer.lock index c142896..b2250c7 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "439da2eb89743fa1d87cf656df00dd7b", + "hash": "9ec1203744073948a67a86ddfbf1d1e2", "content-hash": "83d61f647d294058c67c53ea5fab1dc9", "packages": [ { diff --git a/src/Sikofitt/Tor/TorClient.php b/src/Sikofitt/Tor/TorClient.php index 0711359..4ce2e18 100644 --- a/src/Sikofitt/Tor/TorClient.php +++ b/src/Sikofitt/Tor/TorClient.php @@ -10,79 +10,65 @@ namespace Sikofitt\Tor; use GuzzleHttp\Client; +use GuzzleHttp\ClientInterface; use GuzzleHttp\Handler\CurlMultiHandler; use GuzzleHttp\HandlerStack; use GuzzleTor\Middleware; +use Sikofitt\Tor\Collection\ImageCollection; use Sikofitt\Tor\Exception\BadProxyUrlException; -class TorClient +/** + * Class TorClient + * + * @package Sikofitt\Tor + */ +class TorClient extends Client implements ClientInterface { + + /** + * @var Client + */ private $client; + + /** + * @var string + */ private $proxy; + + /** + * @var string + */ private $torControl; + + /** + * @var HandlerStack + */ private $handlerStack; + + /** + * @var Middleware + */ private $middleware; - public function __construct($proxy = '127.0.0.1:9050', $torControl = '127.0.0.1:9051') - { + private $poolData; + + public function __construct( + $proxy = '127.0.0.1:9050', + $torControl = '127.0.0.1:9051' + ) { $this->proxy = $proxy; $this->torControl = $torControl; + $this->setTorMiddleWare(); + $this->createHandlerStack(); + $this->setClient(); - $this->handlerStack = new HandlerStack(); - $this->handlerStack->setHandler(new CurlMultiHandler()); - $this->handlerStack->push(Middleware::tor($this->proxy, $this->torControl)); - - $this->client = new Client([ - 'verify' => false, - 'handler' => $this->handlerStack, - ]); - } - public function setProxyPort($port) - { - preg_replace('/:\d{4}/', ':' . $port, $this->proxy); - return $this; - } - public function setTorControlPort($port) - { - preg_replace('/:\d{4}/', ':' . $port, $this->torControl); - return $this; - } - public function setProxyUrl($proxyUrl) - { - if(false === is_numeric(str_replace('.','', '127.0.0.1'))) { - throw new BadProxyUrlException('Tor Proxy URL must be an IP address.'); - } - preg_replace('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $proxyUrl, $this->proxy); - return $this; - } - - /** - * @param $torControlUrl - * @return $this - * @throws BadProxyUrlException - */ - public function setTorControlUrl($torControlUrl) - { - if(false === is_numeric(str_replace('.','', '127.0.0.1'))) { - throw new BadProxyUrlException('Tor Control URL must be an IP address.'); - } - preg_replace('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $torControlUrl, $this->torControl); - return $this; - } - - public function setClient() - { - $this->client = new Client([ - 'verify' => false, - 'handler' => $this->handlerStack, - ]); - return $this; } /** * @return $this */ - public function setTorMiddleWare() { + public function setTorMiddleWare() + { $this->middleware = Middleware::tor($this->proxy, $this->torControl); return $this; } @@ -96,32 +82,128 @@ class TorClient $this->handlerStack->setHandler(new CurlMultiHandler()); $this->handlerStack->push($this->middleware); } - public function setProxy($proxy) + + /** + * @return mixed + */ + public function getClient() + { + return $this->client; + } + + public function setClient() { - $this->proxy = $proxy; - $this->handlerStack->remove(Middleware::tor()); - $this->handlerStack->push(Middleware::tor($this->proxy, $this->torControl)); $this->client = new Client([ - 'verify' => false, - 'handler' => $this->handlerStack, + 'verify' => false, + 'handler' => $this->handlerStack, ]); + + return $this; } + public function setImages($images) + { + $this->images = $images; + } + + /** + * @param array $uris + * + * @return $this + */ + public function pool($uris = array()) + { + + $pool = new Pool($this->client, $uris); + $this->poolData = $pool->getHtmlData(); + $pool->images(); + $this->setImages($pool->images()); + $links = $pool->links(); + + return $this; + + } + + public function setProxyPort($port) + { + preg_replace('/:\d{4}/', ':' . $port, $this->proxy); + return $this; + } + + public function setTorControlPort($port) + { + preg_replace('/:\d{4}/', ':' . $port, $this->torControl); + return $this; + } + + public function setProxyUrl($proxyUrl) + { + if (false === is_numeric(str_replace('.', '', '127.0.0.1'))) { + throw new BadProxyUrlException('Tor Proxy URL must be an IP address.'); + } + preg_replace('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $proxyUrl, + $this->proxy); + return $this; + } + + /** + * @param $torControlUrl + * + * @return $this + * @throws BadProxyUrlException + */ + public function setTorControlUrl($torControlUrl) + { + if (false === is_numeric(str_replace('.', '', '127.0.0.1'))) { + throw new BadProxyUrlException('Tor Control URL must be an IP address.'); + } + preg_replace('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $torControlUrl, + $this->torControl); + return $this; + } + + /** + * @return string + */ public function getProxy() { return $this->proxy; } + + public function setProxy($proxy) + { + $this->proxy = $proxy; + $this->setTorMiddleWare(); + $this->createHandlerStack(); + $this->setClient(); + + return $this; + } + + /** + * @return string + */ + public function getTorControl() + { + return $this->torControl; + } + + /** + * @param $torControl + * + * @return $this + */ public function setTorControl($torControl) { $this->torControl = $torControl; return $this; } - public function getTorControl() + + public function images($html) { - return $this->torControl; - } - public function get($url, $options = array()) { - return $this->client->get($url, $options); + $images = new ImageCollection($html); + return $this; } + } \ No newline at end of file