DNSPod.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. class DNSPod implements DNS
  10. {
  11. // 开发依据: https://docs.dnspod.cn/api/
  12. private const API_ENDPOINT = 'https://dnspod.tencentcloudapi.com';
  13. public const KEY = 'dnspod';
  14. public const LABEL = 'Tencent Cloud | DNSPod | 腾讯云';
  15. private string $secretId;
  16. private string $secretKey;
  17. private array $domainInfo;
  18. public function __construct(private readonly string $subdomain)
  19. {
  20. $this->secretId = sysConfig('ddns_key');
  21. $this->secretKey = sysConfig('ddns_secret');
  22. $this->domainInfo = $this->parseDomainInfo();
  23. }
  24. private function parseDomainInfo(): array
  25. {
  26. $domains = Cache::remember('ddns_get_domains', now()->addHour(), function () {
  27. return array_column($this->sendRequest('DescribeDomainList', ['Type' => 'mine'])['DomainList'], 'Name');
  28. });
  29. if ($domains) {
  30. $matched = Arr::first($domains, fn ($domain) => str_contains($this->subdomain, $domain));
  31. }
  32. if (empty($matched)) {
  33. throw new RuntimeException("[DNSPod – DescribeDomainList] The subdomain {$this->subdomain} does not match any domain in your account.");
  34. }
  35. return [
  36. 'sub' => rtrim(substr($this->subdomain, 0, -strlen($matched)), '.'),
  37. 'domain' => $matched,
  38. ];
  39. }
  40. private function sendRequest(string $action, array $parameters = []): array
  41. {
  42. $timestamp = time();
  43. $response = Http::timeout(15)->withHeaders([
  44. 'X-TC-Action' => $action,
  45. 'X-TC-Timestamp' => $timestamp,
  46. 'X-TC-Version' => '2021-03-23',
  47. 'Authorization' => $this->generateSignature($parameters, $timestamp),
  48. 'Host' => 'dnspod.tencentcloudapi.com',
  49. ])->withBody(json_encode($parameters, JSON_FORCE_OBJECT))->post(self::API_ENDPOINT);
  50. $data = $response->json();
  51. if ($data) {
  52. $data = $data['Response'];
  53. if ($response->ok()) {
  54. return $data;
  55. }
  56. Log::error('[DNSPod – '.$action.'] 返回错误信息:'.$data['Error']['Message'] ?? 'Unknown error');
  57. } else {
  58. Log::error('[DNSPod – '.$action.'] 请求失败');
  59. }
  60. exit(400);
  61. }
  62. private function generateSignature(array $parameters, int $timestamp): string
  63. { // 签名
  64. $date = gmdate('Y-m-d', $timestamp);
  65. $canonicalRequest = "POST\n/\n\ncontent-type:application/json\nhost:dnspod.tencentcloudapi.com\n\ncontent-type;host\n".hash('sha256', json_encode($parameters, JSON_FORCE_OBJECT));
  66. $credentialScope = "$date/dnspod/tc3_request";
  67. $stringToSign = "TC3-HMAC-SHA256\n$timestamp\n$credentialScope\n".hash('sha256', $canonicalRequest);
  68. $secretDate = hash_hmac('SHA256', $date, 'TC3'.$this->secretKey, true);
  69. $secretService = hash_hmac('SHA256', 'dnspod', $secretDate, true);
  70. $secretSigning = hash_hmac('SHA256', 'tc3_request', $secretService, true);
  71. $signature = hash_hmac('SHA256', $stringToSign, $secretSigning);
  72. return 'TC3-HMAC-SHA256 Credential='.$this->secretId.'/'.$credentialScope.', SignedHeaders=content-type;host, Signature='.$signature;
  73. }
  74. public function store(string $ip, string $type): bool
  75. {
  76. return (bool) $this->sendRequest('CreateRecord', [
  77. 'Domain' => $this->domainInfo['domain'],
  78. 'SubDomain' => $this->domainInfo['sub'],
  79. 'RecordType' => $type,
  80. 'RecordLine' => '默认',
  81. 'Value' => $ip,
  82. ]);
  83. }
  84. public function update(string $latest_ip, string $original_ip, string $type): bool
  85. {
  86. $recordIds = $this->getRecordIds($type, $original_ip);
  87. if ($recordIds) {
  88. $result = $this->sendRequest('ModifyRecord', [
  89. 'Domain' => $this->domainInfo['domain'],
  90. 'RecordType' => $type,
  91. 'RecordLine' => '默认',
  92. 'Value' => $latest_ip,
  93. 'RecordId' => $recordIds[0],
  94. 'SubDomain' => $this->domainInfo['sub'],
  95. ]);
  96. }
  97. return (bool) ($result ?? false);
  98. }
  99. private function getRecordIds(string $type, string $ip): array
  100. {
  101. $parameters = ['Domain' => $this->domainInfo['domain'], 'Subdomain' => $this->domainInfo['sub']];
  102. if ($type) {
  103. $parameters['RecordType'] = $type;
  104. }
  105. $response = $this->sendRequest('DescribeRecordList', $parameters);
  106. if (isset($response['RecordList'])) {
  107. $records = $response['RecordList'];
  108. if ($ip) {
  109. $records = array_filter($records, static function ($record) use ($ip) {
  110. return $record['Value'] === $ip;
  111. });
  112. }
  113. return array_column($records, 'RecordId');
  114. }
  115. return [];
  116. }
  117. public function destroy(string $type, string $ip): int
  118. {
  119. $recordIds = $this->getRecordIds($type, $ip);
  120. $deletedCount = 0;
  121. foreach ($recordIds as $recordId) {
  122. if ($this->sendRequest('DeleteRecord', ['Domain' => $this->domainInfo['domain'], 'RecordId' => $recordId])) {
  123. $deletedCount++;
  124. }
  125. }
  126. return $deletedCount;
  127. }
  128. }