From b2d4d60f7651c8c4769018abf1bb00ab17491e51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Sat, 5 Oct 2024 10:13:40 +0900 Subject: [PATCH] cfmgrv4 init...1 --- .../MySocket/Cloudflare/CloudflareClient.php | 54 +++++++++++++ .../MySocket/Cloudflare/RecordSocket.php | 10 +-- .../MySocket/Cloudflare/ZoneSocket.php | 10 +-- app/Libraries/MySocket/CloudflareSocket.php | 75 +++++-------------- 4 files changed, 84 insertions(+), 65 deletions(-) create mode 100644 app/Libraries/MySocket/Cloudflare/CloudflareClient.php diff --git a/app/Libraries/MySocket/Cloudflare/CloudflareClient.php b/app/Libraries/MySocket/Cloudflare/CloudflareClient.php new file mode 100644 index 0000000..f14409b --- /dev/null +++ b/app/Libraries/MySocket/Cloudflare/CloudflareClient.php @@ -0,0 +1,54 @@ +request('GET', $uri, $options); + } + public function post($uri, array $options = []): ResponseInterface + { + return $this->request('POST', $uri, $options); + } + public function put($uri, array $options = []): ResponseInterface + { + return $this->request('PUT', $uri, $options); + } + public function patch($uri, array $options = []): ResponseInterface + { + return $this->request('PATCH', $uri, $options); + } + public function delete($uri, array $options = []): ResponseInterface + { + return $this->request('DELETE', $uri, $options); + } + + public function request(string $method, $uri = '', array $options = []): ResponseInterface + { + if (self::$_request >= self::$_request_max) { + log_message('warning', sprintf("--Cloudflare API Call %s초 대기 시작--", self::$_request_timewait)); + sleep(intval(self::$_request_timewait)); + self::$_request = 0; + log_message('warning', sprintf("--Cloudflare API Call %s초 대기 종료--", self::$_request_timewait)); + } + self::$_request++; + $requestOptions = []; + $requestOptions[in_array($method, ['get', 'getAsync']) ? 'query' : 'json'] = $options; + return parent::request($method, $uri, $requestOptions); + } +} diff --git a/app/Libraries/MySocket/Cloudflare/RecordSocket.php b/app/Libraries/MySocket/Cloudflare/RecordSocket.php index 3882296..4e80830 100644 --- a/app/Libraries/MySocket/Cloudflare/RecordSocket.php +++ b/app/Libraries/MySocket/Cloudflare/RecordSocket.php @@ -59,7 +59,7 @@ class RecordSocket extends CloudflareSocket 'content' => $content, 'proxied' => $proxied === 'on' ? true : false ]; - $cf = $this->getRequest('post', "zones/{$this->_zone_entity->getPK()}/dns_records", $datas); + $cf = $this->getClient()->post("zones/{$this->_zone_entity->getPK()}/dns_records", $datas); $cf = json_decode($cf->getBody()); if (!$cf->success) { $message = "Record:" . __FUNCTION__ . "에서 실패:\nrequest:" . var_export($datas, true) . "\nresponse:" . var_export($cf, true); @@ -91,7 +91,7 @@ class RecordSocket extends CloudflareSocket } // 인코딩된 JSON을 확인 // throw new \Exception("Record:" . __FUNCTION__ . "\n" . json_encode($datas, JSON_PRETTY_PRINT) . "\n" . var_export($datas, true)); - $cf = $this->getRequest('put', "zones/{$this->_zone_entity->getPK()}/dns_records/{$entity->getPK()}", $datas); + $cf = $this->getClient()->put("zones/{$this->_zone_entity->getPK()}/dns_records/{$entity->getPK()}", $datas); $cf = json_decode($cf->getBody()); if (!$cf->success) { $message = "Record:" . __FUNCTION__ . "에서 실패:\nrequest:" . var_export($datas, true) . "\nresponse:" . var_export($cf, true); @@ -104,7 +104,7 @@ class RecordSocket extends CloudflareSocket } public function delete(RecordEntity $entity): void { - $cf = $this->getRequest('delete', "zones/{$this->_zone_entity->getPK()}/dns_records/{$entity->getPK()}"); + $cf = $this->getClient()->delete("zones/{$this->_zone_entity->getPK()}/dns_records/{$entity->getPK()}"); $cf = json_decode($cf->getBody()); if (!$cf->success) { $message = "Record:" . __FUNCTION__ . "에서 실패:\nresponse:" . var_export($cf, true); @@ -118,7 +118,7 @@ class RecordSocket extends CloudflareSocket public function sync(RecordEntity $entity): void { // 기존 Sync형태 - $cf = $this->getRequest('get', "zones/{$this->_zone_entity->getPK()}/dns_records/{$entity->getPK()}"); + $cf = $this->getClient()->get("zones/{$this->_zone_entity->getPK()}/dns_records/{$entity->getPK()}"); $cf = json_decode($cf->getBody()); if (!$cf->success) { $message = "Record:" . __FUNCTION__ . "에서 실패:\nresponse:" . var_export($cf, true); @@ -129,7 +129,7 @@ class RecordSocket extends CloudflareSocket log_message("debug", var_export($cf->result, true)); $entity = $this->getModel()->modify($entity, $this->getArrayByResult($cf->result)); //Async형태 - // $promise = $this->getRequest('getAsync', "zones/{$this->_zone_entity->getPK()}/dns_records/{$entity->getPK()}"); + // $promise = $this->getClient()->getAsync("zones/{$this->_zone_entity->getPK()}/dns_records/{$entity->getPK()}"); // $promise->then( // onFulfilled: function ($response) use ($entity): RecordEntity { // $record = json_decode($response->getBody(), true)['result']; diff --git a/app/Libraries/MySocket/Cloudflare/ZoneSocket.php b/app/Libraries/MySocket/Cloudflare/ZoneSocket.php index 1acd627..744c7f4 100644 --- a/app/Libraries/MySocket/Cloudflare/ZoneSocket.php +++ b/app/Libraries/MySocket/Cloudflare/ZoneSocket.php @@ -60,7 +60,7 @@ class ZoneSocket extends CloudflareSocket //Cfzone에서 가져온 값을 zone에 setting private function getCFSetting(ZoneEntity $entity): array { - $cf = $this->getRequest('get', 'zones/' . $entity->getPK() . '/settings'); + $cf = $this->getClient()->get('zones/' . $entity->getPK() . '/settings'); $cf = json_decode($cf->getBody()); if (!$cf->success) { $message = "Zone:" . __FUNCTION__ . "에서 실패:\nresponse:" . var_export($cf, true); @@ -78,7 +78,7 @@ class ZoneSocket extends CloudflareSocket private function setCFSetting(ZoneEntity $entity, string $field, string $value): string { $datas = ['value' => $value]; - $cf = $this->getRequest('patch', 'zones/' . $entity->getPK() . '/settings/' . $field, $datas); + $cf = $this->getClient()->patch('zones/' . $entity->getPK() . '/settings/' . $field, $datas); $cf = json_decode($cf->getBody()); if (!$cf->success || $cf->result->id !== $field) { $message = "Zone:" . __FUNCTION__ . "에서 실패:\nrequest:" . var_export($datas, true) . "\nresponse:" . var_export($cf, true); @@ -97,7 +97,7 @@ class ZoneSocket extends CloudflareSocket 'name' => $domain, 'jump_start' => $jump_start, ]; - $cf = $this->getRequest('post', 'zones/', $datas); + $cf = $this->getClient()->post('zones/', $datas); $cf = json_decode($cf->getBody()); if (!$cf->success) { $message = "Zone:" . __FUNCTION__ . "에서 실패:\nrequest:" . var_export($datas, true) . "\nresponse:" . var_export($cf, true); @@ -128,7 +128,7 @@ class ZoneSocket extends CloudflareSocket } public function delete(ZoneEntity $entity): void { - $cf = $this->getRequest('delete', "zones/{$entity->getPK()}"); + $cf = $this->getClient()->delete("zones/{$entity->getPK()}"); $cf = json_decode($cf->getBody()); if (!$cf->success) { $message = "Zone:" . __FUNCTION__ . "에서 실패:\nresponse:" . var_export($cf, true); @@ -141,7 +141,7 @@ class ZoneSocket extends CloudflareSocket } public function sync(ZoneEntity $entity): void { - $cf = $this->getRequest('get', "zones/{$entity->getPK()}"); + $cf = $this->getClient()->get("zones/{$entity->getPK()}"); $cf = json_decode($cf->getBody()); if (!$cf->success) { $message = "Zone:" . __FUNCTION__ . "에서 실패:\nresponse:" . var_export($cf, true); diff --git a/app/Libraries/MySocket/CloudflareSocket.php b/app/Libraries/MySocket/CloudflareSocket.php index 60a6740..f466926 100644 --- a/app/Libraries/MySocket/CloudflareSocket.php +++ b/app/Libraries/MySocket/CloudflareSocket.php @@ -2,22 +2,14 @@ namespace App\Libraries\MySocket; - -use App\Entities\Cloudflare\AuthEntity; use App\Libraries\CommonLibrary; -use App\Models\Cloudflare\AccountModel; +use App\Libraries\MySocket\Cloudflare\CloudflareClient; use App\Models\Cloudflare\AuthModel; -use Cloudflare\API\Adapter\ResponseException; -use GuzzleHttp\Client; -use GuzzleHttp\Exception\RequestException; +use App\Models\Cloudflare\AccountModel; +use App\Entities\Cloudflare\AuthEntity; abstract class CloudflareSocket extends CommonLibrary { - private static int $_request = 0; - private static int $_request_max = 1000; - private static int $_request_perpage_max = 700; - private static int $_request_timewait = 60; - private $_authModel = null; private $_accountModel = null; private $_client = null; @@ -26,16 +18,13 @@ abstract class CloudflareSocket extends CommonLibrary { parent::__construct(); $this->_auth_entity = $auth_entity; - self::$_request_max = getenv("cfmgr.request.max") ?: 1000; - self::$_request_perpage_max = getenv("cfmgr.request.perpage.max") ?: 700; - self::$_request_timewait = getenv("cfmgr.request.timewait") ?: 60; } abstract protected function getArrayByResult($result, array $formDatas = []): array; - private function getClient(): Client + final protected function getClient(): CloudflareClient { if ($this->_client === null) { // Guzzle HTTP 클라이언트를 설정하면서 Cloudflare API 토큰 사용 - $this->_client = new Client([ + $this->_client = new CloudflareClient([ 'base_uri' => 'https://api.cloudflare.com/client/v4/', 'headers' => [ 'X-Auth-Email' => $this->_auth_entity->getID(), // 인증 토큰 사용 @@ -61,53 +50,29 @@ abstract class CloudflareSocket extends CommonLibrary } return $this->_accountModel; } - final protected function getRequest(string $method, string $uri, array $datas = []) - { - if (!in_array($method, ['get', 'getAsync', 'post', 'put', 'patch', 'delete'])) { - throw new \InvalidArgumentException('Request method must be get, post, put, patch, or delete'); - } - try { - $response = $this->getClient()->$method($uri, [ - in_array($method, ['get', 'getAsync']) ? 'query' : 'json' => $datas, - ]); - if (self::$_request >= self::$_request_max) { - log_message('warning', sprintf("--Cloudflare API Call %s초 대기 시작--", self::$_request_timewait)); - sleep(intval(self::$_request_timewait)); - self::$_request = 0; - log_message('warning', sprintf("--Cloudflare API Call %s초 대기 종료--", self::$_request_timewait)); - } - self::$_request++; - } catch (RequestException $err) { - throw ResponseException::fromRequestException($err); - } - return $response; - } - final protected function reload_procedure($uri): array { - $page = 1; //Page는 1부터 시작해야함 - $perpage_max = self::$_request_perpage_max; - $results = []; + $page = 1; //1부터 시작 + $results = []; do { $query = [ - 'page' => $page, - 'per_page' => $perpage_max, - 'match' => 'all', + 'page' => $page, + 'per_page' => CloudflareClient::REQUEST_PERPAGE_MAX, + 'match' => 'all', ]; - $cf = $this->getClient()->get($uri, $query); - $cf = json_decode($cf->getBody()); + $response = $this->getClient()->get($uri, $query); + $cf = json_decode($response->getBody()); if (!$cf->success) { - throw new \Exception(__FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); + $message = __FUNCTION__ . "에서 실패:\nresponse:" . var_export($cf, true); + log_message("error", $message); + throw new \Exception($message); } - //Loop 제한 : 한페이지에서 가져온 갯수가 perpage_max보다 적다는것은 더이상 다음페이지기 없으므로 0로 종료시키기 위함 - if (count($cf->result) < $perpage_max) { - $results = $cf->result; - $page = 0; - } else { - $results = array_merge($results, $cf->result); - $page++; + $results = array_merge($results, $cf->result); + if (count($cf->result) < CloudflareClient::REQUEST_PERPAGE_MAX) { + break; } - } while (0 < $page); + $page++; + } while (true); return $results; } }