You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
7.7 KiB

1 year ago
  1. <?php
  2. namespace app\lib;
  3. use Exception;
  4. use ZipArchive;
  5. class ThirdPlugins
  6. {
  7. private $url;
  8. private $os;
  9. public function __construct($os)
  10. {
  11. $this->os = $os;
  12. $url = $os == 'Windows' ? config_get('wbt_surl') : config_get('bt_surl');
  13. if(!$url) throw new Exception('请先配置好第三方云端首页URL');
  14. $this->url = $url;
  15. }
  16. //获取插件列表
  17. public function get_plugin_list()
  18. {
  19. $url = $this->os == 'Windows' ? $this->url . 'api/wpanel/get_soft_list' : $this->url . 'api/panel/get_soft_list';
  20. $res = $this->curl($url);
  21. $result = json_decode($res, true);
  22. if($result && isset($result['list']) && isset($result['type'])){
  23. if(empty($result['list']) || empty($result['type'])){
  24. throw new Exception('获取插件列表失败:插件列表为空');
  25. }
  26. return $result;
  27. }else{
  28. throw new Exception('获取插件列表失败:'.(isset($result['msg'])?$result['msg']:'第三方云端连接失败'));
  29. }
  30. }
  31. //下载插件(自动判断是否第三方)
  32. public function download_plugin($plugin_name, $version, $plugin_info){
  33. if($plugin_info['type'] == 10 && isset($plugin_info['versions'][0]['download'])){
  34. $fname = $plugin_info['versions'][0]['download'];
  35. $filemd5 = $plugin_info['versions'][0]['md5'];
  36. $this->download_plugin_other($fname, $filemd5);
  37. if(isset($plugin_info['min_image']) && strpos($plugin_info['min_image'], 'fname=')){
  38. $fname = substr($plugin_info['min_image'], strpos($plugin_info['min_image'], '?fname=')+7);
  39. $this->download_plugin_other($fname);
  40. }
  41. }else{
  42. $this->download_plugin_package($plugin_name, $version);
  43. }
  44. }
  45. //下载插件包
  46. private function download_plugin_package($plugin_name, $version){
  47. $filepath = get_data_dir($this->os).'plugins/package/'.$plugin_name.'-'.$version.'.zip';
  48. $url = $this->url . 'down/download_plugin';
  49. $post = ['name'=>$plugin_name, 'version'=>$version, 'os'=>$this->os];
  50. $this->curl_download($url, $post, $filepath);
  51. if(file_exists($filepath)){
  52. $handle = fopen($filepath, "rb");
  53. $file_head = fread($handle, 4);
  54. fclose($handle);
  55. if(bin2hex($file_head) === '504b0304'){
  56. $zip = new ZipArchive;
  57. if ($zip->open($filepath) === true)
  58. {
  59. $zip->close();
  60. return true;
  61. }else{
  62. @unlink($filepath);
  63. throw new Exception('插件包解压缩失败');
  64. }
  65. }else{
  66. $handle = fopen($filepath, "rb");
  67. $errmsg = htmlspecialchars(fgets($handle));
  68. fclose($handle);
  69. @unlink($filepath);
  70. throw new Exception('下载插件包失败:'.($errmsg?$errmsg:'未知错误'));
  71. }
  72. }else{
  73. throw new Exception('下载插件包失败,本地文件不存在');
  74. }
  75. }
  76. //下载插件主程序文件
  77. public function download_plugin_main($plugin_name, $version){
  78. $filepath = get_data_dir($this->os).'plugins/main/'.$plugin_name.'-'.$version.'.dat';
  79. $url = $this->url . 'down/download_plugin_main';
  80. $post = ['name'=>$plugin_name, 'version'=>$version, 'os'=>$this->os];
  81. $this->curl_download($url, $post, $filepath);
  82. if(file_exists($filepath)){
  83. $line = count(file($filepath));
  84. if($line > 3) return true;
  85. $handle = fopen($filepath, "rb");
  86. $errmsg = htmlspecialchars(fgets($handle));
  87. fclose($handle);
  88. @unlink($filepath);
  89. throw new Exception('下载插件主程序文件失败:'.($errmsg?$errmsg:'未知错误'));
  90. }else{
  91. throw new Exception('下载插件主程序文件失败,本地文件不存在');
  92. }
  93. }
  94. //下载插件其他文件
  95. private function download_plugin_other($fname, $filemd5 = null){
  96. $filepath = get_data_dir().'plugins/other/'.$fname;
  97. @mkdir(dirname($filepath), 0777, true);
  98. $url = $this->url . 'api/Pluginother/get_file?fname='.urlencode($fname);
  99. $this->curl_download($url, false, $filepath);
  100. if(file_exists($filepath)){
  101. $handle = fopen($filepath, "rb");
  102. $file_head = fread($handle, 15);
  103. fclose($handle);
  104. if($file_head === '{"status":false'){
  105. $res = file_get_contents($filepath);
  106. $result = json_decode($res, true);
  107. @unlink($filepath);
  108. throw new Exception('下载插件文件失败:'.($result?$result['msg']:'未知错误'));
  109. }
  110. if($filemd5 && md5_file($filepath) != $filemd5){
  111. $msg = filesize($filepath) < 300 ? file_get_contents($filepath) : '插件文件MD5校验失败';
  112. @unlink($filepath);
  113. throw new Exception($msg);
  114. }
  115. return true;
  116. }else{
  117. throw new Exception('下载插件文件失败,本地文件不存在');
  118. }
  119. }
  120. //获取一键部署列表
  121. public function get_deplist(){
  122. $url = $this->url . 'api/panel/get_deplist';
  123. $post = ['os' => $this->os];
  124. $res = $this->curl($url, http_build_query($post));
  125. $result = json_decode($res, true);
  126. if($result && isset($result['list']) && isset($result['type'])){
  127. if(empty($result['list']) || empty($result['type'])){
  128. throw new Exception('获取一键部署列表失败:一键部署列表为空');
  129. }
  130. return $result;
  131. }else{
  132. throw new Exception('获取一键部署列表失败:'.(isset($result['msg'])?$result['msg']:'第三方云端连接失败'));
  133. }
  134. }
  135. //获取蜘蛛IP列表
  136. public function btwaf_getspiders(){
  137. $url = $this->url . 'api/bt_waf/getSpiders';
  138. $res = $this->curl($url);
  139. $result = json_decode($res, true);
  140. if(isset($result['status']) && !$result['status']){
  141. throw new Exception(isset($result['msg'])?$result['msg']:'获取失败');
  142. }else{
  143. return $result;
  144. }
  145. }
  146. private function curl($url, $post = 0){
  147. $ua = "Mozilla/5.0 (BtCloud; ".request()->root(true).")";
  148. return get_curl($url, $post, 0, 0, 0, $ua);
  149. }
  150. private function curl_download($url, $post, $localpath, $timeout = 300)
  151. {
  152. $ch = curl_init();
  153. curl_setopt($ch, CURLOPT_URL, $url);
  154. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  155. $fp = fopen($localpath, 'w+');
  156. curl_setopt($ch, CURLOPT_FILE, $fp);
  157. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  158. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  159. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (BtCloud; ".request()->root(true).")");
  160. if($post){
  161. curl_setopt($ch, CURLOPT_POST, 1);
  162. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  163. }
  164. curl_exec($ch);
  165. if (curl_errno($ch)) {
  166. $message = curl_error($ch);
  167. curl_close($ch);
  168. fclose($fp);
  169. throw new Exception('下载文件失败:'.$message);
  170. }
  171. $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  172. if($httpcode>299){
  173. curl_close($ch);
  174. fclose($fp);
  175. throw new Exception('下载文件失败:HTTPCODE-'.$httpcode);
  176. }
  177. curl_close($ch);
  178. fclose($fp);
  179. return true;
  180. }
  181. }