Namesilo.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace App\Components\DDNS;
  3. use Arr;
  4. use Log;
  5. class Namesilo
  6. {
  7. private static $apiHost = 'https://www.namesilo.com/api/';
  8. private static $subDomain;
  9. public function __construct($subDomain)
  10. {
  11. self::$subDomain = $subDomain;
  12. }
  13. public function store($ip, $type)
  14. {
  15. $domainInfo = $this->analysisDomain();
  16. if ($domainInfo) {
  17. return $this->send('dnsAddRecord', [
  18. 'domain' => $domainInfo[0],
  19. 'rrtype' => $type,
  20. 'rrhost' => $domainInfo[1],
  21. 'rrvalue' => $ip,
  22. 'rrttl' => 3600,
  23. ]);
  24. }
  25. return false;
  26. }
  27. private function analysisDomain()
  28. {
  29. $domainList = $this->domainList();
  30. if ($domainList) {
  31. if (is_array($domainList)) {
  32. foreach ($domainList as $domain) {
  33. if (str_contains(self::$subDomain, $domain)) {
  34. return [$domain, rtrim(substr(self::$subDomain, 0, -(strlen($domain))), '.')];
  35. }
  36. }
  37. } elseif (str_contains(self::$subDomain, $domainList)) {
  38. return [$domainList, rtrim(substr(self::$subDomain, 0, -(strlen($domainList))), '.')];
  39. }
  40. }
  41. return [];
  42. }
  43. public function domainList()
  44. {
  45. $data = $this->send('listDomains');
  46. if ($data) {
  47. return $data['domains']['domain'];
  48. }
  49. return false;
  50. }
  51. private function send($action, $data = [])
  52. {
  53. $params = [
  54. 'version' => 1,
  55. 'type' => 'xml',
  56. 'key' => sysConfig('ddns_key'),
  57. ];
  58. $query = array_merge($params, $data);
  59. $result = file_get_contents(self::$apiHost.$action.'?'.http_build_query($query));
  60. $result = json_decode(json_encode(simplexml_load_string(trim($result))), true);
  61. if ($result && $result['reply']['code'] === '300' && $result['reply']['detail'] === 'success') {
  62. return $result['reply'];
  63. }
  64. Log::error('[Namesilo API] - ['.$action.'] 请求失败:'.var_export($result, true));
  65. return false;
  66. }
  67. public function update($ip, $type)
  68. {
  69. $recordId = $this->getRecordId($type);
  70. $domainInfo = $this->analysisDomain();
  71. if ($domainInfo && $recordId) {
  72. return $this->send('dnsUpdateRecord', [
  73. 'domain' => $domainInfo[0],
  74. 'rrid' => $recordId[0],
  75. 'rrhost' => $domainInfo[1],
  76. 'rrvalue' => $ip,
  77. 'rrttl' => 3600,
  78. ]);
  79. }
  80. Log::error('[Namesilo API] - [更新] 处理失败:'.var_export($recordId, true).var_export($domainInfo, true));
  81. return false;
  82. }
  83. private function getRecordId($type = null)
  84. {
  85. $domainInfo = $this->analysisDomain();
  86. if ($domainInfo) {
  87. $records = $this->send('dnsListRecords', ['domain' => $domainInfo[0]]);
  88. if ($records && Arr::has($records, 'resource_record')) {
  89. $records = $records['resource_record'];
  90. $data = null;
  91. foreach ($records as $record) {
  92. if (Arr::has($record, ['host', 'type', 'record_id']) && $record['host'] === self::$subDomain) {
  93. if ($type) {
  94. if ($type === $record['type']) {
  95. $data[] = $record['record_id'];
  96. }
  97. } else {
  98. $data[] = $record['record_id'];
  99. }
  100. }
  101. }
  102. return $data ?: false;
  103. }
  104. }
  105. return false;
  106. }
  107. public function destroy($type)
  108. {
  109. $records = $this->getRecordId($type);
  110. $domainInfo = $this->analysisDomain();
  111. if ($records && $domainInfo) {
  112. $count = 0;
  113. foreach ($records as $record) {
  114. $result = $this->send('dnsDeleteRecord', ['domain' => $domainInfo[0], 'rrid' => $record]);
  115. if ($result) {
  116. $count++;
  117. }
  118. }
  119. return $count;
  120. }
  121. return false;
  122. }
  123. }