manageController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. class manageController extends Controller
  3. {
  4. public $initphp_list = array(
  5. 'top',
  6. 'left',
  7. 'login',
  8. 'logout',
  9. 'validcode',
  10. 'node',
  11. 'node_edit',
  12. 'user',
  13. 'ssh',
  14. 'code',
  15. 'del',
  16. 'showdata'
  17. );
  18. public function __construct()
  19. {
  20. parent::__construct();
  21. $this->checkLogin();
  22. }
  23. public function run()
  24. {
  25. $this->view->set_tpl("manage/index");
  26. $this->view->assign("title", "ShadowSocks5管理后台");
  27. $this->view->display();
  28. }
  29. public function showdata()
  30. {
  31. $logs = $this->getLogsDao();
  32. $data = $logs->getAll();
  33. $china = array(
  34. 'bj' => '北京',
  35. 'tj' => '天津',
  36. 'hb' => '河北',
  37. 'shanxi' => '山西',
  38. 'nmg' => '内蒙古',
  39. 'liaoning' => '辽宁',
  40. 'jiling' => '吉林',
  41. 'hlj' => '黑龙江',
  42. 'shanghai' => '上海',
  43. 'shandong' => '山东',
  44. 'ah1' => '安徽',
  45. 'js2' => '江苏',
  46. 'zj3' => '浙江',
  47. 'fj4' => '福建',
  48. 'jx5' => '江西',
  49. 'henan' => '河南',
  50. 'hb1' => '湖北',
  51. 'hunan' => '湖南',
  52. 'guangdong' => '广东',
  53. 'guangxi' => '广西',
  54. 'hainan' => '海南',
  55. 'sx2' => '陕西',
  56. 'gs6' => '甘肃',
  57. 'nx7' => '宁夏',
  58. 'qh8' => '青海',
  59. 'xj9' => '新疆',
  60. 'cq11' => '重庆',
  61. 'sc21' => '四川',
  62. 'gz21' => '贵州',
  63. 'yn22' => '云南',
  64. 'xz33' => '西藏',
  65. 'tw44' => '台湾',
  66. 'xg55' => '香港',
  67. 'am66' => '澳门'
  68. );
  69. $keys = array_keys($china);
  70. $value = array();
  71. foreach ($keys as $k => $v) {
  72. $value[$v] = 0;
  73. }
  74. foreach ($china as $k => $v) {
  75. foreach ($data as $k1 => $v1) {
  76. if (strpos($v1['location'], $v) !== false) {
  77. $value[$k] ++;
  78. }
  79. }
  80. }
  81. $this->view->assign("china", $china);
  82. $this->view->assign("value", $value);
  83. $this->view->set_tpl("index/map");
  84. $this->view->display();
  85. }
  86. /**
  87. * 节点管理
  88. */
  89. public function node()
  90. {
  91. $node = $this->getNodeDao();
  92. $nodelist = $node->getAllNode();
  93. $this->view->assign("node", $nodelist);
  94. $this->view->set_tpl("manage/node");
  95. $this->view->display(); // 模板展示
  96. }
  97. /**
  98. * 批量执行命令管理
  99. */
  100. public function ssh()
  101. {
  102. if ($this->controller->get_gp("do") == "exec") { // 提交更新
  103. set_time_limit(0);
  104. ignore_user_abort();
  105. $server = $this->getServer();
  106. $data = $server->getAll();
  107. echo "<pre>";
  108. foreach ($data as $k => $v) {
  109. echo $v['server']." response:<br>";
  110. $ssh = $this->getssh();
  111. $cmd = $this->controller->get_gp("cmd");
  112. $status = $ssh->login($v['server'], $v['port'], 'root', $v['passwd']);
  113. if (!$status) {
  114. echo $v['server'].' connect fail!';
  115. continue;
  116. }
  117. $str = $ssh->exec($cmd);
  118. echo $str . "\r\n\r\n\r\n";
  119. $ssh->close();
  120. }
  121. echo "</pre>";
  122. die();
  123. }
  124. $this->view->set_tpl("manage/ssh");
  125. $this->view->display(); // 模板展示
  126. }
  127. public function node_edit()
  128. {
  129. $node = $this->getNodeDao();
  130. $id = $this->controller->get_gp("id");
  131. if ($this->controller->get_gp("do") == "update") { // 提交更新
  132. $data = $this->controller->get_gp(array(
  133. 'node_name',
  134. 'node_type',
  135. 'node_server',
  136. 'node_method',
  137. 'node_info',
  138. 'node_status',
  139. 'node_order',
  140. 'idc_server',
  141. 'idc_url',
  142. 'api_veid',
  143. 'api_key'
  144. ));
  145. foreach ($data as $k => $v) {
  146. if ($v == "") {
  147. unset($data[$k]);
  148. }
  149. }
  150. $node->update($id, $data);
  151. JsMessage("更新成功!", '/manage/node');
  152. die();
  153. }
  154. $nodeData = $node->getNodeById($id);
  155. $this->view->assign("data", $nodeData);
  156. $this->view->set_tpl("manage/node_edit");
  157. $this->view->display();
  158. }
  159. /**
  160. * 用户管理
  161. */
  162. public function user()
  163. {
  164. $userService = self::getUserService();
  165. $all = $userService->getAllTran();
  166. $all = round($all / (1027 * 1024 * 1024), 2);
  167. $pager = self::getPagerLibrary();
  168. $page = $this->controller->get_gp("page") ? $page = $this->controller->get_gp("page") : 1;
  169. $pernum = 10;
  170. $count = $userService->getCount();
  171. $start = $pernum * ($page - 1);
  172. $url = "/manage/user";
  173. $page_html = $pager->pager($count, $pernum, $url, $page, true); // 最后一个参数为true则使用默认样式
  174. $userData = $userService->getUsers($pernum, $start);
  175. $this->view->assign("allTran", $all);
  176. $this->view->assign("user", $userData);
  177. $this->view->assign("pager", $page_html);
  178. $this->view->set_tpl("manage/user");
  179. $this->view->display('', false); // 模板展示
  180. }
  181. public function del()
  182. {
  183. $uid = $this->controller->get_gp("id");
  184. $userService = self::getUserService();
  185. if ($userService->delUser($uid)) {
  186. JsMessage("删除成功!");
  187. } else {
  188. JsMessage("删除失败!");
  189. }
  190. }
  191. /**
  192. * 邀请管理
  193. */
  194. public function code()
  195. {
  196. $do = $this->controller->get_gp("do");
  197. if ($do == 'add') {
  198. $num = $this->controller->get_gp("num");
  199. $pre = $this->controller->get_gp("pre");
  200. $platform = $this->controller->get_gp("platform");
  201. $user = $this->getUserService();
  202. $result = $user->add($pre, $num,$platform);
  203. if ($result) {
  204. echo $result;
  205. // JsMessage("添加成功!", "/manage/user");
  206. }
  207. }
  208. $this->view->set_tpl("/manage/node");
  209. $this->view->display();
  210. }
  211. /**
  212. * 用户列表
  213. */
  214. public function userlist()
  215. {
  216. $adminService = InitPHP::getService("user");
  217. $pager = $this->getLibrary('pager');
  218. $page = $this->controller->get_gp("page") ? $page = $this->controller->get_gp("page") : 1;
  219. $pernum = 30;
  220. $count = $adminService->getCount();
  221. $start = $pernum * ($page - 1);
  222. $url = "userlist";
  223. $page_html = $pager->pager($count, $pernum, $url, $page, true); // 最后一个参数为true则使用默认样式
  224. $adminData = $adminService->getUsers($pernum, $start);
  225. $this->view->set_tpl("manage/userlist");
  226. $this->view->assign("data", $adminData);
  227. $this->view->assign("pager", $page_html);
  228. $this->view->display('', false);
  229. }
  230. public function left()
  231. {
  232. $session = $this->getUtil('session');
  233. $adminname = $session->get('adminname');
  234. $config = InitPHP::getConfig();
  235. $leftmenu = $config['menu'];
  236. $this->view->assign("menu", $leftmenu);
  237. $this->view->assign('admin', $adminname);
  238. $this->view->set_tpl("manage/left");
  239. $this->view->display();
  240. }
  241. public function top()
  242. {
  243. $session = $this->getUtil('session');
  244. $adminid = $session->get('adminid');
  245. if (empty($adminid)) {
  246. $this->view->set_tpl("manage/login");
  247. } else {
  248. $this->view->assign('softid', $session->get('softid'));
  249. $this->view->set_tpl("manage/top");
  250. $this->view->display(); // 模板展示
  251. }
  252. }
  253. /**
  254. * 登陆
  255. */
  256. public function login()
  257. {
  258. $info = $this->controller->get_gp(array(
  259. 'username',
  260. 'password',
  261. 'validcode'
  262. ));
  263. if ($info['username']) {
  264. $session = $this->getUtil('session');
  265. $code = $session->get('user_vaildcode_key');
  266. if (strtolower(trim($code)) !== strtolower(trim($info['validcode']))) {
  267. JsMessage('验证码错了!');
  268. die();
  269. } else {
  270. $password = md5($info['password']);
  271. $admin = InitPHP::getService("admin");
  272. $result = $admin->getOne($info['username']);
  273. if ($result) {
  274. if ($result['pass'] === $password) {
  275. $session->set('adminid', intval($result['uid']));
  276. $session->set('adminname', htmlspecialchars($info['username']));
  277. $function = $this->getLibrary("function");
  278. $function->goto_url("/manage");
  279. } else {
  280. JsMessage("密码错误!");
  281. }
  282. } else {
  283. JsMessage("用户名不存在!");
  284. }
  285. }
  286. } else {
  287. $this->view->set_tpl("manage/login");
  288. $this->view->assign("title", "ShadowSocks5管理后台");
  289. $this->view->display();
  290. }
  291. }
  292. /**
  293. * 检查登陆状态
  294. */
  295. public function checkLogin()
  296. {
  297. $list = array(
  298. "login",
  299. "validcode"
  300. );
  301. $session = $this->getUtil('session');
  302. $adminid = $session->get('adminid');
  303. $a = $this->getA();
  304. if (! in_array($a, $list)) {
  305. if (empty($adminid)) {
  306. $function = $this->getLibrary('function');
  307. $function->goto_url('/manage/login');
  308. die();
  309. }
  310. }
  311. }
  312. /**
  313. *
  314. * @return userService
  315. */
  316. private function getUserService()
  317. {
  318. return InitPHP::getService("user");
  319. }
  320. /**
  321. *
  322. * @return nodeDao
  323. */
  324. private function getNodeDao()
  325. {
  326. return InitPHP::getDao("node");
  327. }
  328. /**
  329. *
  330. * @return pagerInit
  331. */
  332. private function getPagerLibrary()
  333. {
  334. return InitPHP::getLibrarys("pager");
  335. }
  336. /**
  337. * 获取验证码函数
  338. */
  339. public function validcode()
  340. {
  341. $captcha = self::getCaptcha();
  342. $code = $captcha->CreateCheckCode();
  343. $session = $this->getUtil('session');
  344. $session->set('user_vaildcode_key', $code);
  345. $captcha->OutCheckImage();
  346. }
  347. /**
  348. *
  349. * @return captchaInit
  350. */
  351. private function getCaptcha()
  352. {
  353. return InitPHP::getLibrarys("captcha");
  354. }
  355. /**
  356. *
  357. * @return sshInit
  358. */
  359. private function getssh()
  360. {
  361. return InitPHP::getLibrarys("ssh");
  362. }
  363. /**
  364. *
  365. * @return sshDao
  366. */
  367. private function getServer()
  368. {
  369. return InitPHP::getDao("ssh");
  370. }
  371. /**
  372. * 退出后台 删除session
  373. */
  374. public function logout()
  375. {
  376. $session = $this->getUtil('session');
  377. $function = $this->getLibrary('function');
  378. $session->clear();
  379. $function->goto_url('/manage/login');
  380. }
  381. /**
  382. *
  383. * @return logsDao
  384. */
  385. private function getLogsDao()
  386. {
  387. return InitPHP::getDao("logs");
  388. }
  389. }