Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Sh committed Apr 16, 2024
1 parent 77f05e2 commit 69a0b89
Show file tree
Hide file tree
Showing 15 changed files with 893 additions and 124 deletions.
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ WORKDIR /var/www/html
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
php composer-setup.php \
php -r "unlink('composer-setup.php');"

COPY composer.phar /usr/local/bin/composer
php -r "unlink('composer-setup.php');" \
mv composer.phar /usr/local/bin/composer

CMD [ "php", "./keep-running.php" ]
10 changes: 10 additions & 0 deletions TestRotateProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use Proxyrequest\ProxyRequestRotate;

require 'vendor/autoload.php';

$proxyRequestRotate = new ProxyRequestRotate('http://proxyrequest.ru','i_am_a_test_token_i_can_intentionally_parse_only_proxyrequest_ru');

//echo $proxyRequestRotate->getProxyResponse();
echo $proxyRequestRotate->sendRequest();
8 changes: 0 additions & 8 deletions exampleRotateProxy.php

This file was deleted.

2 changes: 1 addition & 1 deletion keep-running.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

sleep(1000000);
sleep(getrandmax());
12 changes: 12 additions & 0 deletions src/Conract/ProxyRequestInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php


namespace Proxyrequest\Conract;

interface ProxyRequestInterface
{
public function sendRequest();



}
161 changes: 49 additions & 112 deletions src/ProxyRequestRotate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,36 @@

namespace Proxyrequest;

class ProxyRequestRotate
use Proxyrequest\Conract\ProxyRequestInterface;
use Proxyrequest\Response\ProxyResponse;

class ProxyRequestRotate implements ProxyRequestInterface
{
const SERVER_PUBLIC = 'http://public.proxyrequest.ru';

const TOKEN_PUBLIC = 'i_am_a_test_token_i_can_intentionally_parse_only_proxyrequest_ru';


const FORMAT_JSON = 'json',
FORMAT_TXT = 'txt';

private $format;

/**
* @var mixed|string
*/
private $server;

/**
* @var string
*/
private $urlToGet;

/**
* @var string
*/
private $token;

private $messageErrorLast;


Expand All @@ -24,30 +42,14 @@ class ProxyRequestRotate
private $isMobileOnlyUserAgent;

/**
*
* @var string
* @var array|mixed
*/
private $cookies;

/**
*
* @var string
*/
private $referer;
private $statusCode = null;

/**
*
* Returned in response from server
*
* @var string
*/
private $userAgentUsed;
private $cookiesUsed;
private $refererUsed;

private $responseCookies;
private $responseHeaders;

/**
*
Expand All @@ -57,12 +59,12 @@ class ProxyRequestRotate
* @param [] $cookies
* @param string $referer
*/
public function __construct($urlToGet, $token,
public function __construct($urlToGet, $token = self::TOKEN_PUBLIC,
$isMobileOnlyUserAgent = '', $cookies = [],
$referer = '')
$referer = '', $server = '')
{

$this->server = 'http://public.proxyrequest.ru';;
$this->server = $server ?: self::SERVER_PUBLIC;
$this->urlToGet = $urlToGet;
$this->token = $token;
$this->isMobileOnlyUserAgent = $isMobileOnlyUserAgent;
Expand All @@ -76,69 +78,6 @@ public function setServer($url)
return $this;
}

public function getStatusCode()
{
return $this->statusCode;
}

public function getMessageErrorLast()
{
return $this->messageErrorLast;
}

public function getUserAgentUsed()
{
return $this->userAgentUsed;
}

public function getCookiesUsed()
{
return $this->cookiesUsed;
}

public function getRefererUsed()
{
return $this->refererUsed;
}

/**
* @return array
*/
public function getResponseCookies()
{
return $this->responseCookies;
}

/**
* @return array
*/
public function getResponseHeaders()
{
return $this->responseHeaders;
}


public function getUrlFinal()
{
$urlEncoded = base64_encode($this->urlToGet);

$server = $this->server;

$token = $this->token;

$params = http_build_query([
'token' => $token,
'urlToGet' => $urlEncoded,
'isMobileOnlyUserAgent' => $this->isMobileOnlyUserAgent,
'cookies' => $this->cookies,
'referer' => $this->referer
]);

$urlFinal = "$server/api/forwardRequestInParallelV2?$params";


return $urlFinal;
}

/**
* Returns false if request failed,
Expand All @@ -148,62 +87,60 @@ public function getUrlFinal()
* @staticvar int $timesTried
* @return boolean|string
*/
public function getContent()
public function sendRequest()
{
return '111';

static $timesTried = 0;

$urlFinal = $this->getUrlFinal();
if ($timesTried > 10) {
return new ProxyResponse([]);
}

$urlFinal = $this->getUrlFinal();

try {
$dataInJson = file_get_contents($urlFinal);

} catch (\Exception $e) {

if ($timesTried < 2) {
$timesTried++;
return $this->getContent();
return $this->sendRequest();
}

$timesTried = 0;

return false;
}

$proxyResponse = new ProxyResponse($dataInJson);

$response = json_decode($dataInJson, true);


$this->messageErrorLast = $response['message'];

$value = $response['value'];

/**
* что-то не так на нашей стороне
*/
if (!array_key_exists('statusCode', $value)) {
return false;
if (!$proxyResponse->success) {
return $this->sendRequest();
}

return $proxyResponse;
}

if (!$response['success']) {
return $this->getContent();
}

$statusCode = $value['statusCode'];
private function getUrlFinal()
{
$urlEncoded = base64_encode($this->urlToGet);

$server = $this->server;

$this->userAgentUsed = $value['userAgentUsed'];
$this->statusCode = $statusCode;
$this->refererUsed = $value['refererUsed'];
$this->cookiesUsed = $value['cookiesUsed'];
$token = $this->token;

$this->responseHeaders = $value['headers'];
$params = http_build_query([
'token' => $token,
'urlToGet' => $urlEncoded,
'isMobileOnlyUserAgent' => $this->isMobileOnlyUserAgent,
'cookies' => $this->cookies,
'referer' => $this->referer
]);

$contentInBase64 = $value['content'];
$urlFinal = "$server/api/rotate/$token?$params";

return base64_decode($contentInBase64);
return $urlFinal;
}

}
93 changes: 93 additions & 0 deletions src/Response/ProxyResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Proxyrequest\Response;

class ProxyResponse
{
/**
* @var int
*/
public $success;

/**
* @var string
*/
public $content;

/**
* @var string
*/
public $url;

/**
* @var string
*/
public $content_type;

/**
* @var int
*/
public $http_code;

/**
* @var int
*/
public $request_size;

/**
* @var int
*/
public $size_download;

/**
* @var int
*/
public $download_content_length;

/**
* @var string
*/
public $effective_method;

/**
* @var string
*/
public $request_header;

/**
* @var string
*/
public $scheme;

/**
* @var string
*/
public $primary_ip;


public $keys = [];

public function __construct($dataInJson)
{
$this->success = 0;

$response = json_decode($dataInJson, true);

foreach ($response as $key => $val) {
$this->{$key} = $val;
$this->keys[] = $key;
}
}

public function __toString()
{
return json_encode($this->getValue());
}

public function getValue()
{
return array_map(function ($key) {
return [$key => $this->{$key}];
}, $this->keys);
}
}
Loading

0 comments on commit 69a0b89

Please sign in to comment.