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.

676 lines
25 KiB

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