Namesilo.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 (strpos(self::$subDomain, $domain) !== false) {
  34. return [$domain, rtrim(substr(self::$subDomain, 0, -(strlen($domain))), '.')];
  35. }
  36. }
  37. } elseif (strpos(self::$subDomain, $domainList) !== false) {
  38. return [$domainList, rtrim(substr(self::$subDomain, 0, -(strlen($domainList))), '.')];
  39. }
  40. }
  41. return false;
  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. return $this->send('dnsUpdateRecord', [
  72. 'domain' => $domainInfo[0],
  73. 'rrid' => $recordId[0],
  74. 'rrhost' => $domainInfo[1],
  75. 'rrvalue' => $ip,
  76. 'rrttl' => 3600,
  77. ]);
  78. }
  79. private function getRecordId($type = null)
  80. {
  81. $domainInfo = $this->analysisDomain();
  82. if ($domainInfo) {
  83. $records = $this->send('dnsListRecords', ['domain' => $domainInfo[0]]);
  84. if ($records && Arr::has($records, 'resource_record')) {
  85. $records = $records['resource_record'];
  86. $data = null;
  87. foreach ($records as $record) {
  88. if (Arr::has($record, ['host', 'type', 'record_id']) && $record['host'] === self::$subDomain) {
  89. if ($type) {
  90. if ($type === $record['type']) {
  91. $data[] = $record['record_id'];
  92. }
  93. } else {
  94. $data[] = $record['record_id'];
  95. }
  96. }
  97. }
  98. return $data ?: false;
  99. }
  100. }
  101. return false;
  102. }
  103. public function destroy($type)
  104. {
  105. $records = $this->getRecordId($type);
  106. $domainInfo = $this->analysisDomain();
  107. if ($records && $domainInfo) {
  108. $count = 0;
  109. foreach ($records as $record) {
  110. $result = $this->send('dnsDeleteRecord', ['domain' => $domainInfo[0], 'rrid' => $record]);
  111. if ($result) {
  112. $count++;
  113. }
  114. }
  115. return $count;
  116. }
  117. return false;
  118. }
  119. }