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.

167 lines
5.5 KiB

1 year ago
10 months ago
1 year ago
  1. <?php
  2. namespace app\lib;
  3. use Exception;
  4. use ZipArchive;
  5. class Plugins
  6. {
  7. private static function get_btapi($os){
  8. if(self::is_third($os)){
  9. return new ThirdPlugins($os);
  10. }else{
  11. return new BtPlugins($os);
  12. }
  13. }
  14. private static function is_third($os){
  15. $type = $os == 'Windows' ? config_get('wbt_type') : config_get('bt_type');
  16. return $type == 1;
  17. }
  18. //刷新插件列表
  19. public static function refresh_plugin_list($os = 'Linux'){
  20. $btapi = self::get_btapi($os);
  21. $result = $btapi->get_plugin_list();
  22. self::save_plugin_list($result, $os);
  23. }
  24. //保存插件列表
  25. private static function save_plugin_list($data, $os){
  26. $data['ip'] = '127.0.0.1';
  27. $data['serverid'] = '';
  28. $data['beta'] = 0;
  29. $data['uid'] = 1;
  30. $data['skey'] = '';
  31. $list = [];
  32. foreach($data['list'] as $plugin){
  33. if(isset($plugin['endtime'])) $plugin['endtime'] = 0;
  34. $list[] = $plugin;
  35. }
  36. $data['list'] = $list;
  37. $data['ltd'] = strtotime('+10 year');
  38. $json_file = get_data_dir($os).'config/plugin_list.json';
  39. if(!file_put_contents($json_file, json_encode($data))){
  40. throw new Exception('保存插件列表失败,文件无写入权限');
  41. }
  42. }
  43. //获取插件列表
  44. public static function get_plugin_list($os = 'Linux'){
  45. $json_file = get_data_dir($os).'config/plugin_list.json';
  46. if(file_exists($json_file)){
  47. $data = file_get_contents($json_file);
  48. $json_arr = json_decode($data, true);
  49. if($json_arr){
  50. return $json_arr;
  51. }
  52. }
  53. return false;
  54. }
  55. //获取一个插件信息
  56. public static function get_plugin_info($name, $os = 'Linux'){
  57. $json_arr = self::get_plugin_list($os);
  58. if(!$json_arr) return null;
  59. foreach($json_arr['list'] as $plugin){
  60. if($plugin['name'] == $name){
  61. return $plugin;
  62. }
  63. }
  64. return null;
  65. }
  66. //下载插件(自动判断是否第三方)
  67. public static function download_plugin($plugin_name, $version, $os = 'Linux'){
  68. $plugin_info = Plugins::get_plugin_info($plugin_name, $os);
  69. if(!$plugin_info) throw new Exception('未找到该插件信息');
  70. $btapi = self::get_btapi($os);
  71. $btapi->download_plugin($plugin_name, $version, $plugin_info);
  72. }
  73. //下载插件主程序文件
  74. public static function download_plugin_main($plugin_name, $version, $os = 'Linux'){
  75. $btapi = self::get_btapi($os);
  76. $btapi->download_plugin_main($plugin_name, $version);
  77. }
  78. //本地解密插件主程序文件
  79. public static function decode_plugin_main_local($main_filepath, $os = 'Linux'){
  80. $btapi = new BtPlugins($os);
  81. return $btapi->decode_plugin_main_local($main_filepath);
  82. }
  83. //本地解密模块文件
  84. public static function decode_module_file($filepath){
  85. $src = file_get_contents($filepath);
  86. if($src===false)throw new Exception('文件打开失败');
  87. if(!$src || strpos($src, 'import ')!==false)return 0;
  88. $key = 'Z2B87NEAS2BkxTrh';
  89. $iv = 'WwadH66EGWpeeTT6';
  90. $data_arr = explode("\n", $src);
  91. $de_text = '';
  92. foreach($data_arr as $data){
  93. $data = trim($data);
  94. if(!empty($data)){
  95. $tmp = openssl_decrypt($data, 'aes-128-cbc', $key, 0, $iv);
  96. if($tmp) $de_text .= $tmp;
  97. }
  98. }
  99. if(!empty($de_text) && strpos($de_text, 'import ')!==false){
  100. file_put_contents($filepath, $de_text);
  101. return 1;
  102. }
  103. return 2;
  104. }
  105. //刷新一键部署列表
  106. public static function refresh_deplist($os = 'Linux'){
  107. $btapi = self::get_btapi($os);
  108. $result = $btapi->get_deplist();
  109. $json_file = get_data_dir($os).'config/deployment_list.json';
  110. if(!file_put_contents($json_file, json_encode($result))){
  111. throw new Exception('保存一键部署列表失败,文件无写入权限');
  112. }
  113. }
  114. //获取一键部署列表
  115. public static function get_deplist($os = 'Linux'){
  116. $json_file = get_data_dir($os).'config/deployment_list.json';
  117. if(file_exists($json_file)){
  118. $data = file_get_contents($json_file);
  119. $json_arr = json_decode($data, true);
  120. if($json_arr){
  121. return $json_arr;
  122. }
  123. }
  124. return false;
  125. }
  126. //获取蜘蛛IP列表
  127. public static function btwaf_getspiders(){
  128. $result = cache('btwaf_getspiders');
  129. if($result){
  130. return $result;
  131. }
  132. $btapi = self::get_btapi('Linux');
  133. $result = $btapi->btwaf_getspiders();
  134. cache('btwaf_getspiders', $result, 3600 * 24 * 3);
  135. return $result;
  136. }
  137. //分类获取蜘蛛IP列表
  138. public static function get_spider($type){
  139. $result = cache('get_spider_'.$type);
  140. if($result){
  141. return $result;
  142. }
  143. $url = 'https://www.bt.cn/api/panel/get_spider?spider='.$type;
  144. $data = get_curl($url);
  145. $result = json_decode($data, true);
  146. if(!$result) return [];
  147. cache('get_spider_'.$type, $result, 3600 * 24);
  148. return $result;
  149. }
  150. }