cfmgrv4 init...2

This commit is contained in:
최준흠 2024-10-11 23:55:05 +09:00
parent 6afa33aefe
commit f051e0ae11
6 changed files with 68 additions and 40 deletions

View File

@ -68,7 +68,7 @@ abstract class Cloudflare extends CommonLibrary
log_message("error", $message);
throw new \Exception($message);
}
// log_message("debug", "Page {$page} response: " . var_export($cf->result_info, true));
log_message("debug", "Page {$page} response: " . var_export($cf->result_info, true));
return $cf;
}
final protected function reload_procedure(string $uri): array

View File

@ -3,26 +3,39 @@
namespace App\Libraries\MySocket;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Client;
use App\Entities\Cloudflare\AuthEntity;
class CloudflareSocket extends MySocket
{
private ?Client $_client = null;
public static int $_request = 0;
private static int $_request_max = 1000;
private static int $_request_timewait = 60;
private static int $_request_timewait = 30;
private ?AuthEntity $_auth_entity = null;
public function __construct(AuthEntity $auth_entity)
{
parent::__construct([
'base_uri' => 'https://api.cloudflare.com/client/v4/',
'headers' => [
'X-Auth-Email' => $auth_entity->getID(),
'X-Auth-Key' => $auth_entity->getAuthKey(),
'Content-Type' => 'application/json'
]
]);
parent::__construct();
$this->_auth_entity = $auth_entity;
}
public function getClient(): Client
{
if ($this->_client === null) {
$this->_client = new Client([
'base_uri' => 'https://api.cloudflare.com/client/v4/',
'headers' => [
'X-Auth-Email' => $this->_auth_entity->getID(),
'X-Auth-Key' => $this->_auth_entity->getAuthKey(),
'Accept' => 'application/json'
],
'verify' => env("socket.web.ssl.verify") ?? false, // SSL 인증서 검증을 비활성화
'User-Agent' => $this->getUserAgent()
]);
}
return $this->_client;
}
//override
public function request(string $method, $uri = '', array $options = []): ResponseInterface
public function request(string $method, $uri = '', array $options = [], array $headers = []): ResponseInterface
{
if (self::$_request >= self::$_request_max) {
log_message('warning', sprintf("--Cloudflare API Call %s초 대기 시작--", self::$_request_timewait));
@ -32,6 +45,6 @@ class CloudflareSocket extends MySocket
}
self::$_request++;
// log_message("debug", "현재 request:[" . self::$_request . "]");
return parent::request($method, $uri, $options);
return parent::request($method, $uri, $options, $headers);
}
}

View File

@ -2,8 +2,8 @@
namespace App\Libraries\MySocket\GoogleSocket;
use GuzzleHttp\Client;
use App\Entities\UserSNSEntity;
use App\Libraries\MySocket\MySocket as Client;
class CURL extends GoogleSocket
{

View File

@ -20,7 +20,6 @@ abstract class GoogleSocket extends MySocket
{
$this->session = Services::session();
}
abstract public function getClient(): mixed;
abstract public function createAuthUrl(): string;
abstract public function setToken(string $access_code): void;
abstract public function getUserSNSEntity(): UserSNSEntity;

View File

@ -3,19 +3,18 @@
namespace App\Libraries\MySocket;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Client;
use Cloudflare\API\Adapter\ResponseException;
class MySocket extends Client
abstract class MySocket
{
private $_cookieJar = null;
public function __construct(array $config = [])
protected function __construct()
{
// SSL 인증서 검증을 비활성화
$config['verify'] = env("socket.web.ssl.verify") ?? false;
$config['User-Agent'] = $config['User-Agent'] ?? $this->getUserAgent();
parent::__construct($config);
}
abstract public function getClient(): mixed;
final protected function getCookieJar(): CookieJar
{
if ($this->_cookieJar === null) {
@ -35,45 +34,53 @@ class MySocket extends Client
];
return $userAgents[array_rand($userAgents)];
}
protected function getRequestOptions(string $method, array $options = []): array
protected function getRequestOptions(string $method, array $options = [], array $headers = []): array
{
//cookies->쿠키값 , timeout->5초 안에 응답이 없으면 타임아웃
//method가 get이면 $request['query'] = $options , 다른것이면 $request['json] = $options
$options = [
// 'cookies' => $this->getCookieJar(),
'timeout' => env("socket.web.timeout") ?? 5,
'headers' => $headers,
in_array($method, ['get']) ? 'query' : 'json' => $options
];
return $options;
}
final public function get($uri, array $options = []): ResponseInterface
public function request(string $method, $uri = '', array $options = [], array $headers = []): ResponseInterface
{
return $this->request('GET', $uri, $options);
if (!in_array($method, ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'])) {
throw new \InvalidArgumentException("{$method} => Request method must be GET, POST, PUT, PATCH, or DELETE");
}
try {
$options = $this->getRequestOptions($method, $options, $headers);
log_message("debug", __FUNCTION__ .
"=> 호출 Socket URL:{$uri}\n--------------\n" .
var_export($options, true) .
"\n--------------\n");
$response = $this->getClient()->$method($uri, $options);
} catch (RequestException $err) {
throw ResponseException::fromRequestException($err);
}
return $response;
}
final public function post($uri, array $options = []): ResponseInterface
final public function get($uri, array $options = [], array $headers = []): ResponseInterface
{
return $this->request('POST', $uri, $options);
return $this->request('GET', $uri, $options, $headers);
}
final public function put($uri, array $options = []): ResponseInterface
final public function post($uri, array $options = [], array $headers = []): ResponseInterface
{
return $this->request('PUT', $uri, $options);
return $this->request('POST', $uri, $options, $headers);
}
final public function patch($uri, array $options = []): ResponseInterface
final public function put($uri, array $options = [], array $headers = []): ResponseInterface
{
return $this->request('PATCH', $uri, $options);
return $this->request('PUT', $uri, $options, $headers);
}
final public function delete($uri, array $options = []): ResponseInterface
final public function patch($uri, array $options = [], array $headers = []): ResponseInterface
{
return $this->request('DELETE', $uri, $options);
return $this->request('PATCH', $uri, $options, $headers);
}
public function request(string $method, $uri = '', array $options = []): ResponseInterface
public function delete($uri, array $options = [], array $headers = []): ResponseInterface
{
$options = $this->getRequestOptions($method, $options);
log_message("debug", __FUNCTION__ .
"=> 호출 Socket URL:{$uri}\n--------------\n" .
var_export($options, true) .
"\n--------------\n");
return parent::request($method, $uri, $options);
return $this->request('DELETE', $uri, $options, $headers);
}
}

View File

@ -3,15 +3,24 @@
namespace App\Libraries\MySocket;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Client;
class WebSocket extends MySocket
{
private ?Client $_client = null;
private $_host = null;
public function __construct(string $host)
{
parent::__construct();
$this->_host = $host;
}
public function getClient(): Client
{
if ($this->_client === null) {
$this->_client = new Client();
}
return $this->_client;
}
final public function getURL($uri): string
{
// url에 http 나 https가 포함되어 있지않으면
@ -22,7 +31,7 @@ class WebSocket extends MySocket
}
public function getResponse($uri, array $options = []): ResponseInterface
{
$response = parent::get($this->getURL($uri), $options);
$response = $this->get($this->getURL($uri), $options);
if ($response->getStatusCode() != 200) {
throw new \Exception("error", __FUNCTION__ .
"=> {$uri} 접속실패: " .