HuaweiCloud.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Utils\DDNS;
  3. use App\Utils\Library\Templates\DNS;
  4. use Cache;
  5. use Http;
  6. use Log;
  7. use RuntimeException;
  8. class HuaweiCloud implements DNS
  9. {
  10. // 开发依据: https://support.huaweicloud.com/api-dns/dns_api_64001.html
  11. private const API_ENDPOINT = 'https://dns.myhuaweicloud.com';
  12. public const KEY = 'huaweicloud';
  13. public const LABEL = 'HuaweiCloud 华为云';
  14. private string $accessKeyID;
  15. private string $secretAccessKey;
  16. private string $zoneID;
  17. public function __construct(private readonly string $subdomain)
  18. {
  19. $this->accessKeyID = sysConfig('ddns_key');
  20. $this->secretAccessKey = sysConfig('ddns_secret');
  21. $this->zoneID = $this->getZoneIdentifier();
  22. }
  23. private function getZoneIdentifier(): string
  24. {
  25. $zones = Cache::remember('ddns_get_domains', now()->addHour(), function () {
  26. return array_column($this->sendRequest('ListPublicZones')['zones'] ?? [], 'name', 'id');
  27. });
  28. foreach ($zones as $zoneID => $zoneName) {
  29. if (str_contains("$this->subdomain.", $zoneName)) {
  30. return $zoneID;
  31. }
  32. }
  33. throw new RuntimeException('['.self::LABEL." — ListPublicZones] The subdomain $this->subdomain does not match any domain in your account.");
  34. }
  35. private function sendRequest(string $action, array $parameters = [], array $payload = [], string $recordsetId = ''): array
  36. {
  37. $date = gmdate("Ymd\THis\Z");
  38. $client = Http::timeout(15)->retry(3, 1000)->withHeaders(['Host' => 'dns.myhuaweicloud.com', 'X-Sdk-Date' => $date])->baseUrl(self::API_ENDPOINT)->asJson();
  39. $uri = match ($action) {
  40. 'ListPublicZones' => '/v2/zones',
  41. 'ListRecordSets' => '/v2/recordsets',
  42. 'CreateRecordSet' => "/v2/zones/$this->zoneID/recordsets",
  43. 'UpdateRecordSet' => "/v2/zones/$this->zoneID/recordsets/$recordsetId",
  44. 'DeleteRecordSets' => "/v2.1/zones/$this->zoneID/recordsets",
  45. };
  46. $response = match ($action) {
  47. 'ListPublicZones', 'ListRecordSets' => $client->withHeader('Authorization', $this->generateSignature('GET', $uri, $parameters, $payload, $date))->get($uri, $parameters),
  48. 'CreateRecordSet' => $client->withHeader('Authorization', $this->generateSignature('POST', $uri, $parameters, $payload, $date))->post($uri, $payload),
  49. 'UpdateRecordSet' => $client->withHeader('Authorization', $this->generateSignature('PUT', $uri, $parameters, $payload, $date))->put($uri, $payload),
  50. 'DeleteRecordSets' => $client->withHeader('Authorization', $this->generateSignature('DELETE', $uri, $parameters, $payload, $date))->delete($uri, $payload),
  51. };
  52. $data = $response->json();
  53. if ($response->successful()) {
  54. return $data;
  55. }
  56. if ($data) {
  57. Log::error('['.self::LABEL." — $action] 返回错误信息: ".$data['error_msg'] ?? 'Unknown error');
  58. } else {
  59. Log::error('['.self::LABEL." — $action] 请求失败");
  60. }
  61. exit(400);
  62. }
  63. private function generateSignature(string $method, string $uri, array $parameters, array $payload, string $date): string
  64. { // 签名
  65. $canonicalRequest = "$method\n$uri/\n".http_build_query($parameters)."\nhost:dns.myhuaweicloud.com\nx-sdk-date:$date\n\nhost;x-sdk-date\n".hash('sha256', $payload ? json_encode($payload) : '');
  66. $stringToSign = "SDK-HMAC-SHA256\n$date\n".hash('sha256', $canonicalRequest);
  67. $signature = hash_hmac('SHA256', $stringToSign, $this->secretAccessKey);
  68. return "SDK-HMAC-SHA256 Access=$this->accessKeyID, SignedHeaders=host;x-sdk-date, Signature=$signature";
  69. }
  70. public function update(string $latest_ip, string $original_ip, string $type): bool
  71. {
  72. $recordIds = $this->getRecordIds($type, $original_ip);
  73. if ($recordIds) {
  74. $response = $this->sendRequest('UpdateRecordSet', [], ['name' => "$this->subdomain.", 'type' => $type, 'records' => [$latest_ip]], $recordIds[0]);
  75. return isset($response['status']) && $response['status'] === 'PENDING_UPDATE';
  76. }
  77. return $this->store($latest_ip, $type);
  78. }
  79. private function getRecordIds(string $type, string $ip): array
  80. {
  81. $response = $this->sendRequest('ListRecordSets', ['name' => "$this->subdomain.", 'records' => $ip, 'type' => $type]);
  82. if (isset($response['recordsets'])) {
  83. $records = $response['recordsets'];
  84. return array_column($records, 'id');
  85. }
  86. return [];
  87. }
  88. public function store(string $ip, string $type): bool
  89. {
  90. $response = $this->sendRequest('CreateRecordSet', [], ['name' => "$this->subdomain.", 'type' => $type, 'records' => [$ip]]);
  91. return isset($response['status']) && $response['status'] === 'PENDING_CREATE';
  92. }
  93. public function destroy(string $type, string $ip): int
  94. {
  95. $recordIds = $this->getRecordIds($type, $ip);
  96. if ($recordIds) {
  97. $response = $this->sendRequest('DeleteRecordSets', [], ['recordset_ids' => $recordIds]);
  98. return $response['metadata']['total_count'];
  99. }
  100. return true;
  101. }
  102. }