Namesilo.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Utils\DDNS;
  3. use App\Utils\Library\Templates\DNS;
  4. use Arr;
  5. use Log;
  6. class Namesilo implements DNS
  7. {
  8. // 开发依据: https://www.namesilo.com/api-reference
  9. private const API_HOST = 'https://www.namesilo.com/api/';
  10. private string $apiKey;
  11. private array $domainData;
  12. public function __construct(private readonly string $subDomain)
  13. {
  14. $this->apiKey = sysConfig('ddns_key');
  15. $data = $this->analysisDomain();
  16. if ($data) {
  17. $this->domainData = $data;
  18. } else {
  19. abort(400, '域名存在异常');
  20. }
  21. }
  22. private function analysisDomain(): array
  23. {
  24. $domains = Arr::get($this->send('listDomains'), 'domains.domain');
  25. if ($domains) {
  26. foreach ($domains as $domain) {
  27. if (str_contains($this->subDomain, $domain)) {
  28. return ['rr' => rtrim(substr($this->subDomain, 0, -strlen($domain)), '.'), 'host' => $domain];
  29. }
  30. }
  31. Log::error('[DNS] Namesilo - 错误域名 '.$this->subDomain.' 不在账号拥有域名里');
  32. }
  33. exit(400);
  34. }
  35. private function send(string $operation, array $parameters = []): array
  36. {
  37. $request = simplexml_load_string(file_get_contents(self::API_HOST.$operation.'?'.Arr::query(array_merge(['version' => 1, 'type' => 'xml', 'key' => $this->apiKey], $parameters))));
  38. if ($request) {
  39. $result = json_decode(json_encode($request), true);
  40. if ($result && $result['reply']['code'] === '300') {
  41. return Arr::except($result['reply'], ['code', 'detail']);
  42. }
  43. Log::error('[Namesilo - '.$operation.'] 返回错误信息:'.$result['reply']['detail']);
  44. } else {
  45. Log::error('[Namesilo - '.$operation.'] 请求失败');
  46. }
  47. exit(400);
  48. }
  49. public function store(string $ip, string $type): bool
  50. {
  51. $ret = $this->send('dnsAddRecord', [
  52. 'domain' => $this->domainData['host'],
  53. 'rrtype' => $type,
  54. 'rrhost' => $this->domainData['rr'],
  55. 'rrvalue' => $ip,
  56. 'rrttl' => 3600,
  57. ]);
  58. return (bool) $ret;
  59. }
  60. public function update(string $latest_ip, string $original_ip, string $type): bool
  61. {
  62. $record = Arr::first($this->getRecordId($type, $original_ip));
  63. if ($record) {
  64. $ret = $this->send('dnsUpdateRecord', [
  65. 'domain' => $this->domainData['host'],
  66. 'rrid' => $record,
  67. 'rrhost' => $this->domainData['rr'],
  68. 'rrvalue' => $latest_ip,
  69. 'rrttl' => 3600,
  70. ]);
  71. }
  72. return (bool) ($ret ?? false);
  73. }
  74. private function getRecordId(string $type, string $ip): array|false
  75. {
  76. $records = $this->send('dnsListRecords', ['domain' => $this->domainData['host']]);
  77. if (Arr::has($records, 'resource_record')) {
  78. $records = $records['resource_record'];
  79. if ($ip) {
  80. $filtered = Arr::where($records, function (array $value) use ($ip) {
  81. return $value['host'] === $this->subDomain && $value['value'] === $ip;
  82. });
  83. } elseif ($type) {
  84. $filtered = Arr::where($records, function (array $value) use ($type) {
  85. return $value['host'] === $this->subDomain && $value['type'] === $type;
  86. });
  87. } else {
  88. $filtered = Arr::where($records, function (array $value) {
  89. return $value['host'] === $this->subDomain;
  90. });
  91. }
  92. if ($filtered) {
  93. return data_get($filtered, '*.record_id');
  94. }
  95. }
  96. return false;
  97. }
  98. public function destroy(string $type = '', string $ip = ''): int
  99. {
  100. $records = $this->getRecordId($type, $ip);
  101. $count = 0;
  102. if ($records) {
  103. foreach ($records as $record) {
  104. $result = $this->send('dnsDeleteRecord', ['domain' => $this->domainData['host'], 'rrid' => $record]);
  105. if ($result === []) {
  106. $count++;
  107. }
  108. }
  109. }
  110. return $count;
  111. }
  112. }