阿里云 域名 云解析
这是我当是用的版本
composer require alibabacloud/alidns-20150109 3.0.10
代码部分
创建配置.env
文件。
APP_DEBUG = false// 在阿里云中台创建ALIBABA_CLOUD_ACCESS_KEY_ID = LTA****YbGkALIBABA_CLOUD_ACCESS_KEY_SECRET = mAC****PRl
在config/console.php
文件添加命令。
<?php// +----------------------------------------------------------------------// | 控制台配置// +----------------------------------------------------------------------return [// 指令定义'commands' => ['UpdateDomainRecord' => 'app\command\UpdateDomainRecord',],];
创建app/command/UpdateDomainRecord.php
文件。
<?phpnamespace app\command;use DateInterval;use think\console\Command;use think\console\Input;use think\console\input\Argument;use think\console\Output;use think\facade\Cache;use AlibabaCloud\SDK\Alidns\V20150109\Alidns;use Exception;use AlibabaCloud\Tea\Exception\TeaError;use AlibabaCloud\Tea\Utils\Utils;use Darabonba\OpenApi\Models\Config;use AlibabaCloud\SDK\Alidns\V20150109\Models\DescribeDomainRecordsRequest;use AlibabaCloud\SDK\Alidns\V20150109\Models\UpdateDomainRecordRequest;use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;class UpdateDomainRecord extends Command{protected string $DomainName = '';protected string $RRKeyWord = '';protected function configure(): void{$this->setName('UpdateDomainRecord')->addArgument('DomainName', Argument::OPTIONAL, "域名名称")->addArgument('RRKeyWord', Argument::OPTIONAL, "主机记录的关键字")->setDescription('修改域名解析记录');}protected function execute(Input $input, Output $output): void{$output->writeln("开始执行");$this->DomainName = trim($input->getArgument('DomainName'));$this->RRKeyWord = trim($input->getArgument('RRKeyWord'));$IPv6s = $this->getIPv6();if (empty($IPv6s)) return;$IPv6 = $IPv6s[0];$output->writeln("当前本机 IPv6: $IPv6");$cacheIPv6 = Cache::get('rr_value', '');if ($cacheIPv6 == $IPv6) {$output->writeln("IPv6 没有变化");return;}// 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/311677.html$client = self::createClient(env('ALIBABA_CLOUD_ACCESS_KEY_ID', ''),env('ALIBABA_CLOUD_ACCESS_KEY_SECRET', ''),);$describeDomainRecordsRequest = new DescribeDomainRecordsRequest(["domainName" => $this->DomainName,"RRKeyWord" => $this->RRKeyWord,]);$runtime = new RuntimeOptions([]);try {$result = $client->describeDomainRecordsWithOptions($describeDomainRecordsRequest, $runtime);if ($result->statusCode != 200) return;$domainRecords = array_filter(json_decode(json_encode($result->body->domainRecords->record), true),fn($it): bool => $it['RR'] == $this->RRKeyWord);if (empty($domainRecords)) return;$domainRecord = $domainRecords[0];$output->writeln("当前解析 IPv6: {$domainRecord['value']}");if ($IPv6 == $domainRecord['value']) return;$updateDomainRecordRequest = new UpdateDomainRecordRequest(["recordId" => $domainRecord['recordId'],"RR" => $domainRecord['RR'],"type" => "AAAA","value" => $IPv6,]);$result = $client->updateDomainRecordWithOptions($updateDomainRecordRequest, $runtime);$output->writeln('请求结果: ' . json_encode($result));if ($result->statusCode == 200) {// 缓存阿里云解析的 IPv6 值Cache::set('rr_value', $IPv6, DateInterval::createFromDateString('7 day'));}} catch (Exception $error) {if (!($error instanceof TeaError)) {$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);}// 错误 messagevar_dump($error->message);// 诊断地址var_dump($error->data["Recommend"]);Utils::assertAsString($error->message);}}/*** 使用AK&SK初始化账号Client* @param string $accessKeyId* @param string $accessKeySecret* @return Alidns Client*/private static function createClient(string $accessKeyId, string $accessKeySecret): Alidns{$config = new Config([// 必填,您的 AccessKey ID"accessKeyId" => $accessKeyId,// 必填,您的 AccessKey Secret"accessKeySecret" => $accessKeySecret]);// Endpoint 请参考 https://api.aliyun.com/product/Alidns$config->endpoint = "alidns.cn-beijing.aliyuncs.com";return new Alidns($config);}private function getIPv6(): array{$command = "ip -6 addr show | grep -oP '(?<=inet6\s)[\da-f:]+'";$output = shell_exec($command);return array_values(array_filter(array_map(fn($it): string => trim($it), explode("\n", $output)),fn($it): bool => !empty($it) && strlen($it) >= 35));}}
定时脚本
如果你的定时脚本工具支持选择用户,可以使用下面的脚本。
#!/bin/bashcd /volume1/docker/toolphp think UpdateDomainRecord example.com subdomainsexit 0
如果你的定时脚本工具不支持选择用户,但是你自己需要指定用户,可以使用下面的脚本。
#!/bin/bashcd /www/wwwroot/tool.lsu -c "php think UpdateDomainRecord example.com subdomains" -s /bin/sh wwwexit 0