forked from hafrey1/LunaTV-config
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy path_worker.js
More file actions
1001 lines (919 loc) · 34.2 KB
/
Copy path_worker.js
File metadata and controls
1001 lines (919 loc) · 34.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
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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 统一入口:兼容 Cloudflare Workers 和 Pages Functions
export default {
async fetch(request, env, ctx) {
// Pages Functions 中 KV 需要从 env 中获取
if (env && env.KV && typeof globalThis.KV === 'undefined') {
globalThis.KV = env.KV
}
return handleRequest(request)
}
}
// 常量配置(避免重复创建)
const CORS_HEADERS = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
'Access-Control-Max-Age': '86400',
}
const EXCLUDE_HEADERS = new Set([
'content-encoding', 'content-length', 'transfer-encoding',
'connection', 'keep-alive', 'set-cookie', 'set-cookie2'
])
const JSON_SOURCES = {
'jin18': 'https://raw.githubusercontent.com/hafrey1/LunaTV-config/refs/heads/main/jin18.json',
'jingjian': 'https://raw.githubusercontent.com/hafrey1/LunaTV-config/refs/heads/main/jingjian.json',
'full': 'https://raw.githubusercontent.com/hafrey1/LunaTV-config/refs/heads/main/LunaTV-config.json'
}
const FORMAT_CONFIG = {
'0': { proxy: false, base58: false },
'raw': { proxy: false, base58: false },
'1': { proxy: true, base58: false },
'proxy': { proxy: true, base58: false },
'2': { proxy: false, base58: true },
'base58': { proxy: false, base58: true },
'3': { proxy: true, base58: true },
'proxy-base58': { proxy: true, base58: true }
}
// Base58 编码函数
const BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
function base58Encode(obj) {
const str = JSON.stringify(obj)
const bytes = new TextEncoder().encode(str)
let intVal = 0n
for (let b of bytes) {
intVal = (intVal << 8n) + BigInt(b)
}
let result = ''
while (intVal > 0n) {
const mod = intVal % 58n
result = BASE58_ALPHABET[Number(mod)] + result
intVal = intVal / 58n
}
for (let b of bytes) {
if (b === 0) result = BASE58_ALPHABET[0] + result
else break
}
return result
}
// 🔑 从 URL 中提取唯一标识符(用于生成唯一路径)
function extractSourceId(apiUrl) {
try {
const url = new URL(apiUrl)
const hostname = url.hostname
// 提取主域名作为标识符(去掉子域名和 TLD)
// 例如:caiji.maotaizy.cc → maotai
// iqiyizyapi.com → iqiyi
// api.maoyanapi.top → maoyan
const parts = hostname.split('.')
// 如果是 caiji.xxx.com 或 api.xxx.com 格式,取倒数第二部分
if (parts.length >= 3 && (parts[0] === 'caiji' || parts[0] === 'api' || parts[0] === 'cj' || parts[0] === 'www')) {
return parts[parts.length - 2].toLowerCase().replace(/[^a-z0-9]/g, '')
}
// 否则取第一部分(去掉 zyapi/zy 等后缀)
let name = parts[0].toLowerCase()
name = name.replace(/zyapi$/, '').replace(/zy$/, '').replace(/api$/, '')
return name.replace(/[^a-z0-9]/g, '') || 'source'
} catch {
// URL 解析失败,使用随机标识
return 'source' + Math.random().toString(36).substr(2, 6)
}
}
// JSON api 字段前缀替换(改进版:为每个源生成唯一路径)
function addOrReplacePrefix(obj, newPrefix) {
if (typeof obj !== 'object' || obj === null) return obj
if (Array.isArray(obj)) return obj.map(item => addOrReplacePrefix(item, newPrefix))
const newObj = {}
for (const key in obj) {
if (key === 'api' && typeof obj[key] === 'string') {
let apiUrl = obj[key]
// 去掉旧的代理前缀(如果有)
const urlIndex = apiUrl.indexOf('?url=')
if (urlIndex !== -1) apiUrl = apiUrl.slice(urlIndex + 5)
// 🔑 关键修改:为每个源生成唯一的路径
if (!apiUrl.startsWith(newPrefix)) {
const sourceId = extractSourceId(apiUrl)
// 从 newPrefix 中提取 origin 和基础路径
// 例如:https://xx.fn0.qzz.io/?url= → https://xx.fn0.qzz.io/p/iqiyi?url=
const baseUrl = newPrefix.replace(/\/?\?url=$/, '') // 去掉结尾的 /?url= 或 ?url=
apiUrl = `${baseUrl}/p/${sourceId}?url=${apiUrl}`
}
newObj[key] = apiUrl
} else {
newObj[key] = addOrReplacePrefix(obj[key], newPrefix)
}
}
return newObj
}
// ---------- 安全版:KV 缓存 ----------
async function getCachedJSON(url) {
const kvAvailable = typeof KV !== 'undefined' && KV && typeof KV.get === 'function'
if (kvAvailable) {
const cacheKey = 'CACHE_' + url
const cached = await KV.get(cacheKey)
if (cached) {
try {
return JSON.parse(cached)
} catch (e) {
await KV.delete(cacheKey)
}
}
const res = await fetch(url)
if (!res.ok) throw new Error(`Fetch failed: ${res.status}`)
const data = await res.json()
await KV.put(cacheKey, JSON.stringify(data), { expirationTtl: 600 }) // 缓存十分钟
return data
} else {
const res = await fetch(url)
if (!res.ok) throw new Error(`Fetch failed: ${res.status}`)
return await res.json()
}
}
// v2.0.20: m3u8 KV 缓存 (借鉴 cmliu/edgetunnel 的 KV 缓存思路)
// m3u8 内容大多静态, 同一个剧集每次播放都要重新 fetch + 解析 + 重写太浪费.
// 5 分钟 TTL 兼顾"主播切线路时及时刷新"和"重复请求秒回".
// cacheKey 含 origin, 避免 worker 换域名后返错链接.
async function getCachedM3u8(cacheKey) {
if (typeof KV === 'undefined' || !KV || typeof KV.get !== 'function') return null
try {
return await KV.get(cacheKey)
} catch {
return null
}
}
async function setCachedM3u8(cacheKey, text) {
if (typeof KV === 'undefined' || !KV || typeof KV.put !== 'function') return
try {
await KV.put(cacheKey, text, { expirationTtl: 300 }) // 5 分钟
} catch (e) {
// 写缓存失败不影响主流程
}
}
// ---------- 安全版:错误日志 ----------
async function logError(type, info) {
// 保留错误输出,便于调试
console.error('[ERROR]', type, info)
// 禁止写入 KV
return
}
// ---------- 主逻辑 ----------
async function handleRequest(request) {
// 快速处理 OPTIONS 请求
if (request.method === 'OPTIONS') {
return new Response(null, { status: 204, headers: CORS_HEADERS })
}
const reqUrl = new URL(request.url)
const pathname = reqUrl.pathname
const targetUrlParam = reqUrl.searchParams.get('url')
const formatParam = reqUrl.searchParams.get('format')
const prefixParam = reqUrl.searchParams.get('prefix')
const sourceParam = reqUrl.searchParams.get('source')
const currentOrigin = reqUrl.origin
const defaultPrefix = currentOrigin + '/?url='
// 🩺 健康检查(最常见的性能检查,提前处理)
if (pathname === '/health') {
return new Response('OK', { status: 200, headers: CORS_HEADERS })
}
// 🚀 测速端点:流式返回指定大小随机字节
// 默认 1MB, ?size=N (MB), 范围 1-50. 客户端测得下载速度 = 用户到 worker 的实际速度
if (pathname === '/speed') {
return handleSpeedTest(reqUrl)
}
// 🔑 新增:处理源专属路径 /p/{sourceId}?url=...
// 这样可以让 TVBox 认为每个源是不同的域名/路径
if (pathname.startsWith('/p/') && targetUrlParam) {
return handleProxyRequest(request, targetUrlParam, currentOrigin)
}
// 通用代理请求处理(兼容旧的 /?url=... 格式)
if (targetUrlParam) {
// 单独的 m3u8 端点:拉 m3u8 并重写 .ts 链接
if (pathname === '/m3u8') {
return handleM3u8Request(request, targetUrlParam, currentOrigin)
}
return handleProxyRequest(request, targetUrlParam, currentOrigin)
}
// JSON 格式输出处理
if (formatParam !== null) {
return handleFormatRequest(formatParam, sourceParam, prefixParam, defaultPrefix)
}
// 返回首页文档
return handleHomePage(currentOrigin, defaultPrefix)
}
// ---------- 代理请求处理子模块 ----------
async function handleProxyRequest(request, targetUrlParam, currentOrigin) {
// 🚨 防止递归调用自身
if (targetUrlParam.startsWith(currentOrigin)) {
return errorResponse('Loop detected: self-fetch blocked', { url: targetUrlParam }, 400)
}
// 🚨 防止无效 URL
if (!/^https?:\/\//i.test(targetUrlParam)) {
return errorResponse('Invalid target URL', { url: targetUrlParam }, 400)
}
let fullTargetUrl = targetUrlParam
// 🔑 修复:只提取 url= 参数的值,不要包含后续的 & 参数
const urlMatch = request.url.match(/[?&]url=([^&]+)/)
if (urlMatch) fullTargetUrl = decodeURIComponent(urlMatch[1])
// 🔑 关键修复:提取并传递额外的 query 参数(如 ac=list, ac=detail 等)
const reqUrl = new URL(request.url)
const extraParams = new URLSearchParams()
// 遍历所有 query 参数,把除了 url 之外的参数都加到目标 URL
for (const [key, value] of reqUrl.searchParams) {
if (key !== 'url') {
extraParams.append(key, value)
}
}
let targetURL
try {
targetURL = new URL(fullTargetUrl)
// 🔑 将额外参数追加到目标 URL
for (const [key, value] of extraParams) {
targetURL.searchParams.append(key, value)
}
} catch {
await logError('proxy', { message: 'Invalid URL', url: fullTargetUrl })
return errorResponse('Invalid URL', { url: fullTargetUrl }, 400)
}
// v2.0.20h: 优先用 worker 自带 Cache API, 绕开 Bunny.net 40 天老缓存
if (request.method === 'GET') {
try {
const cache = caches.default
const cacheKey = new Request(targetURL.toString(), { method: 'GET', headers: request.headers })
const cached = await cache.match(cacheKey)
if (cached) {
const h = new Headers(cached.headers)
h.set('Cache-Control', 'no-store')
h.set('X-Cache', 'WORKER-HIT')
return new Response(await cached.arrayBuffer(), { status: cached.status, headers: h })
}
} catch {}
}
// 构造透传请求头,如果客户端没带必要的头,按目标域名补上 fallback
// (典型:lain.bgm.tv 拒无 UA 的请求)
const upstreamHeaders = new Headers(request.headers)
applyDefaultHeadersForUpstream(upstreamHeaders, targetURL)
try {
// v2.0.20f: 强制 upstream 重新校验, 绕开 Bunny.net 等 CDN 的 40 天老缓存
// (cdn-cachedat 显示 TMDB 走的 Bunny.net 缓存是 06/24 写的, 头里 alt-svc 一直老的)
upstreamHeaders.set('Cache-Control', 'max-age=0')
upstreamHeaders.set('Pragma', 'no-cache')
const proxyRequest = new Request(targetURL.toString(), {
method: request.method,
headers: upstreamHeaders,
body: request.method !== 'GET' && request.method !== 'HEAD'
? await request.arrayBuffer()
: undefined,
})
const controller = new AbortController()
// v2.0.28: 视频 .ts 段可能几 MB, 9s 不够 → 30s
const timeoutId = setTimeout(() => controller.abort(), 30000)
// v2.0.28: 用 cf 选项让 CF cache .ts 段
// v2.0.20g: 删 .jpg/.jpeg/.png — 它们是图片, 不是视频段
// 之前错误归到 isTsSegment, 导致 image 走 public,max-age=3600 缓存 Bunny.net 40 天老数据
const isTsSegment = targetURL.pathname.endsWith('.ts') ||
targetURL.pathname.endsWith('.m4s')
const fetchOptions = {
signal: controller.signal,
cf: isTsSegment ? {
cacheTtl: 3600, // CF edge cache 1 小时
cacheEverything: true, // 缓存所有响应 (包括非 200)
} : undefined,
}
const response = await fetch(proxyRequest, fetchOptions)
clearTimeout(timeoutId)
// v2.0.20h: 存到 worker Cache, 下次同 URL 直接 hit
if (request.method === 'GET' && response.status === 200) {
try {
const cache = caches.default
const cacheKey = new Request(targetURL.toString(), { method: 'GET', headers: upstreamHeaders })
const respToCache = new Response(response.clone().body, { status: response.status, headers: response.headers })
await cache.put(cacheKey, respToCache)
} catch (e) {}
}
const responseHeaders = new Headers(CORS_HEADERS)
for (const [key, value] of response.headers) {
if (!EXCLUDE_HEADERS.has(key.toLowerCase())) {
responseHeaders.set(key, value)
}
}
// v2.0.20f: 默认所有响应 no-store, 防止 CF 边缘缓存 Bunny.net 的 40 天老响应
if (response.status === 200) {
responseHeaders.set('Cache-Control', 'no-store, no-cache, must-revalidate')
responseHeaders.set('Pragma', 'no-cache')
}
// v2.0.28: .ts 段加 Cache-Control, 让 CF edge cache
if (isTsSegment && response.status === 200) {
responseHeaders.set('Cache-Control', 'public, max-age=3600')
responseHeaders.set('CDN-Cache-Control', 'public, max-age=3600')
}
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: responseHeaders
})
} catch (err) {
await logError('proxy', { message: err.message || '代理请求失败', url: fullTargetUrl })
return errorResponse('Proxy Error', {
message: err.message || '代理请求失败',
target: fullTargetUrl,
timestamp: new Date().toISOString()
}, 502)
}
}
// 按目标域名补默认请求头 (客户端漏带 UA/Referer 时,补上,避免上游 400/403)
function applyDefaultHeadersForUpstream(headers, targetURL) {
const host = targetURL.hostname.toLowerCase()
// Bangumi 图片/数据系列:lain.bgm.tv / api.bgm.tv / bgm.tv
if (host === 'lain.bgm.tv' || host === 'api.bgm.tv' || host === 'bgm.tv' || host.endsWith('.bgm.tv')) {
// ⚠️ api.bgm.tv v0 API 强制要求 UA 是 "App/Version (URL)" 格式,
// Chrome 标准 UA 会返 400
if (!headers.has('User-Agent')) {
headers.set('User-Agent', 'LunaTV-Mobile/1.0 (https://github.com/djsevenx1/LunaTV-Mobile)')
}
if (!headers.has('Referer')) {
headers.set('Referer', 'https://bgm.tv/')
}
if (!headers.has('Accept')) {
headers.set('Accept', 'image/avif,image/webp,image/apng,image/*,*/*;q=0.8')
}
}
// v2.0.28: 通用 fallback — 视频源/API 补 UA + Referer
// 很多多线视频 CDN 需要 Referer 才返回 m3u8/.ts, 否则 403
if (!headers.has('User-Agent')) {
headers.set('User-Agent', 'Mozilla/5.0 (Linux; Android 13; Mobile) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36')
}
if (!headers.has('Referer')) {
headers.set('Referer', targetURL.origin + '/')
}
if (!headers.has('Accept')) {
headers.set('Accept', '*/*')
}
}
// ---------- M3U8 代理:拉 m3u8 并把 .ts 子链接也走 Worker ----------
async function handleM3u8Request(request, targetUrlParam, currentOrigin) {
if (targetUrlParam.startsWith(currentOrigin)) {
return errorResponse('Loop detected: self-fetch blocked', { url: targetUrlParam }, 400)
}
if (!/^https?:\/\//i.test(targetUrlParam)) {
return errorResponse('Invalid target URL', { url: targetUrlParam }, 400)
}
let fullTargetUrl = targetUrlParam
const urlMatch = request.url.match(/[?&]url=([^&]+)/)
if (urlMatch) fullTargetUrl = decodeURIComponent(urlMatch[1])
// 携带额外 query
const reqUrl = new URL(request.url)
const extraParams = new URLSearchParams()
for (const [key, value] of reqUrl.searchParams) {
if (key !== 'url') extraParams.append(key, value)
}
let targetURL
try {
targetURL = new URL(fullTargetUrl)
for (const [key, value] of extraParams) {
targetURL.searchParams.append(key, value)
}
} catch {
return errorResponse('Invalid URL', { url: fullTargetUrl }, 400)
}
// v2.0.20: KV 缓存查询 (含 origin 避免跨域名错链, ?nocache=1 跳过)
const cacheKey = `M3U8_${currentOrigin}_${targetURL.toString()}`
const nocache = reqUrl.searchParams.get('nocache') === '1'
if (!nocache) {
const cached = await getCachedM3u8(cacheKey)
if (cached !== null) {
return new Response(cached, {
status: 200,
headers: {
'Content-Type': 'application/vnd.apple.mpegurl',
'Cache-Control': 'public, max-age=60',
'X-Cache': 'HIT',
'X-Cache-TTL': '300',
// 借鉴: HTTP/3 Alt-Svc, 让客户端升级到 QUIC (CF 默认开, 几乎零成本)
'Alt-Svc': 'h3=":443"; ma=86400',
...CORS_HEADERS,
},
})
}
}
const baseOrigin = currentOrigin
// v2.0.30: m3u8 端点也要识别 sub-m3u8 (递归), 用 /m3u8?url= 而不是 /?url=
// 否则 /?url= 拿到 sub-m3u8 后只 text() 原样返回, 里面的 .ts 链接没被重写
// libmpv 按 HLS 规范用 m3u8 自身 URL 解析相对 .ts → 错 → duration 出不来
const wrapBase = (rawUrl) => {
const u = (rawUrl || '').toLowerCase()
if (u.endsWith('.m3u8') || u.endsWith('.m3u')) return `${baseOrigin}/m3u8?url=`
return `${baseOrigin}/?url=`
}
// 判断一个 segment 链接是否需要包代理
// 1) 绝对 http(s) 链接:包一层 ?url= 走本 worker
// 2) 相对路径 / 协议相对 //开头:用 m3u8 自身 base 拼成绝对再包
// 3) v2.0.30: m3u8 后缀用 /m3u8?url= (递归重写), 其他用 /?url= (直接转发)
const wrapSegment = (rawLine) => {
const line = rawLine.trim()
if (!line) return line
// 注释行 / EXT 行不动
if (line.startsWith('#')) return line
// 已经是 http 绝对
if (/^https?:\/\//i.test(line)) {
return wrapBase(line) + encodeURIComponent(line)
}
// 协议相对 //host/path
if (line.startsWith('//')) {
const abs = targetURL.protocol + line
return wrapBase(abs) + encodeURIComponent(abs)
}
// 相对路径
try {
const abs = new URL(line, targetURL).toString()
return wrapBase(abs) + encodeURIComponent(abs)
} catch {
return line
}
}
try {
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), 15000)
// v2.0.28: 带 Referer + Origin, 很多视频源需要才能返回 m3u8
const upstream = await fetch(targetURL.toString(), {
headers: {
'User-Agent': 'Mozilla/5.0 (Linux; Android 13; Mobile) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36',
'Referer': targetURL.origin + '/',
'Origin': targetURL.origin,
'Accept': '*/*',
},
signal: controller.signal,
})
clearTimeout(timeoutId)
const text = await upstream.text()
// v2.0.63: 检测上游返回的是不是有效 m3u8 (以 #EXTM3U 开头).
// 有些源给的是 /share/xxx 分享页 (HTML), worker 之前把 HTML 当 m3u8
// 解析, 每行包一层 ?url= 返回伪 m3u8 → libmpv "Failed to recognize
// file format". 现在不是 m3u8 就原样转发 (带原 Content-Type), 让
// 客户端自己处理.
const trimmed = text.trimStart()
if (!trimmed.startsWith('#EXTM3U')) {
const headers = new Headers(upstream.headers)
headers.set('Access-Control-Allow-Origin', '*')
headers.delete('content-encoding')
headers.delete('content-length')
return new Response(text, {
status: upstream.status,
statusText: upstream.statusText,
headers,
})
}
// 简单判断 master playlist (含 #EXT-X-STREAM-INF) → 递归把所有 variant m3u8 也包一层
const isMaster = /#EXT-X-STREAM-INF/i.test(text)
const lines = text.split(/\r?\n/)
const out = []
for (let i = 0; i < lines.length; i++) {
const line = lines[i]
// 对 URI="..." 属性里的链接也包一层
if (/^#EXT-X-KEY/i.test(line) || /^#EXT-X-MAP/i.test(line) || /^#EXT-X-MEDIA/i.test(line) || /^#EXT-X-I-FRAME-STREAM-INF/i.test(line)) {
out.push(line.replace(/URI="([^"]+)"/g, (m, u) => {
let abs
try { abs = new URL(u, targetURL).toString() } catch { abs = u }
return `URI="${wrapBase(abs) + encodeURIComponent(abs)}"`
}))
continue
}
// variant stream 行
if (isMaster && /^#EXT-X-STREAM-INF/i.test(line)) {
out.push(line)
// 紧跟的下一行就是 variant 链接
if (i + 1 < lines.length) {
out.push(wrapSegment(lines[i + 1]))
i++
}
continue
}
out.push(wrapSegment(line))
}
const outText = out.join('\n')
// v2.0.20: 写缓存 (后台 fire-and-forget, 不阻塞响应)
if (!nocache) {
ctxWaitUntil(setCachedM3u8(cacheKey, outText))
}
return new Response(outText, {
status: 200,
headers: {
'Content-Type': 'application/vnd.apple.mpegurl',
'Cache-Control': 'no-store',
'X-Cache': nocache ? 'BYPASS' : 'MISS',
'Alt-Svc': 'h3=":443"; ma=86400',
...CORS_HEADERS,
},
})
} catch (err) {
await logError('m3u8', { message: err.message, url: fullTargetUrl })
return errorResponse('M3U8 Proxy Error', {
message: err.message,
target: fullTargetUrl,
}, 502)
}
}
// v2.0.20: ctx.waitUntil 的安全 fallback
// Pages Functions 没 ctx, Netlify 也没, 直接执行函数即可
function ctxWaitUntil(promise) {
try {
if (typeof ctx !== 'undefined' && ctx && typeof ctx.waitUntil === 'function') {
ctx.waitUntil(promise)
return
}
} catch {}
// 没 ctx 就当普通 promise, 不 await (fire-and-forget)
promise.catch(() => {})
}
// ---------- JSON 格式输出处理子模块 ----------
async function handleFormatRequest(formatParam, sourceParam, prefixParam, defaultPrefix) {
try {
const config = FORMAT_CONFIG[formatParam]
if (!config) {
return errorResponse('Invalid format parameter', { format: formatParam }, 400)
}
const selectedSource = JSON_SOURCES[sourceParam] || JSON_SOURCES['full']
const data = await getCachedJSON(selectedSource)
const newData = config.proxy
? addOrReplacePrefix(data, prefixParam || defaultPrefix)
: data
if (config.base58) {
const encoded = base58Encode(newData)
return new Response(encoded, {
headers: { 'Content-Type': 'text/plain;charset=UTF-8', ...CORS_HEADERS },
})
} else {
return new Response(JSON.stringify(newData), {
headers: { 'Content-Type': 'application/json;charset=UTF-8', ...CORS_HEADERS },
})
}
} catch (err) {
await logError('json', { message: err.message })
return errorResponse(err.message, {}, 500)
}
}
// ---------- 首页文档处理 (LunaTV 风格: 深色 + 绿主色 #22C55E) ----------
async function handleHomePage(currentOrigin, defaultPrefix) {
const html = `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CORSAPI - API 中转代理服务</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
:root {
--luna-green: #22C55E;
--luna-green-deep: #10B981;
--bg: #0F1117;
--card: #1F2937;
--border: #374151;
--text: #FFFFFF;
--sub: #9ca3af;
--muted: #6b7280;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', Arial, sans-serif;
background: var(--bg);
color: var(--text);
min-height: 100vh;
line-height: 1.6;
padding: 24px 16px;
}
.container {
max-width: 880px;
margin: 0 auto;
}
/* 顶部条 (跟 LunaTV Web 顶栏同款) */
.topbar {
display: flex;
align-items: center;
gap: 8px;
padding: 4px 4px 24px 4px;
}
.logo {
width: 28px;
height: 28px;
border-radius: 6px;
background: linear-gradient(135deg, var(--luna-green), var(--luna-green-deep));
display: flex;
align-items: center;
justify-content: center;
font-weight: 800;
color: #052e16;
font-size: 14px;
}
.brand {
font-size: 16px;
font-weight: 700;
color: var(--text);
}
.pill {
margin-left: auto;
display: inline-flex;
align-items: center;
gap: 6px;
padding: 4px 10px;
background: rgba(34, 197, 94, 0.12);
color: var(--luna-green);
border-radius: 999px;
font-size: 12px;
font-weight: 600;
}
.pill .dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--luna-green);
animation: pulse 2s infinite;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.4; }
}
/* 头部 */
.hero {
padding: 32px 28px;
background: var(--card);
border: 1px solid var(--border);
border-radius: 12px;
margin-bottom: 20px;
}
.hero h1 {
font-size: 28px;
font-weight: 800;
margin-bottom: 8px;
color: var(--text);
}
.hero h1 .accent {
color: var(--luna-green);
}
.hero p {
color: var(--sub);
font-size: 14px;
margin-bottom: 16px;
}
.url-card {
background: #0b0e14;
border: 1px solid var(--border);
border-radius: 8px;
padding: 14px 16px;
font-family: 'SF Mono', Consolas, Monaco, monospace;
font-size: 13px;
color: var(--luna-green);
word-break: break-all;
margin-bottom: 8px;
}
.url-card .label {
color: var(--muted);
font-size: 11px;
text-transform: uppercase;
letter-spacing: 0.05em;
margin-bottom: 6px;
font-family: -apple-system, sans-serif;
}
/* 章节 */
.section {
background: var(--card);
border: 1px solid var(--border);
border-radius: 12px;
padding: 20px 24px;
margin-bottom: 16px;
}
.section-title {
display: flex;
align-items: center;
gap: 10px;
font-size: 16px;
font-weight: 700;
color: var(--text);
margin-bottom: 14px;
}
.section-title::before {
content: "";
width: 3px;
height: 14px;
background: linear-gradient(180deg, var(--luna-green), var(--luna-green-deep));
border-radius: 2px;
}
.section p {
color: var(--sub);
font-size: 14px;
margin-bottom: 10px;
}
.section p:last-child {
margin-bottom: 0;
}
pre {
background: #0b0e14;
color: #d1d5db;
padding: 14px 16px;
border-radius: 8px;
border: 1px solid var(--border);
overflow-x: auto;
font-family: 'SF Mono', Consolas, Monaco, monospace;
font-size: 12.5px;
line-height: 1.6;
margin: 10px 0;
white-space: pre;
}
pre .g { color: var(--luna-green); }
pre .d { color: var(--muted); }
code {
background: rgba(34, 197, 94, 0.12);
color: var(--luna-green);
padding: 2px 6px;
border-radius: 4px;
font-family: 'SF Mono', Consolas, Monaco, monospace;
font-size: 12.5px;
}
ul {
list-style: none;
padding: 0;
margin: 8px 0;
}
li {
color: var(--sub);
font-size: 14px;
padding: 6px 0;
display: flex;
align-items: center;
gap: 8px;
}
li::before {
content: "";
width: 5px;
height: 5px;
background: var(--luna-green);
border-radius: 50%;
flex-shrink: 0;
}
/* 网格 */
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 10px;
margin-top: 8px;
}
.feat {
background: rgba(255, 255, 255, 0.03);
border: 1px solid var(--border);
border-radius: 8px;
padding: 12px 14px;
font-size: 13.5px;
color: var(--sub);
}
.feat b {
color: var(--luna-green);
font-weight: 600;
}
/* 底部 */
.footer {
text-align: center;
padding: 20px 0 4px 0;
color: var(--muted);
font-size: 12.5px;
}
.footer a {
color: var(--luna-green);
text-decoration: none;
}
.footer a:hover {
text-decoration: underline;
}
@media (max-width: 600px) {
body { padding: 16px 12px; }
.hero { padding: 22px 18px; }
.hero h1 { font-size: 22px; }
.section { padding: 16px 18px; }
}
</style>
</head>
<body>
<div class="container">
<!-- 顶部条 -->
<div class="topbar">
<div class="logo">L</div>
<span class="brand">CORSAPI</span>
<span class="pill"><span class="dot"></span>运行中</span>
</div>
<!-- 头部 -->
<div class="hero">
<h1>API 中转代理服务 <span class="accent">/ LunaTV 配套</span></h1>
<p>通用 API 中转代理,用于加速和转发跨域 API 请求。部署在 ${detectPlatform()} 上。</p>
<div class="url-card">
<div class="label">基本用法 · 在 API 请求前添加代理 + <code>?url=</code></div>
${defaultPrefix}https://api.example.com/endpoint
</div>
<div class="url-card">
<div class="label">M3U8 代理 · 拉 m3u8 并把 .ts 段链接也走 worker</div>
${currentOrigin}/m3u8?url=https://example.com/index.m3u8
</div>
</div>
<!-- 端点 -->
<div class="section">
<div class="section-title">端点</div>
<ul>
<li><code>GET /?url=<encoded></code> 通用代理,透传 method/headers/body</li>
<li><code>GET /m3u8?url=<encoded></code> m3u8 代理,自动重写 .ts 链接为 <code>worker/?url=...</code></li>
<li><code>GET /p/{source}?url=<encoded></code> 源专属路径,避免缓存冲突</li>
<li><code>GET /health</code> 健康检查</li>
<li><code>GET /?format=1&source=full</code> 输出 LunaTV JSON 配置 (proxy 模式)</li>
</ul>
</div>
<!-- 用法示例 -->
<div class="section">
<div class="section-title">用法示例</div>
<p>代理一个 API 请求:</p>
<pre>原始: <span class="d">https://api.example.com/data?id=123</span>
代理: <span class="g">${currentOrigin}/?url=https://api.example.com/data?id=123</span></pre>
<p>额外 query 参数自动转发到目标 URL:</p>
<pre>请求: <span class="g">${currentOrigin}/?url=https://api.example.com/list&page=1&limit=10</span>
转发: <span class="d">https://api.example.com/list?page=1&limit=10</span></pre>
</div>
<!-- 特性 -->
<div class="section">
<div class="section-title">功能特性</div>
<div class="grid">
<div class="feat"><b>HTTP 全方法</b><br>GET / POST / PUT / DELETE 等</div>
<div class="feat"><b>自动参数转发</b><br>query 透传给目标</div>
<div class="feat"><b>CORS 完整</b><br>Access-Control-* 全套</div>
<div class="feat"><b>超时保护</b><br>9 秒,避免悬挂</div>
<div class="feat"><b>自反循环检测</b><br>防止 worker 套 worker</div>
<div class="feat"><b>bgm.tv fallback</b><br>UA / Referer 自动补</div>
<div class="feat"><b>M3U8 KV 缓存</b><br>5 分钟复用, 减少 worker CPU</div>
<div class="feat"><b>HTTP/3 (QUIC)</b><br>Alt-Svc 头提示升级, CF 默认开</div>
</div>
</div>
<!-- 配套 -->
<div class="section">
<div class="section-title">配套 App</div>
<p>推荐配合 <code>LunaTV-Mobile</code> 使用,在 App 菜单填入 worker 域名即可自动接管 Bangumi 数据/图片代理 + 源加速 + m3u8 端点重写。</p>
<pre>Bangumi 数据源 → <span class="g">CF Worker 加速</span>
Bangumi 图片源 → <span class="g">CF Worker 加速</span>
播放器源测速 → 走 worker (测得延迟 ≈ 实际播放延迟)
m3u8 播放 → 走 <span class="g">/m3u8?url=</span>, .ts 子链接自动重写到 worker</pre>
</div>
<div class="footer">
<a href="https://github.com/SzeMeng76/CORSAPI" target="_blank">SzeMeng76/CORSAPI</a>
·
<a href="https://github.com/djsevenx1/CORSAPI" target="_blank">djsevenx1/CORSAPI</a>
·
<a href="https://github.com/djsevenx1/LunaTV-Mobile" target="_blank">LunaTV-Mobile</a>
<br>
<span style="margin-top: 6px; display: inline-block;">Powered by ${detectPlatform()}</span>
</div>
</div>
</body>
</html>`
return new Response(html, {
status: 200,
headers: { 'Content-Type': 'text/html; charset=utf-8', ...CORS_HEADERS }
})
}
// ---------- 测速端点 ----------
// 流式返回指定 MB 数的随机字节, 客户端测下载速度 = 用户到 worker 的实际带宽
// 默认 1MB, ?size=N (MB), 范围 1-3 (v2.0.84 缩小到 3MB 上限).
// v2.0.84 用 Uint8Array 一次性 Response, 不用 ReadableStream/TransformStream,
// 避免 CF edge backpressure 切断问题.
// v2.0.80 setTimeout 0 → CF edge 误判流空闲切断 (mobile 收到 0 字节)
// v2.0.81 一次性 50MB Uint8Array → 1MB 也 500 (CF Workers 内存峰值问题)
// v2.0.82 start 一次性 enqueue → CF edge 反复切断, 1MB 测试 15s 只传 415KB
// v2.0.83 pull + TransformStream → 太复杂, 仍然会被 backpressure 切
// v2.0.84 Uint8Array(size).fill(0) 不调 crypto.getRandomValues, Response 直接 return.
// 1MB / 2MB / 3MB Uint8Array 内存峰值 < 4MB, 远低于 128MB 限制
// 不用 random 是因为 crypto.getRandomValues 在某些 CF edge 节点会触发额外内存分配
// 测速场景不需要密码学安全随机, 0 字节能填满带宽就够
async function handleSpeedTest(reqUrl) {
const sizeParam = parseInt(reqUrl.searchParams.get('size') || '1', 10)
// v2.0.84: 上限 3MB, 内存峰值 < 4MB 完全安全
const sizeMB = Math.max(1, Math.min(3, isNaN(sizeParam) ? 1 : sizeParam))
const totalBytes = sizeMB * 1024 * 1024
// 一次性 Uint8Array 全填 0, 不调 crypto.getRandomValues (避免某些节点内存爆)
const buffer = new Uint8Array(totalBytes)
return new Response(buffer, {
status: 200,
headers: {
'Content-Type': 'application/octet-stream',
'Content-Length': totalBytes.toString(),
'Content-Disposition': `attachment; filename="speedtest-${sizeMB}mb.bin"`,
'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',
...CORS_HEADERS
}
})
}
// ---------- 统一错误响应处理 ----------
function errorResponse(error, data = {}, status = 400) {
return new Response(JSON.stringify({ error, ...data }), {
status,
headers: { 'Content-Type': 'application/json; charset=utf-8', ...CORS_HEADERS }
})
}
// ---------- 平台检测 (首页 "Powered by ..." 用) ----------
// Cloudflare Workers 没 Deno 全局, Netlify Edge Functions (Deno) 有
function detectPlatform() {
if (typeof Deno !== 'undefined') return 'Netlify Edge Functions (Deno)'
return 'Cloudflare Workers'
}
// ---------- 命名导出 ----------
// 供 netlify/edge-functions/corsapi.js 复用同一个 handleRequest
// 不重复维护两套逻辑. Cloudflare 走上面 export default { fetch } 入口,
// Netlify 走 netlify/edge-functions/corsapi.js 导入这个 handleRequest