From f051e0ae11e470d95276b0e558cd99f6f4a55302 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Fri, 11 Oct 2024 23:55:05 +0900 Subject: [PATCH] cfmgrv4 init...2 --- app/Libraries/Cloudflare/Cloudflare.php | 2 +- app/Libraries/MySocket/CloudflareSocket.php | 35 ++++++++---- app/Libraries/MySocket/GoogleSocket/CURL.php | 2 +- .../MySocket/GoogleSocket/GoogleSocket.php | 1 - app/Libraries/MySocket/MySocket.php | 57 +++++++++++-------- app/Libraries/MySocket/WebSocket.php | 11 +++- 6 files changed, 68 insertions(+), 40 deletions(-) diff --git a/app/Libraries/Cloudflare/Cloudflare.php b/app/Libraries/Cloudflare/Cloudflare.php index dfd2d45..5a63532 100644 --- a/app/Libraries/Cloudflare/Cloudflare.php +++ b/app/Libraries/Cloudflare/Cloudflare.php @@ -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 diff --git a/app/Libraries/MySocket/CloudflareSocket.php b/app/Libraries/MySocket/CloudflareSocket.php index bc040b1..40a6bb0 100644 --- a/app/Libraries/MySocket/CloudflareSocket.php +++ b/app/Libraries/MySocket/CloudflareSocket.php @@ -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); } } diff --git a/app/Libraries/MySocket/GoogleSocket/CURL.php b/app/Libraries/MySocket/GoogleSocket/CURL.php index 7f6c91b..2252668 100644 --- a/app/Libraries/MySocket/GoogleSocket/CURL.php +++ b/app/Libraries/MySocket/GoogleSocket/CURL.php @@ -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 { diff --git a/app/Libraries/MySocket/GoogleSocket/GoogleSocket.php b/app/Libraries/MySocket/GoogleSocket/GoogleSocket.php index 7aa1887..822db7a 100644 --- a/app/Libraries/MySocket/GoogleSocket/GoogleSocket.php +++ b/app/Libraries/MySocket/GoogleSocket/GoogleSocket.php @@ -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; diff --git a/app/Libraries/MySocket/MySocket.php b/app/Libraries/MySocket/MySocket.php index bbf8457..7180d71 100644 --- a/app/Libraries/MySocket/MySocket.php +++ b/app/Libraries/MySocket/MySocket.php @@ -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); } } diff --git a/app/Libraries/MySocket/WebSocket.php b/app/Libraries/MySocket/WebSocket.php index 76dcf11..fbe94ad 100644 --- a/app/Libraries/MySocket/WebSocket.php +++ b/app/Libraries/MySocket/WebSocket.php @@ -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} 접속실패: " .