mc838 commited on
Commit
03436a8
·
verified ·
1 Parent(s): 01e5ee4

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +73 -115
server.js CHANGED
@@ -1,116 +1,74 @@
1
- const express = require('express');
2
- const { exec } = require('child_process');
3
- const fs = require('fs');
4
- const path = require('path');
5
- const app = express();
6
- const port = process.env.PORT || 8080;
7
-
8
- // 使用 HOME 环境变量来创建日志目录
9
- const logDir = path.join(process.env.HOME, 'logs');
10
- if (!fs.existsSync(logDir)) {
11
- fs.mkdirSync(logDir, { recursive: true });
12
- }
13
-
14
- let lastRunTime = null;
15
- let nextRunTime = null;
16
- let lastRunResults = null;
17
-
18
- function log(message) {
19
- const timestamp = new Date().toISOString();
20
- const logMessage = `${timestamp}: ${message}\n`;
21
- console.log(logMessage);
22
- fs.appendFileSync(path.join(logDir, 'app.log'), logMessage);
23
- }
24
-
25
- app.get('/', (req, res) => {
26
- let tableHtml = '';
27
- let summaryHtml = '';
28
-
29
- if (lastRunResults) {
30
- tableHtml = '<h2>上次登录结果:</h2>';
31
- tableHtml += '<table border="1"><tr><th>账号</th><th>类型</th><th>状态</th><th>消息</th></tr>';
32
- lastRunResults.forEach(result => {
33
- tableHtml += `<tr><td>${result.username}</td><td>${result.type}</td><td>${result.success ? '成功' : '失败'}</td><td>${result.message}</td></tr>`;
34
- });
35
- tableHtml += '</table>';
36
-
37
- const successfulLogins = lastRunResults.filter(r => r.success);
38
- const failedLogins = lastRunResults.filter(r => !r.success);
39
-
40
- summaryHtml = '<h2>登录结果统计:</h2>';
41
- summaryHtml += `<p>成功登录的账号:${successfulLogins.length}</p>`;
42
- summaryHtml += `<p>登录失败的账号:${failedLogins.length}</p>`;
43
-
44
- if (failedLogins.length > 0) {
45
- summaryHtml += '<h3>登录失败的账号列表:</h3><ul>';
46
- failedLogins.forEach(({ username, type }) => {
47
- summaryHtml += `<li>${username} (${type})</li>`;
48
- });
49
- summaryHtml += '</ul>';
50
- }
51
- }
52
-
53
- res.send(`
54
- <h1>登录脚本状态</h1>
55
- <p>上次执行时间:${lastRunTime || '尚未执行'}</p>
56
- <p>下次执行时间:${nextRunTime || '未设置'}</p>
57
- <a href="/run">立即执行脚本</a>
58
- <br><br>
59
- <a href="/logs">查看日志</a>
60
- ${tableHtml}
61
- ${summaryHtml}
62
- `);
63
- });
64
-
65
- app.get('/logs', (req, res) => {
66
- const logPath = path.join(logDir, 'app.log');
67
- fs.readFile(logPath, 'utf8', (err, data) => {
68
- if (err) {
69
- res.status(500).send('无法读取日志文件');
70
- return;
71
- }
72
- res.send(`<pre>${data}</pre>`);
73
- });
74
- });
75
-
76
- function runLoginScript() {
77
- log('开始执行登录脚本');
78
- exec('node login.js', (error, stdout, stderr) => {
79
- if (error) {
80
- log(`执行错误: ${error.message}`);
81
- return;
82
- }
83
- if (stderr) {
84
- log(`脚本错误输出: ${stderr}`);
85
- return;
86
- }
87
- log(`脚本输出:\n${stdout}`);
88
-
89
- // 解析输出以获取结果
90
- const lines = stdout.split('\n');
91
- const resultStartIndex = lines.findIndex(line => line.includes('| 账号 | 类型 | 状态 | 消息 |'));
92
- if (resultStartIndex !== -1) {
93
- lastRunResults = lines.slice(resultStartIndex + 2)
94
- .filter(line => line.trim().startsWith('|'))
95
- .map(line => {
96
- const [, username, type, status, message] = line.split('|').map(item => item.trim());
97
- return { username, type, success: status === '成功', message };
98
- });
99
- }
100
- });
101
- lastRunTime = new Date().toISOString();
102
- nextRunTime = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString();
103
- }
104
-
105
- app.get('/run', (req, res) => {
106
- runLoginScript();
107
- res.send('脚本执行已启动,请稍后刷新页面查看结果。');
108
- });
109
-
110
- // 每7天自动运行一次脚本
111
- setInterval(runLoginScript, 7 * 24 * 60 * 60 * 1000);
112
-
113
- app.listen(port, () => {
114
- log(`服务器运行在 http://localhost:${port}`);
115
- runLoginScript(); // 启动时立即运行一次
116
  });
 
1
+ const express = require('express');
2
+ const { exec } = require('child_process');
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ const app = express();
6
+ const port = process.env.PORT || 8080;
7
+
8
+ // 使用 HOME 环境变量来创建日志目录
9
+ const logDir = path.join(process.env.HOME, 'logs');
10
+ if (!fs.existsSync(logDir)) {
11
+ fs.mkdirSync(logDir, { recursive: true });
12
+ }
13
+
14
+ function log(message) {
15
+ const timestamp = new Date().toISOString();
16
+ const logMessage = `${timestamp}: ${message}\n`;
17
+ console.log(logMessage);
18
+ fs.appendFileSync(path.join(logDir, 'app.log'), logMessage);
19
+ }
20
+
21
+ app.get('/', (req, res) => {
22
+ res.send(`
23
+ <h1>登录脚本状态</h1>
24
+ <p>上次执行时间:${lastRunTime || '尚未执行'}</p>
25
+ <p>下次执行时间:${nextRunTime || '未设置'}</p>
26
+ <a href="/run">立即��行脚本</a>
27
+ <br><br>
28
+ <a href="/logs">查看日志</a>
29
+ `);
30
+ });
31
+
32
+ app.get('/logs', (req, res) => {
33
+ const logPath = path.join(logDir, 'app.log');
34
+ fs.readFile(logPath, 'utf8', (err, data) => {
35
+ if (err) {
36
+ res.status(500).send('无法读取日志文件');
37
+ return;
38
+ }
39
+ res.send(`<pre>${data}</pre>`);
40
+ });
41
+ });
42
+
43
+ let lastRunTime = null;
44
+ let nextRunTime = null;
45
+
46
+ function runLoginScript() {
47
+ log('开始执行登录脚本');
48
+ exec('node login.js', (error, stdout, stderr) => {
49
+ if (error) {
50
+ log(`执行错误: ${error.message}`);
51
+ return;
52
+ }
53
+ if (stderr) {
54
+ log(`脚本错误输出: ${stderr}`);
55
+ return;
56
+ }
57
+ log(`脚本输出:\n${stdout}`);
58
+ });
59
+ lastRunTime = new Date().toISOString();
60
+ nextRunTime = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString();
61
+ }
62
+
63
+ app.get('/run', (req, res) => {
64
+ runLoginScript();
65
+ res.send('脚本执行已启动,请查看日志获取详细信息。');
66
+ });
67
+
68
+ // 每7天自动运行一次脚本
69
+ setInterval(runLoginScript, 7 * 24 * 60 * 60 * 1000);
70
+
71
+ app.listen(port, () => {
72
+ log(`服务器运行在 http://localhost:${port}`);
73
+ runLoginScript(); // 启动时立即运行一次
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  });