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.

242 lines
6.3 KiB

1 year ago
1 year ago
  1. <?php
  2. namespace app\lib;
  3. use Exception;
  4. class Btapi
  5. {
  6. private $BT_KEY; //接口密钥
  7. private $BT_PANEL; //面板地址
  8. public function __construct($bt_panel, $bt_key){
  9. $this->BT_PANEL = $bt_panel;
  10. $this->BT_KEY = $bt_key;
  11. }
  12. //获取面板配置信息
  13. public function get_config(){
  14. $url = $this->BT_PANEL.'/config?action=get_config';
  15. $p_data = $this->GetKeyData();
  16. $result = $this->curl($url,$p_data);
  17. $data = json_decode($result,true);
  18. return $data;
  19. }
  20. //获取已登录用户信息
  21. public function get_user_info(){
  22. $url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=get_user_info';
  23. $p_data = $this->GetKeyData();
  24. $result = $this->curl($url,$p_data);
  25. $data = json_decode($result,true);
  26. return $data;
  27. }
  28. //从云端获取插件列表
  29. public function get_plugin_list(){
  30. $url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=get_plugin_list';
  31. $p_data = $this->GetKeyData();
  32. $result = $this->curl($url,$p_data);
  33. $data = json_decode($result,true);
  34. return $data;
  35. }
  36. //下载插件包,返回文件路径
  37. public function get_plugin_filename($plugin_name, $version){
  38. $url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=download_plugin';
  39. $p_data = $this->GetKeyData();
  40. $p_data['plugin_name'] = $plugin_name;
  41. $p_data['version'] = $version;
  42. $result = $this->curl($url,$p_data);
  43. $data = json_decode($result,true);
  44. return $data;
  45. }
  46. //下载插件主程序文件,返回文件路径
  47. public function get_plugin_main_filename($plugin_name, $version){
  48. $url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=download_plugin_main';
  49. $p_data = $this->GetKeyData();
  50. $p_data['plugin_name'] = $plugin_name;
  51. $p_data['version'] = $version;
  52. $result = $this->curl($url,$p_data);
  53. $data = json_decode($result,true);
  54. return $data;
  55. }
  56. //解密插件主程序py代码,返回文件路径
  57. public function get_decode_plugin_main($plugin_name, $version){
  58. $url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=decode_plugin_main';
  59. $p_data = $this->GetKeyData();
  60. $p_data['plugin_name'] = $plugin_name;
  61. $p_data['version'] = $version;
  62. $result = $this->curl($url,$p_data);
  63. $data = json_decode($result,true);
  64. return $data;
  65. }
  66. //下载插件其他文件,返回文件路径
  67. public function get_plugin_other_filename($fname){
  68. $url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=download_plugin_other';
  69. $p_data = $this->GetKeyData();
  70. $p_data['fname'] = $fname;
  71. $result = $this->curl($url,$p_data);
  72. $data = json_decode($result,true);
  73. return $data;
  74. }
  75. //下载文件
  76. public function download($filename, $localpath){
  77. $url = $this->BT_PANEL.'/download';
  78. $p_data = $this->GetKeyData();
  79. $p_data['filename'] = $filename;
  80. $result = $this->curl_download($url.'?'.http_build_query($p_data), $localpath);
  81. return $result;
  82. }
  83. //获取文件base64
  84. public function get_file($filename){
  85. $url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=get_file';
  86. $p_data = $this->GetKeyData();
  87. $p_data['filename'] = $filename;
  88. $result = $this->curl($url,$p_data);
  89. $data = json_decode($result,true);
  90. return $data;
  91. }
  92. //购买第三方插件
  93. public function create_plugin_other_order($pid){
  94. $url = $this->BT_PANEL.'/auth?action=create_plugin_other_order';
  95. $p_data = $this->GetKeyData();
  96. $p_data['pid'] = $pid;
  97. $p_data['cycle'] = '999';
  98. $p_data['type'] = '0';
  99. $result = $this->curl($url,$p_data);
  100. $data = json_decode($result,true);
  101. return $data;
  102. }
  103. //获取一键部署列表
  104. public function get_deplist(){
  105. $url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=get_deplist';
  106. $p_data = $this->GetKeyData();
  107. $result = $this->curl($url,$p_data);
  108. $data = json_decode($result,true);
  109. return $data;
  110. }
  111. //BTWAF-获取蜘蛛列表
  112. public function btwaf_getspiders(){
  113. $url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=btwaf_getspiders';
  114. $p_data = $this->GetKeyData();
  115. $result = $this->curl($url,$p_data);
  116. $result = str_replace("\u0000", '', $result);
  117. $data = json_decode($result,true);
  118. return $data;
  119. }
  120. private function GetKeyData(){
  121. $now_time = time();
  122. $p_data = array(
  123. 'request_token' => md5($now_time.''.md5($this->BT_KEY)),
  124. 'request_time' => $now_time
  125. );
  126. return $p_data;
  127. }
  128. private function curl($url, $data = null, $timeout = 60)
  129. {
  130. //定义cookie保存位置
  131. $cookie_file=app()->getRuntimePath().md5($this->BT_PANEL).'.cookie';
  132. if(!file_exists($cookie_file)){
  133. touch($cookie_file);
  134. }
  135. $ch = curl_init();
  136. curl_setopt($ch, CURLOPT_URL, $url);
  137. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  138. if($data){
  139. curl_setopt($ch, CURLOPT_POST, 1);
  140. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  141. }
  142. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  143. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  144. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  145. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  146. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  147. $output = curl_exec($ch);
  148. curl_close($ch);
  149. return $output;
  150. }
  151. private function curl_download($url, $localpath, $timeout = 300)
  152. {
  153. //定义cookie保存位置
  154. $cookie_file=app()->getRuntimePath().md5($this->BT_PANEL).'.cookie';
  155. if(!file_exists($cookie_file)){
  156. touch($cookie_file);
  157. }
  158. $ch = curl_init();
  159. curl_setopt($ch, CURLOPT_URL, $url);
  160. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  161. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  162. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  163. $fp = fopen($localpath, 'w+');
  164. curl_setopt($ch, CURLOPT_FILE, $fp);
  165. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  166. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  167. curl_exec($ch);
  168. if (curl_errno($ch)) {
  169. $message = curl_error($ch);
  170. curl_close($ch);
  171. fclose($fp);
  172. throw new Exception('下载文件失败:'.$message);
  173. }
  174. $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  175. if($httpcode>299){
  176. curl_close($ch);
  177. fclose($fp);
  178. throw new Exception('下载文件失败:HTTPCODE-'.$httpcode);
  179. }
  180. curl_close($ch);
  181. fclose($fp);
  182. return true;
  183. }
  184. }