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.

265 lines
7.0 KiB

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