-
Notifications
You must be signed in to change notification settings - Fork 25
/
deno_worker.js
336 lines (315 loc) · 12.8 KB
/
deno_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
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
async function handle(request, connInfo) {
const corsHeaders = {
headers: {
"Access-Control-Allow-Origin": "*",
},
};
const corsOptionsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, HEAD, POST, OPTIONS",
"Access-Control-Max-Age": "86400",
};
async function handleRequest(request, connInfo) {
const url = new URL(request.url);
let api_pos = url.origin.length + 1;
let api = url.href.substring(api_pos);
let ip = "";
let redirect = request.method === "POST" ? "manual" : "follow";
let get_cookie = false;
let cookie_plus = false;
let remove_compression = false;
let params = [];
let cdn_info = "cdn_pDvQc7ZT";
let user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36";
if (api === "headers") {
let body = "";
request.headers.forEach((value, key) => body += key + " = " + value + "\n");
if (connInfo && connInfo.remoteAddr) {
body += "connInfo" + " = " + JSON.stringify(connInfo.remoteAddr) + "\n";
}
body += "request_url" + " = " + request.url + "\n";
body += "worker_version = 1.09\n";
return new Response(body, corsHeaders);
}
if (api.startsWith("?")) {
api_pos += 1;
api = api.substring(1);
}
let next_param = true;
while (next_param) {
if (api.startsWith("ip")) {
let pos = api.indexOf("/");
if (pos !== -1) {
ip = api.substring(2, pos);
api_pos += pos + 1;
api = api.substring(pos + 1);
} else {
ip = api.substring(2);
api_pos += api.length;
api = "";
}
} else if (api.startsWith("redirect=")) {
let pos = api.indexOf("/");
if (pos !== -1) {
redirect = api.substring(9, pos);
api_pos += pos + 1;
api = api.substring(pos + 1);
} else {
redirect = api.substring(9);
api_pos += api.length;
api = "";
}
} else if (api.startsWith("get_cookie/")) {
get_cookie = true;
api_pos += 11;
api = api.substring(11);
} else if (api.startsWith("cookie_plus/")) {
cookie_plus = true;
remove_compression = true;
api_pos += 12;
api = api.substring(12);
} else if (api.startsWith("param?") || api.startsWith("param/")) {
api_pos += 6;
api = api.substring(6);
let param;
let pos = api.indexOf("/");
if (pos !== -1) {
param = api.substring(0, pos);
api_pos += pos + 1;
api = api.substring(pos + 1);
} else {
param = api.substring(0);
api_pos += api.length;
api = "";
}
pos = param.indexOf("=");
if (pos !== -1) {
params.push([param.substring(0, pos), param.substring(pos + 1)]);
} else {
params.push([param]);
}
} else {
next_param = false;
}
}
let proxy = url.href.substring(0, api_pos);
let forwarded_proto = request.headers.get("X-Forwarded-Proto");
if (forwarded_proto) forwarded_proto = forwarded_proto.split(",")[0].trim();
if (forwarded_proto === "https") proxy = proxy.replace("http://", "https://");
if (!ip) {
let forwarded_for = request.headers.get("X-Forwarded-For");
if (forwarded_for) ip = forwarded_for.split(",").map(s=>s.trim()).find(s=>s && !s.match(/^(127\.|10\.|172\.1[6-9]|172\.2[0-9]|172\.3[01]|192\.168\.)/)) || "";
}
if (!ip) ip = request.headers.get("cf-connecting-ip");
if (!ip) ip = request.headers.get("X-Real-IP");
if (!ip) ip = connInfo && connInfo.remoteAddr && connInfo.remoteAddr.hostname || "";
if (!api || !/^https?:\/\/[^\/]/.test(api)) {
let error = "Malformed URL";
return new Response(error + ": " + api, {
...corsHeaders,
status: 404,
statusText: error,
});
}
const apiUrl = new URL(api);
let apiBase = apiUrl.href.substring(0, apiUrl.href.lastIndexOf("/") + 1);
// Rewrite request to point to API URL. This also makes the request mutable
// so you can add the correct Origin header to make the API server think
// that this request is not cross-site.
request = new Request(api, request);
let cdn_loop = request.headers.get("CDN-Loop");
if (cdn_loop && cdn_loop.indexOf(cdn_info) !== -1) {
let error = "CDN-Loop detected";
return new Response(error, {
...corsHeaders,
status: 403,
statusText: error,
});
} else {
request.headers.append("CDN-Loop", cdn_info);
}
request.headers.set("Origin", apiUrl.origin);
request.headers.set("Referer", apiUrl.origin + "/");
if (true) {
request.headers.delete("Sec-Fetch-Dest");
request.headers.delete("Sec-Fetch-Mode");
request.headers.delete("Sec-Fetch-Site");
request.headers.delete("Sec-Fetch-User");
request.headers.delete("Sec-CH-UA");
request.headers.delete("Sec-CH-UA-Mobile");
request.headers.delete("Sec-CH-UA-Platform");
request.headers.delete("Host");
}
if (true) {
request.headers.delete("X-Forwarded-For");
request.headers.delete("X-Forwarded-Proto");
//request.headers.delete("X-Real-IP");
//request.headers.delete("cf-connecting-ip");
request.headers.delete("cf-ipcountry");
request.headers.delete("cf-ray");
request.headers.delete("cf-visitor");
}
if (ip && ip !== "no") {
request.headers.set("X-Forwarded-For", ip);
request.headers.set("X-Forwarded-Proto", "https");
request.headers.set("X-Real-IP", ip);
request.headers.set("cf-connecting-ip", ip);
}
if (apiUrl.hostname === "rezka.ag" || apiUrl.hostname === "hdrezka.ag" || apiUrl.hostname === "hdrezka.me" || apiUrl.hostname === "hdrezka.sh" || apiUrl.hostname === "hdrezka.cm" || apiUrl.hostname === "hdrezka.kim" || apiUrl.hostname === "hdrezka.la" || apiUrl.hostname === "rezka.pub" || apiUrl.hostname === "kinopub.me") {
request.headers.set("User-Agent", user_agent);
}
if (apiUrl.hostname.endsWith(".svetacdn.in")) {
request.headers.set("Origin", "https://videocdn.tv");
request.headers.set("Referer", "https://videocdn.tv/");
}
if (apiUrl.hostname === "api.lumex.pw") {
request.headers.set("User-Agent", user_agent);
request.headers.set("Origin", "https://p.lumex.pw");
request.headers.set("Referer", "https://p.lumex.pw/");
request.headers.set("Sec-Fetch-Dest", "empty");
request.headers.set("Sec-Fetch-Mode", "cors");
request.headers.set("Sec-Fetch-Site", "same-site");
}
if (apiUrl.hostname.endsWith("cdnmovies-stream.online") || apiUrl.hostname.endsWith("cdnmovies-hls-stream.online") || apiUrl.hostname.endsWith(".sarnage.cc")) {
request.headers.set("Origin", "https://cdnmovies.net");
request.headers.set("Referer", "https://cdnmovies.net/");
}
if (apiUrl.hostname.endsWith(".bazon.site")) {
request.headers.set("User-Agent", user_agent);
request.headers.set("Origin", "https://bazon.cc");
request.headers.set("Referer", "https://bazon.cc/");
}
if (["kodikapi.com", "kodik.biz", "kodik.info"].indexOf(apiUrl.hostname) !== -1) {
request.headers.delete("Origin");
request.headers.delete("Referer");
}
if (apiUrl.hostname === "kinoplay.site" || apiUrl.hostname === "kinoplay1.site" || apiUrl.hostname === "kinoplay2.site") {
request.headers.set("Cookie", "invite=a246a3f46c82fe439a45c3dbbbb24ad5");
}
if (remove_compression) {
let encoding = (request.headers.get("Accept-Encoding") || "");
if (encoding.includes("zstd") || encoding.includes("deflate")) {
encoding = encoding.split(",").filter(enc=>!(enc.includes("zstd") || enc.includes("deflate"))).join(",") || "identity";
request.headers.set("Accept-Encoding", encoding);
}
}
params.forEach(param => {
if (param[0]) {
if (param[1]) {
request.headers.set(decodeURIComponent(param[0]), decodeURIComponent(param[1] || ""));
} else {
request.headers.delete(decodeURIComponent(param[0]));
}
}
});
let response = await fetch(request, {
redirect: redirect,
});
// Recreate the response so you can modify the headers
response = new Response(response.body, response);
// Set CORS headers
response.headers.set("Access-Control-Allow-Origin", "*");
// Append to/Add Vary header so browser will cache response correctly
response.headers.append("Vary", "Origin");
if (response.status >= 200 && response.status < 300) {
let ctype = (response.headers.get("Content-Type") || "").split(";")[0].toLowerCase();
if (get_cookie || cookie_plus) {
let json = {};
json.cookie = response.headers.getSetCookie();
if (cookie_plus) {
let headers = {};
for (let key of response.headers.keys()) {
if (key === 'set-cookie') {
headers[key] = json.cookie;
} else {
headers[key] = response.headers.get(key);
}
}
json.headers = headers;
if (ctype.startsWith("text/") || ["application/json", "application/xml", "application/x-mpegurl", "application/vnd.apple.mpegurl", "application/dash+xml"].indexOf(ctype) !== -1) {
json.body = await response.text();
}
}
return new Response(JSON.stringify(json), {
headers: {
"Access-Control-Allow-Origin": "*",
"Vary": "Origin",
"Content-Type": "application/json; charset=utf-8",
},
});
}
}
// Fix redirect URL
if (response.status >= 300 && response.status < 400) {
let target = response.headers.get("Location");
if (target) {
response.headers.set("Location", fixLink(target, proxy, apiUrl, apiBase));
}
}
return response;
}
function fixLink(link, proxy, url, base) {
if (link) {
if (link.indexOf("://") !== -1) return proxy + link;
if (link.startsWith("//")) return proxy + url.protocol + link;
if (link.startsWith("/")) return proxy + url.origin + link;
if (link.startsWith("?")) return proxy + url.origin + url.pathname + link;
if (link.startsWith("#")) return proxy + url.origin + url.pathname + url.search + link;
return proxy + base + link;
}
return link;
}
async function handleOptions(request, connInfo) {
if (
request.headers.get("Origin") !== null &&
request.headers.get("Access-Control-Request-Method") !== null &&
request.headers.get("Access-Control-Request-Headers") !== null
) {
// Handle CORS preflight requests.
return new Response(null, {
headers: {
...corsOptionsHeaders,
"Access-Control-Allow-Headers": request.headers.get(
"Access-Control-Request-Headers"
),
},
});
} else {
// Handle standard OPTIONS request.
return new Response(null, {
headers: {
Allow: "GET, HEAD, POST, OPTIONS",
},
});
}
}
try {
if (request.method === "OPTIONS") {
// Handle CORS preflight requests
return await handleOptions(request, connInfo);
} else if (
request.method === "GET" ||
request.method === "HEAD" ||
request.method === "POST"
) {
// Handle requests to the API server
return await handleRequest(request, connInfo);
} else {
let error = "Method Not Allowed";
return new Response(error + ": " + request.method, {
...corsHeaders,
status: 405,
statusText: error,
});
}
} catch (err) {
let error = "Internal Server Error";
return new Response(error + ": " + err + "\n" + (err.stack || ""), {
...corsHeaders,
status: 500,
statusText: error,
});
}
}
const port = parseInt(Deno.env.get("PORT") || "8000");
Deno.serve({port: port}, handle);