Namesilo.php 3.8 KB

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