-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathCSPTest.vue
More file actions
818 lines (716 loc) · 18.7 KB
/
CSPTest.vue
File metadata and controls
818 lines (716 loc) · 18.7 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
<template>
<div class="csp-test-container">
<!-- 固定头部区域 -->
<div class="test-header">
<div class="header-content">
<div class="header-left">
<h1 class="page-title">
<icon-safe class="title-icon" />
CSP测试工具
</h1>
<p class="page-subtitle">测试内容安全策略(CSP)绕过功能和Referrer策略设置</p>
</div>
<div class="nav-button-group">
<div class="nav-button-grid">
<a-button type="primary" status="success" @click="goToActionTest" class="nav-grid-button">
<icon-arrow-left />
<span>Action测试</span>
</a-button>
<a-button type="primary" status="warning" @click="goToVideoTest" class="nav-grid-button">
<icon-play-arrow />
<span>视频测试</span>
</a-button>
<a-button type="primary" status="normal" @click="goToSettings" class="nav-grid-button">
<icon-settings />
<span>CSP设置</span>
</a-button>
<!-- 预留位置,可以添加更多测试工具 -->
<div class="nav-grid-placeholder">
<span class="placeholder-text">更多工具</span>
</div>
</div>
</div>
</div>
</div>
<!-- 可滚动内容区域 -->
<div class="test-content">
<!-- 当前CSP配置 -->
<div class="test-section">
<h2>当前CSP配置</h2>
<div class="config-info">
<div class="config-item">
<label>CSP绕过状态:</label>
<span :class="cspConfig.enabled ? 'status-enabled' : 'status-disabled'">
{{ cspConfig.enabled ? '✅ 启用' : '❌ 禁用' }}
</span>
</div>
<div class="config-item">
<label>Referrer策略:</label>
<code class="config-value">{{ cspConfig.referrerPolicy }}</code>
</div>
<div class="config-item">
<label>策略名称:</label>
<span class="config-name">{{ getCurrentReferrerPolicyName() }}</span>
</div>
</div>
</div>
<!-- 测试操作 -->
<div class="test-section">
<h2>测试操作</h2>
<div class="test-buttons">
<a-button type="outline" status="success" shape="round" @click="testApplyCSP">
<icon-play-arrow />
测试应用CSP绕过
</a-button>
<a-button type="outline" status="success" shape="round" @click="testReferrerPolicy">
<icon-link />
测试Referrer策略
</a-button>
<a-button type="outline" status="success" shape="round" @click="refreshConfig">
<icon-refresh />
刷新配置
</a-button>
<a-button type="outline" status="warning" shape="round" @click="clearResults">
<icon-delete />
清除结果
</a-button>
</div>
</div>
<!-- 视频播放测试 -->
<div class="test-section">
<h2>视频播放测试</h2>
<div class="video-test">
<a-input
v-model="testVideoUrl"
placeholder="输入测试视频URL"
style="margin-bottom: 16px;"
/>
<div class="video-test-buttons">
<a-button type="primary" @click="testVideoPlay" :disabled="!testVideoUrl">
<icon-play-arrow />
测试视频播放
</a-button>
<a-button @click="testVideoUrl = defaultTestUrl" type="outline">
使用默认测试链接
</a-button>
</div>
<div v-if="videoTestResult" class="video-result">
{{ videoTestResult }}
</div>
</div>
</div>
<!-- 测试结果 -->
<div class="test-section">
<h2>测试结果</h2>
<div class="test-results">
<div v-if="testResults.length === 0" class="no-results">
暂无测试结果
</div>
<div v-for="(result, index) in testResults" :key="index"
:class="['test-result', result.type]">
<div class="result-header">
<span class="timestamp">{{ result.timestamp }}</span>
<span :class="['result-status', result.type]">
{{ getStatusIcon(result.type) }} {{ getStatusText(result.type) }}
</span>
</div>
<div class="result-message">{{ result.message }}</div>
</div>
</div>
</div>
<!-- 系统信息 -->
<div class="test-section">
<h2>系统信息</h2>
<div class="system-info">
<div class="info-item">
<label>浏览器:</label>
<span>{{ browserInfo.name }} {{ browserInfo.version }}</span>
</div>
<div class="info-item">
<label>用户代理:</label>
<code class="user-agent">{{ browserInfo.userAgent }}</code>
</div>
<div class="info-item">
<label>支持的视频格式:</label>
<div class="format-support">
<span v-for="(supported, format) in videoSupport" :key="format"
:class="['format-item', supported ? 'supported' : 'not-supported']">
{{ format.toUpperCase() }}: {{ supported ? '✅' : '❌' }}
</span>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Message } from '@arco-design/web-vue'
import {
IconSafe,
IconArrowLeft,
IconSettings,
IconPlayArrow,
IconLink,
IconRefresh,
IconDelete
} from '@arco-design/web-vue/es/icon'
import {
getCSPConfig,
applyCSPBypass,
setVideoReferrerPolicy,
REFERRER_POLICIES_LIST,
getCurrentReferrerPolicy,
setGlobalReferrerPolicy
} from '@/utils/csp'
const router = useRouter()
const cspConfig = ref({
enabled: false,
referrerPolicy: 'no-referrer'
})
const testResults = ref([])
const testVideoUrl = ref('')
const videoTestResult = ref('')
const defaultTestUrl = 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4'
// 浏览器信息
const browserInfo = computed(() => {
const ua = navigator.userAgent
let name = 'Unknown'
let version = 'Unknown'
if (ua.includes('Chrome')) {
name = 'Chrome'
const match = ua.match(/Chrome\/(\d+)/)
if (match) version = match[1]
} else if (ua.includes('Firefox')) {
name = 'Firefox'
const match = ua.match(/Firefox\/(\d+)/)
if (match) version = match[1]
} else if (ua.includes('Safari')) {
name = 'Safari'
const match = ua.match(/Version\/(\d+)/)
if (match) version = match[1]
} else if (ua.includes('Edge')) {
name = 'Edge'
const match = ua.match(/Edge\/(\d+)/)
if (match) version = match[1]
}
return {
name,
version,
userAgent: ua
}
})
// 视频格式支持检测
const videoSupport = computed(() => {
const video = document.createElement('video')
return {
mp4: video.canPlayType('video/mp4') !== '',
webm: video.canPlayType('video/webm') !== '',
ogg: video.canPlayType('video/ogg') !== '',
hls: video.canPlayType('application/vnd.apple.mpegurl') !== '' || window.Hls?.isSupported()
}
})
const addTestResult = (message, type = 'info') => {
testResults.value.unshift({
timestamp: new Date().toLocaleTimeString(),
message,
type
})
// 限制结果数量
if (testResults.value.length > 10) {
testResults.value = testResults.value.slice(0, 10)
}
}
const getCurrentReferrerPolicyName = () => {
const policy = REFERRER_POLICIES_LIST.find(p => p.value === cspConfig.value.referrerPolicy)
return policy ? policy.label : '未知策略'
}
// 导航方法
const goToActionTest = () => {
router.push('/action-test')
}
const goToVideoTest = () => {
router.push('/video-test')
}
const goToSettings = () => {
router.push('/settings')
}
// 状态显示方法
const getStatusIcon = (type) => {
switch (type) {
case 'success': return '✅'
case 'error': return '❌'
case 'warning': return '⚠️'
case 'info': return 'ℹ️'
default: return '📝'
}
}
const getStatusText = (type) => {
switch (type) {
case 'success': return '成功'
case 'error': return '错误'
case 'warning': return '警告'
case 'info': return '信息'
default: return '未知'
}
}
// 清除结果
const clearResults = () => {
testResults.value = []
videoTestResult.value = ''
addTestResult('测试结果已清除', 'info')
}
const refreshConfig = () => {
try {
cspConfig.value = getCSPConfig()
addTestResult('配置刷新成功', 'success')
} catch (error) {
addTestResult(`配置刷新失败: ${error.message}`, 'error')
}
}
const testApplyCSP = async () => {
try {
addTestResult('开始测试CSP绕过...', 'info')
const result = await applyCSPBypass()
if (result.success) {
addTestResult(`CSP绕过应用成功: ${result.message}`, 'success')
} else {
addTestResult(`CSP绕过应用失败: ${result.message}`, 'warning')
}
} catch (error) {
addTestResult(`CSP绕过测试出错: ${error.message}`, 'error')
}
}
const testReferrerPolicy = () => {
try {
addTestResult('开始测试Referrer策略...', 'info')
const currentPolicy = getCurrentReferrerPolicy()
addTestResult(`当前Referrer策略: ${currentPolicy}`, 'info')
// 测试设置策略
setGlobalReferrerPolicy(cspConfig.value.referrerPolicy)
addTestResult(`已设置全局Referrer策略为: ${cspConfig.value.referrerPolicy}`, 'success')
// 测试视频元素策略
const testElement = document.createElement('video')
setVideoReferrerPolicy(testElement, cspConfig.value.referrerPolicy)
addTestResult(`已为测试视频元素设置Referrer策略`, 'success')
} catch (error) {
addTestResult(`Referrer策略测试出错: ${error.message}`, 'error')
}
}
const testVideoPlay = () => {
try {
addTestResult('开始测试视频播放...', 'info')
if (!testVideoUrl.value) {
addTestResult('请输入测试视频URL', 'warning')
return
}
// 创建测试视频元素
const video = document.createElement('video')
video.src = testVideoUrl.value
video.crossOrigin = 'anonymous'
// 应用CSP策略
if (cspConfig.value.enabled) {
setVideoReferrerPolicy(video, cspConfig.value.referrerPolicy)
addTestResult('已为测试视频应用CSP策略', 'info')
}
// 测试加载
video.addEventListener('loadstart', () => {
addTestResult('视频开始加载', 'info')
})
video.addEventListener('canplay', () => {
addTestResult('视频可以播放', 'success')
videoTestResult.value = '✅ 视频加载成功'
})
video.addEventListener('error', (e) => {
addTestResult(`视频加载失败: ${e.message || '未知错误'}`, 'error')
videoTestResult.value = '❌ 视频加载失败'
})
// 开始加载
video.load()
} catch (error) {
addTestResult(`视频播放测试出错: ${error.message}`, 'error')
videoTestResult.value = `❌ 测试出错: ${error.message}`
}
}
onMounted(() => {
refreshConfig()
addTestResult('CSP测试页面已加载', 'success')
})
</script>
<style scoped>
.csp-test-container {
height: 100%;
display: flex;
flex-direction: column;
background: var(--color-bg-1);
overflow: hidden;
}
/* 固定头部区域 */
.test-header {
position: sticky;
top: 0;
z-index: 10;
background: var(--color-bg-1);
border-bottom: 1px solid var(--color-border-2);
padding: 24px 32px;
backdrop-filter: blur(8px);
}
.header-content {
width: 100%;
margin: 0;
display: flex;
justify-content: space-between;
align-items: flex-start;
}
.header-left {
flex: 1;
}
.page-title {
display: flex;
align-items: center;
gap: 12px;
font-size: 28px;
font-weight: 600;
color: var(--color-text-1);
margin: 0 0 8px 0;
}
.title-icon {
font-size: 32px;
color: var(--color-primary-6);
}
.page-subtitle {
color: var(--color-text-3);
font-size: 16px;
margin: 0;
line-height: 1.5;
}
.nav-button-group {
max-width: fit-content;
max-height: 200px;
overflow-y: auto;
padding: 4px;
border-radius: 8px;
background: var(--color-bg-2);
border: 1px solid var(--color-border-2);
}
.nav-button-grid {
display: grid;
grid-template-columns: auto auto;
gap: 6px;
padding: 6px;
justify-content: start;
}
.nav-grid-button {
min-height: 32px;
padding: 6px 12px;
display: inline-flex;
align-items: center;
justify-content: center;
gap: 4px;
font-size: 12px;
font-weight: 500;
border-radius: 6px;
transition: all 0.3s ease;
text-align: center;
line-height: 1;
white-space: nowrap;
width: fit-content;
}
.nav-grid-button:hover {
transform: translateY(-1px);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
background: var(--color-bg-3);
border-color: var(--color-primary-6);
}
.nav-grid-button span {
font-size: 12px;
line-height: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.nav-grid-placeholder {
min-height: 32px;
padding: 6px 12px;
display: inline-flex;
align-items: center;
justify-content: center;
gap: 4px;
font-size: 12px;
font-weight: 500;
background: var(--color-bg-3);
border: 1px dashed var(--color-border-3);
border-radius: 6px;
transition: all 0.3s ease;
text-align: center;
line-height: 1;
white-space: nowrap;
width: fit-content;
}
.nav-grid-placeholder:hover {
border-color: var(--color-primary-6);
background: var(--color-primary-1);
}
.placeholder-text {
font-size: 12px;
line-height: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
color: var(--color-text-4);
text-align: center;
}
/* 可滚动内容区域 */
.test-content {
flex: 1;
overflow-y: auto;
padding: 32px;
width: 100%;
margin: 0;
}
.test-section {
margin-bottom: 32px;
padding: 24px;
background: var(--color-bg-2);
border-radius: 12px;
border: 1px solid var(--color-border-2);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
transition: all 0.3s ease;
}
.test-section:hover {
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
border-color: var(--color-border-3);
}
.test-section h2 {
color: var(--color-text-1);
margin-bottom: 20px;
font-size: 20px;
font-weight: 600;
display: flex;
align-items: center;
gap: 8px;
}
.test-section h2::before {
content: '';
width: 4px;
height: 20px;
background: var(--color-primary-6);
border-radius: 2px;
}
/* 配置信息样式 */
.config-info {
display: flex;
flex-direction: column;
gap: 16px;
}
.config-item {
display: flex;
align-items: center;
gap: 12px;
padding: 12px 16px;
background: var(--color-bg-1);
border-radius: 8px;
border: 1px solid var(--color-border-2);
}
.config-item label {
font-weight: 600;
color: var(--color-text-2);
min-width: 120px;
}
.status-enabled {
color: var(--color-success-6);
font-weight: 600;
}
.status-disabled {
color: var(--color-danger-6);
font-weight: 600;
}
.config-value {
background: var(--color-fill-2);
padding: 4px 8px;
border-radius: 4px;
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
font-size: 13px;
}
.config-name {
color: var(--color-text-1);
font-weight: 500;
}
/* 按钮样式 */
.test-buttons {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 16px;
}
/* 视频测试样式 */
.video-test {
display: flex;
flex-direction: column;
gap: 16px;
}
.video-test-buttons {
display: flex;
gap: 12px;
flex-wrap: wrap;
}
.video-result {
padding: 16px;
border-radius: 8px;
background: var(--color-bg-1);
border: 1px solid var(--color-border-2);
font-weight: 600;
color: var(--color-text-1);
}
/* 测试结果样式 */
.test-results {
max-height: 400px;
overflow-y: auto;
border: 1px solid var(--color-border-2);
border-radius: 8px;
background: var(--color-bg-1);
}
.no-results {
padding: 32px;
text-align: center;
color: var(--color-text-3);
font-style: italic;
}
.test-result {
padding: 16px;
border-bottom: 1px solid var(--color-border-2);
transition: background 0.2s ease;
}
.test-result:last-child {
border-bottom: none;
}
.test-result:hover {
background: var(--color-fill-1);
}
.result-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 8px;
}
.timestamp {
font-size: 12px;
color: var(--color-text-3);
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
}
.result-status {
font-size: 12px;
font-weight: 600;
padding: 2px 8px;
border-radius: 12px;
display: flex;
align-items: center;
gap: 4px;
}
.result-status.success {
background: var(--color-success-light-1);
color: var(--color-success-6);
}
.result-status.error {
background: var(--color-danger-light-1);
color: var(--color-danger-6);
}
.result-status.warning {
background: var(--color-warning-light-1);
color: var(--color-warning-6);
}
.result-status.info {
background: var(--color-info-light-1);
color: var(--color-info-6);
}
.result-message {
color: var(--color-text-1);
line-height: 1.5;
word-break: break-word;
}
/* 系统信息样式 */
.system-info {
display: flex;
flex-direction: column;
gap: 16px;
}
.info-item {
display: flex;
flex-direction: column;
gap: 8px;
padding: 16px;
background: var(--color-bg-1);
border-radius: 8px;
border: 1px solid var(--color-border-2);
}
.info-item label {
font-weight: 600;
color: var(--color-text-2);
font-size: 14px;
}
.user-agent {
background: var(--color-fill-2);
padding: 8px 12px;
border-radius: 6px;
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
font-size: 12px;
word-break: break-all;
line-height: 1.4;
}
.format-support {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.format-item {
padding: 4px 8px;
border-radius: 4px;
font-size: 12px;
font-weight: 600;
}
.format-item.supported {
background: var(--color-success-light-1);
color: var(--color-success-6);
}
.format-item.not-supported {
background: var(--color-danger-light-1);
color: var(--color-danger-6);
}
/* 响应式设计 */
@media (max-width: 768px) {
.test-header {
padding: 16px 20px;
}
.header-content {
flex-direction: column;
gap: 16px;
}
.nav-buttons {
align-items: stretch;
}
.page-title {
font-size: 24px;
}
.test-content {
padding: 20px;
}
.test-section {
padding: 20px;
margin-bottom: 24px;
}
.test-buttons {
grid-template-columns: 1fr;
}
.config-item {
flex-direction: column;
align-items: flex-start;
}
.config-item label {
min-width: auto;
}
.video-test-buttons {
flex-direction: column;
}
}
</style>