-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (115 loc) · 5.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const express = require('express');
const fs = require('fs');
const path = require('path');
const useragent = require('useragent'); // 引入 useragent 库
const ini = require('ini'); // 引入 ini 模块
const app = express();
// 读取 config.json 文件并解析
const configJsonFilePath = path.join(__dirname, 'config.json');
let configJson = JSON.parse(fs.readFileSync(configJsonFilePath, 'utf-8'));
let port = configJson.port ? parseInt(configJson.port, 10) : 2;
console.log(`从 config.json 中读取的端口值(port)为:${port}`);
let refreshInterval = configJson.loadtime;
console.log(`从 config.json 中读取的配置文件刷新延迟(loadtime)为:${refreshInterval} s`);
refreshInterval = refreshInterval * 1000;
let counter = 0;
// 读取 data.ini 中的 user 值
function readUserFromConfig() {
const dataFilePath = path.join(__dirname, 'data.ini'); // 修改文件路径
const config = fs.readFileSync(dataFilePath, 'utf-8'); // 修改文件路径
const match = config.match(/user=(\d+)/);
return match ? parseInt(match[1], 10) : 0;
}
// 将 user 值写入 data.ini
function writeUserToConfig(user) {
const dataFilePath = path.join(__dirname, 'data.ini'); // 修改文件路径
fs.writeFileSync(dataFilePath, `user=${user}`, 'utf-8'); // 修改文件路径
}
console.log(`从 data.ini 中读取的 user 值为:${counter}`); // 调试信息
counter = readUserFromConfig();
// 输出 reload 配置信息
function outputReloadConfig() {
const reloadConfig = configJson.reload;
for (const key in reloadConfig) {
if (reloadConfig.hasOwnProperty(key)) {
console.log(`已设定 /reload/${key} 重定向到 ${reloadConfig[key]}`);
}
}
}
outputReloadConfig();
// 配置静态文件目录
app.use(express.static(path.join(__dirname, 'main')));
// 中间件:记录访问信息
function logAccess(req, res, next) {
const ipAddress = req.headers['x-forwarded-for'] || req.ip || req.socket.remoteAddress;
const userAgent = req.headers['user-agent'];
const agent = useragent.parse(userAgent); // 解析 user-agent 字符串
const logMessage = `有用户访问页面\n - IP: ${ipAddress}\n - 访问页面: ${req.originalUrl}\n - 浏览器: ${agent.family} ${agent.major}.${agent.minor}.${agent.patch}\n - 操作系统: ${agent.os.family} ${agent.os.major}.${agent.os.minor}.${agent.os.patch}\n`;
// 输出到控制台
console.log(logMessage);
next();
}
// 将 logAccess 中间件应用到所有路由
app.use(logAccess);
// 设置主页为 main/index.html
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'main', 'index.html'));
});
app.use('/zipdownload', express.static(path.join(__dirname, 'zipdownload')));
app.get('/zipdownload', (req, res) => {
const zipDir = path.join(__dirname, 'zipdownload');
fs.readdir(zipDir, (err, files) => {
if (err) {
return res.status(500).send('无法读取文件列表');
}
let fileListHtml = '<html><head><title>文件列表</title></head><body>';
fileListHtml += '<h1>文件列表</h1><ul>';
files.forEach(file => {
fileListHtml += `<li><a href="/zipdownload/${file}">${file}</a></li>`;
});
fileListHtml += '</ul></body></html>';
res.send(fileListHtml);
});
});
app.get('/api/blnum', (req, res) => {
counter++;
writeUserToConfig(counter);
res.json({ user: counter });
});
app.get('/reset', (req, res) => {
counter = 0;
res.json({ message: 'Counter reset' });
});
app.get('/api/showbluser', (req, res) => {
res.json({ user: counter });
});
app.get('/api/server', (req, res) => {
res.json({ 'port': port, 'localip': 'http://localhost:' + port, 'publicip': 'http://pcfs.top:' + port });
});
// 新增 /reload/:url 路由处理重定向
app.get('/reload/:url', (req, res) => {
const reloadConfig = configJson.reload;
const targetUrl = reloadConfig[req.params.url];
if (targetUrl) {
res.redirect(targetUrl);
} else {
res.status(404).send('重定向目标未找到');
}
});
let lastRefreshTime = Date.now();
setInterval(() => {
configJson = JSON.parse(fs.readFileSync(configJsonFilePath, 'utf-8'));
port = configJson.port ? parseInt(configJson.port, 10) : 2;
console.log(`从 config.json 中读取的端口值为:${port}`); // 调试信息
outputReloadConfig();
lastRefreshTime = Date.now();
}, refreshInterval); // 300000 毫秒 = 5 分钟
app.get('/api/loadtime', (req, res) => {
const timeSinceLastRefresh = Date.now() - lastRefreshTime;
const timeUntilNextRefresh = refreshInterval - timeSinceLastRefresh;
const timeUntilNextRefreshSeconds = Math.ceil(timeUntilNextRefresh / 1000); // 转换为秒
res.json({ loadtime: timeUntilNextRefreshSeconds, unit: 'seconds' });
});
app.listen(port, () => {
console.log(`\nBloret-Launcher-Server 服务已经运行:\n 本地位于: http://localhost:${port}\n 外部位于: http://pcfs.top:${port}\n`);
});