main.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. //##############################################################################
  2. // config panel
  3. //##############################################################################
  4. require("./createAPPJson");
  5. const os = require('os');
  6. const PORT = 3001;
  7. const USER = 'admin';
  8. const PASS = 'admin';
  9. const SESSTION_AGE = 10 * 60000; // 10 minutes
  10. //##############################################################################
  11. // inital packages
  12. //##############################################################################
  13. const path = require('path');
  14. const express = require('express');
  15. const app = express();
  16. const exec = require("child_process").exec;
  17. const fs = require('fs');
  18. var session = require('express-session');
  19. // Use the session middleware
  20. app.use(session({ secret: 'keyboard cat', cookie: { maxAge: SESSTION_AGE } }));
  21. // add assets path
  22. app.use('/assets', express.static(path.join(__dirname, 'assets')));
  23. // for parse post
  24. app.use(express.json()); // to support JSON-encoded bodies
  25. app.use(express.urlencoded()); // to support URL-encoded bodies
  26. //
  27. //##############################################################################
  28. // rounting urls
  29. //##############################################################################
  30. app.get('/', function (req, res) {
  31. // check is login
  32. if (req.session.islogin) {
  33. // show index page
  34. res.sendFile(path.join(__dirname, 'www/index.html'));
  35. } else {
  36. // redirect to login page
  37. res.writeHead(302, {
  38. 'Location': '/login'
  39. });
  40. res.end();
  41. }
  42. });
  43. app.get('/login', function (req, res) {
  44. // render login page
  45. res.sendFile(path.join(__dirname, 'www/login.html'));
  46. });
  47. app.post('/loginCheck', function (req, res) {
  48. // check username and password
  49. if (req.body.username === USER && req.body.passwd == PASS) {
  50. // login process
  51. req.session.islogin = true;
  52. // redirect to panel
  53. res.writeHead(302, {
  54. 'Location': '/'
  55. });
  56. } else {
  57. // user or password incrrect go back to login
  58. res.writeHead(302, {
  59. 'Location': '/login'
  60. });
  61. }
  62. res.end();
  63. });
  64. app.get('/getProccess', function (req, res) {
  65. // check is user logined
  66. if (!req.session.islogin) {
  67. res.writeHead(302, {
  68. 'Location': '/login'
  69. });
  70. res.end();
  71. } else {
  72. // send json header
  73. res.writeHead(200, {
  74. 'Content-Type': 'application/json'
  75. });
  76. // get json list from the json
  77. exec("pm2 jlist", (error, stdout, stderr) => {
  78. //do whatever here
  79. res.write(stdout);
  80. res.end();
  81. });
  82. }
  83. });
  84. app.post('/addProccess', function (req, res) {
  85. // check is user logined
  86. if (!req.session.islogin) {
  87. res.writeHead(302, {
  88. 'Location': '/login'
  89. });
  90. res.end();
  91. } else {
  92. // get json list from the json
  93. if (req.body.path === undefined) {
  94. res.writeHead(302, {
  95. 'Location': '/'
  96. });
  97. res.end();
  98. return false;
  99. }
  100. // check is file exists
  101. if (fs.existsSync(req.body.path)) {
  102. // add process
  103. exec('pm2 start "' + req.body.path + '" ' + req.body.args + '', (error, stdout, stderr) => {
  104. // save notificarion
  105. // req.session.notication = error + '\n--------\n' + stdout + '\n--------\n' + stderr;
  106. if (error != null) {
  107. req.session.notication = error + stderr;
  108. } else {
  109. req.session.notication = '进程:' + req.body.path + '启动成功';
  110. }
  111. res.writeHead(302, {
  112. 'Location': '/'
  113. });
  114. res.end();
  115. return true;
  116. });
  117. } else {
  118. // go back
  119. res.writeHead(302, {
  120. 'Location': '/'
  121. });
  122. res.end();
  123. return false;
  124. }
  125. }
  126. });
  127. app.get('/restart', function (req, res) {
  128. // send json header
  129. if (!req.session.islogin) {
  130. res.writeHead(302, {
  131. 'Location': '/login'
  132. });
  133. res.end();
  134. } else {
  135. // check id exits
  136. if (req.query.id) {
  137. // restart the process
  138. exec("pm2 restart " + req.query.id, (error, stdout, stderr) => {
  139. res.writeHead(302, {
  140. 'Location': '/'
  141. });
  142. // req.session.notication = error + '\n--------\n' + stdout + '\n--------\n' + stderr;
  143. if (error != null) {
  144. req.session.notication = error + stderr;
  145. } else {
  146. req.session.notication = '进程:' + req.query.id + '重启成功';
  147. }
  148. res.end();
  149. });
  150. }
  151. }
  152. });
  153. app.get('/start', function (req, res) {
  154. // send json header
  155. if (!req.session.islogin) {
  156. res.writeHead(302, {
  157. 'Location': '/login'
  158. });
  159. res.end();
  160. } else {
  161. // check id exits
  162. if (req.query.id) {
  163. // start the process
  164. exec("pm2 start " + req.query.id, (error, stdout, stderr) => {
  165. res.writeHead(302, {
  166. 'Location': '/'
  167. });
  168. // req.session.notication = error + '\n--------\n' + stdout + '\n--------\n' + stderr;
  169. if (error != null) {
  170. req.session.notication = error + stderr;
  171. } else {
  172. req.session.notication = '进程:' + req.query.id + '启动成功';
  173. }
  174. res.end();
  175. });
  176. }
  177. }
  178. });
  179. app.get('/stop', function (req, res) {
  180. // send json header
  181. if (!req.session.islogin) {
  182. res.writeHead(302, {
  183. 'Location': '/login'
  184. });
  185. res.end();
  186. } else {
  187. // check id exits
  188. if (req.query.id) {
  189. // stop the process
  190. exec("pm2 stop " + req.query.id, (error, stdout, stderr) => {
  191. res.writeHead(302, {
  192. 'Location': '/'
  193. });
  194. // req.session.notication = error + '\n--------\n' + stdout + '\n--------\n' + stderr;
  195. if (error != null) {
  196. req.session.notication = error + stderr;
  197. } else {
  198. req.session.notication = '进程:' + req.query.id + '停止成功';
  199. }
  200. res.end();
  201. });
  202. }
  203. }
  204. });
  205. app.get('/delete', function (req, res) {
  206. // send json header
  207. if (!req.session.islogin) {
  208. res.writeHead(302, {
  209. 'Location': '/login'
  210. });
  211. res.end();
  212. } else {
  213. // check id exits
  214. if (req.query.id) {
  215. // delete the process
  216. exec("pm2 delete " + req.query.id, (error, stdout, stderr) => {
  217. res.writeHead(302, {
  218. 'Location': '/'
  219. });
  220. // req.session.notication = error + '\n--------\n' + stdout + '\n--------\n' + stderr;
  221. if (error != null) {
  222. req.session.notication = error + stderr;
  223. } else {
  224. req.session.notication = '进程:' + req.query.id + '关闭成功';
  225. }
  226. res.end();
  227. });
  228. }
  229. }
  230. });
  231. app.get('/dump', function (req, res) {
  232. // send json header
  233. if (!req.session.islogin) {
  234. res.writeHead(302, {
  235. 'Location': '/login'
  236. });
  237. res.end();
  238. } else {
  239. // save process
  240. exec("pm2 save", (error, stdout, stderr) => {
  241. res.writeHead(302, {
  242. 'Location': '/'
  243. });
  244. //req.session.notication = error + '\n--------\n' + stdout + '\n--------\n' + stderr;
  245. if (error != null) {
  246. req.session.notication = error + stderr;
  247. } else {
  248. req.session.notication = '当前进程保存成功';
  249. }
  250. res.end();
  251. });
  252. }
  253. });
  254. app.get('/deleteall', function (req, res) {
  255. // send json header
  256. if (!req.session.islogin) {
  257. res.writeHead(302, {
  258. 'Location': '/login'
  259. });
  260. res.end();
  261. } else {
  262. // save process
  263. exec("pm2 delete all", (error, stdout, stderr) => {
  264. res.writeHead(302, {
  265. 'Location': '/'
  266. });
  267. //req.session.notication = error + '\n--------\n' + stdout + '\n--------\n' + stderr;
  268. if (error != null) {
  269. req.session.notication = error + stderr;
  270. } else {
  271. req.session.notication = '所有进程已经关闭';
  272. }
  273. res.end();
  274. });
  275. }
  276. });
  277. app.get('/flush', function (req, res) {
  278. // send json header
  279. if (!req.session.islogin) {
  280. res.writeHead(302, {
  281. 'Location': '/login'
  282. });
  283. res.end();
  284. } else {
  285. // save process
  286. exec("pm2 flush", (error, stdout, stderr) => {
  287. res.writeHead(302, {
  288. 'Location': '/'
  289. });
  290. //req.session.notication = error + '\n--------\n' + stdout + '\n--------\n' + stderr;
  291. if (error != null) {
  292. req.session.notication = error + stderr;
  293. } else {
  294. req.session.notication = '清空日志成功';
  295. }
  296. res.end();
  297. });
  298. }
  299. });
  300. app.get('/notification', function (req, res) {
  301. // send json header
  302. if (!req.session.islogin) {
  303. res.writeHead(302, {
  304. 'Location': '/login'
  305. });
  306. res.end();
  307. return false;
  308. } else {
  309. if (!req.session.notication) {
  310. res.write('-');
  311. } else {
  312. var message = req.session.notication;
  313. delete req.session.notication;
  314. res.write(message);
  315. }
  316. res.end();
  317. }
  318. });
  319. app.get('/sysInfo', function (req, res) {
  320. // send json header
  321. if (!req.session.islogin) {
  322. res.writeHead(302, {
  323. 'Location': '/login'
  324. });
  325. res.end();
  326. return false;
  327. } else {
  328. var message = "";
  329. message += `主机 - ${os.hostname()}\n`;
  330. message += `操作系统 - ${os.platform()}\n`;
  331. message += `系统版本 - ${os.release()}\n`;
  332. message += `CPU架构 - ${os.arch()}\n`;
  333. message += `总内存 - ${(os.totalmem() / 1024 / 1024).toFixed(2)}MB\n`;
  334. message += `空闲内存 - ${(os.freemem() / 1024 / 1024).toFixed(2)}MB\n`;
  335. message += `内存占比 - ${(os.freemem() / os.totalmem() * 100).toFixed(2)}%`;
  336. res.write(message);
  337. res.end();
  338. }
  339. });
  340. /// get folder list
  341. app.get('/folder', function (req, res) {
  342. // check is login ?
  343. if (!req.session.islogin) {
  344. res.writeHead(302, {
  345. 'Location': '/login'
  346. });
  347. res.end();
  348. } else {
  349. // check path and set default tab
  350. if (req.query.path === undefined) {
  351. var chossedPath = '/';
  352. } else {
  353. var chossedPath = req.query.path;
  354. }
  355. // send json header
  356. res.writeHead(200, {
  357. 'Content-Type': 'application/json'
  358. });
  359. // check choosed is exists
  360. if (fs.existsSync(chossedPath)) {
  361. // read folder
  362. fs.readdir(chossedPath, (err, files) => {
  363. // creat list
  364. var lst = [];
  365. chossedPath = chossedPath + '/';
  366. chossedPath = chossedPath.replace('//', '/');
  367. // set back folder in list
  368. var e = path.join(chossedPath, '..');
  369. lst.push({ 'name': '..', 'path': e });
  370. files.forEach(file => {
  371. var tmp = { 'name': file, 'path': chossedPath + file };
  372. lst.push(tmp);
  373. });
  374. // send buffer
  375. res.write(JSON.stringify(lst));
  376. res.end();
  377. });
  378. } else {
  379. res.write('[]');
  380. res.end();
  381. }
  382. }
  383. });
  384. app.get('/logout', function (req, res) {
  385. // remover session
  386. delete req.session.islogin;
  387. // redirect to login page
  388. res.writeHead(302, {
  389. 'Location': '/login'
  390. });
  391. res.end();
  392. });
  393. app.get('/log', function (req, res) {
  394. // send json header
  395. if (!req.session.islogin) {
  396. res.writeHead(302, {
  397. 'Location': '/login'
  398. });
  399. res.end();
  400. } else {
  401. // check id exits
  402. if (req.query.id) {
  403. // log of the process
  404. var proc = require('child_process').spawn("pm2", ['log', req.query.id], {
  405. shell: process.platform === 'win32'
  406. });
  407. req.session.notication = '';
  408. proc.stdout.on('data', (data) => {
  409. req.session.notication = req.session.notication + data;
  410. });
  411. setTimeout(function () {
  412. proc.stdin.end();
  413. res.writeHead(302, {
  414. 'Location': '/'
  415. });
  416. res.end();
  417. }, 2000);
  418. }
  419. }
  420. });
  421. app.get('/pinfo', function (req, res) {
  422. // send json header
  423. if (!req.session.islogin) {
  424. res.writeHead(302, {
  425. 'Location': '/login'
  426. });
  427. res.end();
  428. } else {
  429. // check id exits
  430. if (req.query.id) {
  431. // log of the process
  432. var proc = require('child_process').spawn("pm2", ['info', req.query.id], {
  433. shell: process.platform === 'win32'
  434. });
  435. req.session.notication = '';
  436. proc.stdout.on('data', (data) => {
  437. req.session.notication = req.session.notication + data;
  438. });
  439. setTimeout(function () {
  440. proc.stdin.end();
  441. res.writeHead(302, {
  442. 'Location': '/'
  443. });
  444. res.end();
  445. }, 2000);
  446. }
  447. }
  448. });
  449. //##############################################################################
  450. // finazle
  451. //##############################################################################
  452. app.listen(PORT, function () {
  453. console.log('游戏服务管理面板开始监听:' + PORT + ' \n 首页: http://localhost:' + PORT);
  454. });