作者 刘锟

AlibabaCloud

  1 +<?php
  2 +
  3 +namespace App\Console\Commands\Domain;
  4 +
  5 +use App\Models\Devops\ServersIp;
  6 +use App\Services\AlibabaCloudService;
  7 +use Illuminate\Console\Command;
  8 +
  9 +class EmergencyRecords extends Command
  10 +{
  11 + protected $signature = 'emergency_records {server_id} {type}';
  12 + protected $description = '紧急修改域名解析';
  13 +
  14 + public function handle()
  15 + {
  16 + //服务器id
  17 + $server_id = $this->argument('server_id');
  18 + //类型,1受灾,2恢复
  19 + $type = $this->argument('type');
  20 +
  21 + try {
  22 + //获取服务器的所有可用ip
  23 + $server_ip_model = new ServersIp();
  24 + $server_ip_list = $server_ip_model->where('servers_id', $server_id)->where('status', 0)->get();
  25 + if ($server_ip_list->count() > 0) {
  26 + foreach ($server_ip_list as $value) {
  27 + if (empty($value->ip) || empty($value->domain)) {
  28 + $this->output('ID ' . $value->id . ' 数据错误');
  29 + continue;
  30 + }
  31 +
  32 + //获取解析记录
  33 + $domain_array = explode('.', $value->domain);
  34 + $domain_rr = $domain_array[0];
  35 + $record = AlibabaCloudService::describeDomainRecords('globalso.com', $domain_rr);
  36 + $record_status_code = $record['statusCode'] ?? 0;
  37 + if ($record_status_code != 200) {
  38 + $this->output('获取主机记录 ' . $domain_rr . ' 解析数据失败');
  39 + continue;
  40 + }
  41 + $record_detail = $record['body']['DomainRecords']['Record'][0] ?? [];
  42 + if (!$record_detail) {
  43 + $this->output('主机记录 ' . $domain_rr . ' 解析数据不存在');
  44 + continue;
  45 + }
  46 +
  47 + //目标ip跟解析记录当前ip一样的数据,不用修改
  48 + $target_ip = $type == 1 ? '43.153.1.240' : $value->ip;
  49 + if ($target_ip == $record_detail['Value']) {
  50 + $this->output('主机记录 ' . $domain_rr . ' 的值已为 ' . $target_ip);
  51 + continue;
  52 + }
  53 +
  54 + //修改解析记录
  55 + $record_id = $record_detail['RecordId'];
  56 + $record_rr = $record_detail['RR'];
  57 + $record_type = $record_detail['Type'];
  58 +
  59 + $record_edit = AlibabaCloudService::updateDomainRecord($record_id, $record_rr, $record_type, $target_ip);
  60 + $record_edit_status_code = $record_edit['statusCode'] ?? 0;
  61 + if ($record_edit_status_code == 200) {
  62 + $this->output('修改主机记录 ' . $record_rr . ' 的值为 ' . $target_ip . ' 成功');
  63 + } else {
  64 + $this->output('修改主机记录 ' . $record_rr . ' 的值为 ' . $target_ip . ' 失败');
  65 + }
  66 + }
  67 + }
  68 + } catch (\Exception $e) {
  69 + $this->output($e->getMessage());
  70 + }
  71 + }
  72 +
  73 + /**
  74 + * 输出处理日志
  75 + * @param $message
  76 + */
  77 + public function output($message)
  78 + {
  79 + echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
  80 + }
  81 +}
  1 +<?php
  2 +
  3 +namespace App\Services;
  4 +
  5 +use AlibabaCloud\SDK\Alidns\V20150109\Alidns;
  6 +use AlibabaCloud\SDK\Alidns\V20150109\Models\AddDomainRecordRequest;
  7 +use AlibabaCloud\SDK\Alidns\V20150109\Models\DescribeDomainRecordsRequest;
  8 +use AlibabaCloud\SDK\Alidns\V20150109\Models\UpdateDomainRecordRemarkRequest;
  9 +use AlibabaCloud\SDK\Alidns\V20150109\Models\UpdateDomainRecordRequest;
  10 +use AlibabaCloud\Tea\Utils\Utils;
  11 +use Darabonba\OpenApi\Models\Config;
  12 +
  13 +class AlibabaCloudService
  14 +{
  15 + /**
  16 + * 创建客户端
  17 + * @return Alidns
  18 + * @author Akun
  19 + * @date 2024/09/21 16:16
  20 + */
  21 + public static function createClient()
  22 + {
  23 + $config = new Config([
  24 + 'accessKeyId' => env('ALIBABA_CLOUD_KEY'),
  25 + 'accessKeySecret' => env('ALIBABA_CLOUD_SECRET'),
  26 + ]);
  27 +
  28 + return new Alidns($config);
  29 + }
  30 +
  31 + /**
  32 + * 获取域名解析记录列表
  33 + * @param $domain
  34 + * @param $keyword
  35 + * @param $status
  36 + * @return array
  37 + * @author Akun
  38 + * @date 2024/09/21 17:19
  39 + */
  40 + public static function describeDomainRecords($domain, $keyword, $status = 'Enable')
  41 + {
  42 + $client = self::createClient();
  43 +
  44 + $request = new DescribeDomainRecordsRequest([
  45 + 'domainName' => $domain,
  46 + 'RRKeyWord' => $keyword,
  47 + 'status' => $status,
  48 + ]);
  49 +
  50 + $response = $client->DescribeDomainRecords($request);
  51 +
  52 + return Utils::toArray($response);
  53 + }
  54 +
  55 + /**
  56 + * 添加解析记录
  57 + * @param $domain
  58 + * @param $rr
  59 + * @param $type
  60 + * @param $value
  61 + * @return array
  62 + * @author Akun
  63 + * @date 2024/09/21 17:33
  64 + */
  65 + public static function addDomainRecord($domain, $rr, $type, $value)
  66 + {
  67 + $client = self::createClient();
  68 +
  69 + $request = new AddDomainRecordRequest([
  70 + 'domainName' => $domain,
  71 + 'RR' => $rr,
  72 + 'type' => $type,
  73 + 'value' => $value
  74 + ]);
  75 +
  76 + $response = $client->AddDomainRecord($request);
  77 +
  78 + return Utils::toArray($response);
  79 + }
  80 +
  81 + /**
  82 + * 修改解析记录备注
  83 + * @param $record_id
  84 + * @param $remark
  85 + * @return array
  86 + * @author Akun
  87 + * @date 2024/09/21 17:38
  88 + */
  89 + public static function updateDomainRecordRemark($record_id, $remark)
  90 + {
  91 + $client = self::createClient();
  92 +
  93 + $request = new UpdateDomainRecordRemarkRequest([
  94 + 'recordId' => $record_id,
  95 + 'remark' => $remark,
  96 + ]);
  97 +
  98 + $response = $client->UpdateDomainRecordRemark($request);
  99 +
  100 + return Utils::toArray($response);
  101 + }
  102 +
  103 + /**
  104 + * 修改解析记录详情
  105 + * @param $record_id
  106 + * @param $rr
  107 + * @param $type
  108 + * @param $value
  109 + * @return array
  110 + * @author Akun
  111 + * @date 2024/09/23 10:55
  112 + */
  113 + public static function updateDomainRecord($record_id, $rr, $type, $value)
  114 + {
  115 + $client = self::createClient();
  116 +
  117 + $request = new UpdateDomainRecordRequest([
  118 + 'recordId' => $record_id,
  119 + 'RR' => $rr,
  120 + 'type' => $type,
  121 + 'value' => $value,
  122 + ]);
  123 +
  124 + $response = $client->UpdateDomainRecord($request);
  125 +
  126 + return Utils::toArray($response);
  127 + }
  128 +}
@@ -5,6 +5,8 @@ @@ -5,6 +5,8 @@
5 "keywords": ["framework", "laravel"], 5 "keywords": ["framework", "laravel"],
6 "license": "MIT", 6 "license": "MIT",
7 "require": { 7 "require": {
  8 + "alibabacloud/alidns-20150109": "*",
  9 + "alibabacloud/darabonba-openapi": "^0.2.12",
8 "barryvdh/laravel-dompdf": "^2.0", 10 "barryvdh/laravel-dompdf": "^2.0",
9 "bensampo/laravel-enum": "^4.2", 11 "bensampo/laravel-enum": "^4.2",
10 "beyondcode/laravel-websockets": "^1.14", 12 "beyondcode/laravel-websockets": "^1.14",