Namecheap.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace App\Utils\DDNS;
  3. use App\Utils\IP;
  4. use App\Utils\Library\Templates\DNS;
  5. use Arr;
  6. use Cache;
  7. use Http;
  8. use Log;
  9. use RuntimeException;
  10. class Namecheap implements DNS
  11. {
  12. // 开发依据: https://www.namecheap.com/support/api/methods/
  13. private const API_ENDPOINT = 'https://api.namecheap.com/xml.response';
  14. public const KEY = 'namecheap';
  15. public const LABEL = 'Namecheap';
  16. private string $username;
  17. private string $apiKey;
  18. private array $domainInfo;
  19. private array $domainRecords;
  20. public function __construct(private readonly string $subdomain)
  21. {
  22. $this->username = sysConfig('ddns_key');
  23. $this->apiKey = sysConfig('ddns_secret');
  24. $this->domainInfo = $this->parseDomainInfo();
  25. $this->domainRecords = $this->fetchDomainRecords();
  26. }
  27. private function parseDomainInfo(): array
  28. {
  29. $domains = Cache::remember('ddns_get_domains', now()->addHour(), function () {
  30. return array_map(static function ($domain) {
  31. return $domain['@attributes']['Name'];
  32. }, $this->sendRequest('namecheap.domains.getList')['DomainGetListResult']['Domain']);
  33. });
  34. if ($domains) {
  35. $matched = Arr::first($domains, fn ($domain) => str_contains($this->subdomain, $domain));
  36. }
  37. if (empty($matched)) {
  38. throw new RuntimeException('['.self::LABEL." — domains.getList] The subdomain $this->subdomain does not match any domain in your account.");
  39. }
  40. $domainParts = explode('.', $matched);
  41. return [
  42. 'sub' => rtrim(substr($this->subdomain, 0, -strlen($matched)), '.'),
  43. 'domain' => $domainParts[0],
  44. 'tld' => end($domainParts),
  45. ];
  46. }
  47. private function sendRequest(string $action, array $parameters = []): array
  48. {
  49. $parameters = array_merge([
  50. 'ApiUser' => $this->username,
  51. 'ApiKey' => $this->apiKey,
  52. 'UserName' => $this->username,
  53. 'ClientIp' => IP::getClientIP(),
  54. 'Command' => $action,
  55. ], $parameters);
  56. $response = Http::timeout(15)->retry(3, 1000)->get(self::API_ENDPOINT, $parameters);
  57. $data = $response->body();
  58. if ($data) {
  59. $data = json_decode(json_encode(simplexml_load_string($data)), true);
  60. if ($response->successful() && $data['@attributes']['Status'] === 'OK') {
  61. return $data['CommandResponse'];
  62. }
  63. Log::error('['.self::LABEL." — $action] 返回错误信息: ".$data['Errors']['Error'] ?? 'Unknown error');
  64. } else {
  65. Log::error('['.self::LABEL." — $action] 请求失败");
  66. }
  67. exit(400);
  68. }
  69. private function fetchDomainRecords(): array
  70. {
  71. $records = $this->sendRequest('namecheap.domains.dns.getHosts', ['SLD' => $this->domainInfo['domain'], 'TLD' => $this->domainInfo['tld']]);
  72. if (isset($records['DomainDNSGetHostsResult']['host'])) {
  73. $hosts = $records['DomainDNSGetHostsResult']['host'];
  74. if (isset($hosts[0])) {
  75. foreach ($hosts as $record) {
  76. $ret[] = $this->parseRecordData($record);
  77. }
  78. } else {
  79. $ret[] = $this->parseRecordData($hosts);
  80. }
  81. }
  82. return $ret ?? [];
  83. }
  84. private function parseRecordData(array $record): array
  85. {
  86. return [
  87. 'name' => $record['@attributes']['Name'],
  88. 'type' => $record['@attributes']['Type'],
  89. 'address' => $record['@attributes']['Address'],
  90. 'ttl' => $record['@attributes']['TTL'],
  91. ];
  92. }
  93. public function store(string $ip, string $type): bool
  94. {
  95. $this->domainRecords[] = [
  96. 'name' => $this->domainInfo['sub'],
  97. 'type' => $type,
  98. 'address' => $ip,
  99. 'ttl' => '60',
  100. ];
  101. return $this->updateDomainRecords();
  102. }
  103. private function updateDomainRecords(): bool
  104. {
  105. $para = ['SLD' => $this->domainInfo['domain'], 'TLD' => $this->domainInfo['tld']];
  106. foreach ($this->domainRecords as $index => $record) {
  107. $para['HostName'.($index + 1)] = $record['name'];
  108. $para['RecordType'.($index + 1)] = $record['type'];
  109. $para['Address'.($index + 1)] = $record['address'];
  110. $para['TTL'.($index + 1)] = $record['ttl'];
  111. }
  112. return $this->sendRequest('namecheap.domains.dns.setHosts', $para)['DomainDNSSetHostsResult']['@attributes']['IsSuccess'] === 'true';
  113. }
  114. public function update(string $latest_ip, string $original_ip, string $type): bool
  115. {
  116. foreach ($this->domainRecords as &$record) {
  117. if ($record['address'] === $original_ip && $record['name'] === $this->domainInfo['sub'] && $record['type'] === $type) {
  118. $record['address'] = $latest_ip;
  119. return $this->updateDomainRecords();
  120. }
  121. }
  122. return false;
  123. }
  124. public function destroy(string $type, string $ip): int|bool
  125. {
  126. if ($ip) {
  127. $this->domainRecords = array_filter($this->domainRecords, function ($record) use ($ip) {
  128. return $record['address'] !== $ip || $record['name'] !== $this->domainInfo['sub'];
  129. });
  130. } elseif ($type) {
  131. $this->domainRecords = array_filter($this->domainRecords, function ($record) use ($type) {
  132. return $record['type'] !== $type || $record['name'] !== $this->domainInfo['sub'];
  133. });
  134. } else {
  135. $this->domainRecords = array_filter($this->domainRecords, function ($record) {
  136. return $record['name'] !== $this->domainInfo['sub'];
  137. });
  138. }
  139. return $this->updateDomainRecords();
  140. }
  141. }