| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533 |
- //##############################################################################
- // config panel
- //##############################################################################
- require("./createAPPJson");
- const os = require('os');
- const PORT = 3001;
- const USER = 'admin';
- const PASS = 'admin';
- const SESSTION_AGE = 10 * 60000; // 10 minutes
- //##############################################################################
- // inital packages
- //##############################################################################
- const path = require('path');
- const express = require('express');
- const app = express();
- const exec = require("child_process").exec;
- const fs = require('fs');
- var session = require('express-session');
- // Use the session middleware
- app.use(session({ secret: 'keyboard cat', cookie: { maxAge: SESSTION_AGE } }));
- // add assets path
- app.use('/assets', express.static(path.join(__dirname, 'assets')));
- // for parse post
- app.use(express.json()); // to support JSON-encoded bodies
- app.use(express.urlencoded()); // to support URL-encoded bodies
- //
- //##############################################################################
- // rounting urls
- //##############################################################################
- app.get('/', function (req, res) {
- // check is login
- if (req.session.islogin) {
- // show index page
- res.sendFile(path.join(__dirname, 'www/index.html'));
- } else {
- // redirect to login page
- res.writeHead(302, {
- 'Location': '/login'
- });
- res.end();
- }
- });
- app.get('/login', function (req, res) {
- // render login page
- res.sendFile(path.join(__dirname, 'www/login.html'));
- });
- app.post('/loginCheck', function (req, res) {
- // check username and password
- if (req.body.username === USER && req.body.passwd == PASS) {
- // login process
- req.session.islogin = true;
- // redirect to panel
- res.writeHead(302, {
- 'Location': '/'
- });
- } else {
- // user or password incrrect go back to login
- res.writeHead(302, {
- 'Location': '/login'
- });
- }
- res.end();
- });
- app.get('/getProccess', function (req, res) {
- // check is user logined
- if (!req.session.islogin) {
- res.writeHead(302, {
- 'Location': '/login'
- });
- res.end();
- } else {
- // send json header
- res.writeHead(200, {
- 'Content-Type': 'application/json'
- });
- // get json list from the json
- exec("pm2 jlist", (error, stdout, stderr) => {
- //do whatever here
- res.write(stdout);
- res.end();
- });
- }
- });
- app.post('/addProccess', function (req, res) {
- // check is user logined
- if (!req.session.islogin) {
- res.writeHead(302, {
- 'Location': '/login'
- });
- res.end();
- } else {
- // get json list from the json
- if (req.body.path === undefined) {
- res.writeHead(302, {
- 'Location': '/'
- });
- res.end();
- return false;
- }
- // check is file exists
- if (fs.existsSync(req.body.path)) {
- // add process
- exec('pm2 start "' + req.body.path + '" ' + req.body.args + '', (error, stdout, stderr) => {
- // save notificarion
- // req.session.notication = error + '\n--------\n' + stdout + '\n--------\n' + stderr;
- if (error != null) {
- req.session.notication = error + stderr;
- } else {
- req.session.notication = '进程:' + req.body.path + '启动成功';
- }
- res.writeHead(302, {
- 'Location': '/'
- });
- res.end();
- return true;
- });
- } else {
- // go back
- res.writeHead(302, {
- 'Location': '/'
- });
- res.end();
- return false;
- }
- }
- });
- app.get('/restart', function (req, res) {
- // send json header
- if (!req.session.islogin) {
- res.writeHead(302, {
- 'Location': '/login'
- });
- res.end();
- } else {
- // check id exits
- if (req.query.id) {
- // restart the process
- exec("pm2 restart " + req.query.id, (error, stdout, stderr) => {
- res.writeHead(302, {
- 'Location': '/'
- });
- // req.session.notication = error + '\n--------\n' + stdout + '\n--------\n' + stderr;
- if (error != null) {
- req.session.notication = error + stderr;
- } else {
- req.session.notication = '进程:' + req.query.id + '重启成功';
- }
- res.end();
- });
- }
- }
- });
- app.get('/start', function (req, res) {
- // send json header
- if (!req.session.islogin) {
- res.writeHead(302, {
- 'Location': '/login'
- });
- res.end();
- } else {
- // check id exits
- if (req.query.id) {
- // start the process
- exec("pm2 start " + req.query.id, (error, stdout, stderr) => {
- res.writeHead(302, {
- 'Location': '/'
- });
- // req.session.notication = error + '\n--------\n' + stdout + '\n--------\n' + stderr;
- if (error != null) {
- req.session.notication = error + stderr;
- } else {
- req.session.notication = '进程:' + req.query.id + '启动成功';
- }
- res.end();
- });
- }
- }
- });
- app.get('/stop', function (req, res) {
- // send json header
- if (!req.session.islogin) {
- res.writeHead(302, {
- 'Location': '/login'
- });
- res.end();
- } else {
- // check id exits
- if (req.query.id) {
- // stop the process
- exec("pm2 stop " + req.query.id, (error, stdout, stderr) => {
- res.writeHead(302, {
- 'Location': '/'
- });
- // req.session.notication = error + '\n--------\n' + stdout + '\n--------\n' + stderr;
- if (error != null) {
- req.session.notication = error + stderr;
- } else {
- req.session.notication = '进程:' + req.query.id + '停止成功';
- }
- res.end();
- });
- }
- }
- });
- app.get('/delete', function (req, res) {
- // send json header
- if (!req.session.islogin) {
- res.writeHead(302, {
- 'Location': '/login'
- });
- res.end();
- } else {
- // check id exits
- if (req.query.id) {
- // delete the process
- exec("pm2 delete " + req.query.id, (error, stdout, stderr) => {
- res.writeHead(302, {
- 'Location': '/'
- });
- // req.session.notication = error + '\n--------\n' + stdout + '\n--------\n' + stderr;
- if (error != null) {
- req.session.notication = error + stderr;
- } else {
- req.session.notication = '进程:' + req.query.id + '关闭成功';
- }
- res.end();
- });
- }
- }
- });
- app.get('/dump', function (req, res) {
- // send json header
- if (!req.session.islogin) {
- res.writeHead(302, {
- 'Location': '/login'
- });
- res.end();
- } else {
- // save process
- exec("pm2 save", (error, stdout, stderr) => {
- res.writeHead(302, {
- 'Location': '/'
- });
- //req.session.notication = error + '\n--------\n' + stdout + '\n--------\n' + stderr;
- if (error != null) {
- req.session.notication = error + stderr;
- } else {
- req.session.notication = '当前进程保存成功';
- }
- res.end();
- });
- }
- });
- app.get('/deleteall', function (req, res) {
- // send json header
- if (!req.session.islogin) {
- res.writeHead(302, {
- 'Location': '/login'
- });
- res.end();
- } else {
- // save process
- exec("pm2 delete all", (error, stdout, stderr) => {
- res.writeHead(302, {
- 'Location': '/'
- });
- //req.session.notication = error + '\n--------\n' + stdout + '\n--------\n' + stderr;
- if (error != null) {
- req.session.notication = error + stderr;
- } else {
- req.session.notication = '所有进程已经关闭';
- }
- res.end();
- });
- }
- });
- app.get('/flush', function (req, res) {
- // send json header
- if (!req.session.islogin) {
- res.writeHead(302, {
- 'Location': '/login'
- });
- res.end();
- } else {
- // save process
- exec("pm2 flush", (error, stdout, stderr) => {
- res.writeHead(302, {
- 'Location': '/'
- });
- //req.session.notication = error + '\n--------\n' + stdout + '\n--------\n' + stderr;
- if (error != null) {
- req.session.notication = error + stderr;
- } else {
- req.session.notication = '清空日志成功';
- }
- res.end();
- });
- }
- });
- app.get('/notification', function (req, res) {
- // send json header
- if (!req.session.islogin) {
- res.writeHead(302, {
- 'Location': '/login'
- });
- res.end();
- return false;
- } else {
- if (!req.session.notication) {
- res.write('-');
- } else {
- var message = req.session.notication;
- delete req.session.notication;
- res.write(message);
- }
- res.end();
- }
- });
- app.get('/sysInfo', function (req, res) {
- // send json header
- if (!req.session.islogin) {
- res.writeHead(302, {
- 'Location': '/login'
- });
- res.end();
- return false;
- } else {
- var message = "";
- message += `主机 - ${os.hostname()}\n`;
- message += `操作系统 - ${os.platform()}\n`;
- message += `系统版本 - ${os.release()}\n`;
- message += `CPU架构 - ${os.arch()}\n`;
- message += `总内存 - ${(os.totalmem() / 1024 / 1024).toFixed(2)}MB\n`;
- message += `空闲内存 - ${(os.freemem() / 1024 / 1024).toFixed(2)}MB\n`;
- message += `内存占比 - ${(os.freemem() / os.totalmem() * 100).toFixed(2)}%`;
- res.write(message);
- res.end();
- }
- });
- /// get folder list
- app.get('/folder', function (req, res) {
- // check is login ?
- if (!req.session.islogin) {
- res.writeHead(302, {
- 'Location': '/login'
- });
- res.end();
- } else {
- // check path and set default tab
- if (req.query.path === undefined) {
- var chossedPath = '/';
- } else {
- var chossedPath = req.query.path;
- }
- // send json header
- res.writeHead(200, {
- 'Content-Type': 'application/json'
- });
- // check choosed is exists
- if (fs.existsSync(chossedPath)) {
- // read folder
- fs.readdir(chossedPath, (err, files) => {
- // creat list
- var lst = [];
- chossedPath = chossedPath + '/';
- chossedPath = chossedPath.replace('//', '/');
- // set back folder in list
- var e = path.join(chossedPath, '..');
- lst.push({ 'name': '..', 'path': e });
- files.forEach(file => {
- var tmp = { 'name': file, 'path': chossedPath + file };
- lst.push(tmp);
- });
- // send buffer
- res.write(JSON.stringify(lst));
- res.end();
- });
- } else {
- res.write('[]');
- res.end();
- }
- }
- });
- app.get('/logout', function (req, res) {
- // remover session
- delete req.session.islogin;
- // redirect to login page
- res.writeHead(302, {
- 'Location': '/login'
- });
- res.end();
- });
- app.get('/log', function (req, res) {
- // send json header
- if (!req.session.islogin) {
- res.writeHead(302, {
- 'Location': '/login'
- });
- res.end();
- } else {
- // check id exits
- if (req.query.id) {
- // log of the process
- var proc = require('child_process').spawn("pm2", ['log', req.query.id], {
- shell: process.platform === 'win32'
- });
- req.session.notication = '';
- proc.stdout.on('data', (data) => {
- req.session.notication = req.session.notication + data;
- });
- setTimeout(function () {
- proc.stdin.end();
- res.writeHead(302, {
- 'Location': '/'
- });
- res.end();
- }, 2000);
- }
- }
- });
- app.get('/pinfo', function (req, res) {
- // send json header
- if (!req.session.islogin) {
- res.writeHead(302, {
- 'Location': '/login'
- });
- res.end();
- } else {
- // check id exits
- if (req.query.id) {
- // log of the process
- var proc = require('child_process').spawn("pm2", ['info', req.query.id], {
- shell: process.platform === 'win32'
- });
- req.session.notication = '';
- proc.stdout.on('data', (data) => {
- req.session.notication = req.session.notication + data;
- });
- setTimeout(function () {
- proc.stdin.end();
- res.writeHead(302, {
- 'Location': '/'
- });
- res.end();
- }, 2000);
- }
- }
- });
- //##############################################################################
- // finazle
- //##############################################################################
- app.listen(PORT, function () {
- console.log('游戏服务管理面板开始监听:' + PORT + ' \n 首页: http://localhost:' + PORT);
- });
|