Namesilo.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Utils\DDNS;
  3. use App\Utils\Library\Templates\DNS;
  4. use Arr;
  5. use Cache;
  6. use Log;
  7. use RuntimeException;
  8. class Namesilo implements DNS
  9. {
  10. // 开发依据: https://www.namesilo.com/api-reference
  11. private const API_ENDPOINT = 'https://www.namesilo.com/api/';
  12. public const KEY = 'namesilo';
  13. public const LABEL = 'Namesilo';
  14. private string $apiKey;
  15. private array $domainInfo;
  16. public function __construct(private readonly string $subdomain)
  17. {
  18. $this->apiKey = sysConfig('ddns_key');
  19. $this->domainInfo = $this->parseDomainInfo();
  20. }
  21. private function parseDomainInfo(): array
  22. {
  23. $domains = Cache::remember('ddns_get_domains', now()->addHour(), function () {
  24. return $this->sendRequest('listDomains')['domains']['domain'];
  25. });
  26. if ($domains) {
  27. $matched = Arr::first($domains, fn ($domain) => str_contains($this->subdomain, $domain));
  28. }
  29. if (empty($matched)) {
  30. throw new RuntimeException("[Namesilo – listDomains] The subdomain {$this->subdomain} does not match any domain in your account.");
  31. }
  32. return [
  33. 'sub' => rtrim(substr($this->subdomain, 0, -strlen($matched)), '.'),
  34. 'domain' => $matched,
  35. ];
  36. }
  37. private function sendRequest(string $operation, array $parameters = []): array
  38. {
  39. $request = simplexml_load_string(file_get_contents(self::API_ENDPOINT.$operation.'?'.Arr::query(array_merge(['version' => 1, 'type' => 'xml', 'key' => $this->apiKey], $parameters))));
  40. if ($request) {
  41. $data = json_decode(json_encode($request), true);
  42. if ($data && $data['reply']['code'] === '300') {
  43. return $data['reply'];
  44. }
  45. Log::error('[Namesilo – '.$operation.'] 返回错误信息:'.$data['reply']['detail'] ?? 'Unknown error');
  46. } else {
  47. Log::error('[Namesilo – '.$operation.'] 请求失败');
  48. }
  49. exit(400);
  50. }
  51. public function store(string $ip, string $type): bool
  52. {
  53. return (bool) $this->sendRequest('dnsAddRecord', [
  54. 'domain' => $this->domainInfo['domain'],
  55. 'rrtype' => $type,
  56. 'rrhost' => $this->domainInfo['sub'],
  57. 'rrvalue' => $ip,
  58. 'rrttl' => 3600,
  59. ]);
  60. }
  61. public function update(string $latest_ip, string $original_ip, string $type): bool
  62. {
  63. $record = Arr::first($this->getRecordIds($type, $original_ip));
  64. if ($record) {
  65. $ret = $this->sendRequest('dnsUpdateRecord', [
  66. 'domain' => $this->domainInfo['domain'],
  67. 'rrid' => $record,
  68. 'rrhost' => $this->domainInfo['sub'],
  69. 'rrvalue' => $latest_ip,
  70. 'rrttl' => 3600,
  71. ]);
  72. }
  73. return (bool) ($ret ?? false);
  74. }
  75. private function getRecordIds(string $type, string $ip): array|false
  76. {
  77. $response = $this->sendRequest('dnsListRecords', ['domain' => $this->domainInfo['domain']]);
  78. if (isset($response['resource_record'])) {
  79. $records = $response['resource_record'];
  80. if ($ip) {
  81. $records = array_filter($records, function ($record) use ($ip) {
  82. return $record['host'] === $this->subdomain && $record['value'] === $ip;
  83. });
  84. } elseif ($type) {
  85. $records = array_filter($records, function ($record) use ($type) {
  86. return $record['host'] === $this->subdomain && $record['type'] === $type;
  87. });
  88. } else {
  89. $records = array_filter($records, function ($record) {
  90. return $record['host'] === $this->subdomain;
  91. });
  92. }
  93. return array_column($records, 'record_id');
  94. }
  95. return [];
  96. }
  97. public function destroy(string $type, string $ip): int
  98. {
  99. $recordIds = $this->getRecordIds($type, $ip);
  100. $deletedCount = 0;
  101. foreach ($recordIds as $recordId) {
  102. if ($this->sendRequest('dnsDeleteRecord', ['domain' => $this->domainInfo['domain'], 'rrid' => $recordId])) {
  103. $deletedCount++;
  104. }
  105. }
  106. return $deletedCount;
  107. }
  108. }