-
Notifications
You must be signed in to change notification settings - Fork 17
/
m3u8proxy(cf_worker).js
180 lines (160 loc) · 5.15 KB
/
m3u8proxy(cf_worker).js
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
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: ["*"], // Replace with specific origins if needed
originWhitelist: [], // Add allowed origins if applicable
};
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 (
options.originBlacklist.includes("*") &&
!options.originWhitelist.includes(origin)
) {
return new Response(
`The origin "${origin}" was blacklisted by the operator of this proxy.`,
{ 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");
if (m3u8.includes("RESOLUTION=")) {
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(
`/m3u8-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": "*",
},
});
}
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 (
options.originBlacklist.includes("*") &&
!options.originWhitelist.includes(origin)
) {
return new Response(
`The origin "${origin}" was blacklisted by the operator of this proxy.`,
{ status: 403 }
);
}
if (!targetUrl) {
return new Response("URL is required", { status: 400 });
}
const url = new URL(targetUrl);
const requestHeaders = new 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,
});
try {
const response = await fetch(url.href, {
method: request.method,
headers: requestHeaders,
});
if (!response.ok) {
return new Response("Failed to fetch segment", {
status: response.status,
});
}
const contentType = "video/mp2t";
return new Response(response.body, {
status: response.status,
headers: {
"Content-Type": contentType,
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Methods": "*",
},
});
} catch (error) {
return new Response(error.message, { status: 500 });
}
}