Namesilo.php 4.3 KB

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