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); log_message("error", $message);
throw new \Exception($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; return $cf;
} }
final protected function reload_procedure(string $uri): array final protected function reload_procedure(string $uri): array

View File

@ -3,26 +3,39 @@
namespace App\Libraries\MySocket; namespace App\Libraries\MySocket;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Client;
use App\Entities\Cloudflare\AuthEntity; use App\Entities\Cloudflare\AuthEntity;
class CloudflareSocket extends MySocket class CloudflareSocket extends MySocket
{ {
private ?Client $_client = null;
public static int $_request = 0; public static int $_request = 0;
private static int $_request_max = 1000; 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) public function __construct(AuthEntity $auth_entity)
{ {
parent::__construct([ parent::__construct();
'base_uri' => 'https://api.cloudflare.com/client/v4/', $this->_auth_entity = $auth_entity;
'headers' => [ }
'X-Auth-Email' => $auth_entity->getID(), public function getClient(): Client
'X-Auth-Key' => $auth_entity->getAuthKey(), {
'Content-Type' => 'application/json' 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 //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) { if (self::$_request >= self::$_request_max) {
log_message('warning', sprintf("--Cloudflare API Call %s초 대기 시작--", self::$_request_timewait)); log_message('warning', sprintf("--Cloudflare API Call %s초 대기 시작--", self::$_request_timewait));
@ -32,6 +45,6 @@ class CloudflareSocket extends MySocket
} }
self::$_request++; self::$_request++;
// log_message("debug", "현재 request:[" . 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; namespace App\Libraries\MySocket\GoogleSocket;
use GuzzleHttp\Client;
use App\Entities\UserSNSEntity; use App\Entities\UserSNSEntity;
use App\Libraries\MySocket\MySocket as Client;
class CURL extends GoogleSocket class CURL extends GoogleSocket
{ {

View File

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

View File

@ -3,19 +3,18 @@
namespace App\Libraries\MySocket; namespace App\Libraries\MySocket;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Cookie\CookieJar; use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Cloudflare\API\Adapter\ResponseException;
class MySocket extends Client abstract class MySocket
{ {
private $_cookieJar = null; 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 final protected function getCookieJar(): CookieJar
{ {
if ($this->_cookieJar === null) { if ($this->_cookieJar === null) {
@ -35,45 +34,53 @@ class MySocket extends Client
]; ];
return $userAgents[array_rand($userAgents)]; 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초 안에 응답이 없으면 타임아웃 //cookies->쿠키값 , timeout->5초 안에 응답이 없으면 타임아웃
//method가 get이면 $request['query'] = $options , 다른것이면 $request['json] = $options //method가 get이면 $request['query'] = $options , 다른것이면 $request['json] = $options
$options = [ $options = [
// 'cookies' => $this->getCookieJar(), // 'cookies' => $this->getCookieJar(),
'timeout' => env("socket.web.timeout") ?? 5, 'timeout' => env("socket.web.timeout") ?? 5,
'headers' => $headers,
in_array($method, ['get']) ? 'query' : 'json' => $options in_array($method, ['get']) ? 'query' : 'json' => $options
]; ];
return $options; return $options;
} }
public function request(string $method, $uri = '', array $options = [], array $headers = []): ResponseInterface
final public function get($uri, array $options = []): 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); return $this->request('DELETE', $uri, $options, $headers);
log_message("debug", __FUNCTION__ .
"=> 호출 Socket URL:{$uri}\n--------------\n" .
var_export($options, true) .
"\n--------------\n");
return parent::request($method, $uri, $options);
} }
} }

View File

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