From 4b914ccb219ec5f21cd37b56812d564270fef9e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Sat, 12 Oct 2024 09:38:32 +0900 Subject: [PATCH] cfmgrv4 init...2 --- app/Controllers/CLI/Cloudflare.php | 2 -- app/Libraries/Cloudflare/Cloudflare.php | 19 ++++++------ app/Libraries/Cloudflare/Record.php | 32 ++++++++++----------- app/Libraries/Cloudflare/Zone.php | 12 ++++---- app/Libraries/MySocket/CloudflareSocket.php | 2 +- app/Libraries/MySocket/MySocket.php | 22 +++++++------- app/Models/CommonModel.php | 9 +++--- 7 files changed, 50 insertions(+), 48 deletions(-) diff --git a/app/Controllers/CLI/Cloudflare.php b/app/Controllers/CLI/Cloudflare.php index e4a5fc0..4d5a634 100644 --- a/app/Controllers/CLI/Cloudflare.php +++ b/app/Controllers/CLI/Cloudflare.php @@ -59,12 +59,10 @@ class Cloudflare extends BaseController foreach ($auths as $auth) { $accounts += $this->account_process($auth); } - log_message("debug", "\n-------------총 Accounts:" . count($accounts) . "--------------------\n"); $zones = []; foreach ($accounts as $key => $account) { $zones += $this->zone_process($account); } - log_message("debug", "\n-------------총 Zones:" . count($zones) . "--------------------\n"); foreach ($zones as $key => $zone) { $this->record_process($zone); } diff --git a/app/Libraries/Cloudflare/Cloudflare.php b/app/Libraries/Cloudflare/Cloudflare.php index 5a63532..e80e399 100644 --- a/app/Libraries/Cloudflare/Cloudflare.php +++ b/app/Libraries/Cloudflare/Cloudflare.php @@ -51,24 +51,25 @@ abstract class Cloudflare extends CommonLibrary private function reload_page(string $uri, int $page, int $per_page = 20): mixed { $query = [ - 'name' => '', - 'status' => '', - 'order' => 'name', - 'direction' => 'asc', 'page' => $page, 'per_page' => $per_page, 'match' => 'all', - 'cache_buster' => time(), // 캐시 무효화를 위한 파라미터 추가 + 'cache_buster' => uniqid(), // 매 요청마다 고유한 값 사용 + ]; + // 요청 헤더에 캐시 제어 추가 + $headers = [ + 'Cache-Control' => 'no-cache', + 'Pragma' => 'no-cache', ]; // log_message("debug", var_export($query, true)); - $response = $this->getMySocket()->get($uri, $query); + $response = $this->getMySocket()->get($uri, $query, $headers); $cf = json_decode($response->getBody()); if (!$cf->success) { $message = __FUNCTION__ . "에서 실패:\nresponse:" . var_export($cf, true); 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 @@ -79,7 +80,9 @@ abstract class Cloudflare extends CommonLibrary $per_page = $cf->result_info->per_page; $total_page = $cf->result_info->total_pages; $results = $cf->result; - for ($i = $page + 1; $i < $total_page; $i++) { + for ($i = $page + 1; $i <= $total_page; $i++) { + // API 제한을 고려한 지연 추가 + usleep(100000); // 0.1초 대기 $cf = $this->reload_page($uri, $i, $per_page); $results = array_merge($results, $cf->result); log_message("debug", "현재: page[{$i}/{$total_page}] , result수[" . count($results) . "]"); diff --git a/app/Libraries/Cloudflare/Record.php b/app/Libraries/Cloudflare/Record.php index 1522ab1..7cd47c8 100644 --- a/app/Libraries/Cloudflare/Record.php +++ b/app/Libraries/Cloudflare/Record.php @@ -12,7 +12,7 @@ class Record extends Cloudflare private $_zone_entity = null; public function __construct(ZoneEntity $zone_entity) { - $this->_zone_entity = $zone_entity; + $this->_zone_entity = $zone_entity; $account_entity = $this->getAccountModel()->getEntityByPK($this->_zone_entity->getParent()); if ($account_entity === null) { throw new \Exception("해당 계정정보를 찾을수 없습니다."); @@ -32,15 +32,15 @@ class Record extends Cloudflare } public function getArrayByResult($result, array $formDatas = []): array { - $formDatas[RecordModel::PK] = $result->id; + $formDatas[RecordModel::PK] = $result->id; $formDatas[RecordModel::PARENT] = $result->zone_id; - $formDatas[RecordModel::TITLE] = $result->name; - $formDatas['type'] = $result->type; - $formDatas['content'] = $result->content; - $formDatas['ttl'] = (int)$result->ttl; - $formDatas['proxiable'] = $result->proxiable ? "on" : "off"; - $formDatas['proxied'] = $result->proxied ? "on" : "off"; - $formDatas['locked'] = "on"; + $formDatas[RecordModel::TITLE] = $result->name; + $formDatas['type'] = $result->type; + $formDatas['content'] = $result->content; + $formDatas['ttl'] = (int) $result->ttl; + $formDatas['proxiable'] = $result->proxiable ? "on" : "off"; + $formDatas['proxied'] = $result->proxied ? "on" : "off"; + $formDatas['locked'] = "on"; if (isset($result->locked) && $result->locked) { $formDatas['locked'] = "off"; } @@ -53,8 +53,8 @@ class Record extends Cloudflare //Socket용 //호스트생성을 위해 Cloudflare에 전송 $datas = [ - 'name' => $host, - 'type' => $type, + 'name' => $host, + 'type' => $type, 'content' => $content, 'proxied' => $proxied === 'on' ? true : false ]; @@ -73,19 +73,19 @@ class Record extends Cloudflare { //TTL값은 CDN(proxied)가 사용함일때는 무조건 1, 않함일때는 120이 적용 $datas = [ - 'type' => isset($formDatas['type']) ? $formDatas['type'] : $entity->type, - 'name' => isset($formDatas['host']) ? $formDatas['host'] : $entity->host, + 'type' => isset($formDatas['type']) ? $formDatas['type'] : $entity->type, + 'name' => isset($formDatas['host']) ? $formDatas['host'] : $entity->host, 'content' => isset($formDatas['content']) ? $formDatas['content'] : $entity->content, 'proxied' => $entity->proxied == 'on' ? true : false, - 'ttl' => intval($entity->ttl), + 'ttl' => intval($entity->ttl), ]; if (isset($formDatas['proxied'])) { if ($formDatas['proxied'] === 'on') { $datas['proxied'] = true; - $datas['ttl'] = 1; + $datas['ttl'] = 1; } else { $datas['proxied'] = false; - $datas['ttl'] = 120; + $datas['ttl'] = 120; } } // 인코딩된 JSON을 확인 diff --git a/app/Libraries/Cloudflare/Zone.php b/app/Libraries/Cloudflare/Zone.php index 723f95c..b09609b 100644 --- a/app/Libraries/Cloudflare/Zone.php +++ b/app/Libraries/Cloudflare/Zone.php @@ -30,10 +30,10 @@ class Zone extends Cloudflare } protected function getArrayByResult($result, array $formDatas = []): array { - $formDatas[ZoneModel::PK] = $result->id; + $formDatas[ZoneModel::PK] = $result->id; $formDatas[ZoneModel::PARENT] = $result->account->id; - $formDatas[ZoneModel::TITLE] = $result->name; - $formDatas['status'] = $result->status; + $formDatas[ZoneModel::TITLE] = $result->name; + $formDatas['status'] = $result->status; //$formDatas['type'] = $result->type; // full 이게있는데 뭔지 잘모름 $formDatas['name_servers'] = 'none'; if (isset($result->name_servers)) { @@ -54,7 +54,7 @@ class Zone extends Cloudflare // $formDatas['updated_at'] = $result->modified_on; $formDatas['updated_at'] = date("Y-m-d H:i:s"); $formDatas['created_at'] = $result->created_on; - $formDatas['plan'] = $result->plan->name; + $formDatas['plan'] = $result->plan->name; return $formDatas; } //Cfzone에서 가져온 값을 zone에 setting @@ -92,8 +92,8 @@ class Zone extends Cloudflare //Socket용 //도메인생성을 위해 Cloudflare에 전송 $datas = [ - 'accountId' => $this->_account_entity->getPK(), - 'name' => $domain, + 'accountId' => $this->_account_entity->getPK(), + 'name' => $domain, 'jump_start' => $jump_start, ]; $cf = $this->getMySocket()->post('zones/', $datas); diff --git a/app/Libraries/MySocket/CloudflareSocket.php b/app/Libraries/MySocket/CloudflareSocket.php index 40a6bb0..41ba006 100644 --- a/app/Libraries/MySocket/CloudflareSocket.php +++ b/app/Libraries/MySocket/CloudflareSocket.php @@ -11,7 +11,7 @@ class CloudflareSocket extends MySocket private ?Client $_client = null; public static int $_request = 0; private static int $_request_max = 1000; - private static int $_request_timewait = 30; + private static int $_request_timewait = 10; private ?AuthEntity $_auth_entity = null; public function __construct(AuthEntity $auth_entity) { diff --git a/app/Libraries/MySocket/MySocket.php b/app/Libraries/MySocket/MySocket.php index 7180d71..62b49e7 100644 --- a/app/Libraries/MySocket/MySocket.php +++ b/app/Libraries/MySocket/MySocket.php @@ -48,15 +48,15 @@ abstract class MySocket } public function request(string $method, $uri = '', array $options = [], array $headers = []): ResponseInterface { - if (!in_array($method, ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'])) { - throw new \InvalidArgumentException("{$method} => Request method must be GET, POST, PUT, PATCH, or DELETE"); + 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"); + // 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); @@ -65,22 +65,22 @@ abstract class MySocket } final public function get($uri, array $options = [], array $headers = []): ResponseInterface { - return $this->request('GET', $uri, $options, $headers); + return $this->request(__FUNCTION__, $uri, $options, $headers); } final public function post($uri, array $options = [], array $headers = []): ResponseInterface { - return $this->request('POST', $uri, $options, $headers); + return $this->request(__FUNCTION__, $uri, $options, $headers); } final public function put($uri, array $options = [], array $headers = []): ResponseInterface { - return $this->request('PUT', $uri, $options, $headers); + return $this->request(__FUNCTION__, $uri, $options, $headers); } final public function patch($uri, array $options = [], array $headers = []): ResponseInterface { - return $this->request('PATCH', $uri, $options, $headers); + return $this->request(__FUNCTION__, $uri, $options, $headers); } public function delete($uri, array $options = [], array $headers = []): ResponseInterface { - return $this->request('DELETE', $uri, $options, $headers); + return $this->request(__FUNCTION__, $uri, $options, $headers); } } diff --git a/app/Models/CommonModel.php b/app/Models/CommonModel.php index 5e8e9d1..ac2f277 100644 --- a/app/Models/CommonModel.php +++ b/app/Models/CommonModel.php @@ -75,7 +75,8 @@ abstract class CommonModel extends Model $rule .= $action == "create" ? "|is_unique[{$this->table}.{$field}]" : ""; } else { $rule = "required|numeric"; - }; + } + ; break; case $this->getTitleField(): $rule = "required|string"; @@ -132,7 +133,7 @@ abstract class CommonModel extends Model //$formDatas에 전달된 값이 없는경우 if (!array_key_exists($field, $formDatas)) { $randomBytes = bin2hex(random_bytes(32)); - $value = sprintf( + $value = sprintf( '%08s-%04s-%04x-%04x-%12s', substr($randomBytes, 0, 8), substr($randomBytes, 8, 4), @@ -166,7 +167,7 @@ abstract class CommonModel extends Model if (!$this->save($entity)) { throw new \Exception("저장오류:" . var_export($this->errors(), true)); } - log_message("notice", $this->getTable() . " => " . __FUNCTION__ . " DB에 {$entity->getTitle()} 저장이 완료되었습니다."); + log_message("debug", $this->getTable() . " => " . __FUNCTION__ . " DB에 {$entity->getTitle()} 저장이 완료되었습니다."); return $entity; } catch (\Exception $e) { $message = sprintf( @@ -189,7 +190,7 @@ abstract class CommonModel extends Model $entity = $this->save_process($entity); //primaryKey가 자동입력이면 if ($this->useAutoIncrement) { - $pkField = $this->getPKField(); + $pkField = $this->getPKField(); $entity->$pkField = $this->getInsertID(); } // log_message("debug", $this->getTable() . " => " . __FUNCTION__ . " DB 작업 완료");