stnh70 commited on
Commit
81af6fe
·
verified ·
1 Parent(s): 76e1cbe

Update deno.ts

Browse files
Files changed (1) hide show
  1. deno.ts +100 -32
deno.ts CHANGED
@@ -4,15 +4,6 @@ async function handle(request, connInfo) {
4
  "Access-Control-Allow-Origin": "*",
5
  },
6
  };
7
- // 增强的 CORS 头部
8
- // const corsHeaders = {
9
- // "Access-Control-Allow-Origin": "*",
10
- // "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS, HEAD, PATCH",
11
- // "Access-Control-Allow-Headers": "*",
12
- // "Access-Control-Expose-Headers": "*",
13
- // "Access-Control-Max-Age": "86400",
14
- // "Access-Control-Allow-Credentials": "false",
15
- // };
16
  const corsOptionsHeaders = {
17
  "Access-Control-Allow-Origin": "*",
18
  "Access-Control-Allow-Methods": "GET, HEAD, POST, OPTIONS",
@@ -27,14 +18,13 @@ async function handle(request, connInfo) {
27
  let proxy = "";
28
  let proxy_enc = "";
29
  let enc = "";
30
- let ip = "";
31
  let redirect = request.method === "POST" ? "manual" : "follow";
32
  let get_cookie = false;
33
  let cookie_plus = false;
34
- let get_redirect = false;
35
  let remove_compression = false;
36
  let params = [];
37
- let cdn_info = "cdn_pDvQc7ZT";
38
  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";
39
 
40
  if (api === "headers") {
@@ -44,7 +34,7 @@ async function handle(request, connInfo) {
44
  body += "connInfo" + " = " + JSON.stringify(connInfo.remoteAddr) + "\n";
45
  }
46
  body += "request_url" + " = " + request.url + "\n";
47
- body += "worker_version = 1.12\n";
48
  return new Response(body, corsHeaders);
49
  }
50
 
@@ -86,11 +76,6 @@ async function handle(request, connInfo) {
86
  remove_compression = true;
87
  api_pos += 12;
88
  api = api.substring(12);
89
- } else if (api.startsWith("get_redirect/")) {
90
- get_redirect = true;
91
- redirect = "manual";
92
- api_pos += 13;
93
- api = api.substring(13);
94
  } else if (api.startsWith("param?") || api.startsWith("param/")) {
95
  api_pos += 6;
96
  api = api.substring(6);
@@ -162,6 +147,22 @@ async function handle(request, connInfo) {
162
  const apiUrl = new URL(api);
163
  let apiBase = apiUrl.href.substring(0, apiUrl.href.lastIndexOf("/") + 1);
164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  // Rewrite request to point to API URL. This also makes the request mutable
166
  // so you can add the correct Origin header to make the API server think
167
  // that this request is not cross-site.
@@ -237,6 +238,10 @@ async function handle(request, connInfo) {
237
  if (apiUrl.hostname === "kinoplay.site" || apiUrl.hostname === "kinoplay1.site" || apiUrl.hostname === "kinoplay2.site") {
238
  request.headers.set("Cookie", "invite=a246a3f46c82fe439a45c3dbbbb24ad5");
239
  }
 
 
 
 
240
  if (remove_compression) {
241
  let encoding = (request.headers.get("Accept-Encoding") || "");
242
  if (encoding.includes("zstd") || encoding.includes("deflate")) {
@@ -256,7 +261,6 @@ async function handle(request, connInfo) {
256
  let response = await fetch(request, {
257
  redirect: redirect,
258
  });
259
- let currentUrl = response.url;
260
 
261
  // Recreate the response so you can modify the headers
262
  response = new Response(response.body, response);
@@ -267,21 +271,11 @@ async function handle(request, connInfo) {
267
  // Append to/Add Vary header so browser will cache response correctly
268
  response.headers.append("Vary", "Origin");
269
 
270
- if (response.status >= 200 && response.status < (get_redirect ? 400 : 300)) {
271
  let ctype = (response.headers.get("Content-Type") || "").split(";")[0].toLowerCase();
272
- if (get_cookie || cookie_plus || get_redirect) {
273
  let json = {};
274
- json.currentUrl = currentUrl;
275
- if (get_redirect) {
276
- let target = response.headers.get("Location");
277
- if (target) {
278
- json.redirectUrl = fixLink(target, apiUrl, apiBase);
279
- response.headers.set("Location", json.redirectUrl);
280
- }
281
- }
282
- if (get_cookie || cookie_plus) {
283
- json.cookie = response.headers.getSetCookie();
284
- }
285
  if (cookie_plus) {
286
  let headers = {};
287
  for (let key of response.headers.keys()) {
@@ -304,6 +298,20 @@ async function handle(request, connInfo) {
304
  },
305
  });
306
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
307
  }
308
 
309
  // Fix redirect URL
@@ -359,6 +367,66 @@ async function handle(request, connInfo) {
359
  return link;
360
  }
361
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362
  async function handleOptions(request, connInfo) {
363
  if (
364
  request.headers.get("Origin") !== null &&
 
4
  "Access-Control-Allow-Origin": "*",
5
  },
6
  };
 
 
 
 
 
 
 
 
 
7
  const corsOptionsHeaders = {
8
  "Access-Control-Allow-Origin": "*",
9
  "Access-Control-Allow-Methods": "GET, HEAD, POST, OPTIONS",
 
18
  let proxy = "";
19
  let proxy_enc = "";
20
  let enc = "";
21
+ let ip = "no";
22
  let redirect = request.method === "POST" ? "manual" : "follow";
23
  let get_cookie = false;
24
  let cookie_plus = false;
 
25
  let remove_compression = false;
26
  let params = [];
27
+ let cdn_info = "cdn_c8Bc9aMo";
28
  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";
29
 
30
  if (api === "headers") {
 
34
  body += "connInfo" + " = " + JSON.stringify(connInfo.remoteAddr) + "\n";
35
  }
36
  body += "request_url" + " = " + request.url + "\n";
37
+ body += "apn_version = 1.11\n";
38
  return new Response(body, corsHeaders);
39
  }
40
 
 
76
  remove_compression = true;
77
  api_pos += 12;
78
  api = api.substring(12);
 
 
 
 
 
79
  } else if (api.startsWith("param?") || api.startsWith("param/")) {
80
  api_pos += 6;
81
  api = api.substring(6);
 
147
  const apiUrl = new URL(api);
148
  let apiBase = apiUrl.href.substring(0, apiUrl.href.lastIndexOf("/") + 1);
149
 
150
+ let clientUserAgent = request.headers.get("User-Agent") || '';
151
+ let clientOrigin = request.headers.get("Origin") || '';
152
+ if(
153
+ (/\blampishe\b|\bprisma_client\b/).test(clientUserAgent) ||
154
+ clientOrigin.endsWith("lampishe.cc") ||
155
+ clientOrigin.endsWith("prisma.ws") ||
156
+ clientOrigin.endsWith("bylampa.online")
157
+ ) {
158
+ let error = "Malformed URL";
159
+ return new Response(error + ": " + api, {
160
+ ...corsHeaders,
161
+ status: 404,
162
+ statusText: error,
163
+ });
164
+ }
165
+
166
  // Rewrite request to point to API URL. This also makes the request mutable
167
  // so you can add the correct Origin header to make the API server think
168
  // that this request is not cross-site.
 
238
  if (apiUrl.hostname === "kinoplay.site" || apiUrl.hostname === "kinoplay1.site" || apiUrl.hostname === "kinoplay2.site") {
239
  request.headers.set("Cookie", "invite=a246a3f46c82fe439a45c3dbbbb24ad5");
240
  }
241
+ if (apiUrl.pathname.endsWith(".m3u8") || apiUrl.pathname.endsWith(".m3u") || apiUrl.pathname.endsWith(".M3U8") || apiUrl.pathname.endsWith(".M3U") || apiUrl.pathname.endsWith(".mpd") || apiUrl.pathname.endsWith(".MPD")) {
242
+ request.headers.delete("Range");
243
+ remove_compression = true;
244
+ }
245
  if (remove_compression) {
246
  let encoding = (request.headers.get("Accept-Encoding") || "");
247
  if (encoding.includes("zstd") || encoding.includes("deflate")) {
 
261
  let response = await fetch(request, {
262
  redirect: redirect,
263
  });
 
264
 
265
  // Recreate the response so you can modify the headers
266
  response = new Response(response.body, response);
 
271
  // Append to/Add Vary header so browser will cache response correctly
272
  response.headers.append("Vary", "Origin");
273
 
274
+ if (response.status >= 200 && response.status < 300) {
275
  let ctype = (response.headers.get("Content-Type") || "").split(";")[0].toLowerCase();
276
+ if (get_cookie || cookie_plus) {
277
  let json = {};
278
+ json.cookie = response.headers.getSetCookie();
 
 
 
 
 
 
 
 
 
 
279
  if (cookie_plus) {
280
  let headers = {};
281
  for (let key of response.headers.keys()) {
 
298
  },
299
  });
300
  }
301
+ if (["application/x-mpegurl", "application/vnd.apple.mpegurl"].indexOf(ctype) !== -1) {
302
+ let body = edit_m3u8(await response.text(), apiUrl, apiBase, proxy, proxy_enc, enc);
303
+ response.headers.delete("Content-Length");
304
+ response.headers.delete("Content-Range");
305
+ response.headers.set("Accept-Ranges", "none");
306
+ return new Response(body, response);
307
+ }
308
+ if (["application/dash+xml"].indexOf(ctype) !== -1) {
309
+ let body = edit_mpd(await response.text(), apiUrl, apiBase, proxy, proxy_enc, enc);
310
+ response.headers.delete("Content-Length");
311
+ response.headers.delete("Content-Range");
312
+ response.headers.set("Accept-Ranges", "none");
313
+ return new Response(body, response);
314
+ }
315
  }
316
 
317
  // Fix redirect URL
 
367
  return link;
368
  }
369
 
370
+ function edit_m3u8(m3u8, url, base, proxy, proxy_enc, enc) {
371
+ try {
372
+ return m3u8.split("\n").map(org_line => {
373
+ let line = org_line.trim();
374
+ if (line.charAt(0) === "#") {
375
+ return org_line.replace(/\bURI="([^"]*)"/g, (str, link) => {
376
+ link = link.trim();
377
+ if (link) {
378
+ return 'URI="' + proxyLink(fixLink(link, url, base), proxy, proxy_enc, enc) + '"';
379
+ }
380
+ return str;
381
+ });
382
+ } else if(line) {
383
+ return proxyLink(fixLink(line, url, base), proxy, proxy_enc, enc);
384
+ }
385
+ return org_line;
386
+ }).join("\n");
387
+ } catch (err) {
388
+ return m3u8;
389
+ }
390
+ }
391
+
392
+ function unescapeXml(str) {
393
+ return str.replace(/&lt;|&gt;|&amp;|&apos;|&quot;/g, function (c) {
394
+ switch (c) {
395
+ case "&lt;": return "<";
396
+ case "&gt;": return ">";
397
+ case "&amp;": return "&";
398
+ case "&apos;": return "'";
399
+ case "&quot;": return '"';
400
+ }
401
+ });
402
+ }
403
+
404
+ function escapeXml(str) {
405
+ return str.replace(/[<>&'"]/g, function (c) {
406
+ switch (c) {
407
+ case "<": return "&lt;";
408
+ case ">": return "&gt;";
409
+ case "&": return "&amp;";
410
+ case "'": return "&apos;";
411
+ case '"': return "&quot;";
412
+ }
413
+ });
414
+ }
415
+
416
+ function edit_mpd(mpd, url, base, proxy, proxy_enc, enc) {
417
+ try {
418
+ return mpd.replace(/<BaseURL>([^<]*)/g, (str, link) => {
419
+ link = link.trim();
420
+ if (link) {
421
+ return "<BaseURL>" + escapeXml(proxyLink(fixLink(unescapeXml(link), url, base), proxy, proxy_enc, (enc === "enc2" ? "enc1" : enc)));
422
+ }
423
+ return str;
424
+ });
425
+ } catch (err) {
426
+ return mpd;
427
+ }
428
+ }
429
+
430
  async function handleOptions(request, connInfo) {
431
  if (
432
  request.headers.get("Origin") !== null &&