This commit is contained in:
zyimm 2022-10-31 16:13:17 +08:00
commit 25f8cdf75d
13 changed files with 7643 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.idea
vendor

26
composer.json Normal file
View File

@ -0,0 +1,26 @@
{
"name": "zyimm/ddns",
"autoload": {
"psr-4": {
"Zyimm\\Ddns\\": "src/"
}
},
"authors": [
{
"name": "zyimm"
}
],
"require": {
"php": ">=7.4",
"alibabacloud/darabonba-openapi": "^0.2.6",
"alibabacloud/alidns-20150109": "3.0.0",
"ext-json": "*"
},
"require-dev": {
"hyperf/nano": "^1.0",
"friendsofphp/php-cs-fixer": "^3.0",
"phpunit/phpunit": "^9.4",
"hyperf/ide-helper": "v2.2.*",
"qiutuleng/hyperf-dump-server": "^1.2"
}
}

7309
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

16
src/Base.php Normal file
View File

@ -0,0 +1,16 @@
<?php
namespace Zyimm\Ddns;
abstract class Base
{
public Client $client;
public array $data = [];
public function __construct(Client $client, array $data)
{
$this->client = $client;
$this->data = $data;
}
}

37
src/Client.php Normal file
View File

@ -0,0 +1,37 @@
<?php
namespace Zyimm\Ddns;
use AlibabaCloud\SDK\Alidns\V20150109\Alidns;
use Darabonba\OpenApi\Models\Config;
use Zyimm\Ddns\Resolve\Record;
class Client
{
private \Zyimm\Ddns\Config $config;
public Alidns $aliDns;
public function __construct(\Zyimm\Ddns\Config $config)
{
$this->config = $config;
$dnsConfig = new Config([
// 您的 AccessKey ID
"accessKeyId" => $this->config->getAccessKeyId(),
// 您的 AccessKey Secret
"accessKeySecret" => $this->config->getAccessKeySecret()
]);
// 访问的域名
$dnsConfig->endpoint = "alidns.cn-hangzhou.aliyuncs.com";
$this->aliDns = new Alidns($dnsConfig);
}
/**
* @param array $data
* @return Record
*/
public function record(array $data = []): Record
{
return new Record($this, $data);
}
}

35
src/Config.php Normal file
View File

@ -0,0 +1,35 @@
<?php
namespace Zyimm\Ddns;
class Config
{
private string $accessKeyId;
private string $accessKeySecret;
public function getAccessKeyId(): string
{
return $this->accessKeyId;
}
public function setAccessKeyId($accessKeyId): Config
{
$this->accessKeyId = $accessKeyId;
return $this;
}
public function getAccessKeySecret(): string
{
return $this->accessKeySecret;
}
public function setAccessKeySecret($accessKeySecret): Config
{
$this->accessKeySecret = $accessKeySecret;
return $this;
}
}

31
src/Resolve/Record.php Normal file
View File

@ -0,0 +1,31 @@
<?php
namespace Zyimm\Ddns\Resolve;
use Zyimm\Ddns\Base;
use Zyimm\Ddns\Resolve\Record\Add;
use Zyimm\Ddns\Resolve\Record\Lists;
use Zyimm\Ddns\Resolve\Record\Update;
class Record extends Base
{
public function Lists(): Lists
{
return (new Lists($this))->data($this->data);
}
public function add(): Add
{
return (new Add($this))->data($this->data);
}
public function update(): Update
{
return (new Update($this))->data($this->data);
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Zyimm\Ddns\Resolve\Record;
use AlibabaCloud\SDK\Alidns\V20150109\Models\AddDomainRecordRequest;
class Add extends Base
{
public string $domainName;
public string $RR;
public string $type;
public string $value;
public function handle()
{
$this->record->client->aliDns->AddDomainRecord(new AddDomainRecordRequest([
'domainName' => $this->domainName,
'RR' => $this->RR,
'type' => $this->type,
'value' => $this->value
]));
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Zyimm\Ddns\Resolve\Record;
use Zyimm\Ddns\Resolve\Record;
abstract class Base
{
public Record $record;
public function __construct(Record $record)
{
$this->record = $record;
}
public function data(array $data)
{
foreach ($data as $k => $v) {
if(property_exists($this, $k)){
$this->{$k} = $v;
}
}
return $this;
}
abstract public function handle();
}

View File

@ -0,0 +1,21 @@
<?php
namespace Zyimm\Ddns\Resolve\Record;
use AlibabaCloud\SDK\Alidns\V20150109\Models\DescribeDomainRecordsRequest;
use AlibabaCloud\SDK\Alidns\V20150109\Models\DescribeDomainRecordsResponse;
class Lists extends Base
{
public string $domainName;
public function handle(): DescribeDomainRecordsResponse
{
return $this->record->client->aliDns
->describeDomainRecords(
new DescribeDomainRecordsRequest([
'domainName' => $this->domainName
])
);
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace Zyimm\Ddns\Resolve\Record;
use AlibabaCloud\SDK\Alidns\V20150109\Models\UpdateDomainRecordRequest;
class Update extends Base
{
public string $recordId;
public string $domainName = '';
public string $RR = '';
public string $type = '';
public string $value = '';
public function handle()
{
$param = [
'recordId' => $this->recordId,
'domainName' => $this->domainName,
'RR' => $this->RR,
'type' => $this->type,
'value' => $this->value
];
foreach ($param as $k => $v) {
if (empty($v)) {
unset($param[$k]);
}
}
$this->record->client->aliDns->updateDomainRecord(new UpdateDomainRecordRequest($param));
}
}

28
test/DdnsTest.php Normal file
View File

@ -0,0 +1,28 @@
<?php
namespace Zyimm\Ddns\Test;
use Hyperf\Nano\Factory\AppFactory;
use PHPUnit\Framework\TestCase;
class DdnsTest extends TestCase
{
public function test()
{
$app = AppFactory::create();
$app->get('/', function () {
$user = $this->request->input('user', 'nano');
$method = $this->request->getMethod();
return [
'message' => "hello {$user}",
'method' => $method,
];
});
$app->run();
}
}

49
test/test.php Normal file
View File

@ -0,0 +1,49 @@
<?php
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\Nano\Factory\AppFactory;
use Hyperf\Utils\Codec\Json;
use Zyimm\Ddns\Client;
use Zyimm\Ddns\Config;
require_once __DIR__.'/../vendor/autoload.php';
$app = AppFactory::create();
$app->get('/', function () {
$config = (new Config())
->setAccessKeyId('***')
->setAccessKeySecret('***');
$client = new Client($config);
$client->record([
'recordId' => "793484666963632128",
'RR' => 'media',
'type' => 'A',
'value' => "****"
])->update()->handle();
return $client->record([
'domainName' => '***.com'
])->lists()->handle()->toMap();
});
$app->addExceptionHandler(function ($throwable, $response) {
$data = [
'code' => $throwable->getCode() <= 0 ? 500 : $throwable->getCode(),
'message' => empty($throwable->getMessage()) ? '未知错误' : $throwable->getMessage(),
'result' => (object) []
];
if (env('APP_ENV') == 'dev') {
$data['trace'] = $throwable->getTrace();
}
return $response
->withAddedHeader('content-type', 'application/json; charset=utf-8')
->withStatus(200)->withBody(new SwooleStream(Json::encode($data)));
});
$app->run();