ClouDNS.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 ClouDNS implements DNS
  10. {
  11. // 开发依据: https://www.cloudns.net/wiki/article/41/
  12. private const API_ENDPOINT = 'https://api.cloudns.net/dns/';
  13. public const KEY = 'cloudns';
  14. public const LABEL = 'ClouDNS';
  15. private string $authID;
  16. private string $authPassword;
  17. private array $domainInfo;
  18. public function __construct(private readonly string $subdomain)
  19. {
  20. $this->authID = sysConfig('ddns_key');
  21. $this->authPassword = 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('list-zones', ['page' => 1, 'rows-per-page' => 100]) ?? [], '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('['.self::LABEL." — DescribeDomains] 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. $response = Http::timeout(15)->get(self::API_ENDPOINT."$action.json", ['auth-id' => $this->authID, 'auth-password' => $this->authPassword, ...$parameters]);
  43. if ($response->successful()) {
  44. $data = $response->json();
  45. if (isset($data['status']) && $data['status'] === 'Failed') {
  46. Log::error('['.self::LABEL." — $action] 返回错误信息: ".$data['statusDescription'] ?? 'Unknown error');
  47. } else {
  48. return $data;
  49. }
  50. } else {
  51. Log::error('['.self::LABEL." — $action] 请求失败");
  52. }
  53. exit(400);
  54. }
  55. public function store(string $ip, string $type): bool
  56. {
  57. $result = $this->sendRequest('add-record', ['domain-name' => $this->domainInfo['domain'], 'record-type' => $type, 'host' => $this->domainInfo['sub'], 'record' => $ip, 'ttl' => 300]);
  58. return $result['status'] === 'Success';
  59. }
  60. public function update(string $latest_ip, string $original_ip, string $type): bool
  61. {
  62. $recordIds = $this->getRecordIds($type, $original_ip);
  63. if ($recordIds) {
  64. $ret = $this->sendRequest('mod-record', ['domain-name' => $this->domainInfo['domain'], 'record-id' => $recordIds[0], 'host' => $this->domainInfo['sub'], 'record' => $latest_ip, 'ttl' => 300]);
  65. if (count($recordIds) > 1) {
  66. $this->destroy($type, $original_ip);
  67. }
  68. }
  69. return ($ret['status'] ?? false) === 'Success';
  70. }
  71. private function getRecordIds(string $type, string $ip): array
  72. { // 域名信息
  73. $records = $this->sendRequest('records', ['domain-name' => $this->domainInfo['domain'], 'host' => $this->domainInfo['sub'], 'type' => $type]) ?? [];
  74. if ($ip) {
  75. $records = array_filter($records, static function ($record) use ($ip) {
  76. return $record['record'] === $ip;
  77. });
  78. }
  79. return array_column($records, 'id');
  80. }
  81. public function destroy(string $type, string $ip): int
  82. {
  83. $recordIds = $this->getRecordIds($type, $ip);
  84. $deletedCount = 0;
  85. foreach ($recordIds as $recordId) {
  86. $result = $this->sendRequest('delete-record', ['domain-name' => $this->domainInfo['domain'], 'record-id' => $recordId]);
  87. if (isset($result['status']) && $result['status'] === 'Success') {
  88. $deletedCount++;
  89. }
  90. }
  91. return $deletedCount;
  92. }
  93. }