Update server.js
Browse files
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 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 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 |
});
|