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.

448 lines
16 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
12 months ago
1 year ago
12 months ago
1 year ago
12 months ago
1 year ago
12 months ago
1 year ago
12 months ago
1 year ago
12 months ago
1 year ago
12 months ago
1 year ago
12 months ago
1 year ago
  1. <?php
  2. namespace app\controller;
  3. use think\facade\Db;
  4. use app\BaseController;
  5. use app\lib\Plugins;
  6. class Api extends BaseController
  7. {
  8. //获取插件列表
  9. public function get_plugin_list(){
  10. if(!$this->checklist()) return json('你的服务器被禁止使用此云端');
  11. $record = Db::name('record')->where('ip',$this->clientip)->find();
  12. if($record){
  13. Db::name('record')->where('id',$record['id'])->update(['usetime'=>date("Y-m-d H:i:s")]);
  14. }else{
  15. Db::name('record')->insert(['ip'=>$this->clientip, 'addtime'=>date("Y-m-d H:i:s"), 'usetime'=>date("Y-m-d H:i:s")]);
  16. }
  17. $json_arr = Plugins::get_plugin_list();
  18. if(!$json_arr) return json((object)[]);
  19. return json($json_arr);
  20. }
  21. //获取插件列表(win)
  22. public function get_plugin_list_win(){
  23. if(!$this->checklist()) return json('你的服务器被禁止使用此云端');
  24. $record = Db::name('record')->where('ip',$this->clientip)->find();
  25. if($record){
  26. Db::name('record')->where('id',$record['id'])->update(['usetime'=>date("Y-m-d H:i:s")]);
  27. }else{
  28. Db::name('record')->insert(['ip'=>$this->clientip, 'addtime'=>date("Y-m-d H:i:s"), 'usetime'=>date("Y-m-d H:i:s")]);
  29. }
  30. $json_arr = Plugins::get_plugin_list('Windows');
  31. if(!$json_arr) return json((object)[]);
  32. return json($json_arr);
  33. }
  34. //下载插件包
  35. public function download_plugin(){
  36. $plugin_name = input('post.name');
  37. $version = input('post.version');
  38. $os = input('post.os');
  39. if(!$plugin_name || !$version){
  40. return '参数不能为空';
  41. }
  42. if(!in_array($os,['Windows','Linux'])) $os = 'Linux';
  43. if(!preg_match('/^[a-zA-Z0-9_]+$/', $plugin_name) || !preg_match('/^[0-9.]+$/', $version)){
  44. return '参数不正确';
  45. }
  46. if(!$this->checklist()) '你的服务器被禁止使用此云端';
  47. $filepath = get_data_dir($os).'plugins/package/'.$plugin_name.'-'.$version.'.zip';
  48. if(file_exists($filepath)){
  49. $filename = $plugin_name.'.zip';
  50. $this->output_file($filepath, $filename);
  51. }else{
  52. return '云端不存在该插件包';
  53. }
  54. }
  55. //下载插件主文件
  56. public function download_plugin_main(){
  57. $plugin_name = input('post.name');
  58. $version = input('post.version');
  59. $os = input('post.os');
  60. if(!$plugin_name || !$version){
  61. return '参数不能为空';
  62. }
  63. if(!in_array($os,['Windows','Linux'])) $os = 'Linux';
  64. if(!preg_match('/^[a-zA-Z0-9_]+$/', $plugin_name) || !preg_match('/^[0-9.]+$/', $version)){
  65. return '参数不正确';
  66. }
  67. if(!$this->checklist()) '你的服务器被禁止使用此云端';
  68. $filepath = get_data_dir($os).'plugins/main/'.$plugin_name.'-'.$version.'.dat';
  69. if(file_exists($filepath)){
  70. $filename = $plugin_name.'_main.py';
  71. $this->output_file($filepath, $filename);
  72. }else{
  73. $filepath = get_data_dir($os).'plugins/folder/'.$plugin_name.'-'.$version.'/'.$plugin_name.'/'.$plugin_name.'_main.py';
  74. if(file_exists($filepath)){
  75. $filename = $plugin_name.'_main.py';
  76. $this->output_file($filepath, $filename);
  77. }else{
  78. return '云端不存在该插件主文件';
  79. }
  80. }
  81. }
  82. //下载插件其他文件
  83. public function download_plugin_other(){
  84. $fname = input('get.fname');
  85. if(!$fname){
  86. return json(['status'=>false, 'msg'=>'参数不能为空']);
  87. }
  88. if(strpos(dirname($fname),'.')!==false)return json(['status'=>false, 'msg'=>'参数不正确']);
  89. if(!$this->checklist()) return json(['status'=>false, 'msg'=>'你的服务器被禁止使用此云端']);
  90. $filepath = get_data_dir().'plugins/other/'.$fname;
  91. if(file_exists($filepath)){
  92. $filename = basename($fname);
  93. $this->output_file($filepath, $filename);
  94. }else{
  95. return json(['status'=>false, 'msg'=>'云端不存在该插件文件']);
  96. }
  97. }
  98. public function get_update_logs(){
  99. $type = input('get.type');
  100. if($type == 'Windows'){
  101. $version = config_get('new_version_win');
  102. $data = [
  103. [
  104. 'title' => 'Linux面板'.$version,
  105. 'body' => config_get('update_msg_win'),
  106. 'addtime' => config_get('update_date_win')
  107. ]
  108. ];
  109. }else{
  110. $version = config_get('new_version');
  111. $data = [
  112. [
  113. 'title' => 'Linux面板'.$version,
  114. 'body' => config_get('update_msg'),
  115. 'addtime' => config_get('update_date')
  116. ]
  117. ];
  118. }
  119. return jsonp($data);
  120. }
  121. public function get_version(){
  122. $version = config_get('new_version');
  123. return $version;
  124. }
  125. public function get_version_win(){
  126. $version = config_get('new_version_win');
  127. return $version;
  128. }
  129. //安装统计
  130. public function setup_count(){
  131. return 'ok';
  132. }
  133. //检测更新
  134. public function check_update(){
  135. $version = config_get('new_version');
  136. $down_url = request()->root(true).'/install/update/LinuxPanel-'.$version.'.zip';
  137. $data = [
  138. 'force' => false,
  139. 'version' => $version,
  140. 'downUrl' => $down_url,
  141. 'updateMsg' => config_get('update_msg'),
  142. 'uptime' => config_get('update_date'),
  143. 'is_beta' => 0,
  144. 'adviser' => -1,
  145. 'btb' => '',
  146. 'beta' => [
  147. 'version' => $version,
  148. 'downUrl' => $down_url,
  149. 'updateMsg' => config_get('update_msg'),
  150. 'uptime' => config_get('update_date'),
  151. ]
  152. ];
  153. return json($data);
  154. }
  155. //检测更新(win)
  156. public function check_update_win(){
  157. $version = config_get('new_version_win');
  158. $down_url = request()->root(true).'/win/panel/panel_'.$version.'.zip';
  159. $data = [
  160. 'force' => false,
  161. 'version' => $version,
  162. 'downUrl' => $down_url,
  163. 'updateMsg' => config_get('update_msg_win'),
  164. 'uptime' => config_get('update_date_win'),
  165. 'is_beta' => 0,
  166. 'py_version' => '3.8.6',
  167. 'adviser' => -1,
  168. 'is_rec' => -1,
  169. 'btb' => '',
  170. 'beta' => [
  171. 'py_version' => '3.8.6',
  172. 'version' => $version,
  173. 'downUrl' => $down_url,
  174. 'updateMsg' => config_get('update_msg_win'),
  175. 'uptime' => config_get('update_date_win'),
  176. ]
  177. ];
  178. return json($data);
  179. }
  180. //宝塔云监控获取最新版本
  181. public function btm_latest_version(){
  182. $data = [
  183. 'version' => config_get('new_version_btm'),
  184. 'description' => config_get('update_msg_btm'),
  185. 'create_time' => config_get('update_date_btm')
  186. ];
  187. return json($data);
  188. }
  189. //宝塔云监控更新日志
  190. public function btm_update_history(){
  191. $data = [
  192. [
  193. 'version' => config_get('new_version_btm'),
  194. 'description' => config_get('update_msg_btm'),
  195. 'create_time' => config_get('update_date_btm')
  196. ]
  197. ];
  198. return json($data);
  199. }
  200. //宝塔云WAF最新版本
  201. public function btwaf_latest_version(){
  202. $data = [
  203. 'version' => '2.5',
  204. 'description' => '暂无更新日志',
  205. 'create_time' => 1701252997,
  206. ];
  207. $data = bin2hex(json_encode($data));
  208. return json(['status'=>true,'err_no'=>0,'msg'=>'获取成功','data'=>$data]);
  209. }
  210. //获取内测版更新日志
  211. public function get_beta_logs(){
  212. return json(['beta_ps'=>'当前暂无内测版', 'list'=>[]]);
  213. }
  214. //检查用户绑定是否正确
  215. public function check_auth_key(){
  216. return '1';
  217. }
  218. //从云端验证域名是否可访问
  219. public function check_domain(){
  220. $domain = input('post.domain',null,'trim');
  221. $ssl = input('post.ssl/d');
  222. if(!$domain) return json(['status'=>false, 'msg'=>'域名不能为空']);
  223. if(!strpos($domain,'.')) return json(['status'=>false, 'msg'=>'域名格式不正确']);
  224. $domain = str_replace('*.','',$domain);
  225. $ip = gethostbyname($domain);
  226. if(!$ip || $ip == $domain){
  227. return json(['status'=>false, 'msg'=>'无法访问']);
  228. }else{
  229. return json(['status'=>true, 'msg'=>'访问正常']);
  230. }
  231. }
  232. //同步时间
  233. public function get_time(){
  234. return time();
  235. }
  236. //同步时间
  237. public function get_win_date(){
  238. return date("Y-m-d H:i:s");
  239. }
  240. //查询是否专业版(废弃)
  241. public function is_pro(){
  242. return json(['endtime'=>true, 'code'=>1]);
  243. }
  244. //获取产品推荐信息
  245. public function get_plugin_remarks(){
  246. return json(['list'=>[], 'pro_list'=>[], 'kfqq'=>'', 'kf'=>'', 'qun'=>'']);
  247. }
  248. //获取指定插件评分
  249. public function get_plugin_socre(){
  250. return json(['total'=>0, 'split'=>[0,0,0,0,0],'page'=>"<div><span class='Pcurrent'>1</span><span class='Pcount'>共计0条数据</span></div>",'data'=>[]]);
  251. }
  252. //提交插件评分
  253. public function plugin_score(){
  254. return json(['status'=>true, 'msg'=>'您的评分已成功提交,感谢您的支持!']);
  255. }
  256. //获取IP地址
  257. public function get_ip_address(){
  258. return $this->clientip;
  259. }
  260. //绑定账号
  261. public function get_auth_token(){
  262. if(!input('?post.data')) return json(['status'=>false, 'msg'=>'参数不能为空']);
  263. $reqData = hex2bin(input('post.data'));
  264. parse_str($reqData, $arr);
  265. $serverid = $arr['serverid'];
  266. $userinfo = ['uid'=>1, 'username'=>'Administrator', 'address'=>'127.0.0.1', 'serverid'=>$serverid, 'access_key'=>random(48), 'secret_key'=>random(48), 'ukey'=>md5(time()), 'state'=>1];
  267. $data = bin2hex(json_encode($userinfo));
  268. return json(['status'=>true, 'msg'=>'登录成功!', 'data'=>$data]);
  269. }
  270. //绑定账号新
  271. public function authorization_login(){
  272. if(!input('?post.data')) return json(['status'=>false, 'msg'=>'参数不能为空']);
  273. $reqData = hex2bin(input('post.data'));
  274. parse_str($reqData, $arr);
  275. $serverid = $arr['serverid'];
  276. $userinfo = ['uid'=>1, 'username'=>'Administrator', 'ip'=>'127.0.0.1', 'server_id'=>$serverid, 'access_key'=>random(48), 'secret_key'=>random(48)];
  277. $data = bin2hex(json_encode($userinfo));
  278. return json(['status'=>true, 'err_no'=>0, 'msg'=>'账号绑定成功', 'data'=>$data]);
  279. }
  280. //刷新授权信息
  281. public function authorization_info(){
  282. if(!input('?post.data')) return json(['status'=>false, 'msg'=>'参数不能为空']);
  283. $reqData = hex2bin(input('post.data'));
  284. parse_str($reqData, $arr);
  285. $id = isset($arr['id'])&&$arr['id']>0?$arr['id']:1;
  286. $userinfo = ['id'=>$id, 'product'=>$arr['product'], 'status'=>2, 'clients'=>9999, 'durations'=>0, 'end_time'=>strtotime('+10 year')];
  287. $data = bin2hex(json_encode($userinfo));
  288. return json(['status'=>true, 'err_no'=>0, 'data'=>$data]);
  289. }
  290. //刷新授权信息
  291. public function update_license(){
  292. if(!input('?post.data')) return json(['status'=>false, 'msg'=>'参数不能为空']);
  293. $reqData = hex2bin(input('post.data'));
  294. parse_str($reqData, $arr);
  295. if(!isset($arr['product']) || !isset($arr['serverid'])) return json(['status'=>false, 'msg'=>'缺少参数']);
  296. $license_data = ['product'=>$arr['product'], 'uid'=>random(32), 'phone'=>'138****8888', 'auth_id'=>random(32), 'server_id'=>substr($arr['serverid'], 0, 32), 'auth'=>['apis'=>[], 'menu'=>[], 'extra'=>['type'=>3,'location'=>-1,'smart_cc'=>-1,'site'=>0]], 'pages'=>[], 'end_time'=>strtotime('+10 year')];
  297. $json = json_encode($license_data);
  298. [$public_key, $private_key] = generateKeyPairs();
  299. $public_key = pemToBase64($public_key);
  300. $key1 = random(32);
  301. $key2 = substr($public_key, 0, 32);
  302. $encrypted1 = licenseEncrypt($json, $key1);
  303. $encrypted2 = licenseEncrypt($key1, $key2);
  304. $sign_data = $encrypted1.'.'.$encrypted2;
  305. openssl_sign($sign_data, $signature, $private_key, OPENSSL_ALGO_SHA256);
  306. $signature = base64_encode($signature);
  307. $license = base64_encode($sign_data.'.'.$signature);
  308. $data = bin2hex(json_encode(['public_key'=>$public_key, 'license'=>$license]));
  309. return json(['status'=>true, 'err_no'=>0, 'msg'=>'授权获取成功', 'data'=>$data]);
  310. }
  311. public function is_obtained_btw_trial(){
  312. $data = ['is_obtained'=>0];
  313. $data = bin2hex(json_encode($data));
  314. return json(['status'=>true, 'err_no'=>0, 'data'=>$data, 'msg'=>'检测成功']);
  315. }
  316. //一键部署列表
  317. public function get_deplist(){
  318. $os = input('post.os');
  319. $json_arr = Plugins::get_deplist($os);
  320. if(!$json_arr) return json([]);
  321. return json($json_arr);
  322. }
  323. //获取宝塔SSL列表
  324. public function get_ssl_list(){
  325. $data = bin2hex('[]');
  326. return json(['status'=>true, 'msg'=>'', 'data'=>$data]);
  327. }
  328. public function return_success(){
  329. return json(['status'=>true, 'msg'=>1, 'data'=>(object)[]]);
  330. }
  331. public function return_error(){
  332. return json(['status'=>false, 'msg'=>'不支持当前操作']);
  333. }
  334. public function return_error2(){
  335. return json(['success'=>false, 'res'=>'不支持当前操作']);
  336. }
  337. public function return_empty(){
  338. return '';
  339. }
  340. public function return_empty_array(){
  341. return json([]);
  342. }
  343. public function return_page_data(){
  344. return json(['page'=>"<div><span class='Pcurrent'>1</span><span class='Pnumber'>1/0</span><span class='Pline'>从1-1000条</span><span class='Pcount'>共计0条数据</span></div>", 'data'=>[]]);
  345. }
  346. public function btwaf_getspiders(){
  347. try{
  348. $result = Plugins::btwaf_getspiders();
  349. return json($result);
  350. }catch(\Exception $e){
  351. return json(['status'=>false, 'msg'=>$e->getMessage()]);
  352. }
  353. }
  354. //检查黑白名单
  355. private function checklist(){
  356. if(config_get('whitelist') == 1){
  357. if(Db::name('white')->where('ip', $this->clientip)->where('enable', 1)->find()){
  358. return true;
  359. }
  360. return false;
  361. }else{
  362. if(Db::name('black')->where('ip', $this->clientip)->where('enable', 1)->find()){
  363. return false;
  364. }
  365. return true;
  366. }
  367. }
  368. //下载大文件
  369. private function output_file($filepath, $filename){
  370. $filesize = filesize($filepath);
  371. $filemd5 = md5_file($filepath);
  372. ob_clean();
  373. header("Content-Type: application/octet-stream");
  374. header("Content-Disposition: attachment; filename={$filename}.zip");
  375. header("Content-Length: {$filesize}");
  376. header("File-size: {$filesize}");
  377. header("Content-md5: {$filemd5}");
  378. $read_buffer = 1024 * 100;
  379. $handle = fopen($filepath, 'rb');
  380. $sum_buffer = 0;
  381. while(!feof($handle) && $sum_buffer<$filesize) {
  382. echo fread($handle, min($read_buffer, ($filesize - $sum_buffer) + 1));
  383. $sum_buffer += $read_buffer;
  384. flush();
  385. }
  386. fclose($handle);
  387. exit;
  388. }
  389. public function logerror(){
  390. $content = date('Y-m-d H:i:s')."\r\n";
  391. $content.=$_SERVER['REQUEST_METHOD'].' '.$_SERVER['REQUEST_URI']."\r\n";
  392. if($_SERVER['REQUEST_METHOD'] == 'POST'){
  393. $content.=file_get_contents('php://input')."\r\n";
  394. }
  395. $handle = fopen(app()->getRootPath()."record.txt", 'a');
  396. fwrite($handle, $content."\r\n");
  397. fclose($handle);
  398. return json(['status'=>false, 'msg'=>'不支持当前操作']);
  399. }
  400. }