forked from LibreSpark/LibreTV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
221 lines (189 loc) · 6.2 KB
/
server.mjs
File metadata and controls
221 lines (189 loc) · 6.2 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import path from 'path';
import express from 'express';
import axios from 'axios';
import cors from 'cors';
import { fileURLToPath } from 'url';
import fs from 'fs';
import crypto from 'crypto';
import dotenv from 'dotenv';
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const config = {
port: process.env.PORT || 8080,
password: process.env.PASSWORD || '',
adminpassword: process.env.ADMINPASSWORD || '',
corsOrigin: process.env.CORS_ORIGIN || '*',
timeout: parseInt(process.env.REQUEST_TIMEOUT || '5000'),
maxRetries: parseInt(process.env.MAX_RETRIES || '2'),
cacheMaxAge: process.env.CACHE_MAX_AGE || '1d',
userAgent: process.env.USER_AGENT || 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
debug: process.env.DEBUG === 'true'
};
const log = (...args) => {
if (config.debug) {
console.log('[DEBUG]', ...args);
}
};
const app = express();
app.use(cors({
origin: config.corsOrigin,
methods: ['GET', 'POST'],
allowedHeaders: ['Content-Type', 'Authorization']
}));
app.use((req, res, next) => {
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('X-Frame-Options', 'SAMEORIGIN');
res.setHeader('X-XSS-Protection', '1; mode=block');
next();
});
function sha256Hash(input) {
return new Promise((resolve) => {
const hash = crypto.createHash('sha256');
hash.update(input);
resolve(hash.digest('hex'));
});
}
async function renderPage(filePath, password) {
let content = fs.readFileSync(filePath, 'utf8');
if (password !== '') {
const sha256 = await sha256Hash(password);
content = content.replace('{{PASSWORD}}', sha256);
}
// 添加ADMINPASSWORD注入
if (config.adminpassword !== '') {
const adminSha256 = await sha256Hash(config.adminpassword);
content = content.replace('{{ADMINPASSWORD}}', adminSha256);
}
return content;
}
app.get(['/', '/index.html', '/player.html'], async (req, res) => {
try {
let filePath;
switch (req.path) {
case '/player.html':
filePath = path.join(__dirname, 'player.html');
break;
default: // '/' 和 '/index.html'
filePath = path.join(__dirname, 'index.html');
break;
}
const content = await renderPage(filePath, config.password);
res.send(content);
} catch (error) {
console.error('页面渲染错误:', error);
res.status(500).send('读取静态页面失败');
}
});
app.get('/s=:keyword', async (req, res) => {
try {
const filePath = path.join(__dirname, 'index.html');
const content = await renderPage(filePath, config.password);
res.send(content);
} catch (error) {
console.error('搜索页面渲染错误:', error);
res.status(500).send('读取静态页面失败');
}
});
function isValidUrl(urlString) {
try {
const parsed = new URL(urlString);
const allowedProtocols = ['http:', 'https:'];
// 从环境变量获取阻止的主机名列表
const blockedHostnames = (process.env.BLOCKED_HOSTS || 'localhost,127.0.0.1,0.0.0.0,::1').split(',');
// 从环境变量获取阻止的 IP 前缀
const blockedPrefixes = (process.env.BLOCKED_IP_PREFIXES || '192.168.,10.,172.').split(',');
if (!allowedProtocols.includes(parsed.protocol)) return false;
if (blockedHostnames.includes(parsed.hostname)) return false;
for (const prefix of blockedPrefixes) {
if (parsed.hostname.startsWith(prefix)) return false;
}
return true;
} catch {
return false;
}
}
// 修复反向代理处理过的路径
app.use('/proxy', (req, res, next) => {
const targetUrl = req.url.replace(/^\//, '').replace(/(https?:)\/([^/])/, '$1//$2');
req.url = '/' + encodeURIComponent(targetUrl);
next();
});
// 代理路由
app.get('/proxy/:encodedUrl', async (req, res) => {
try {
const encodedUrl = req.params.encodedUrl;
const targetUrl = decodeURIComponent(encodedUrl);
// 安全验证
if (!isValidUrl(targetUrl)) {
return res.status(400).send('无效的 URL');
}
log(`代理请求: ${targetUrl}`);
// 添加请求超时和重试逻辑
const maxRetries = config.maxRetries;
let retries = 0;
const makeRequest = async () => {
try {
return await axios({
method: 'get',
url: targetUrl,
responseType: 'stream',
timeout: config.timeout,
headers: {
'User-Agent': config.userAgent
}
});
} catch (error) {
if (retries < maxRetries) {
retries++;
log(`重试请求 (${retries}/${maxRetries}): ${targetUrl}`);
return makeRequest();
}
throw error;
}
};
const response = await makeRequest();
// 转发响应头(过滤敏感头)
const headers = { ...response.headers };
const sensitiveHeaders = (
process.env.FILTERED_HEADERS ||
'content-security-policy,cookie,set-cookie,x-frame-options,access-control-allow-origin'
).split(',');
sensitiveHeaders.forEach(header => delete headers[header]);
res.set(headers);
// 管道传输响应流
response.data.pipe(res);
} catch (error) {
console.error('代理请求错误:', error.message);
if (error.response) {
res.status(error.response.status || 500);
error.response.data.pipe(res);
} else {
res.status(500).send(`请求失败: ${error.message}`);
}
}
});
app.use(express.static(path.join(__dirname), {
maxAge: config.cacheMaxAge
}));
app.use((err, req, res, next) => {
console.error('服务器错误:', err);
res.status(500).send('服务器内部错误');
});
app.use((req, res) => {
res.status(404).send('页面未找到');
});
// 启动服务器
app.listen(config.port, () => {
console.log(`服务器运行在 http://localhost:${config.port}`);
if (config.password !== '') {
console.log('用户登录密码已设置');
}
if (config.adminpassword !== '') {
console.log('管理员登录密码已设置');
}
if (config.debug) {
console.log('调试模式已启用');
console.log('配置:', { ...config, password: config.password ? '******' : '', adminpassword: config.adminpassword? '******' : '' });
}
});