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.

449 lines
18 KiB

2 years ago
3 weeks ago
2 years ago
9 months ago
2 years ago
9 months ago
2 years ago
9 months ago
2 years ago
2 years ago
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use think\facade\Db;
  5. use think\facade\View;
  6. use think\facade\Request;
  7. use think\facade\Cache;
  8. use app\lib\Btapi;
  9. use app\lib\Plugins;
  10. class Admin extends BaseController
  11. {
  12. public function verifycode()
  13. {
  14. return captcha();
  15. }
  16. public function login(){
  17. if(request()->islogin){
  18. return redirect('/admin');
  19. }
  20. if(request()->isAjax()){
  21. $username = input('post.username',null,'trim');
  22. $password = input('post.password',null,'trim');
  23. $code = input('post.code',null,'trim');
  24. if(empty($username) || empty($password)){
  25. return json(['code'=>-1, 'msg'=>'用户名或密码不能为空']);
  26. }
  27. if(!captcha_check($code)){
  28. return json(['code'=>-1, 'msg'=>'验证码错误']);
  29. }
  30. if($username == config_get('admin_username') && $password == config_get('admin_password')){
  31. Db::name('log')->insert(['uid' => 0, 'action' => '登录后台', 'data' => 'IP:'.$this->clientip, 'addtime' => date("Y-m-d H:i:s")]);
  32. $session = md5($username.config_get('admin_password'));
  33. $expiretime = time()+2562000;
  34. $token = authcode("{$username}\t{$session}\t{$expiretime}", 'ENCODE', config_get('syskey'));
  35. cookie('admin_token', $token, ['expire' => $expiretime, 'httponly' => true]);
  36. config_set('admin_lastlogin', date('Y-m-d H:i:s'));
  37. return json(['code'=>0]);
  38. }else{
  39. return json(['code'=>-1, 'msg'=>'用户名或密码错误']);
  40. }
  41. }
  42. return view();
  43. }
  44. public function logout()
  45. {
  46. cookie('admin_token', null);
  47. return redirect('/admin/login');
  48. }
  49. public function index()
  50. {
  51. $stat = ['total'=>0, 'free'=>0, 'pro'=>0, 'ltd'=>0, 'third'=>0];
  52. $json_arr = Plugins::get_plugin_list();
  53. if($json_arr){
  54. foreach($json_arr['list'] as $plugin){
  55. $stat['total']++;
  56. if($plugin['type']==10) $stat['third']++;
  57. elseif($plugin['type']==12) $stat['ltd']++;
  58. elseif($plugin['type']==8) $stat['pro']++;
  59. elseif($plugin['type']==5 || $plugin['type']==6 || $plugin['type']==7) $stat['free']++;
  60. }
  61. }
  62. $stat['runtime'] = Db::name('config')->where('key','runtime')->value('value') ?? '<font color="red">未运行</font>';
  63. $stat['record_total'] = Db::name('record')->count();
  64. $stat['record_isuse'] = Db::name('record')->whereTime('usetime', '>=', strtotime('-7 days'))->count();
  65. View::assign('stat', $stat);
  66. $tmp = 'version()';
  67. $mysqlVersion = Db::query("select version()")[0][$tmp];
  68. $info = [
  69. 'framework_version' => app()::VERSION,
  70. 'php_version' => PHP_VERSION,
  71. 'mysql_version' => $mysqlVersion,
  72. 'software' => $_SERVER['SERVER_SOFTWARE'],
  73. 'os' => php_uname(),
  74. 'date' => date("Y-m-d H:i:s"),
  75. ];
  76. View::assign('info', $info);
  77. return view();
  78. }
  79. public function set(){
  80. if(request()->isAjax()){
  81. $params = Request::param();
  82. foreach ($params as $key => $value) {
  83. config_set($key, $value);
  84. }
  85. cache('configs', NULL);
  86. return json(['code'=>0]);
  87. }
  88. $mod = input('param.mod', 'sys');
  89. View::assign('mod', $mod);
  90. View::assign('conf', config('sys'));
  91. $runtime = Db::name('config')->where('key','runtime')->value('value') ?? '<font color="red">未运行</font>';
  92. View::assign('runtime', $runtime);
  93. View::assign('is_user_www', isset($_SERVER['USER']) && $_SERVER['USER'] == 'www');
  94. return view();
  95. }
  96. public function setaccount(){
  97. $params = Request::param();
  98. if(isset($params['username']))$params['username']=trim($params['username']);
  99. if(isset($params['oldpwd']))$params['oldpwd']=trim($params['oldpwd']);
  100. if(isset($params['newpwd']))$params['newpwd']=trim($params['newpwd']);
  101. if(isset($params['newpwd2']))$params['newpwd2']=trim($params['newpwd2']);
  102. if(empty($params['username'])) return json(['code'=>-1, 'msg'=>'用户名不能为空']);
  103. config_set('admin_username', $params['username']);
  104. if(!empty($params['oldpwd']) && !empty($params['newpwd']) && !empty($params['newpwd2'])){
  105. if(config_get('admin_password') != $params['oldpwd']){
  106. return json(['code'=>-1, 'msg'=>'旧密码不正确']);
  107. }
  108. if($params['newpwd'] != $params['newpwd2']){
  109. return json(['code'=>-1, 'msg'=>'两次新密码输入不一致']);
  110. }
  111. config_set('admin_password', $params['newpwd']);
  112. }
  113. cache('configs', NULL);
  114. cookie('admin_token', null);
  115. return json(['code'=>0]);
  116. }
  117. public function testbturl(){
  118. $bt_type = input('post.bt_type/d');
  119. if($bt_type == 1){
  120. $bt_surl = input('post.bt_surl');
  121. if(!$bt_surl)return json(['code'=>-1, 'msg'=>'参数不能为空']);
  122. $res = get_curl($bt_surl . 'api/SetupCount');
  123. if(strpos($res, 'ok')!==false){
  124. return json(['code'=>0, 'msg'=>'第三方云端连接测试成功!']);
  125. }else{
  126. return json(['code'=>-1, 'msg'=>'第三方云端连接测试失败']);
  127. }
  128. }else{
  129. $bt_url = input('post.bt_url');
  130. $bt_key = input('post.bt_key');
  131. if(!$bt_url || !$bt_key)return json(['code'=>-1, 'msg'=>'参数不能为空']);
  132. $btapi = new Btapi($bt_url, $bt_key);
  133. $result = $btapi->get_config();
  134. if($result && isset($result['status']) && ($result['status']==1 || isset($result['sites_path']))){
  135. $result = $btapi->get_user_info();
  136. if($result && isset($result['username'])){
  137. return json(['code'=>0, 'msg'=>'面板连接测试成功!']);
  138. }else{
  139. return json(['code'=>-1, 'msg'=>'面板连接测试成功,但未安装专用插件/未登录账号']);
  140. }
  141. }else{
  142. return json(['code'=>-1, 'msg'=>isset($result['msg'])?$result['msg']:'面板地址无法连接']);
  143. }
  144. }
  145. }
  146. public function plugins(){
  147. $typelist = [];
  148. $json_arr = Plugins::get_plugin_list();
  149. if($json_arr){
  150. foreach($json_arr['type'] as $type){
  151. if($type['title'] == '一键部署') continue;
  152. $typelist[$type['id']] = $type['title'];
  153. }
  154. }
  155. View::assign('typelist', $typelist);
  156. return view();
  157. }
  158. public function pluginswin(){
  159. $typelist = [];
  160. $json_arr = Plugins::get_plugin_list('Windows');
  161. if($json_arr){
  162. foreach($json_arr['type'] as $type){
  163. if($type['title'] == '一键部署') continue;
  164. $typelist[$type['id']] = $type['title'];
  165. }
  166. }
  167. View::assign('typelist', $typelist);
  168. return view();
  169. }
  170. public function pluginsen(){
  171. $typelist = [];
  172. $json_arr = Plugins::get_plugin_list('en');
  173. if($json_arr){
  174. foreach($json_arr['type'] as $type){
  175. if($type['title'] == '一键部署') continue;
  176. $typelist[$type['id']] = $type['title'];
  177. }
  178. }
  179. View::assign('typelist', $typelist);
  180. return view();
  181. }
  182. public function plugins_data(){
  183. $type = input('post.type/d');
  184. $keyword = input('post.keyword', null, 'trim');
  185. $os = input('get.os');
  186. if(!$os) $os = 'Linux';
  187. $json_arr = Plugins::get_plugin_list($os);
  188. if(!$json_arr) return json([]);
  189. $typelist = [];
  190. foreach($json_arr['type'] as $row){
  191. $typelist[$row['id']] = $row['title'];
  192. }
  193. $list = [];
  194. foreach($json_arr['list'] as $plugin){
  195. if($type > 0 && $plugin['type']!=$type) continue;
  196. if(!empty($keyword) && $keyword != $plugin['name'] && stripos($plugin['title'], $keyword)===false) continue;
  197. $versions = [];
  198. foreach($plugin['versions'] as $version){
  199. $ver = $version['m_version'].'.'.$version['version'];
  200. if(isset($version['download'])){
  201. $status = false;
  202. if(file_exists(get_data_dir().'plugins/other/'.$version['download'])){
  203. $status = true;
  204. }
  205. $versions[] = ['status'=>$status, 'type'=>1, 'version'=>$ver, 'download'=>$version['download'], 'md5'=>$version['md5']];
  206. }else{
  207. $status = false;
  208. if(file_exists(get_data_dir($os).'plugins/package/'.$plugin['name'].'-'.$ver.'.zip')){
  209. $status = true;
  210. }
  211. $versions[] = ['status'=>$status, 'type'=>0, 'version'=>$ver];
  212. }
  213. }
  214. if($plugin['name'] == 'obs') $plugin['ps'] = substr($plugin['ps'],0,strpos($plugin['ps'],'<a '));
  215. $list[] = [
  216. 'id' => $plugin['id'],
  217. 'name' => $plugin['name'],
  218. 'title' => $plugin['title'],
  219. 'type' => $plugin['type'],
  220. 'typename' => isset($typelist[$plugin['type']]) ? $typelist[$plugin['type']] : '未知',
  221. 'desc' => str_replace('target="_blank"','target="_blank" rel="noopener noreferrer"',$plugin['ps']),
  222. 'price' => $plugin['price'],
  223. 'author' => isset($plugin['author']) ? $plugin['author'] : '官方',
  224. 'versions' => $versions
  225. ];
  226. }
  227. return json($list);
  228. }
  229. public function download_plugin(){
  230. $name = input('post.name', null, 'trim');
  231. $version = input('post.version', null, 'trim');
  232. $os = input('post.os');
  233. if(!$os) $os = 'Linux';
  234. if(!$name || !$version) return json(['code'=>-1, 'msg'=>'参数不能为空']);
  235. try{
  236. Plugins::download_plugin($name, $version, $os);
  237. Db::name('log')->insert(['uid' => 0, 'action' => '下载插件', 'data' => $name.'-'.$version.' os:'.$os, 'addtime' => date("Y-m-d H:i:s")]);
  238. return json(['code'=>0,'msg'=>'下载成功']);
  239. }catch(\Exception $e){
  240. return json(['code'=>-1, 'msg'=>$e->getMessage()]);
  241. }
  242. }
  243. public function refresh_plugins(){
  244. $os = input('get.os');
  245. if(!$os) $os = 'Linux';
  246. try{
  247. Plugins::refresh_plugin_list($os);
  248. Db::name('log')->insert(['uid' => 0, 'action' => '刷新插件列表', 'data' => '刷新'.$os.'插件列表成功', 'addtime' => date("Y-m-d H:i:s")]);
  249. return json(['code'=>0,'msg'=>'获取最新插件列表成功!']);
  250. }catch(\Exception $e){
  251. return json(['code'=>-1, 'msg'=>$e->getMessage()]);
  252. }
  253. }
  254. public function record(){
  255. return view();
  256. }
  257. public function record_data(){
  258. $ip = input('post.ip', null, 'trim');
  259. $offset = input('post.offset/d');
  260. $limit = input('post.limit/d');
  261. $select = Db::name('record');
  262. if(!empty($ip)){
  263. $select->where('ip', $ip);
  264. }
  265. $total = $select->count();
  266. $rows = $select->order('id','desc')->limit($offset, $limit)->select();
  267. return json(['total'=>$total, 'rows'=>$rows]);
  268. }
  269. public function log(){
  270. return view();
  271. }
  272. public function log_data(){
  273. $action = input('post.action', null, 'trim');
  274. $offset = input('post.offset/d');
  275. $limit = input('post.limit/d');
  276. $select = Db::name('log');
  277. if(!empty($action)){
  278. $select->where('action', $action);
  279. }
  280. $total = $select->count();
  281. $rows = $select->order('id','desc')->limit($offset, $limit)->select();
  282. return json(['total'=>$total, 'rows'=>$rows]);
  283. }
  284. public function list(){
  285. $type = input('param.type', 'black');
  286. View::assign('type', $type);
  287. View::assign('typename', $type=='white'?'白名单':'黑名单');
  288. return view();
  289. }
  290. public function list_data(){
  291. $type = input('param.type', 'black');
  292. $ip = input('post.ip', null, 'trim');
  293. $offset = input('post.offset/d');
  294. $limit = input('post.limit/d');
  295. $tablename = $type == 'black' ? 'black' : 'white';
  296. $select = Db::name($tablename);
  297. if(!empty($ip)){
  298. $select->where('ip', $ip);
  299. }
  300. $total = $select->count();
  301. $rows = $select->order('id','desc')->limit($offset, $limit)->select();
  302. return json(['total'=>$total, 'rows'=>$rows]);
  303. }
  304. public function list_op(){
  305. $type = input('param.type', 'black');
  306. $tablename = $type == 'black' ? 'black' : 'white';
  307. $act = input('post.act', null);
  308. if($act == 'get'){
  309. $id = input('post.id/d');
  310. if(!$id) return json(['code'=>-1, 'msg'=>'no id']);
  311. $data = Db::name($tablename)->where('id', $id)->find();
  312. return json(['code'=>0, 'data'=>$data]);
  313. }elseif($act == 'add'){
  314. $ip = input('post.ip', null, 'trim');
  315. if(!$ip) return json(['code'=>-1, 'msg'=>'IP不能为空']);
  316. if(Db::name($tablename)->where('ip', $ip)->find()){
  317. return json(['code'=>-1, 'msg'=>'该IP已存在']);
  318. }
  319. Db::name($tablename)->insert([
  320. 'ip' => $ip,
  321. 'enable' => 1,
  322. 'addtime' => date("Y-m-d H:i:s")
  323. ]);
  324. return json(['code'=>0, 'msg'=>'succ']);
  325. }elseif($act == 'edit'){
  326. $id = input('post.id/d');
  327. $ip = input('post.ip', null, 'trim');
  328. if(!$id || !$ip) return json(['code'=>-1, 'msg'=>'IP不能为空']);
  329. if(Db::name($tablename)->where('ip', $ip)->where('id', '<>', $id)->find()){
  330. return json(['code'=>-1, 'msg'=>'该IP已存在']);
  331. }
  332. Db::name($tablename)->where('id', $id)->update([
  333. 'ip' => $ip
  334. ]);
  335. return json(['code'=>0, 'msg'=>'succ']);
  336. }elseif($act == 'enable'){
  337. $id = input('post.id/d');
  338. $enable = input('post.enable/d');
  339. if(!$id) return json(['code'=>-1, 'msg'=>'no id']);
  340. Db::name($tablename)->where('id', $id)->update([
  341. 'enable' => $enable
  342. ]);
  343. return json(['code'=>0, 'msg'=>'succ']);
  344. }elseif($act == 'del'){
  345. $id = input('post.id/d');
  346. if(!$id) return json(['code'=>-1, 'msg'=>'no id']);
  347. Db::name($tablename)->where('id', $id)->delete();
  348. return json(['code'=>0, 'msg'=>'succ']);
  349. }
  350. return json(['code'=>-1, 'msg'=>'no act']);
  351. }
  352. public function deplist(){
  353. $deplist_linux = get_data_dir().'config/deployment_list.json';
  354. $deplist_win = get_data_dir('Windows').'config/deployment_list.json';
  355. $deplist_linux_time = file_exists($deplist_linux) ? date("Y-m-d H:i:s", filemtime($deplist_linux)) : '不存在';
  356. $deplist_win_time = file_exists($deplist_win) ? date("Y-m-d H:i:s", filemtime($deplist_win)) : '不存在';
  357. View::assign('deplist_linux_time', $deplist_linux_time);
  358. View::assign('deplist_win_time', $deplist_win_time);
  359. return view();
  360. }
  361. public function refresh_deplist(){
  362. $os = input('get.os');
  363. if(!$os) $os = 'Linux';
  364. try{
  365. Plugins::refresh_deplist($os);
  366. Db::name('log')->insert(['uid' => 0, 'action' => '刷新一键部署列表', 'data' => '刷新'.$os.'一键部署列表成功', 'addtime' => date("Y-m-d H:i:s")]);
  367. return json(['code'=>0,'msg'=>'获取最新一键部署列表成功!']);
  368. }catch(\Exception $e){
  369. return json(['code'=>-1, 'msg'=>$e->getMessage()]);
  370. }
  371. }
  372. public function cleancache(){
  373. Cache::clear();
  374. return json(['code'=>0,'msg'=>'succ']);
  375. }
  376. public function ssl(){
  377. if(request()->isAjax()){
  378. $domain_list = input('post.domain_list', null, 'trim');
  379. $common_name = input('post.common_name', null, 'trim');
  380. $validity = input('post.validity/d');
  381. if(empty($domain_list) || empty($validity)){
  382. return json(['code'=>-1, 'msg'=>'参数不能为空']);
  383. }
  384. $array = explode("\n", $domain_list);
  385. $domain_list = [];
  386. foreach($array as $domain){
  387. $domain = trim($domain);
  388. if(empty($domain)) continue;
  389. if(!checkDomain($domain)) return json(['code'=>-1, 'msg'=>'域名或IP格式不正确:'.$domain]);
  390. $domain_list[] = $domain;
  391. }
  392. if(empty($domain_list)) return json(['code'=>-1, 'msg'=>'域名列表不能为空']);
  393. if(empty($common_name)) $common_name = $domain_list[0];
  394. $result = makeSelfSignSSL($common_name, $domain_list, $validity);
  395. if(!$result){
  396. return json(['code'=>-1, 'msg'=>'生成证书失败']);
  397. }
  398. return json(['code'=>0, 'msg'=>'生成证书成功', 'cert'=>$result['cert'], 'key'=>$result['key']]);
  399. }
  400. $dir = app()->getBasePath().'script/';
  401. $ssl_path = app()->getRootPath().'public/ssl/baota_root.pfx';
  402. $ssl_path_mac = app()->getRootPath().'public/ssl/baota_root.crt';
  403. $isca = file_exists($dir.'ca.crt') && file_exists($dir.'ca.key') && file_exists($ssl_path) && file_exists($ssl_path_mac);
  404. View::assign('isca', $isca);
  405. return view();
  406. }
  407. }