一分钟搭建影视站,支持Docker等部署方式
新的开发服务器,在本地实现转发代理
WLongbd39c872ca35
diff --git a/.gitignore b/.gitignorenew file mode 100644index 0000000..8d056c8--- /dev/null+++ b/.gitignore@@ -0,0 +1,2 @@+.vscode/+node_modules/diff --git a/package.json b/package.jsonindex f5a0558..f564b12 100644--- a/package.json+++ b/package.json@@ -5,9 +5,12 @@"private": true,"type": "module","scripts": {- "dev": "npx http-server -p 8080"+ "dev": "node server.mjs"},"dependencies": {+ "axios": "^1.9.0",+ "cors": "^2.8.5",+ "express": "^5.1.0","node-fetch": "^3.3.2"},"author": "bestZwei",diff --git a/server.mjs b/server.mjsnew file mode 100644index 0000000..64bad91--- /dev/null+++ b/server.mjs@@ -0,0 +1,86 @@+import path from 'path';+import express from 'express';+import axios from 'axios';+import cors from 'cors';+import { fileURLToPath } from 'url';++const __filename = fileURLToPath(import.meta.url);+const __dirname = path.dirname(__filename);++const app = express();+const port = 8080;++// 启用 CORS+app.use(cors());++// 静态文件路径+app.use(express.static('./'));++/*+app.get('/', async (req, res) => {+ try {+ const content = await fs.readFile(path.join(__dirname, 'index.html'), 'utf8');+ res.send(content)+ } catch (error) {+ console.error(error)+ res.status(500).send('读取 index.html 失败')+ }+})+*/++app.get('/proxy/:encodedUrl', async (req, res) => {+ try {+ // 获取 URL 编码的参数+ const encodedUrl = req.params.encodedUrl;++ // 对 URL 进行解码+ const targetUrl = decodeURIComponent(encodedUrl);++ // 安全验证+ if (!isValidUrl(targetUrl)) {+ return res.status(400).send('Invalid URL');+ }++ // 发起请求+ const response = await axios({+ method: 'get',+ url: targetUrl,+ responseType: 'stream',+ timeout: 5000+ });++ // 转发响应头(过滤敏感头)+ const headers = { ...response.headers };+ delete headers['content-security-policy'];+ delete headers['cookie'];+ res.set(headers);++ // 管道传输响应流+ response.data.pipe(res);+ } catch (error) {+ if (error.response) {+ error.response.data.pipe(res)+ } else {+ res.status(500).send(error.message)+ }+ }+});++// 安全验证:检查是否为合法 URL+const isValidUrl = (urlString) => {+ try {+ const parsed = new URL(urlString);+ const allowedProtocols = ['http:', 'https:'];+ const blockedHostnames = ['localhost', '127.0.0.1'];++ return allowedProtocols.includes(parsed.protocol) &&+ !blockedHostnames.includes(parsed.hostname);+ } catch {+ return false;+ }+};++app.listen(port, () => {+ console.log(`服务器运行在 http://localhost:${port}`)+});+