Added TorClient
new file: src/Sikofitt/Tor/Exception/BadProxyUrlException.php new file: src/Sikofitt/Tor/TorClient.php
This commit is contained in:
parent
a41fc59c10
commit
5772d9f173
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: sikof
|
||||
* Date: 12/23/2016
|
||||
* Time: 11:43 AM
|
||||
*/
|
||||
|
||||
namespace Sikofitt\Tor\Exception;
|
||||
|
||||
|
||||
class BadProxyUrlException extends \InvalidArgumentException {}
|
|
@ -0,0 +1,119 @@
|
|||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: sikof
|
||||
* Date: 12/23/2016
|
||||
* Time: 11:09 AM
|
||||
*/
|
||||
|
||||
namespace Sikofitt\Tor;
|
||||
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Handler\CurlMultiHandler;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use GuzzleTor\Middleware;
|
||||
use Sikofitt\Tor\Exception\BadProxyUrlException;
|
||||
|
||||
class TorClient
|
||||
{
|
||||
private $client;
|
||||
private $proxy;
|
||||
private $torControl;
|
||||
private $handlerStack;
|
||||
private $middleware;
|
||||
|
||||
public function __construct($proxy = '127.0.0.1:9050', $torControl = '127.0.0.1:9051')
|
||||
{
|
||||
$this->proxy = $proxy;
|
||||
$this->torControl = $torControl;
|
||||
|
||||
$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;
|
||||
}
|
||||
public function setTorMiddleWare() {
|
||||
$this->middleware = Middleware::tor($this->proxy, $this->torControl);
|
||||
return $this;
|
||||
}
|
||||
public function createHandlerStack()
|
||||
{
|
||||
$this->handlerStack = new HandlerStack();
|
||||
$this->handlerStack->setHandler(new CurlMultiHandler());
|
||||
$this->handlerStack->push($this->middleware);
|
||||
}
|
||||
public function setProxy($proxy)
|
||||
{
|
||||
$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,
|
||||
]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getProxy()
|
||||
{
|
||||
return $this->proxy;
|
||||
}
|
||||
public function setTorControl($torControl)
|
||||
{
|
||||
$this->torControl = $torControl;
|
||||
return $this;
|
||||
}
|
||||
public function getTorControl()
|
||||
{
|
||||
return $this->torControl;
|
||||
}
|
||||
public function get($url, $options = array()) {
|
||||
return $this->client->get($url, $options);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue