aigems commited on
Commit
5788bb9
·
1 Parent(s): da5d623
Files changed (4) hide show
  1. app.js +15 -0
  2. package.json +1 -1
  3. public/js/main.js +15 -1
  4. routes/command.js +9 -3
app.js CHANGED
@@ -3,6 +3,7 @@ const winston = require('winston');
3
  const fs = require('fs').promises;
4
  const path = require('path');
5
  const rateLimit = require('express-rate-limit');
 
6
  const helmet = require('helmet');
7
  const jwt = require('jsonwebtoken');
8
 
@@ -77,6 +78,20 @@ app.use((err, req, res, next) => {
77
  res.status(500).json({ error: '服务器内部错误' });
78
  });
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  app.listen(port, () => {
81
  logger.info(`Web 命令执行应用正在监听 http://localhost:${port}`);
82
  });
 
3
  const fs = require('fs').promises;
4
  const path = require('path');
5
  const rateLimit = require('express-rate-limit');
6
+ const { promises: fsPromises } = require('fs');
7
  const helmet = require('helmet');
8
  const jwt = require('jsonwebtoken');
9
 
 
78
  res.status(500).json({ error: '服务器内部错误' });
79
  });
80
 
81
+ // 确保命令历史文件存在
82
+ const historyFilePath = path.join(__dirname, 'data', 'command_history.json');
83
+ fsPromises.access(historyFilePath)
84
+ .catch(() => fsPromises.writeFile(historyFilePath, '[]'))
85
+ .then(() => {
86
+ app.listen(port, () => {
87
+ logger.info(`Web 命令执行应用正在监听 http://localhost:${port}`);
88
+ });
89
+ })
90
+ .catch(err => {
91
+ logger.error('无法创建命令历史文件:', err);
92
+ process.exit(1);
93
+ });
94
+
95
  app.listen(port, () => {
96
  logger.info(`Web 命令执行应用正在监听 http://localhost:${port}`);
97
  });
package.json CHANGED
@@ -21,6 +21,6 @@
21
  "jest": "^27.0.6"
22
  },
23
  "engines": {
24
- "node": ">=18.0.0"
25
  }
26
  }
 
21
  "jest": "^27.0.6"
22
  },
23
  "engines": {
24
+ "node": ">=14.0.0"
25
  }
26
  }
public/js/main.js CHANGED
@@ -94,4 +94,18 @@ async function login() {
94
 
95
  document.getElementById('loginButton').addEventListener('click', login);
96
 
97
- loadCommandHistory();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
  document.getElementById('loginButton').addEventListener('click', login);
96
 
97
+ loadCommandHistory();
98
+
99
+ function checkLoginStatus() {
100
+ const token = localStorage.getItem('token');
101
+ if (token) {
102
+ document.getElementById('loginForm').style.display = 'none';
103
+ document.getElementById('commandInterface').style.display = 'block';
104
+ loadCommandHistory();
105
+ }
106
+ }
107
+
108
+ // 在页面加载时调用此函数
109
+ window.addEventListener('load', checkLoginStatus);
110
+
111
+ document.getElementById('executeButton').addEventListener('click', executeCommand);
routes/command.js CHANGED
@@ -14,10 +14,16 @@ const historyFilePath = path.join(__dirname, '..', 'data', 'command_history.json
14
 
15
  router.get('/command-history', async (req, res) => {
16
  try {
17
- const history = await fs.readFile(historyFilePath, 'utf-8');
18
- res.json(JSON.parse(history));
 
 
 
 
 
 
19
  } catch (error) {
20
- logger.error('读取命令历史失败:', error);
21
  res.status(500).json({ error: '无法读取命令历史' });
22
  }
23
  });
 
14
 
15
  router.get('/command-history', async (req, res) => {
16
  try {
17
+ let history = [];
18
+ try {
19
+ const historyData = await fs.readFile(historyFilePath, 'utf-8');
20
+ history = JSON.parse(historyData);
21
+ } catch (readError) {
22
+ logger.warn('读取命令历史失败,使用空数组:', readError);
23
+ }
24
+ res.json(history);
25
  } catch (error) {
26
+ logger.error('处理命令历史请求失败:', error);
27
  res.status(500).json({ error: '无法读取命令历史' });
28
  }
29
  });