-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathm3u8proxy(cf_worker).js
More file actions
151 lines (134 loc) · 3.99 KB
/
m3u8proxy(cf_worker).js
File metadata and controls
151 lines (134 loc) · 3.99 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
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const url = new URL(request.url);
if (url.pathname === "/m3u8-proxy") {
return handleM3U8Proxy(request);
} else if (url.pathname === "/ts-proxy") {
return handleTsProxy(request);
}
return new Response("Not Found", { status: 404 });
}
const options = {
originBlacklist: [],
originWhitelist: ["*"],
};
const isOriginAllowed = (origin, options) => {
if (options.originWhitelist.includes("*")) {
return true;
}
if (
options.originWhitelist.length &&
!options.originWhitelist.includes(origin)
) {
return false;
}
if (
options.originBlacklist.length &&
options.originBlacklist.includes(origin)
) {
return false;
}
return true;
};
async function handleM3U8Proxy(request) {
const { searchParams } = new URL(request.url);
const targetUrl = searchParams.get("url");
const headers = JSON.parse(searchParams.get("headers") || "{}");
const origin = request.headers.get("Origin") || "";
if (!isOriginAllowed(origin, options)) {
return new Response(`The origin "${origin}" is not allowed.`, {
status: 403,
});
}
if (!targetUrl) {
return new Response("URL is required", { status: 400 });
}
try {
const response = await fetch(targetUrl, { headers });
if (!response.ok) {
return new Response("Failed to fetch the m3u8 file", {
status: response.status,
});
}
let m3u8 = await response.text();
m3u8 = m3u8
.split("\n")
.filter((line) => !line.startsWith("#EXT-X-MEDIA:TYPE=AUDIO"))
.join("\n");
const lines = m3u8.split("\n");
const newLines = [];
lines.forEach((line) => {
if (line.startsWith("#")) {
if (line.startsWith("#EXT-X-KEY:")) {
const regex = /https?:\/\/[^\""\s]+/g;
const keyUrl = regex.exec(line)?.[0] ?? "";
const newUrl = `/ts-proxy?url=${encodeURIComponent(
keyUrl
)}&headers=${encodeURIComponent(JSON.stringify(headers))}`;
newLines.push(line.replace(keyUrl, newUrl));
} else {
newLines.push(line);
}
} else {
const uri = new URL(line, targetUrl);
newLines.push(
`/ts-proxy?url=${encodeURIComponent(
uri.href
)}&headers=${encodeURIComponent(JSON.stringify(headers))}`
);
}
});
return new Response(newLines.join("\n"), {
headers: {
"Content-Type": "application/vnd.apple.mpegurl",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Methods": "*",
},
});
} catch (error) {
return new Response(error.message, { status: 500 });
}
}
async function handleTsProxy(request) {
const { searchParams } = new URL(request.url);
const targetUrl = searchParams.get("url");
const headers = JSON.parse(searchParams.get("headers") || "{}");
const origin = request.headers.get("Origin") || "";
if (!isOriginAllowed(origin, options)) {
return new Response(`The origin "${origin}" is not allowed.`, {
status: 403,
});
}
if (!targetUrl) {
return new Response("URL is required", { status: 400 });
}
try {
const response = await fetch(targetUrl, {
method: request.method,
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
...headers,
},
});
if (!response.ok) {
return new Response("Failed to fetch segment", {
status: response.status,
});
}
return new Response(response.body, {
status: response.status,
headers: {
"Content-Type": "video/mp2t",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Methods": "*",
},
});
} catch (error) {
return new Response(error.message, { status: 500 });
}
}