diff --git a/src/Sikofitt/Tor/Exception/BadProxyUrlException.php b/src/Sikofitt/Tor/Exception/BadProxyUrlException.php new file mode 100644 index 0000000..269f7cb --- /dev/null +++ b/src/Sikofitt/Tor/Exception/BadProxyUrlException.php @@ -0,0 +1,12 @@ +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); + } +} \ No newline at end of file