add:域名管理first commit

This commit is contained in:
JaguarJack 2020-09-26 15:10:41 +08:00
parent 097b0e1ee5
commit 25c5faf440
13 changed files with 566 additions and 0 deletions

View File

@ -0,0 +1,50 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain;
use catchAdmin\domain\support\contract\DomainActionInterface;
use catchAdmin\domain\support\driver\aliyun\Domain;
use catcher\ModuleService;
class DomainService extends ModuleService
{
public function register()
{
parent::register(); // TODO: Change the autogenerated stub
$this->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;
}
}
}

37
catch/domain/config.php Normal file
View File

@ -0,0 +1,37 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
return [
'domains' => [
// 默认阿里云
'default' => 'aliyun',
/**
* 阿里云配置
*
*/
'aliyun' => [
'api_domain' => 'http://alidns.aliyuncs.com',
'access_key' => 'LTAI4G2JF2iiJEfnQYm4vhvr',
'access_secret' => 'YDe2sff7uDN1nRPdfvVAFCW6lLaOrC',
],
/**
* 腾讯云配置
*
*/
'qcloud' => [
'access_key' => '',
'access_secret' => '',
]
]
];

View File

@ -0,0 +1,88 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\controller;
use catchAdmin\domain\support\contract\DomainActionInterface;
use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
class Domain extends CatchController
{
protected $domain;
public function __construct(DomainActionInterface $domain)
{
$this->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));
}
}

15
catch/domain/module.json Normal file
View File

@ -0,0 +1,15 @@
{
"name": "域名管理",
"alias": "domain",
"description": "域名,阿里云,腾讯云",
"version": "1.0.0",
"keywords": [],
"order": 0,
"services": [
"\\catchAdmin\\domain\\DomainService"
],
"aliases": {},
"files": [],
"requires": [],
"enable": true
}

18
catch/domain/route.php Normal file
View File

@ -0,0 +1,18 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
// you should use `$router`
$router->group(function () use ($router){
// 域名管理
$router->resource('domain', '\catchAdmin\domain\controller\Domain');
// 域名解析管理
})->middleware('auth');

View File

@ -0,0 +1,41 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\support;
use catchAdmin\domain\support\signature\Aliyun;
/**
* 公共参数
*
* Class CommonParams
* @package catchAdmin\domain\support
*/
class CommonParams
{
public static function aliyun(array $params, $method = 'GET')
{
date_default_timezone_set('UTC');
$params = array_merge($params, [
'Format' => '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;
}
}

View File

@ -0,0 +1,54 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\support;
use think\Paginator;
class Transformer
{
public static function aliyunDomainPaginate($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'],
'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']);
}
}

View File

@ -0,0 +1,22 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\support\contract;
interface DomainActionInterface
{
public function getList(array $params);
public function store(array $params);
public function delete(array $params);
public function read(array $params);
}

View File

@ -0,0 +1,24 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\support\contract;
interface DomainRecordInterface
{
public function getList(array $params);
public function store(array $params);
public function delete(array $params);
public function read(array $params);
public function update(array $params);
}

View File

@ -0,0 +1,25 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\support\driver;
use catcher\facade\Http;
use catchAdmin\domain\support\CommonParams;
trait ApiTrait
{
public function get(array $params)
{
$name = config('catch.domains.default');
return Http::query(CommonParams::{$name}($params))
->get(config('catch.domains.' . $name . '.api_domain'))->json();
}
}

View File

@ -0,0 +1,55 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\support\driver\aliyun;
use catchAdmin\domain\support\contract\DomainActionInterface;
use catchAdmin\domain\support\driver\ApiTrait;
use catchAdmin\domain\support\Transformer;
class Domain implements DomainActionInterface
{
use ApiTrait;
public function getList(array $params)
{
// TODO: Implement getList() method.
return Transformer::aliyunDomainPaginate($this->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']
]);
}
}

View File

@ -0,0 +1,73 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\support\driver\aliyun;
use catchAdmin\domain\support\contract\DomainRecordInterface;
use catchAdmin\domain\support\driver\ApiTrait;
class DomainRecord implements DomainRecordInterface
{
use ApiTrait;
public function getList(array $params)
{
// TODO: Implement getList() method.
return $this->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']
]);
}
}

View File

@ -0,0 +1,64 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\support\signature;
class Aliyun
{
/**
* @var array
*/
protected $params;
/**
* Aliyun constructor.
* @param $params
*/
public function __construct(array $params)
{
$this->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));
}
}