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.

254 lines
6.8 KiB

1 week ago
2 years 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. //BTWAF-获取堡塔恶意情报IP库
  121. public function btwaf_getmalicious(){
  122. $url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=btwaf_getmalicious';
  123. $p_data = $this->GetKeyData();
  124. $result = $this->curl($url,$p_data);
  125. $data = json_decode($result,true);
  126. return $data;
  127. }
  128. private function GetKeyData(){
  129. $now_time = time();
  130. $p_data = array(
  131. 'request_token' => md5($now_time.''.md5($this->BT_KEY)),
  132. 'request_time' => $now_time
  133. );
  134. return $p_data;
  135. }
  136. private function curl($url, $data = null, $timeout = 60)
  137. {
  138. //定义cookie保存位置
  139. $cookie_file=app()->getRuntimePath().md5($this->BT_PANEL).'.cookie';
  140. if(!file_exists($cookie_file)){
  141. touch($cookie_file);
  142. }
  143. $ch = curl_init();
  144. curl_setopt($ch, CURLOPT_URL, $url);
  145. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  146. if($data){
  147. curl_setopt($ch, CURLOPT_POST, 1);
  148. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  149. }
  150. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  151. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  152. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  153. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  154. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  155. $output = curl_exec($ch);
  156. curl_close($ch);
  157. return $output;
  158. }
  159. private function curl_download($url, $localpath, $timeout = 300)
  160. {
  161. //定义cookie保存位置
  162. $cookie_file=app()->getRuntimePath().md5($this->BT_PANEL).'.cookie';
  163. if(!file_exists($cookie_file)){
  164. touch($cookie_file);
  165. }
  166. $ch = curl_init();
  167. curl_setopt($ch, CURLOPT_URL, $url);
  168. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  169. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  170. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  171. $fp = fopen($localpath, 'w+');
  172. curl_setopt($ch, CURLOPT_FILE, $fp);
  173. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  174. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  175. curl_exec($ch);
  176. if (curl_errno($ch)) {
  177. $message = curl_error($ch);
  178. curl_close($ch);
  179. fclose($fp);
  180. throw new Exception('下载文件失败:'.$message);
  181. }
  182. $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  183. if($httpcode>299){
  184. curl_close($ch);
  185. fclose($fp);
  186. throw new Exception('下载文件失败:HTTPCODE-'.$httpcode);
  187. }
  188. curl_close($ch);
  189. fclose($fp);
  190. return true;
  191. }
  192. }