Amazon.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace App\Utils\DDNS;
  3. use App\Utils\Library\Templates\DNS;
  4. use Arr;
  5. use Cache;
  6. use Http;
  7. use Log;
  8. use RuntimeException;
  9. use SimpleXMLElement;
  10. class Amazon implements DNS
  11. {
  12. // 开发依据: https://docs.aws.amazon.com/zh_cn/Route53/latest/APIReference/Welcome.html
  13. private const API_ENDPOINT = 'https://route53.amazonaws.com';
  14. public const KEY = 'amazon';
  15. public const LABEL = 'Amazon Route 53';
  16. private string $accessKeyID;
  17. private string $secretAccessKey;
  18. private string $zoneID;
  19. public function __construct(private readonly string $subdomain)
  20. {
  21. $this->accessKeyID = sysConfig('ddns_key');
  22. $this->secretAccessKey = sysConfig('ddns_secret');
  23. $this->zoneID = $this->getZoneIdentifier();
  24. }
  25. private function getZoneIdentifier(): string
  26. {
  27. $zones = Cache::remember('ddns_get_domains', now()->addHour(), function () {
  28. return array_column($this->sendRequest('ListHostedZones')['HostedZones'] ?? [], 'Name', 'Id');
  29. });
  30. foreach ($zones as $zoneID => $zoneName) {
  31. if (str_contains("$this->subdomain.", $zoneName)) {
  32. return $zoneID;
  33. }
  34. }
  35. throw new RuntimeException('['.self::LABEL." — ListHostedZones] The subdomain $this->subdomain does not match any domain in your account.");
  36. }
  37. private function sendRequest(string $action, string $payload = ''): array
  38. {
  39. $timestamp = time();
  40. $client = Http::timeout(15)->retry(3, 1000)->withHeaders(['Host' => 'route53.amazonaws.com', 'X-Amz-Date' => gmdate("Ymd\THis\Z", $timestamp), 'Accept' => 'application/json'])->baseUrl(self::API_ENDPOINT);
  41. $uri = match ($action) {
  42. 'ListHostedZones' => '/2013-04-01/hostedzone',
  43. 'ListResourceRecordSets', 'ChangeResourceRecordSets' => "/2013-04-01$this->zoneID/rrset",
  44. };
  45. $response = match ($action) {
  46. 'ListHostedZones', 'ListResourceRecordSets' => $client->withHeader('Authorization', $this->generateSignature('GET', $uri, $payload, $timestamp))->get($uri),
  47. 'ChangeResourceRecordSets' => $client->withHeader('Authorization', $this->generateSignature('POST', $uri, $payload, $timestamp))->withBody($payload, 'application/xml')->post($uri),
  48. };
  49. $data = $response->json();
  50. if ($response->successful()) {
  51. return $data;
  52. }
  53. if ($data) {
  54. Log::error('['.self::LABEL." — $action] 返回错误信息: ".$data['message'] ?? 'Unknown error');
  55. } else {
  56. Log::error('['.self::LABEL." — $action] 请求失败");
  57. }
  58. exit(400);
  59. }
  60. private function generateSignature(string $method, string $uri, string $payload, int $timestamp): string
  61. {
  62. $dateTime = gmdate("Ymd\THis\Z", $timestamp);
  63. $date = gmdate('Ymd', $timestamp);
  64. $canonicalRequest = "$method\n$uri\n\naccept:application/json\nhost:route53.amazonaws.com\nx-amz-date:$dateTime\n\naccept;host;x-amz-date\n".hash('sha256', $payload);
  65. $credentialScope = "$date/us-east-1/route53/aws4_request";
  66. $stringToSign = "AWS4-HMAC-SHA256\n$dateTime\n$credentialScope\n".hash('sha256', $canonicalRequest);
  67. $dateKey = hash_hmac('SHA256', $date, "AWS4$this->secretAccessKey", true);
  68. $regionKey = hash_hmac('SHA256', 'us-east-1', $dateKey, true);
  69. $serviceKey = hash_hmac('SHA256', 'route53', $regionKey, true);
  70. $signingKey = hash_hmac('SHA256', 'aws4_request', $serviceKey, true);
  71. $signature = hash_hmac('SHA256', $stringToSign, $signingKey);
  72. return "AWS4-HMAC-SHA256 Credential=$this->accessKeyID/$credentialScope, SignedHeaders=accept;host;x-amz-date, Signature=$signature";
  73. }
  74. public function update(string $latest_ip, string $original_ip, string $type): bool
  75. {
  76. $records = $this->getRecords($type);
  77. if ($records) {
  78. $recordCount = count($records[$type]);
  79. $records[$type] = array_filter($records[$type], static fn ($ip) => $ip !== $original_ip);
  80. $requiredAction = count($records[$type]) !== $recordCount;
  81. }
  82. if (! in_array($latest_ip, $records[$type] ?? [], true)) {
  83. $records[$type][] = $latest_ip;
  84. $requiredAction = true;
  85. }
  86. if ($requiredAction ?? false) {
  87. $response = $this->sendRequest('ChangeResourceRecordSets', $this->generateXml('UPSERT', $records));
  88. return isset($response['ChangeInfo']['Status']) && $response['ChangeInfo']['Status'] === 'PENDING';
  89. }
  90. return true;
  91. }
  92. private function getRecords(string $type): array
  93. {
  94. $response = $this->sendRequest('ListResourceRecordSets');
  95. if (! isset($response['ResourceRecordSets'])) {
  96. return [];
  97. }
  98. $records = $response['ResourceRecordSets'];
  99. if ($type) {
  100. $records = array_filter($records, function ($record) use ($type) {
  101. return $record['Type'] === $type && $record['Name'] === "$this->subdomain.";
  102. });
  103. } else {
  104. $records = array_filter($records, function ($record) {
  105. return $record['Name'] === "$this->subdomain.";
  106. });
  107. }
  108. return array_reduce($records, static function ($carry, $record) {
  109. $carry[$record['Type']] = Arr::pluck($record['ResourceRecords'], 'Value');
  110. return $carry;
  111. }, []);
  112. }
  113. private function generateXml(string $action, array $records): string
  114. {
  115. $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><ChangeResourceRecordSetsRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/"></ChangeResourceRecordSetsRequest>');
  116. $changeBatch = $xml->addChild('ChangeBatch');
  117. $changes = $changeBatch->addChild('Changes');
  118. foreach ($records as $type => $ips) {
  119. $change = $changes->addChild('Change');
  120. $change->addChild('Action', $action);
  121. $resourceRecordSet = $change->addChild('ResourceRecordSet');
  122. $resourceRecordSet->addChild('Name', "$this->subdomain.");
  123. $resourceRecordSet->addChild('Type', $type);
  124. $resourceRecordSet->addChild('TTL', 300);
  125. $resourceRecords = $resourceRecordSet->addChild('ResourceRecords');
  126. foreach ($ips as $ip) {
  127. $resourceRecord = $resourceRecords->addChild('ResourceRecord');
  128. $resourceRecord->addChild('Value', $ip);
  129. }
  130. }
  131. $dom = dom_import_simplexml($xml)->ownerDocument;
  132. $dom->formatOutput = true;
  133. return $dom->saveXML();
  134. }
  135. public function store(string $ip, string $type): bool
  136. {
  137. $records = $this->getRecords($type);
  138. if (! in_array($ip, $records[$type] ?? [], true)) {
  139. $records[$type][] = $ip;
  140. $response = $this->sendRequest('ChangeResourceRecordSets', $this->generateXml('UPSERT', $records));
  141. return isset($response['ChangeInfo']['Status']) && $response['ChangeInfo']['Status'] === 'PENDING';
  142. }
  143. return true;
  144. }
  145. public function destroy(string $type, string $ip): bool
  146. {
  147. $records = $this->getRecords($type);
  148. if (! $records) {
  149. return true; // 无记录可操作,直接返回
  150. }
  151. if ($type && $ip) {
  152. $filteredRecords = array_filter($records[$type], static fn ($hasIp) => $hasIp !== $ip);
  153. if (count($records[$type]) !== count($filteredRecords)) {
  154. if (count($filteredRecords) !== 0) {
  155. $action = 'UPSERT';
  156. $records[$type] = $filteredRecords;
  157. }
  158. } else {
  159. return true;
  160. }
  161. }
  162. $response = $this->sendRequest('ChangeResourceRecordSets', $this->generateXml($action ?? 'DELETE', $records));
  163. return isset($response['ChangeInfo']['Status']) && $response['ChangeInfo']['Status'] === 'PENDING';
  164. }
  165. }