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.

588 lines
22 KiB

2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
2 months ago
2 years ago
6 months ago
2 years ago
2 months ago
2 years ago
6 months ago
2 years ago
6 months ago
2 years ago
6 months ago
2 years ago
6 months ago
2 years ago
2 months ago
2 years ago
2 months ago
5 months ago
2 years ago
2 months ago
2 years ago
1 year ago
1 year ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
2 months ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
6 months ago
6 months ago
2 years 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. //获取插件列表(aapanel)
  35. public function get_plugin_list_en(){
  36. if(!$this->checklist()) return json('你的服务器被禁止使用此云端');
  37. $record = Db::name('record')->where('ip',$this->clientip)->find();
  38. if($record){
  39. Db::name('record')->where('id',$record['id'])->update(['usetime'=>date("Y-m-d H:i:s")]);
  40. }else{
  41. Db::name('record')->insert(['ip'=>$this->clientip, 'addtime'=>date("Y-m-d H:i:s"), 'usetime'=>date("Y-m-d H:i:s")]);
  42. }
  43. $json_arr = Plugins::get_plugin_list('en');
  44. if(!$json_arr) return json((object)[]);
  45. return json($json_arr);
  46. }
  47. //下载插件包
  48. public function download_plugin(){
  49. $plugin_name = input('post.name');
  50. $version = input('post.version');
  51. $os = input('post.os');
  52. if(!$plugin_name || !$version){
  53. return '参数不能为空';
  54. }
  55. if(!in_array($os,['Windows','Linux'])) $os = 'Linux';
  56. if(!preg_match('/^[a-zA-Z0-9_]+$/', $plugin_name) || !preg_match('/^[0-9.]+$/', $version)){
  57. return '参数不正确';
  58. }
  59. if(!$this->checklist()) return '你的服务器被禁止使用此云端';
  60. $filepath = get_data_dir($os).'plugins/package/'.$plugin_name.'-'.$version.'.zip';
  61. if(file_exists($filepath)){
  62. $filename = $plugin_name.'.zip';
  63. $this->output_file($filepath, $filename);
  64. }else{
  65. return '云端不存在该插件包';
  66. }
  67. }
  68. //下载插件包aapanel
  69. public function download_plugin_en(){
  70. $plugin_name = input('post.name');
  71. $version = input('post.version');
  72. if(!$plugin_name || !$version){
  73. return '参数不能为空';
  74. }
  75. if(!preg_match('/^[a-zA-Z0-9_]+$/', $plugin_name) || !preg_match('/^[0-9.]+$/', $version)){
  76. return '参数不正确';
  77. }
  78. if(!$this->checklist()) return '你的服务器被禁止使用此云端';
  79. $filepath = get_data_dir('en').'plugins/package/'.$plugin_name.'-'.$version.'.zip';
  80. if(file_exists($filepath)){
  81. $filename = $plugin_name.'.zip';
  82. $this->output_file($filepath, $filename);
  83. }else{
  84. return '云端不存在该插件包';
  85. }
  86. }
  87. //下载插件主文件
  88. public function download_plugin_main(){
  89. $plugin_name = input('post.name');
  90. $version = input('post.version');
  91. $os = input('post.os');
  92. if(!$plugin_name || !$version){
  93. return '参数不能为空';
  94. }
  95. if(!in_array($os,['Windows','Linux'])) $os = 'Linux';
  96. if(!preg_match('/^[a-zA-Z0-9_]+$/', $plugin_name) || !preg_match('/^[0-9.]+$/', $version)){
  97. return '参数不正确';
  98. }
  99. if(!$this->checklist()) return '你的服务器被禁止使用此云端';
  100. $filepath = get_data_dir($os).'plugins/package/'.$plugin_name.'-'.$version.'.zip';
  101. $mainfilepath = get_data_dir($os).'plugins/folder/'.$plugin_name.'-'.$version.'/'.$plugin_name.'/'.$plugin_name.'_main.py';
  102. if(file_exists($mainfilepath)){
  103. $filename = $plugin_name.'_main.py';
  104. $this->output_file($mainfilepath, $filename);
  105. }elseif(file_exists($filepath)){
  106. $zip = new \ZipArchive;
  107. if ($zip->open($filepath) === true){
  108. echo $zip->getFromName($plugin_name.'/'.$plugin_name.'_main.py');
  109. }else{
  110. return '插件包解压缩失败';
  111. }
  112. }else{
  113. return '云端不存在该插件主文件';
  114. }
  115. }
  116. //下载插件其他文件
  117. public function download_plugin_other(){
  118. $fname = input('get.fname');
  119. if(!$fname){
  120. $fname = input('get.filename');
  121. if(!$fname){
  122. return json(['status'=>false, 'msg'=>'参数不能为空']);
  123. }
  124. }
  125. if(strpos(dirname($fname),'.')!==false)return json(['status'=>false, 'msg'=>'参数不正确']);
  126. if(!$this->checklist()) return json(['status'=>false, 'msg'=>'你的服务器被禁止使用此云端']);
  127. $filepath = get_data_dir().'plugins/other/'.$fname;
  128. if(file_exists($filepath)){
  129. $filename = basename($fname);
  130. $this->output_file($filepath, $filename);
  131. }else{
  132. return json(['status'=>false, 'msg'=>'云端不存在该插件文件']);
  133. }
  134. }
  135. public function get_update_logs(){
  136. $type = input('get.type');
  137. if($type == 'Windows'){
  138. $version = config_get('new_version_win');
  139. $data = [
  140. [
  141. 'title' => 'Linux面板'.$version,
  142. 'body' => config_get('update_msg_win'),
  143. 'addtime' => config_get('update_date_win')
  144. ]
  145. ];
  146. }else{
  147. $version = config_get('new_version');
  148. $data = [
  149. [
  150. 'title' => 'Linux面板'.$version,
  151. 'body' => config_get('update_msg'),
  152. 'addtime' => config_get('update_date')
  153. ]
  154. ];
  155. }
  156. return jsonp($data);
  157. }
  158. public function get_version(){
  159. $version = config_get('new_version');
  160. return $version;
  161. }
  162. public function get_version_win(){
  163. $version = config_get('new_version_win');
  164. return $version;
  165. }
  166. public function get_version_en(){
  167. $version = config_get('new_version_en');
  168. return $version;
  169. }
  170. public function get_panel_version(){
  171. $version = config_get('new_version');
  172. $file = app()->getRootPath().'public/install/update/LinuxPanel-'.$version.'.zip';
  173. $hash = hash_file('sha256', $file);
  174. $data = [
  175. 'version' => $version,
  176. 'hash' => $hash,
  177. 'update_time' => filemtime($file),
  178. ];
  179. return json($data);
  180. }
  181. //安装统计
  182. public function setup_count(){
  183. return 'ok';
  184. }
  185. //检测更新
  186. public function check_update(){
  187. $version = config_get('new_version');
  188. $down_url = request()->root(true).'/install/update/LinuxPanel-'.$version.'.zip';
  189. $data = [
  190. 'force' => false,
  191. 'version' => $version,
  192. 'downUrl' => $down_url,
  193. 'updateMsg' => config_get('update_msg'),
  194. 'uptime' => config_get('update_date'),
  195. 'is_beta' => 0,
  196. 'adviser' => -1,
  197. 'btb' => '',
  198. 'beta' => [
  199. 'version' => $version,
  200. 'downUrl' => $down_url,
  201. 'updateMsg' => config_get('update_msg'),
  202. 'uptime' => config_get('update_date'),
  203. ]
  204. ];
  205. return json($data);
  206. }
  207. //检测更新(win)
  208. public function check_update_win(){
  209. $version = config_get('new_version_win');
  210. $down_url = request()->root(true).'/win/panel/panel_'.$version.'.zip';
  211. $data = [
  212. 'force' => false,
  213. 'version' => $version,
  214. 'downUrl' => $down_url,
  215. 'updateMsg' => config_get('update_msg_win'),
  216. 'uptime' => config_get('update_date_win'),
  217. 'is_beta' => 0,
  218. 'py_version' => '3.8.6',
  219. 'adviser' => -1,
  220. 'is_rec' => -1,
  221. 'btb' => '',
  222. 'beta' => [
  223. 'py_version' => '3.8.6',
  224. 'version' => $version,
  225. 'downUrl' => $down_url,
  226. 'updateMsg' => config_get('update_msg_win'),
  227. 'uptime' => config_get('update_date_win'),
  228. ]
  229. ];
  230. return json($data);
  231. }
  232. //检测更新(aapanel)
  233. public function check_update_en(){
  234. $version = config_get('new_version_en');
  235. $down_url = request()->root(true).'/install/update/LinuxPanel_EN-'.$version.'.zip';
  236. $data = [
  237. 'force' => false,
  238. 'version' => $version,
  239. 'downUrl' => $down_url,
  240. 'updateMsg' => config_get('update_msg_en'),
  241. 'uptime' => config_get('update_date_en'),
  242. 'is_beta' => 0,
  243. 'btb' => '',
  244. 'beta' => [
  245. 'version' => $version,
  246. 'downUrl' => $down_url,
  247. 'updateMsg' => config_get('update_msg_en'),
  248. 'uptime' => config_get('update_date_en'),
  249. ]
  250. ];
  251. return json($data);
  252. }
  253. //宝塔云监控获取最新版本
  254. public function btm_latest_version(){
  255. $data = [
  256. 'version' => config_get('new_version_btm'),
  257. 'description' => config_get('update_msg_btm'),
  258. 'create_time' => config_get('update_date_btm')
  259. ];
  260. return json($data);
  261. }
  262. //宝塔云监控更新日志
  263. public function btm_update_history(){
  264. $data = [
  265. [
  266. 'version' => config_get('new_version_btm'),
  267. 'description' => config_get('update_msg_btm'),
  268. 'create_time' => config_get('update_date_btm')
  269. ]
  270. ];
  271. return json($data);
  272. }
  273. //宝塔云WAF最新版本
  274. public function btwaf_latest_version(){
  275. $type = input('?post.type') ? input('post.type') : 0;
  276. if($type == 1){
  277. $data = [
  278. 'version' => '1.1',
  279. 'description' => '暂无更新日志',
  280. 'create_time' => 1705315163,
  281. ];
  282. }else{
  283. $data = [
  284. 'version' => '3.0',
  285. 'description' => '暂无更新日志',
  286. 'create_time' => 1705315163,
  287. ];
  288. }
  289. $data = bin2hex(json_encode($data));
  290. return json(['status'=>true,'err_no'=>0,'msg'=>'获取成功','data'=>$data]);
  291. }
  292. //获取内测版更新日志
  293. public function get_beta_logs(){
  294. return json(['beta_ps'=>'当前暂无内测版', 'list'=>[]]);
  295. }
  296. //检查用户绑定是否正确
  297. public function check_auth_key(){
  298. return '1';
  299. }
  300. //从云端验证域名是否可访问
  301. public function check_domain(){
  302. $domain = input('post.domain',null,'trim');
  303. $ssl = input('post.ssl/d');
  304. if(!$domain) return json(['status'=>false, 'msg'=>'域名不能为空']);
  305. if(!strpos($domain,'.')) return json(['status'=>false, 'msg'=>'域名格式不正确']);
  306. $domain = str_replace('*.','',$domain);
  307. $ip = gethostbyname($domain);
  308. if(!$ip || $ip == $domain){
  309. return json(['status'=>false, 'msg'=>'无法访问']);
  310. }else{
  311. return json(['status'=>true, 'msg'=>'访问正常']);
  312. }
  313. }
  314. //同步时间
  315. public function get_time(){
  316. return time();
  317. }
  318. //同步时间
  319. public function get_win_date(){
  320. return date("Y-m-d H:i:s");
  321. }
  322. //查询是否专业版(废弃)
  323. public function is_pro(){
  324. return json(['endtime'=>true, 'code'=>1]);
  325. }
  326. //获取产品推荐信息
  327. public function get_plugin_remarks(){
  328. return json(['list'=>[], 'pro_list'=>[], 'kfqq'=>'', 'kf'=>'', 'qun'=>'']);
  329. }
  330. //获取指定插件评分
  331. public function get_plugin_socre(){
  332. return json(['total'=>0, 'split'=>[0,0,0,0,0],'page'=>"<div><span class='Pcurrent'>1</span><span class='Pcount'>共计0条数据</span></div>",'data'=>[]]);
  333. }
  334. //提交插件评分
  335. public function plugin_score(){
  336. return json(['status'=>true, 'msg'=>'您的评分已成功提交,感谢您的支持!']);
  337. }
  338. //获取IP地址
  339. public function get_ip_address(){
  340. return $this->clientip;
  341. }
  342. //绑定账号
  343. public function get_auth_token(){
  344. if(!input('?post.data')) return json(['status'=>false, 'msg'=>'参数不能为空']);
  345. $reqData = hex2bin(input('post.data'));
  346. parse_str($reqData, $arr);
  347. $serverid = $arr['serverid'];
  348. $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];
  349. $data = bin2hex(json_encode($userinfo));
  350. return json(['status'=>true, 'msg'=>'登录成功!', 'data'=>$data]);
  351. }
  352. //绑定账号新
  353. public function authorization_login(){
  354. if(!input('?post.data')) return json(['status'=>false, 'msg'=>'参数不能为空']);
  355. $reqData = hex2bin(input('post.data'));
  356. parse_str($reqData, $arr);
  357. $serverid = $arr['serverid'];
  358. $userinfo = ['uid'=>1, 'username'=>'Administrator', 'ip'=>'127.0.0.1', 'server_id'=>$serverid, 'access_key'=>random(48), 'secret_key'=>random(48)];
  359. $data = bin2hex(json_encode($userinfo));
  360. return json(['status'=>true, 'err_no'=>0, 'msg'=>'账号绑定成功', 'data'=>$data]);
  361. }
  362. //刷新授权信息
  363. public function authorization_info(){
  364. if(!input('?post.data')) return json(['status'=>false, 'msg'=>'参数不能为空']);
  365. $reqData = hex2bin(input('post.data'));
  366. parse_str($reqData, $arr);
  367. $id = isset($arr['id'])&&$arr['id']>0?$arr['id']:1;
  368. $userinfo = ['id'=>$id, 'product'=>$arr['product'], 'status'=>2, 'clients'=>9999, 'durations'=>0, 'end_time'=>strtotime('+10 year')];
  369. $data = bin2hex(json_encode($userinfo));
  370. return json(['status'=>true, 'err_no'=>0, 'data'=>$data]);
  371. }
  372. //刷新授权信息
  373. public function update_license(){
  374. if(!input('?post.data')) return json(['status'=>false, 'msg'=>'参数不能为空']);
  375. $reqData = hex2bin(input('post.data'));
  376. parse_str($reqData, $arr);
  377. if(!isset($arr['product']) || !isset($arr['serverid'])) return json(['status'=>false, 'msg'=>'缺少参数']);
  378. $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')];
  379. $json = json_encode($license_data);
  380. [$public_key, $private_key] = generateKeyPairs();
  381. $public_key = pemToBase64($public_key);
  382. $key1 = random(32);
  383. $key2 = substr($public_key, 0, 32);
  384. $encrypted1 = licenseEncrypt($json, $key1);
  385. $encrypted2 = licenseEncrypt($key1, $key2);
  386. $sign_data = $encrypted1.'.'.$encrypted2;
  387. openssl_sign($sign_data, $signature, $private_key, OPENSSL_ALGO_SHA256);
  388. $signature = base64_encode($signature);
  389. $license = base64_encode($sign_data.'.'.$signature);
  390. $data = bin2hex(json_encode(['public_key'=>$public_key, 'license'=>$license]));
  391. return json(['status'=>true, 'err_no'=>0, 'msg'=>'授权获取成功', 'data'=>$data]);
  392. }
  393. public function is_obtained_btw_trial(){
  394. $data = ['is_obtained'=>0];
  395. $data = bin2hex(json_encode($data));
  396. return json(['status'=>true, 'err_no'=>0, 'data'=>$data, 'msg'=>'检测成功']);
  397. }
  398. //一键部署列表
  399. public function get_deplist(){
  400. $os = input('post.os');
  401. $json_arr = Plugins::get_deplist($os);
  402. if(!$json_arr) return json([]);
  403. return json($json_arr);
  404. }
  405. //获取宝塔SSL列表
  406. public function get_ssl_list(){
  407. $data = bin2hex('[]');
  408. return json(['status'=>true, 'msg'=>'', 'data'=>$data]);
  409. }
  410. public function return_success(){
  411. return json(['status'=>true, 'msg'=>1, 'data'=>(object)[]]);
  412. }
  413. public function return_error(){
  414. return json(['status'=>false, 'msg'=>'不支持当前操作']);
  415. }
  416. public function return_error2(){
  417. return json(['success'=>false, 'res'=>'不支持当前操作']);
  418. }
  419. public function return_empty(){
  420. return '';
  421. }
  422. public function return_empty_array(){
  423. return json([]);
  424. }
  425. public function return_page_data(){
  426. 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'=>[]]);
  427. }
  428. public function nps_check(){
  429. return json(['err_no'=>0, 'success'=>true, 'res'=>true, 'nonce'=>time()]);
  430. }
  431. public function nps_questions(){
  432. return json(['err_no'=>0, 'success'=>true, 'res'=>[], 'nonce'=>time()]);
  433. }
  434. public function nps_submit(){
  435. return json(['err_no'=>0, 'success'=>true, 'res'=>'Success', 'nonce'=>time()]);
  436. }
  437. //获取所有蜘蛛IP列表
  438. public function btwaf_getspiders(){
  439. try{
  440. $result = Plugins::btwaf_getspiders();
  441. return json($result);
  442. }catch(\Exception $e){
  443. return json(['status'=>false, 'msg'=>$e->getMessage()]);
  444. }
  445. }
  446. //分类获取蜘蛛IP列表
  447. public function get_spider(){
  448. $type = input('get.spider/d');
  449. if(!$type) return json([]);
  450. $result = Plugins::get_spider($type);
  451. return json($result);
  452. }
  453. //检查黑白名单
  454. private function checklist(){
  455. if(config_get('whitelist') == 1){
  456. if(Db::name('white')->where('ip', $this->clientip)->where('enable', 1)->find()){
  457. return true;
  458. }
  459. return false;
  460. }else{
  461. if(Db::name('black')->where('ip', $this->clientip)->where('enable', 1)->find()){
  462. return false;
  463. }
  464. return true;
  465. }
  466. }
  467. //下载大文件
  468. private function output_file($filepath, $filename){
  469. $filesize = filesize($filepath);
  470. $filemd5 = md5_file($filepath);
  471. ob_clean();
  472. header("Content-Type: application/octet-stream");
  473. header("Content-Disposition: attachment; filename={$filename}.zip");
  474. header("Content-Length: {$filesize}");
  475. header("File-size: {$filesize}");
  476. header("Content-md5: {$filemd5}");
  477. $read_buffer = 1024 * 100;
  478. $handle = fopen($filepath, 'rb');
  479. $sum_buffer = 0;
  480. while(!feof($handle) && $sum_buffer<$filesize) {
  481. echo fread($handle, min($read_buffer, ($filesize - $sum_buffer) + 1));
  482. $sum_buffer += $read_buffer;
  483. flush();
  484. }
  485. fclose($handle);
  486. exit;
  487. }
  488. public function logerror(){
  489. $content = date('Y-m-d H:i:s')."\r\n";
  490. $content.=$_SERVER['REQUEST_METHOD'].' '.$_SERVER['REQUEST_URI']."\r\n";
  491. if($_SERVER['REQUEST_METHOD'] == 'POST'){
  492. $content.=file_get_contents('php://input')."\r\n";
  493. }
  494. $handle = fopen(app()->getRootPath()."record.txt", 'a');
  495. fwrite($handle, $content."\r\n");
  496. fclose($handle);
  497. return json(['status'=>false, 'msg'=>'不支持当前操作']);
  498. }
  499. //生成自签名SSL证书
  500. public function bt_cert(){
  501. $data = input('post.data');
  502. $param = json_decode($data, true);
  503. if(!$param || !isset($param['action']) || !isset($param['domain'])) return json(['status'=>false, 'msg'=>'参数错误']);
  504. $dir = app()->getBasePath().'script/';
  505. $ssl_path = app()->getRootPath().'public/ssl/baota_root.pfx';
  506. $isca = file_exists($dir.'ca.crt') && file_exists($dir.'ca.key') && file_exists($ssl_path);
  507. if(!$isca) return json(['status'=>false, 'msg'=>'CA证书不存在']);
  508. if($param['action'] == 'get_domain_cert'){
  509. if(!$this->checklist()) return json(['status'=>false, 'msg'=>'你的服务器被禁止使用此云端']);
  510. $domain = $param['domain'];
  511. if(empty($domain)) return json(['status'=>false, 'msg'=>'域名不能为空']);
  512. $domain_list = explode(',', $domain);
  513. foreach($domain_list as $d){
  514. if(!checkDomain($d)) return json(['status'=>false, 'msg'=>'域名或IP格式不正确:'.$d]);
  515. }
  516. $common_name = $domain_list[0];
  517. $validity = 3650;
  518. $result = makeSelfSignSSL($common_name, $domain_list, $validity);
  519. if(!$result){
  520. return json(['status'=>false, 'msg'=>'生成证书失败']);
  521. }
  522. $ca_pfx = base64_encode(file_get_contents($ssl_path));
  523. return json(['status'=>true, 'msg'=>'生成证书成功', 'cert'=>$result['cert'], 'key'=>$result['key'], 'pfx'=>$ca_pfx, 'password'=>'']);
  524. }else{
  525. return json(['status'=>false, 'msg'=>'不支持当前操作']);
  526. }
  527. }
  528. }