diff --git a/catch/domain/DomainService.php b/catch/domain/DomainService.php new file mode 100644 index 0000000..2395aa2 --- /dev/null +++ b/catch/domain/DomainService.php @@ -0,0 +1,50 @@ +registerInstance(); + } + + public function loadConfig() + { + return require __DIR__ . DIRECTORY_SEPARATOR . 'config.php'; + } + + public function loadRouteFrom() + { + // TODO: Implement loadRouteFrom() method. + return __DIR__ . DIRECTORY_SEPARATOR . 'route.php'; + } + + protected function registerInstance() + { + $default = config('catch.domains.default'); + + switch ($default) { + case 'aliyun': + $this->app->instance(DomainActionInterface::class, new Domain); + break; + default: + break; + } + } +} diff --git a/catch/domain/config.php b/catch/domain/config.php new file mode 100644 index 0000000..3f8e214 --- /dev/null +++ b/catch/domain/config.php @@ -0,0 +1,37 @@ + [ + // 默认阿里云 + 'default' => 'aliyun', + + /** + * 阿里云配置 + * + */ + 'aliyun' => [ + 'api_domain' => 'http://alidns.aliyuncs.com', + + 'access_key' => 'LTAI4G2JF2iiJEfnQYm4vhvr', + + 'access_secret' => 'YDe2sff7uDN1nRPdfvVAFCW6lLaOrC', + ], + + /** + * 腾讯云配置 + * + */ + 'qcloud' => [ + 'access_key' => '', + 'access_secret' => '', + ] + ] +]; \ No newline at end of file diff --git a/catch/domain/controller/Domain.php b/catch/domain/controller/Domain.php new file mode 100644 index 0000000..7d465f9 --- /dev/null +++ b/catch/domain/controller/Domain.php @@ -0,0 +1,88 @@ +domain = $domain; + } + + /** + * 列表 + * + * @time 2020/09/11 18:14 + * @param Request $request + * @return \think\Response + */ + public function index(Request $request): \think\Response + { + return CatchResponse::paginate($this->domain->getList($request->param())); + } + + /** + * 保存 + * + * @time 2020/09/11 18:14 + * @param Request Request + * @return \think\Response + */ + public function save(Request $request) + { + return CatchResponse::success($this->domain->add($request->post())); + } + + /** + * 读取 + * + * @time 2020/09/11 18:14 + * @param $name + * @return \think\Response + */ + public function read($name) + { + return CatchResponse::success($this->domain->info($name)); + } + + /** + * 更新 + * + * @time 2020/09/11 18:14 + * @param Request $request + * @return \think\Response + */ + public function update(Request $request, $name) + { + return CatchResponse::success($this->model->updateBy($id, $request->post())); + } + + /** + * 删除 + * + * @time 2020/09/11 18:14 + * @param $name + * @return \think\Response + */ + public function delete($name) + { + return CatchResponse::success($this->domain->delete($name)); + } + + +} \ No newline at end of file diff --git a/catch/domain/module.json b/catch/domain/module.json new file mode 100644 index 0000000..61aa08d --- /dev/null +++ b/catch/domain/module.json @@ -0,0 +1,15 @@ +{ + "name": "域名管理", + "alias": "domain", + "description": "域名,阿里云,腾讯云", + "version": "1.0.0", + "keywords": [], + "order": 0, + "services": [ + "\\catchAdmin\\domain\\DomainService" + ], + "aliases": {}, + "files": [], + "requires": [], + "enable": true +} diff --git a/catch/domain/route.php b/catch/domain/route.php new file mode 100644 index 0000000..6a9615e --- /dev/null +++ b/catch/domain/route.php @@ -0,0 +1,18 @@ +group(function () use ($router){ + // 域名管理 + $router->resource('domain', '\catchAdmin\domain\controller\Domain'); + // 域名解析管理 +})->middleware('auth'); + diff --git a/catch/domain/support/CommonParams.php b/catch/domain/support/CommonParams.php new file mode 100644 index 0000000..102dfc6 --- /dev/null +++ b/catch/domain/support/CommonParams.php @@ -0,0 +1,41 @@ + 'json', + 'Version' => '2015-01-09', + 'AccessKeyId' => config('catch.domains.aliyun.access_key'), + 'SignatureMethod' => 'HMAC-SHA1', + 'Timestamp' => date('Y-m-d\TH:i:s\Z'), + 'SignatureVersion' => '1.0', + 'SignatureNonce' => uniqid() + ]); + + $params['Signature'] = (new Aliyun($params))->signature($method); + + return $params; + } +} \ No newline at end of file diff --git a/catch/domain/support/Transformer.php b/catch/domain/support/Transformer.php new file mode 100644 index 0000000..1fa04a7 --- /dev/null +++ b/catch/domain/support/Transformer.php @@ -0,0 +1,54 @@ + $item['DomainName'], + 'created_at' => date('Y-m-d', $item['CreateTimestamp']/1000), + 'dns_server' => $item['DnsServers']['DnsServer'], + 'from' => $item['VersionName'], + 'expired_at' => substr($item['InstanceEndTime'], 0, 10), + 'record_count' => $item['RecordCount'], + 'registrant_email' => $item['RegistrantEmail'], + 'tags' => $item['Tags']['Tag'], + 'id' => $item['DomainId'] + ]; + } + var_dump($list); + return Paginator::make($list, $data['PageSize'], $data['PageNumber'], $data['TotalCount']); + } + + public static function aliyunDomainRecordPaginate($data) + { + $list = []; + + foreach ($data['Domains']['Domain'] as $item) { + $list[] = [ + 'name' => $item['DomainName'], + 'created_at' => date('Y-m-d', $item['CreateTimestamp']/1000), + 'dns_server' => $item['DnsServers']['DnsServer'], + 'from' => $item['VersionName'], + 'tags' => $item['Tags']['Tag'], + ]; + } + + return Paginator::make($list, $data['PageSize'], $data['PageNumber'], $data['TotalCount']); + } +} \ No newline at end of file diff --git a/catch/domain/support/contract/DomainActionInterface.php b/catch/domain/support/contract/DomainActionInterface.php new file mode 100644 index 0000000..0cd449d --- /dev/null +++ b/catch/domain/support/contract/DomainActionInterface.php @@ -0,0 +1,22 @@ +get(config('catch.domains.' . $name . '.api_domain'))->json(); + } +} \ No newline at end of file diff --git a/catch/domain/support/driver/aliyun/Domain.php b/catch/domain/support/driver/aliyun/Domain.php new file mode 100644 index 0000000..06b31e0 --- /dev/null +++ b/catch/domain/support/driver/aliyun/Domain.php @@ -0,0 +1,55 @@ +get([ + 'Action' => 'DescribeDomains', + 'StarMark' => true, + 'SearchModel' => 'LIKE', + 'PageNumber' => $params['page'] ?? 1, + 'PageSize' => $params['limit'] ?? 20, + ])); + } + + public function store(array $params) + { + // TODO: Implement add() method. + } + + public function delete(array $params) + { + // TODO: Implement delete() method. + return $this->get([ + 'Action' => 'DeleteDomain', + 'DomainName' => $params['name'], + ]); + } + + public function read(array $params) + { + // TODO: Implement info() method. + return $this->get([ + 'Action' => 'DescribeDomainInfo', + 'DomainName' => $params['name'] + ]); + } +} \ No newline at end of file diff --git a/catch/domain/support/driver/aliyun/DomainRecord.php b/catch/domain/support/driver/aliyun/DomainRecord.php new file mode 100644 index 0000000..358b3ba --- /dev/null +++ b/catch/domain/support/driver/aliyun/DomainRecord.php @@ -0,0 +1,73 @@ +get([ + 'Action' => 'DescribeDomainRecords', + 'DomainName' => $params['name'], + 'PageNumber' => $params['page'] ?? 1, + 'PageSize' => $params['limit'] ?? 20, + '' + ]); + } + + public function store(array $params) + { + // TODO: Implement add() method. + return $this->get([ + 'Action' => 'AddDomainRecord', + 'DomainName' => $params['name'], + 'RR' => $params['record'], + 'Type' => $params['type'], + 'Value' => $params['ip'] + ]); + } + + public function delete(array $params) + { + // TODO: Implement delete() method. + return $this->get([ + 'Action' => 'DeleteDomainRecord', + 'RecordId' => $params['record_id'] + ]); + } + + public function read(array $params) + { + // TODO: Implement info() method. + return $this->get([ + 'Action' => 'DescribeDomainRecord', + 'RecordId' => $params['record_id'], + ]); + } + + public function update(array $params) + { + // TODO: Implement update() method. + return $this->get([ + 'Action' => 'UpdateDomainRecord', + 'RecordId' => $params['record_id'], + 'RR' => $params['record'], + 'Type' => $params['type'], + 'Value' => $params['ip'] + ]); + } +} \ No newline at end of file diff --git a/catch/domain/support/signature/Aliyun.php b/catch/domain/support/signature/Aliyun.php new file mode 100644 index 0000000..208dc33 --- /dev/null +++ b/catch/domain/support/signature/Aliyun.php @@ -0,0 +1,64 @@ +params = $params; + } + + /** + * encode + * + * @time 2020年09月25日 + * @param $str + * @return string|string[]|null + */ + protected function percentEncode(string $str) + { + return preg_replace(['/\+/', '/\*/', '/%7E/'], ['%20', '%2A', '~'], urlencode($str)); + } + + /** + * 签名 + * + * @time 2020年09月25日 + * @param $method + * @return string + */ + public function signature(string $method) + { + ksort($this->params); + + $queryString = ''; + + foreach ($this->params as $key => $param) { + $queryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($param); + } + + $signString = $method . '&' . + $this->percentEncode('/') . '&' . + $this->percentEncode(substr($queryString, 1)); + + return base64_encode(hash_hmac('sha1', $signString, config('catch.domains.aliyun.access_secret'). '&', true)); + } +} \ No newline at end of file