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.

719 lines
26 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
7 months ago
2 years ago
1 year ago
2 years ago
7 months 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
7 months ago
2 years ago
7 months ago
11 months ago
7 months ago
2 years ago
7 months ago
2 years ago
2 years ago
2 years ago
2 years ago
4 days ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
7 months ago
7 months ago
2 years ago
2 years ago
2 years ago
5 months ago
2 years ago
7 months ago
2 years ago
2 years ago
1 year ago
1 year 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 cloudc_version_info(){
  310. return json(['status'=>true,'msg'=>'获取成功','data'=>[
  311. 'version' => '1.0.5',
  312. 'download' => '',
  313. 'uptime' => '2025/06/16',
  314. 'upmsg' => '暂无更新日志'
  315. ]]);
  316. }
  317. //宝塔云控版本信息
  318. public function cloudc_get_version(){
  319. return json(['status'=>true,'msg'=>'','oid'=>'','data'=>[
  320. 'officialVersion' => [
  321. 'version' => '1.0.5',
  322. 'download' => '',
  323. 'uptime' => '2025/06/16',
  324. 'updateMsg' => '暂无更新日志'
  325. ],
  326. ]]);
  327. }
  328. //宝塔云控授权信息
  329. public function cloudc_order_status(){
  330. $data = [
  331. 'status' => true,
  332. 'msg' => '获取成功',
  333. 'oid' => '',
  334. 'data' => [
  335. 'id' => 1,
  336. 'address' => real_ip(),
  337. 'buytime' => time(),
  338. 'endtime' => time() + 86400 * 3650,
  339. 'num' => 9999,
  340. 'max_num' => 9999,
  341. 'pid' => 100000023,
  342. 'renew_price' => 0,
  343. 'state' => 1,
  344. ]
  345. ];
  346. return json($data);
  347. }
  348. //获取内测版更新日志
  349. public function get_beta_logs(){
  350. return json(['beta_ps'=>'当前暂无内测版', 'list'=>[]]);
  351. }
  352. //检查用户绑定是否正确
  353. public function check_auth_key(){
  354. return '1';
  355. }
  356. //从云端验证域名是否可访问
  357. public function check_domain(){
  358. $domain = input('post.domain',null,'trim');
  359. $ssl = input('post.ssl/d');
  360. if(!$domain) return json(['status'=>false, 'msg'=>'域名不能为空']);
  361. if(!strpos($domain,'.')) return json(['status'=>false, 'msg'=>'域名格式不正确']);
  362. $domain = str_replace('*.','',$domain);
  363. $ip = gethostbyname($domain);
  364. if(!$ip || $ip == $domain){
  365. return json(['status'=>false, 'msg'=>'无法访问']);
  366. }else{
  367. return json(['status'=>true, 'msg'=>'访问正常']);
  368. }
  369. }
  370. //同步时间
  371. public function get_time(){
  372. return time();
  373. }
  374. //同步时间
  375. public function get_win_date(){
  376. return date("Y-m-d H:i:s");
  377. }
  378. //查询是否专业版(废弃)
  379. public function is_pro(){
  380. return json(['endtime'=>true, 'code'=>1]);
  381. }
  382. //获取产品推荐信息
  383. public function get_plugin_remarks(){
  384. return json(['list'=>[], 'pro_list'=>[], 'kfqq'=>'', 'kf'=>'', 'qun'=>'']);
  385. }
  386. //获取指定插件评分
  387. public function get_plugin_socre(){
  388. return json(['total'=>0, 'split'=>[0,0,0,0,0],'page'=>"<div><span class='Pcurrent'>1</span><span class='Pcount'>共计0条数据</span></div>",'data'=>[]]);
  389. }
  390. //提交插件评分
  391. public function plugin_score(){
  392. return json(['status'=>true, 'msg'=>'您的评分已成功提交,感谢您的支持!']);
  393. }
  394. //获取IP地址
  395. public function get_ip_address(){
  396. return $this->clientip;
  397. }
  398. //绑定账号
  399. public function get_auth_token(){
  400. if(!input('?post.data')) return json(['status'=>false, 'msg'=>'参数不能为空']);
  401. $reqData = hex2bin(input('post.data'));
  402. parse_str($reqData, $arr);
  403. $serverid = $arr['serverid'];
  404. $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];
  405. $data = bin2hex(json_encode($userinfo));
  406. return json(['status'=>true, 'msg'=>'登录成功!', 'data'=>$data]);
  407. }
  408. //绑定账号新
  409. public function authorization_login(){
  410. if(!input('?post.data')) return json(['status'=>false, 'msg'=>'参数不能为空']);
  411. $reqData = hex2bin(input('post.data'));
  412. parse_str($reqData, $arr);
  413. $serverid = $arr['serverid'];
  414. $userinfo = ['uid'=>1, 'username'=>'Administrator', 'ip'=>'127.0.0.1', 'server_id'=>$serverid, 'access_key'=>random(48), 'secret_key'=>random(48)];
  415. $data = bin2hex(json_encode($userinfo));
  416. return json(['status'=>true, 'err_no'=>0, 'msg'=>'账号绑定成功', 'data'=>$data]);
  417. }
  418. //刷新授权信息
  419. public function authorization_info(){
  420. if(!input('?post.data')) return json(['status'=>false, 'msg'=>'参数不能为空']);
  421. $reqData = hex2bin(input('post.data'));
  422. parse_str($reqData, $arr);
  423. $id = isset($arr['id'])&&$arr['id']>0?$arr['id']:1;
  424. $userinfo = ['id'=>$id, 'product'=>$arr['product'], 'status'=>2, 'clients'=>9999, 'durations'=>0, 'end_time'=>strtotime('+10 year')];
  425. $data = bin2hex(json_encode($userinfo));
  426. return json(['status'=>true, 'err_no'=>0, 'data'=>$data]);
  427. }
  428. //刷新授权信息
  429. public function update_license(){
  430. if(!input('?post.data')) return json(['status'=>false, 'msg'=>'参数不能为空']);
  431. $reqData = hex2bin(input('post.data'));
  432. parse_str($reqData, $arr);
  433. if(!isset($arr['product']) || !isset($arr['serverid'])) return json(['status'=>false, 'msg'=>'缺少参数']);
  434. $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')];
  435. $json = json_encode($license_data);
  436. [$public_key, $private_key] = generateKeyPairs();
  437. $public_key = pemToBase64($public_key);
  438. $key1 = random(32);
  439. $key2 = substr($public_key, 0, 32);
  440. $encrypted1 = licenseEncrypt($json, $key1);
  441. $encrypted2 = licenseEncrypt($key1, $key2);
  442. $sign_data = $encrypted1.'.'.$encrypted2;
  443. openssl_sign($sign_data, $signature, $private_key, OPENSSL_ALGO_SHA256);
  444. $signature = base64_encode($signature);
  445. $license = base64_encode($sign_data.'.'.$signature);
  446. $data = bin2hex(json_encode(['public_key'=>$public_key, 'license'=>$license]));
  447. return json(['status'=>true, 'err_no'=>0, 'msg'=>'授权获取成功', 'data'=>$data]);
  448. }
  449. public function is_obtained_btw_trial(){
  450. $data = ['is_obtained'=>0];
  451. $data = bin2hex(json_encode($data));
  452. return json(['status'=>true, 'err_no'=>0, 'data'=>$data, 'msg'=>'检测成功']);
  453. }
  454. //一键部署列表
  455. public function get_deplist(){
  456. $os = input('post.os');
  457. $json_arr = Plugins::get_deplist($os);
  458. if(!$json_arr) return json([]);
  459. return json($json_arr);
  460. }
  461. //获取宝塔SSL列表
  462. public function get_ssl_list(){
  463. $data = bin2hex('[]');
  464. return json(['status'=>true, 'msg'=>'', 'data'=>$data]);
  465. }
  466. //获取堡塔云WAF恶意IP库
  467. public function get_malicious_ip_list()
  468. {
  469. $cacheKey = 'malicious_ip_list';
  470. // 尝试从缓存获取
  471. if (Cache::has($cacheKey)) {
  472. return json(json_decode(Cache::get($cacheKey), true));
  473. }
  474. $url = 'https://api.bt.cn/bt_waf/get_malicious_ip';
  475. $postData = json_encode([
  476. 'x_bt_token' => 'MzI3YjAzOGQ3Yjk3NjUxYjVlMDkyMGFm'
  477. ]);
  478. $ch = curl_init();
  479. curl_setopt($ch, CURLOPT_URL, $url);
  480. curl_setopt($ch, CURLOPT_POST, 1);
  481. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  482. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  483. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  484. 'Content-Type: application/json',
  485. 'Content-Length: ' . strlen($postData)
  486. ]);
  487. $response = curl_exec($ch);
  488. if (curl_errno($ch)) {
  489. return json(['status'=>true, 'msg'=>'', 'data'=>bin2hex('[]')]);
  490. }
  491. curl_close($ch);
  492. Cache::set($cacheKey, $response, 86400); //缓存一天
  493. return json(json_decode($response, true));
  494. }
  495. public function return_success(){
  496. return json(['status'=>true, 'msg'=>1, 'data'=>(object)[]]);
  497. }
  498. public function return_error(){
  499. return json(['status'=>false, 'msg'=>'不支持当前操作']);
  500. }
  501. public function return_error2(){
  502. return json(['success'=>false, 'res'=>'不支持当前操作']);
  503. }
  504. public function return_empty(){
  505. return '';
  506. }
  507. public function return_empty_array(){
  508. return json([]);
  509. }
  510. public function return_page_data(){
  511. 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'=>[]]);
  512. }
  513. public function nps_check(){
  514. return json(['err_no'=>0, 'success'=>true, 'res'=>true, 'nonce'=>time()]);
  515. }
  516. public function nps_questions(){
  517. return json(['err_no'=>0, 'success'=>true, 'res'=>[], 'nonce'=>time()]);
  518. }
  519. public function nps_submit(){
  520. return json(['err_no'=>0, 'success'=>true, 'res'=>'Success', 'nonce'=>time()]);
  521. }
  522. public function get_user_give_away(){
  523. return json(['no_exceed_limit'=>false, 'user_give'=>true]);
  524. }
  525. //获取所有蜘蛛IP列表
  526. public function btwaf_getspiders(){
  527. try{
  528. $result = Plugins::btwaf_getspiders();
  529. return json($result);
  530. }catch(\Exception $e){
  531. return json(['status'=>false, 'msg'=>$e->getMessage()]);
  532. }
  533. }
  534. //分类获取蜘蛛IP列表
  535. public function get_spider(){
  536. $type = input('get.spider/d');
  537. if(!$type) return json([]);
  538. $result = Plugins::get_spider($type);
  539. return json($result);
  540. }
  541. //获取堡塔恶意情报IP库
  542. public function btwaf_getmalicious(){
  543. try{
  544. $result = Plugins::btwaf_getmalicious();
  545. return json($result);
  546. }catch(\Exception $e){
  547. return json(['success'=>false, 'res'=>$e->getMessage()]);
  548. }
  549. }
  550. //检查是否国内IP
  551. public function check_cnip(){
  552. $clientip = bindec(decbin(ip2long($this->clientip)));
  553. $json_file = app()->getBasePath().'lib/cn.json';
  554. $arr = json_decode(file_get_contents($json_file), true);
  555. if(!$arr) return 'False';
  556. foreach($arr as $ip_arr){
  557. if($clientip >= $ip_arr[0] && $clientip <= $ip_arr[1]){
  558. return 'True';
  559. }
  560. }
  561. return 'False';
  562. }
  563. //邮件配额
  564. public function email_user_surplus(){
  565. $data = [
  566. 'free' => ['surplus' => '120000', 'total' => '120000', 'used' => '0'],
  567. 'period' => ['surplus' => '2000000', 'total' => '2000000', 'used' => '0'],
  568. ];
  569. return json(['success'=>true, 'msg'=>'获取成功', 'res'=>$data]);
  570. }
  571. //检查黑白名单
  572. private function checklist(){
  573. if(config_get('whitelist') == 1){
  574. if(Db::name('white')->where('ip', $this->clientip)->where('enable', 1)->find()){
  575. return true;
  576. }
  577. return false;
  578. }else{
  579. if(Db::name('black')->where('ip', $this->clientip)->where('enable', 1)->find()){
  580. return false;
  581. }
  582. return true;
  583. }
  584. }
  585. //下载大文件
  586. private function output_file($filepath, $filename){
  587. $filesize = filesize($filepath);
  588. $filemd5 = md5_file($filepath);
  589. ob_clean();
  590. header("Content-Type: application/octet-stream");
  591. header("Content-Disposition: attachment; filename={$filename}.zip");
  592. header("Content-Length: {$filesize}");
  593. header("File-size: {$filesize}");
  594. header("Content-md5: {$filemd5}");
  595. $read_buffer = 1024 * 100;
  596. $handle = fopen($filepath, 'rb');
  597. $sum_buffer = 0;
  598. while(!feof($handle) && $sum_buffer<$filesize) {
  599. echo fread($handle, min($read_buffer, ($filesize - $sum_buffer) + 1));
  600. $sum_buffer += $read_buffer;
  601. flush();
  602. }
  603. fclose($handle);
  604. exit;
  605. }
  606. public function logerror(){
  607. $content = date('Y-m-d H:i:s')."\r\n";
  608. $content.=$_SERVER['REQUEST_METHOD'].' '.$_SERVER['REQUEST_URI']."\r\n";
  609. if($_SERVER['REQUEST_METHOD'] == 'POST'){
  610. $content.=file_get_contents('php://input')."\r\n";
  611. }
  612. $handle = fopen(app()->getRootPath()."record.txt", 'a');
  613. fwrite($handle, $content."\r\n");
  614. fclose($handle);
  615. return json(['status'=>false, 'msg'=>'不支持当前操作']);
  616. }
  617. //生成自签名SSL证书
  618. public function bt_cert(){
  619. $data = input('post.data');
  620. $param = json_decode($data, true);
  621. if(!$param || !isset($param['action']) || !isset($param['domain'])) return json(['status'=>false, 'msg'=>'参数错误']);
  622. $dir = app()->getBasePath().'script/';
  623. $ssl_path = app()->getRootPath().'public/ssl/baota_root.pfx';
  624. $isca = file_exists($dir.'ca.crt') && file_exists($dir.'ca.key') && file_exists($ssl_path);
  625. if(!$isca) return json(['status'=>false, 'msg'=>'CA证书不存在']);
  626. if($param['action'] == 'get_domain_cert'){
  627. if(!$this->checklist()) return json(['status'=>false, 'msg'=>'你的服务器被禁止使用此云端']);
  628. $domain = $param['domain'];
  629. if(empty($domain)) return json(['status'=>false, 'msg'=>'域名不能为空']);
  630. $domain_list = explode(',', $domain);
  631. foreach($domain_list as $d){
  632. if(!checkDomain($d)) return json(['status'=>false, 'msg'=>'域名或IP格式不正确:'.$d]);
  633. }
  634. $common_name = $domain_list[0];
  635. $validity = 3650;
  636. $result = makeSelfSignSSL($common_name, $domain_list, $validity);
  637. if(!$result){
  638. return json(['status'=>false, 'msg'=>'生成证书失败']);
  639. }
  640. $ca_pfx = base64_encode(file_get_contents($ssl_path));
  641. return json(['status'=>true, 'msg'=>'生成证书成功', 'cert'=>$result['cert'], 'key'=>$result['key'], 'pfx'=>$ca_pfx, 'password'=>'']);
  642. }else{
  643. return json(['status'=>false, 'msg'=>'不支持当前操作']);
  644. }
  645. }
  646. }