Amazon.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. $record = $this->getRecords($type, $original_ip);
  77. if ($record) {
  78. array_walk_recursive($record, static function (&$value) use ($latest_ip, $original_ip) {
  79. if ($value === $original_ip) {
  80. $value = $latest_ip;
  81. }
  82. });
  83. $response = $this->sendRequest('ChangeResourceRecordSets', $this->generateChangeResourceRecordSetsXml('UPSERT', $record));
  84. return isset($response['ChangeInfo']['Status']) && $response['ChangeInfo']['Status'] === 'PENDING';
  85. }
  86. return $this->store($latest_ip, $type);
  87. }
  88. private function getRecords(string $type, string $ip): array
  89. {
  90. $response = $this->sendRequest('ListResourceRecordSets');
  91. if (isset($response['ResourceRecordSets'])) {
  92. $records = $response['ResourceRecordSets'];
  93. $filterCondition = function ($record) use ($type, $ip) {
  94. if ($type && $record['Type'] !== $type) {
  95. return false;
  96. }
  97. if ($ip && ! in_array($ip, Arr::pluck($record['ResourceRecords'], 'Value'), true)) {
  98. return false;
  99. }
  100. return $record['Name'] === "$this->subdomain.";
  101. };
  102. $filteredRecords = array_filter($records, $filterCondition);
  103. return array_map(static function ($record) {
  104. $resourceValues = Arr::pluck($record['ResourceRecords'], 'Value');
  105. return [
  106. 'Name' => $record['Name'],
  107. 'Type' => $record['Type'],
  108. 'TTL' => $record['TTL'],
  109. 'ResourceRecords' => $resourceValues,
  110. ];
  111. }, $filteredRecords);
  112. }
  113. return [];
  114. }
  115. private function generateChangeResourceRecordSetsXml(string $action, array $records): string
  116. {
  117. $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><ChangeResourceRecordSetsRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/"></ChangeResourceRecordSetsRequest>');
  118. $changeBatch = $xml->addChild('ChangeBatch');
  119. $changes = $changeBatch->addChild('Changes');
  120. foreach ($records as $record) {
  121. $change = $changes->addChild('Change');
  122. $change->addChild('Action', $action);
  123. $resourceRecordSet = $change->addChild('ResourceRecordSet');
  124. $resourceRecordSet->addChild('Name', htmlspecialchars($record['Name'])); // Escape special characters in Name
  125. $resourceRecordSet->addChild('Type', $record['Type']);
  126. $resourceRecordSet->addChild('TTL', $record['TTL']);
  127. $resourceRecords = $resourceRecordSet->addChild('ResourceRecords');
  128. foreach ($record['ResourceRecords'] as $value) {
  129. $resourceRecord = $resourceRecords->addChild('ResourceRecord');
  130. $resourceRecord->addChild('Value', $value);
  131. }
  132. }
  133. $dom = dom_import_simplexml($xml)->ownerDocument;
  134. $dom->formatOutput = true;
  135. return $dom->saveXML();
  136. }
  137. public function store(string $ip, string $type): bool
  138. {
  139. $response = $this->sendRequest('ChangeResourceRecordSets', $this->generateChangeResourceRecordSetsXml('UPSERT', [['Name' => $this->subdomain, 'Type' => $type, 'TTL' => 300, 'ResourceRecords' => [$ip]]]));
  140. return isset($response['ChangeInfo']['Status']) && $response['ChangeInfo']['Status'] === 'PENDING';
  141. }
  142. public function destroy(string $type, string $ip): bool
  143. {
  144. $records = $this->getRecords($type, $ip);
  145. if ($records) {
  146. $response = $this->sendRequest('ChangeResourceRecordSets', $this->generateChangeResourceRecordSetsXml('DELETE', $records));
  147. return isset($response['ChangeInfo']['Status']) && $response['ChangeInfo']['Status'] === 'PENDING';
  148. }
  149. return true;
  150. }
  151. }