manageController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. /**
  128. *
  129. */
  130. public function node_edit()
  131. {
  132. $node = $this->getNodeDao();
  133. $id = $this->controller->get_gp("id");
  134. if ($this->controller->get_gp("do") == "update") { // 提交更新
  135. $data = $this->controller->get_gp(array(
  136. 'node_name',
  137. 'node_type',
  138. 'node_server',
  139. 'node_method',
  140. 'node_info',
  141. 'node_status',
  142. 'node_order',
  143. 'idc_server',
  144. 'idc_url',
  145. 'api_veid',
  146. 'api_key'
  147. ));
  148. foreach ($data as $k => $v) {
  149. if ($v == "") {
  150. unset($data[$k]);
  151. }
  152. }
  153. $node->update($id, $data);
  154. JsMessage("更新成功!", '/manage/node');
  155. die();
  156. }
  157. $nodeData = $node->getNodeById($id);
  158. $this->view->assign("data", $nodeData);
  159. $this->view->set_tpl("manage/node_edit");
  160. $this->view->display();
  161. }
  162. /**
  163. * 用户管理
  164. */
  165. public function user()
  166. {
  167. $userService = self::getUserService();
  168. $all = $userService->getAllTran();
  169. $all = round($all / (1024 * 1024 * 1024), 2);
  170. $pager = self::getPagerLibrary();
  171. $page = $this->controller->get_gp("page") ? $page = $this->controller->get_gp("page") : 1;
  172. $pernum = 10;
  173. $count = $userService->getCount();
  174. $start = $pernum * ($page - 1);
  175. $url = "/manage/user";
  176. $page_html = $pager->pager($count, $pernum, $url, true); // 最后一个参数为true则使用默认样式
  177. $userData = $userService->getUsers($pernum, $start);
  178. $this->view->assign("allTran", $all);
  179. $this->view->assign("user", $userData);
  180. $this->view->assign("pager", $page_html);
  181. $this->view->set_tpl("manage/user");
  182. $this->view->display('', false); // 模板展示
  183. }
  184. public function del()
  185. {
  186. $uid = $this->controller->get_gp("id");
  187. $userService = self::getUserService();
  188. if ($userService->delUser($uid)) {
  189. JsMessage("删除成功!");
  190. } else {
  191. JsMessage("删除失败!");
  192. }
  193. }
  194. /**
  195. * 邀请管理
  196. */
  197. public function code()
  198. {
  199. $do = $this->controller->get_gp("do");
  200. if ($do == 'add') {
  201. $num = $this->controller->get_gp("num");
  202. $pre = $this->controller->get_gp("pre");
  203. $platform = $this->controller->get_gp("platform");
  204. $user = $this->getUserService();
  205. $result = $user->add($pre, $num,$platform);
  206. if ($result) {
  207. echo $result;
  208. // JsMessage("添加成功!", "/manage/user");
  209. }
  210. }
  211. $this->view->set_tpl("/manage/node");
  212. $this->view->display();
  213. }
  214. /**
  215. * 用户列表
  216. */
  217. public function userlist()
  218. {
  219. $adminService = InitPHP::getService("user");
  220. $pager = $this->getLibrary('pager');
  221. $page = $this->controller->get_gp("page") ? $page = $this->controller->get_gp("page") : 1;
  222. $pernum = 30;
  223. $count = $adminService->getCount();
  224. $start = $pernum * ($page - 1);
  225. $url = "userlist";
  226. $page_html = $pager->pager($count, $pernum, $url, $page, true); // 最后一个参数为true则使用默认样式
  227. $adminData = $adminService->getUsers($pernum, $start);
  228. $this->view->set_tpl("manage/userlist");
  229. $this->view->assign("data", $adminData);
  230. $this->view->assign("pager", $page_html);
  231. $this->view->display('', false);
  232. }
  233. public function left()
  234. {
  235. $session = $this->getUtil('session');
  236. $adminname = $session->get('adminname');
  237. $config = InitPHP::getConfig();
  238. $leftmenu = $config['menu'];
  239. $this->view->assign("menu", $leftmenu);
  240. $this->view->assign('admin', $adminname);
  241. $this->view->set_tpl("manage/left");
  242. $this->view->display();
  243. }
  244. public function top()
  245. {
  246. $session = $this->getUtil('session');
  247. $adminid = $session->get('adminid');
  248. if (empty($adminid)) {
  249. $this->view->set_tpl("manage/login");
  250. } else {
  251. $this->view->assign('softid', $session->get('softid'));
  252. $this->view->set_tpl("manage/top");
  253. $this->view->display(); // 模板展示
  254. }
  255. }
  256. /**
  257. * 登陆
  258. */
  259. public function login()
  260. {
  261. $info = $this->controller->get_gp(array(
  262. 'username',
  263. 'password',
  264. 'validcode'
  265. ));
  266. if ($info['username']) {
  267. $session = $this->getUtil('session');
  268. $code = $session->get('user_vaildcode_key');
  269. if (strtolower(trim($code)) !== strtolower(trim($info['validcode']))) {
  270. JsMessage('验证码错了!');
  271. die();
  272. } else {
  273. $password = md5($info['password']);
  274. $admin = InitPHP::getService("admin");
  275. $result = $admin->getOne($info['username']);
  276. if ($result) {
  277. if ($result['pass'] === $password) {
  278. $session->set('adminid', intval($result['uid']));
  279. $session->set('adminname', htmlspecialchars($info['username']));
  280. $function = $this->getLibrary("function");
  281. $function->goto_url("/manage");
  282. } else {
  283. JsMessage("密码错误!");
  284. }
  285. } else {
  286. JsMessage("用户名不存在!");
  287. }
  288. }
  289. } else {
  290. $this->view->set_tpl("manage/login");
  291. $this->view->assign("title", "ShadowSocks5管理后台");
  292. $this->view->display();
  293. }
  294. }
  295. /**
  296. * 检查登陆状态
  297. */
  298. public function checkLogin()
  299. {
  300. $list = array(
  301. "login",
  302. "validcode"
  303. );
  304. $session = $this->getUtil('session');
  305. $adminid = $session->get('adminid');
  306. $a = $this->getA();
  307. if (! in_array($a, $list)) {
  308. if (empty($adminid)) {
  309. $function = $this->getLibrary('function');
  310. $function->goto_url('/manage/login');
  311. die();
  312. }
  313. }
  314. }
  315. /**
  316. *
  317. * @return object|userService
  318. */
  319. private function getUserService()
  320. {
  321. return InitPHP::getService("user");
  322. }
  323. /**
  324. *
  325. * @return nodeDao|object
  326. */
  327. private function getNodeDao()
  328. {
  329. return InitPHP::getDao("node");
  330. }
  331. /**
  332. *
  333. * @return object|pagerInit
  334. */
  335. private function getPagerLibrary()
  336. {
  337. return InitPHP::getLibrarys("pager");
  338. }
  339. /**
  340. * 获取验证码函数
  341. */
  342. public function validcode()
  343. {
  344. $captcha = self::getCaptcha();
  345. $code = $captcha->CreateCheckCode();
  346. $session = $this->getUtil('session');
  347. $session->set('user_vaildcode_key', $code);
  348. $captcha->OutCheckImage();
  349. }
  350. /**
  351. *
  352. * @return captchaInit|object
  353. */
  354. private function getCaptcha()
  355. {
  356. return InitPHP::getLibrarys("captcha");
  357. }
  358. /**
  359. *
  360. * @return object|sshInit
  361. */
  362. private function getssh()
  363. {
  364. return InitPHP::getLibrarys("ssh");
  365. }
  366. /**
  367. *
  368. * @return object|sshDao
  369. */
  370. private function getServer()
  371. {
  372. return InitPHP::getDao("ssh");
  373. }
  374. /**
  375. * 退出后台 删除session
  376. */
  377. public function logout()
  378. {
  379. $session = $this->getUtil('session');
  380. $function = $this->getLibrary('function');
  381. $session->clear();
  382. $function->goto_url('/manage/login');
  383. }
  384. /**
  385. *
  386. * @return logsDao|object
  387. */
  388. private function getLogsDao()
  389. {
  390. return InitPHP::getDao("logs");
  391. }
  392. }