Skip to content

Commit a630901

Browse files
author
Taois
committed
fix:修复csp机制
1 parent 38e7592 commit a630901

File tree

5 files changed

+225
-35
lines changed

5 files changed

+225
-35
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
# MediaTool 参数说明和示例
2+
3+
## 1. 命令行支持的参数
4+
5+
### 可通过命令行传递的参数
6+
7+
| 参数 | 命令行标志 | 默认值 | 说明 | 示例 |
8+
|------|------------|--------|------|------|
9+
| config | `-config` | `"config.json"` | 配置文件路径 | `./pvideo -config myconfig.json` |
10+
| port | `-port` | `"2525"` | 服务器端口 | `./pvideo -port 8080` |
11+
| site | `-site` | `""` | 反代域名 | `./pvideo -site https://mydomain.com` |
12+
| proxy | `-proxy` | `""` | 代理地址 | `./pvideo -proxy socks5://127.0.0.1:1080` |
13+
| debug | `-debug` | `false` | 调试模式 | `./pvideo -debug` |
14+
| dns | `-dns` | `""` | DNS服务器 | `./pvideo -dns 8.8.8.8` |
15+
16+
### 只能通过配置文件设置的参数
17+
18+
| 参数 | 类型 | 默认值 | 说明 |
19+
|------|------|--------|------|
20+
| localUrl | string | `""` | 本地服务URL,用于生成代理链接 |
21+
| connect | int64 | `32` | 最大并发连接数 |
22+
| ssl | object | `null` | SSL证书配置(cert, key) |
23+
24+
### 命令行使用示例
25+
26+
```bash
27+
# 基本启动
28+
./pvideo
29+
30+
# 指定端口和调试模式
31+
./pvideo -port 8080 -debug
32+
33+
# 使用SOCKS5代理
34+
./pvideo -proxy socks5://127.0.0.1:1080 -dns 8.8.8.8
35+
36+
# 完整参数示例
37+
./pvideo -config config.json -port 7777 -site https://mydomain.com -proxy http://proxy.example.com:8080 -debug -dns 8.8.8.8
38+
```
39+
40+
## 2. 多线程代理参数和示例
41+
42+
### 多线程代理支持的URL参数
43+
44+
| 参数 | 必需 | 说明 | 示例值 |
45+
|------|------|------|--------|
46+
| url || 目标文件URL | `https://example.com/video.mp4` |
47+
| form || 编码格式 | `base64`(URL和header使用base64编码) |
48+
| header || 自定义请求头 | JSON格式的请求头 |
49+
| thread || 线程数 | `4`(默认根据文件大小自动计算) |
50+
| size || 分块大小 | `128K`(默认), `256K`, `1M`, `2M`, `512B`, `1024` |
51+
| limit || 限制条件 | `30S`(时间限制), `100C`(次数限制) |
52+
| single || 单线程模式 | `true`, `false` |
53+
| mark || 缓存标记 | 自定义缓存标识 |
54+
55+
### 多线程代理使用示例(也可以不需要带proxy路径:http://localhost:7777?url=https://example.com/video.mp4)
56+
57+
#### 基本用法
58+
```
59+
http://localhost:7777/proxy?url=https://example.com/video.mp4
60+
```
61+
62+
#### 自定义线程数和分块大小
63+
```
64+
http://localhost:7777/proxy?url=https://example.com/video.mp4&thread=8&size=256K
65+
```
66+
67+
#### 使用自定义请求头
68+
```
69+
http://localhost:7777/proxy?url=https://example.com/video.mp4&header={"User-Agent":"Custom-Agent","Referer":"https://example.com"}
70+
```
71+
72+
#### 使用base64编码(避免URL编码问题)
73+
```
74+
http://localhost:7777/proxy?url=aHR0cHM6Ly9leGFtcGxlLmNvbS92aWRlby5tcDQ=&form=base64&header=eyJVc2VyLUFnZW50IjoiQ3VzdG9tLUFnZW50In0=
75+
```
76+
77+
#### 设置时间限制(30秒后重新获取真实URL)
78+
```
79+
http://localhost:7777/proxy?url=https://example.com/video.mp4&limit=30S
80+
```
81+
82+
#### 设置次数限制(100次请求后重新获取真实URL)
83+
```
84+
http://localhost:7777/proxy?url=https://example.com/video.mp4&limit=100C
85+
```
86+
87+
#### 单线程模式(适用于例如网页请求)
88+
```
89+
http://localhost:7777/proxy?single=true&url=https://example.com/small-file.jpg
90+
```
91+
92+
#### 使用缓存标记(mark参数)
93+
```
94+
# 任务1:普通下载
95+
http://localhost:7777/proxy?url=https://example.com/video.mp4&mark=normal_download
96+
97+
# 任务2:带认证的下载(相同URL,不同处理方式)
98+
http://localhost:7777/proxy?url=https://example.com/video.mp4&mark=auth_download&header={"Authorization":"Bearer token123"}
99+
100+
# 任务3:移动端下载(相同URL,不同User-Agent)
101+
http://localhost:7777/proxy?url=https://example.com/video.mp4&mark=mobile_download&header={"User-Agent":"Mobile-App/1.0"}
102+
```
103+
104+
### 缓存标记(mark)说明
105+
106+
`mark` 参数是一个**缓存标识符**,用于区分和管理不同的下载任务:
107+
108+
**主要作用**
109+
1. **缓存隔离**: 相同URL的不同任务使用独立缓存
110+
2. **连接复用**: 相同mark的请求复用HTTP连接
111+
3. **任务标识**: 便于调试和日志追踪
112+
113+
**使用场景**
114+
115+
#### 场景1: 相同文件的不同下载方式
116+
```
117+
# 高清版本
118+
http://localhost:7777/proxy?url=https://cdn.example.com/video.mp4&mark=hd_version&size=1M
119+
120+
# 标清版本(相同URL,不同处理参数)
121+
http://localhost:7777/proxy?url=https://cdn.example.com/video.mp4&mark=sd_version&size=256K
122+
```
123+
124+
#### 场景2: 不同用户的相同资源
125+
```
126+
# 用户A下载
127+
http://localhost:7777/proxy?url=https://example.com/file.zip&mark=user_a&header={"Cookie":"session=abc123"}
128+
129+
# 用户B下载
130+
http://localhost:7777/proxy?url=https://example.com/file.zip&mark=user_b&header={"Cookie":"session=def456"}
131+
```
132+
133+
#### 场景3: 不同平台的相同内容
134+
```
135+
# PC端下载
136+
http://localhost:7777/proxy?url=https://example.com/app.apk&mark=pc_client
137+
138+
# 移动端下载
139+
http://localhost:7777/proxy?url=https://example.com/app.apk&mark=mobile_client
140+
```
141+
142+
**默认行为**
143+
- 如果不指定 `mark` 参数,系统会使用完整的 `url` 作为缓存标记
144+
- 建议在有多种下载需求时主动设置 `mark` 参数
145+
146+
147+
148+
## 3. M3U8参数和示例
149+
150+
### M3U8支持的URL参数
151+
152+
| 参数 | 必需 | 说明 | 示例值 |
153+
|------|------|------|--------|
154+
| url || M3U8播放列表URL | `https://example.com/playlist.m3u8` |
155+
| form || 编码格式 | `base64`(URL和header使用base64编码) |
156+
| header || 自定义请求头 | JSON格式的请求头 |
157+
| type || 文件类型标识 | `m3u8` |
158+
159+
### M3U8使用示例
160+
161+
#### 基本用法
162+
```
163+
http://localhost:7777/m3u8?url=https://example.com/playlist.m3u8
164+
```
165+
166+
#### 使用自定义请求头
167+
```
168+
http://localhost:7777/m3u8?url=https://example.com/playlist.m3u8&header={"User-Agent":"Mozilla/5.0","Referer":"https://example.com"}
169+
```
170+
171+
#### 使用base64编码
172+
```
173+
http://localhost:7777/m3u8?url=aHR0cHM6Ly9leGFtcGxlLmNvbS9wbGF5bGlzdC5tM3U4&form=base64&header=eyJVc2VyLUFnZW50IjoiTW96aWxsYS81LjAifQ==
174+
```
175+
176+
#### 指定类型标识
177+
```
178+
http://localhost:7777/m3u8?url=https://example.com/playlist.m3u8&type=m3u8
179+
```
180+
181+
### M3U8处理说明
182+
183+
1. **自动转换**: M3U8文件中的相对路径会自动转换为完整URL
184+
2. **代理重写**: 所有媒体文件URL会被重写为通过本地代理访问
185+
3. **嵌套支持**: 支持嵌套的M3U8文件(主播放列表包含子播放列表)
186+
4. **加密支持**: 支持AES加密的M3U8流,密钥URL也会被代理
187+
5. **Base64编码**: 当使用`form=base64`时,输出的代理URL也会使用base64编码

dashboard/pnpm-lock.yaml

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dashboard/src/utils/csp.js

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -94,39 +94,29 @@ export function fetchWithNoReferrer(url, options = {}) {
9494
})
9595
}
9696

97-
/**
98-
* 检查URL是否可能需要特殊的referrer策略
99-
* @param {string} url - 视频URL
100-
* @returns {boolean} 是否需要特殊处理
101-
*/
102-
export function needsSpecialReferrerPolicy(url) {
103-
if (!url) return false
104-
105-
// 常见的需要no-referrer的域名模式
106-
const specialDomains = [
107-
'qq.com',
108-
'iqiyi.com',
109-
'youku.com',
110-
'bilibili.com',
111-
'douyin.com',
112-
'tiktok.com',
113-
'youtube.com',
114-
'vimeo.com'
115-
]
116-
117-
return specialDomains.some(domain => url.includes(domain))
118-
}
97+
11998

12099
/**
121100
* 智能设置referrer策略
122-
* 根据URL自动判断是否需要设置no-referrer
101+
* 根据CSP绕过配置来决定referrer策略
123102
* @param {string} videoUrl - 视频URL
124103
* @param {HTMLVideoElement} videoElement - 视频元素(可选)
125104
*/
126105
export function smartSetReferrerPolicy(videoUrl, videoElement = null) {
127-
const policy = needsSpecialReferrerPolicy(videoUrl)
128-
? REFERRER_POLICIES.NO_REFERRER
129-
: REFERRER_POLICIES.STRICT_ORIGIN_WHEN_CROSS_ORIGIN
106+
// 获取CSP配置
107+
const config = getCSPConfig()
108+
109+
let policy
110+
if (config.autoBypass) {
111+
// 如果启用了CSP绕过,使用配置中的referrer策略
112+
policy = config.referrerPolicy
113+
console.log(`根据CSP配置设置referrer策略: ${policy} (URL: ${videoUrl})`)
114+
} else {
115+
// 如果未启用CSP绕过,保持当前策略或使用默认策略
116+
const currentPolicy = getCurrentReferrerPolicy()
117+
policy = currentPolicy || REFERRER_POLICIES.STRICT_ORIGIN_WHEN_CROSS_ORIGIN
118+
console.log(`CSP绕过未启用,保持当前策略: ${policy} (URL: ${videoUrl})`)
119+
}
130120

131121
// 设置全局策略
132122
setGlobalReferrerPolicy(policy)
@@ -136,7 +126,6 @@ export function smartSetReferrerPolicy(videoUrl, videoElement = null) {
136126
setVideoReferrerPolicy(videoElement, policy)
137127
}
138128

139-
console.log(`智能设置referrer策略: ${policy} (URL: ${videoUrl})`)
140129
return policy
141130
}
142131

@@ -151,9 +140,6 @@ export function resetReferrerPolicy() {
151140
* CSP绕过配置对象
152141
*/
153142
export const CSP_BYPASS_CONFIG = {
154-
// 是否启用CSP绕过功能
155-
enabled: true,
156-
157143
// 默认referrer策略
158144
referrerPolicy: REFERRER_POLICIES.NO_REFERRER,
159145

dashboard/src/views/Settings.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,7 +1353,7 @@ const loadConfig = async () => {
13531353
// 加载CSP设置
13541354
try {
13551355
const cspConfig = getCSPConfig()
1356-
settings.cspBypass = cspConfig.enabled
1356+
settings.cspBypass = cspConfig.autoBypass
13571357
settings.referrerPolicy = cspConfig.referrerPolicy
13581358
} catch (error) {
13591359
console.error('Failed to load CSP config:', error)
@@ -1367,7 +1367,7 @@ const saveSettings = () => {
13671367
// 保存CSP设置
13681368
try {
13691369
saveCSPConfig({
1370-
enabled: settings.cspBypass,
1370+
autoBypass: settings.cspBypass,
13711371
referrerPolicy: settings.referrerPolicy
13721372
})
13731373

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
**一款功能强大、智能化的现代视频播放器,支持多格式播放、智能跳过、直播功能等特色功能**
1111

12-
[🚀 在线演示](https://drplayer.playdreamer.cn/) | [📖 文档](https://hipy.playdreamer.cn/) | [🐛 问题反馈](https://github.com/your-repo/issues)
12+
[🚀 在线演示](https://drplayer.playdreamer.cn/) | [📖 文档](https://hipy.playdreamer.cn/) | [🐛 问题反馈](https://github.com/hjdhnx/DrPlayer/issues)
1313

1414
</div>
1515

@@ -57,7 +57,7 @@
5757
- **Shaka Player** - DASH 流媒体播放支持
5858

5959
### 开发工具
60-
- **TypeScript** - 类型安全的 JavaScript
60+
- **JavaScript** - 现代化的 JavaScript 开发
6161
- **ESLint** - 代码质量检查
6262
- **Prettier** - 代码格式化工具
6363

@@ -193,7 +193,7 @@ export const CSP_BYPASS_CONFIG = {
193193

194194
- 项目主页:[https://drplayer.playdreamer.cn/](https://drplayer.playdreamer.cn/)
195195
- 文档站点:[https://hipy.playdreamer.cn/](https://hipy.playdreamer.cn/)
196-
- 问题反馈:[GitHub Issues](https://github.com/your-repo/issues)
196+
- 问题反馈:[GitHub Issues](https://github.com/hjdhnx/DrPlayer/issues)
197197

198198
---
199199

0 commit comments

Comments
 (0)