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.

493 lines
18 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
12 months ago
10 months 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
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. $type = input('?post.type') ? input('post.type') : 0;
  203. if($type == 1){
  204. $data = [
  205. 'version' => '1.1',
  206. 'description' => '暂无更新日志',
  207. 'create_time' => 1705315163,
  208. ];
  209. }else{
  210. $data = [
  211. 'version' => '3.0',
  212. 'description' => '暂无更新日志',
  213. 'create_time' => 1705315163,
  214. ];
  215. }
  216. $data = bin2hex(json_encode($data));
  217. return json(['status'=>true,'err_no'=>0,'msg'=>'获取成功','data'=>$data]);
  218. }
  219. //获取内测版更新日志
  220. public function get_beta_logs(){
  221. return json(['beta_ps'=>'当前暂无内测版', 'list'=>[]]);
  222. }
  223. //检查用户绑定是否正确
  224. public function check_auth_key(){
  225. return '1';
  226. }
  227. //从云端验证域名是否可访问
  228. public function check_domain(){
  229. $domain = input('post.domain',null,'trim');
  230. $ssl = input('post.ssl/d');
  231. if(!$domain) return json(['status'=>false, 'msg'=>'域名不能为空']);
  232. if(!strpos($domain,'.')) return json(['status'=>false, 'msg'=>'域名格式不正确']);
  233. $domain = str_replace('*.','',$domain);
  234. $ip = gethostbyname($domain);
  235. if(!$ip || $ip == $domain){
  236. return json(['status'=>false, 'msg'=>'无法访问']);
  237. }else{
  238. return json(['status'=>true, 'msg'=>'访问正常']);
  239. }
  240. }
  241. //同步时间
  242. public function get_time(){
  243. return time();
  244. }
  245. //同步时间
  246. public function get_win_date(){
  247. return date("Y-m-d H:i:s");
  248. }
  249. //查询是否专业版(废弃)
  250. public function is_pro(){
  251. return json(['endtime'=>true, 'code'=>1]);
  252. }
  253. //获取产品推荐信息
  254. public function get_plugin_remarks(){
  255. return json(['list'=>[], 'pro_list'=>[], 'kfqq'=>'', 'kf'=>'', 'qun'=>'']);
  256. }
  257. //获取指定插件评分
  258. public function get_plugin_socre(){
  259. return json(['total'=>0, 'split'=>[0,0,0,0,0],'page'=>"<div><span class='Pcurrent'>1</span><span class='Pcount'>共计0条数据</span></div>",'data'=>[]]);
  260. }
  261. //提交插件评分
  262. public function plugin_score(){
  263. return json(['status'=>true, 'msg'=>'您的评分已成功提交,感谢您的支持!']);
  264. }
  265. //获取IP地址
  266. public function get_ip_address(){
  267. return $this->clientip;
  268. }
  269. //绑定账号
  270. public function get_auth_token(){
  271. if(!input('?post.data')) return json(['status'=>false, 'msg'=>'参数不能为空']);
  272. $reqData = hex2bin(input('post.data'));
  273. parse_str($reqData, $arr);
  274. $serverid = $arr['serverid'];
  275. $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];
  276. $data = bin2hex(json_encode($userinfo));
  277. return json(['status'=>true, 'msg'=>'登录成功!', 'data'=>$data]);
  278. }
  279. //绑定账号新
  280. public function authorization_login(){
  281. if(!input('?post.data')) return json(['status'=>false, 'msg'=>'参数不能为空']);
  282. $reqData = hex2bin(input('post.data'));
  283. parse_str($reqData, $arr);
  284. $serverid = $arr['serverid'];
  285. $userinfo = ['uid'=>1, 'username'=>'Administrator', 'ip'=>'127.0.0.1', 'server_id'=>$serverid, 'access_key'=>random(48), 'secret_key'=>random(48)];
  286. $data = bin2hex(json_encode($userinfo));
  287. return json(['status'=>true, 'err_no'=>0, 'msg'=>'账号绑定成功', 'data'=>$data]);
  288. }
  289. //刷新授权信息
  290. public function authorization_info(){
  291. if(!input('?post.data')) return json(['status'=>false, 'msg'=>'参数不能为空']);
  292. $reqData = hex2bin(input('post.data'));
  293. parse_str($reqData, $arr);
  294. $id = isset($arr['id'])&&$arr['id']>0?$arr['id']:1;
  295. $userinfo = ['id'=>$id, 'product'=>$arr['product'], 'status'=>2, 'clients'=>9999, 'durations'=>0, 'end_time'=>strtotime('+10 year')];
  296. $data = bin2hex(json_encode($userinfo));
  297. return json(['status'=>true, 'err_no'=>0, 'data'=>$data]);
  298. }
  299. //刷新授权信息
  300. public function update_license(){
  301. if(!input('?post.data')) return json(['status'=>false, 'msg'=>'参数不能为空']);
  302. $reqData = hex2bin(input('post.data'));
  303. parse_str($reqData, $arr);
  304. if(!isset($arr['product']) || !isset($arr['serverid'])) return json(['status'=>false, 'msg'=>'缺少参数']);
  305. $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')];
  306. $json = json_encode($license_data);
  307. [$public_key, $private_key] = generateKeyPairs();
  308. $public_key = pemToBase64($public_key);
  309. $key1 = random(32);
  310. $key2 = substr($public_key, 0, 32);
  311. $encrypted1 = licenseEncrypt($json, $key1);
  312. $encrypted2 = licenseEncrypt($key1, $key2);
  313. $sign_data = $encrypted1.'.'.$encrypted2;
  314. openssl_sign($sign_data, $signature, $private_key, OPENSSL_ALGO_SHA256);
  315. $signature = base64_encode($signature);
  316. $license = base64_encode($sign_data.'.'.$signature);
  317. $data = bin2hex(json_encode(['public_key'=>$public_key, 'license'=>$license]));
  318. return json(['status'=>true, 'err_no'=>0, 'msg'=>'授权获取成功', 'data'=>$data]);
  319. }
  320. public function is_obtained_btw_trial(){
  321. $data = ['is_obtained'=>0];
  322. $data = bin2hex(json_encode($data));
  323. return json(['status'=>true, 'err_no'=>0, 'data'=>$data, 'msg'=>'检测成功']);
  324. }
  325. //一键部署列表
  326. public function get_deplist(){
  327. $os = input('post.os');
  328. $json_arr = Plugins::get_deplist($os);
  329. if(!$json_arr) return json([]);
  330. return json($json_arr);
  331. }
  332. //获取宝塔SSL列表
  333. public function get_ssl_list(){
  334. $data = bin2hex('[]');
  335. return json(['status'=>true, 'msg'=>'', 'data'=>$data]);
  336. }
  337. public function return_success(){
  338. return json(['status'=>true, 'msg'=>1, 'data'=>(object)[]]);
  339. }
  340. public function return_error(){
  341. return json(['status'=>false, 'msg'=>'不支持当前操作']);
  342. }
  343. public function return_error2(){
  344. return json(['success'=>false, 'res'=>'不支持当前操作']);
  345. }
  346. public function return_empty(){
  347. return '';
  348. }
  349. public function return_empty_array(){
  350. return json([]);
  351. }
  352. public function return_page_data(){
  353. 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'=>[]]);
  354. }
  355. //获取所有蜘蛛IP列表
  356. public function btwaf_getspiders(){
  357. try{
  358. $result = Plugins::btwaf_getspiders();
  359. return json($result);
  360. }catch(\Exception $e){
  361. return json(['status'=>false, 'msg'=>$e->getMessage()]);
  362. }
  363. }
  364. //分类获取蜘蛛IP列表
  365. public function get_spider(){
  366. $type = input('get.spider/d');
  367. if(!$type) return json([]);
  368. $result = Plugins::get_spider($type);
  369. return json($result);
  370. }
  371. //检查黑白名单
  372. private function checklist(){
  373. if(config_get('whitelist') == 1){
  374. if(Db::name('white')->where('ip', $this->clientip)->where('enable', 1)->find()){
  375. return true;
  376. }
  377. return false;
  378. }else{
  379. if(Db::name('black')->where('ip', $this->clientip)->where('enable', 1)->find()){
  380. return false;
  381. }
  382. return true;
  383. }
  384. }
  385. //下载大文件
  386. private function output_file($filepath, $filename){
  387. $filesize = filesize($filepath);
  388. $filemd5 = md5_file($filepath);
  389. ob_clean();
  390. header("Content-Type: application/octet-stream");
  391. header("Content-Disposition: attachment; filename={$filename}.zip");
  392. header("Content-Length: {$filesize}");
  393. header("File-size: {$filesize}");
  394. header("Content-md5: {$filemd5}");
  395. $read_buffer = 1024 * 100;
  396. $handle = fopen($filepath, 'rb');
  397. $sum_buffer = 0;
  398. while(!feof($handle) && $sum_buffer<$filesize) {
  399. echo fread($handle, min($read_buffer, ($filesize - $sum_buffer) + 1));
  400. $sum_buffer += $read_buffer;
  401. flush();
  402. }
  403. fclose($handle);
  404. exit;
  405. }
  406. public function logerror(){
  407. $content = date('Y-m-d H:i:s')."\r\n";
  408. $content.=$_SERVER['REQUEST_METHOD'].' '.$_SERVER['REQUEST_URI']."\r\n";
  409. if($_SERVER['REQUEST_METHOD'] == 'POST'){
  410. $content.=file_get_contents('php://input')."\r\n";
  411. }
  412. $handle = fopen(app()->getRootPath()."record.txt", 'a');
  413. fwrite($handle, $content."\r\n");
  414. fclose($handle);
  415. return json(['status'=>false, 'msg'=>'不支持当前操作']);
  416. }
  417. //生成自签名SSL证书
  418. public function bt_cert(){
  419. $data = input('post.data');
  420. $param = json_decode($data, true);
  421. if(!$param || !isset($param['domain'])) return json(['status'=>false, 'msg'=>'参数错误']);
  422. $dir = app()->getBasePath().'script/';
  423. $ssl_path = app()->getRootPath().'public/ssl/baota_root.pfx';
  424. $isca = file_exists($dir.'ca.crt') && file_exists($dir.'ca.key') && file_exists($ssl_path);
  425. if(!$isca) return json(['status'=>false, 'msg'=>'CA证书不存在']);
  426. $domain = $param['domain'];
  427. if(empty($domain)) return json(['status'=>false, 'msg'=>'域名不能为空']);
  428. $domain_list = explode(',', $domain);
  429. foreach($domain_list as $d){
  430. if(!checkDomain($d)) return json(['status'=>false, 'msg'=>'域名或IP格式不正确:'.$d]);
  431. }
  432. $common_name = $domain_list[0];
  433. $validity = 3650;
  434. $result = makeSelfSignSSL($common_name, $domain_list, $validity);
  435. if(!$result){
  436. return json(['status'=>false, 'msg'=>'生成证书失败']);
  437. }
  438. $ca_pfx = base64_encode(file_get_contents($ssl_path));
  439. return json(['status'=>true, 'msg'=>'生成证书成功', 'cert'=>$result['cert'], 'key'=>$result['key'], 'pfx'=>$ca_pfx, 'password'=>'']);
  440. }
  441. }