JOHN SILLVESTOR commited on
Commit
5a06504
·
verified ·
1 Parent(s): a969418

Fix Chrome Android stream: full-file 200 Range, ticket auth without Referer

Browse files
Files changed (1) hide show
  1. server.js +27 -14
server.js CHANGED
@@ -16,13 +16,13 @@ const HASH_LEN = 6;
16
  const TOKEN_TTL = 600;
17
  const MIN_ALIGN = 4096;
18
  const MB = 1024 * 1024;
19
- const HTTP_WINDOW = 2 * MB;
20
  const GET_SIZES = [524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096];
21
  const FIRST_GET = 131072;
22
  const HOT_HEAD = 524288;
23
  const HOT_TAIL = 1048576;
24
  const BOT_MAX = 24;
25
  const PIPE_MS = 45000;
 
26
  const IP_STREAM_MAX = 48;
27
  const BODY_MAX = 262144;
28
  const FILE_TTL = 15 * 60 * 1000;
@@ -715,15 +715,9 @@ async function fillDuration(bot, rec, msgId) {
715
 
716
  function parseRange(h, size) {
717
  if (!size) return { start: 0, end: 0, partial: false };
718
- if (!h) {
719
- const end = Math.min(size - 1, HTTP_WINDOW - 1);
720
- return { start: 0, end, partial: end < size - 1 };
721
- }
722
  const m = /^bytes=(\d*)-(\d*)$/.exec(String(h).trim());
723
- if (!m) {
724
- const end = Math.min(size - 1, HTTP_WINDOW - 1);
725
- return { start: 0, end, partial: end < size - 1 };
726
- }
727
  let start;
728
  let end;
729
  if (m[1] === "") {
@@ -737,8 +731,12 @@ function parseRange(h, size) {
737
  if (!Number.isFinite(start) || start < 0) start = 0;
738
  if (!Number.isFinite(end) || end >= size) end = size - 1;
739
  if (start > end) start = 0;
740
- if (end - start + 1 > HTTP_WINDOW) end = start + HTTP_WINDOW - 1;
741
- return { start, end, partial: true };
 
 
 
 
742
  }
743
 
744
  function pickGetSize(room, want, first) {
@@ -914,7 +912,8 @@ async function pipeRange(bot, rec, start, end, req, res, status, headers, msgId)
914
  if (pipeDead(req, res)) return;
915
  res.writeHead(status, headers);
916
  if (typeof res.flushHeaders === "function") res.flushHeaders();
917
- const stop = () => pipeDead(req, res) || Date.now() - t0 > PIPE_MS;
 
918
  let pos = start;
919
  const prefix = hotPrefix(msgId, pos, end);
920
  if (prefix && prefix.length) {
@@ -1007,10 +1006,23 @@ async function handleGenerate(req, res, url) {
1007
  }
1008
 
1009
  async function handleStream(req, res, url, msgId) {
1010
- if (!fromBlog(req)) {
 
1011
  res.writeHead(403);
1012
  return res.end();
1013
  }
 
 
 
 
 
 
 
 
 
 
 
 
1014
  const k = url.searchParams.get("k") || "";
1015
  const ticket = openTicket(k);
1016
  if (!ticket || ticket.m !== msgId) {
@@ -1061,10 +1073,11 @@ async function handleStream(req, res, url, msgId) {
1061
  "Accept-Ranges": "bytes",
1062
  "Cache-Control": "private, no-store, no-transform",
1063
  "Content-Disposition": "inline",
 
1064
  "X-Content-Type-Options": "nosniff",
1065
  "X-Accel-Buffering": "no"
1066
  };
1067
- const partial = rng.start > 0 || rng.end < size - 1 || !!req.headers.range;
1068
  headers["Content-Length"] = String(rng.end - rng.start + 1);
1069
  if (partial) {
1070
  headers["Content-Range"] = "bytes " + rng.start + "-" + rng.end + "/" + size;
 
16
  const TOKEN_TTL = 600;
17
  const MIN_ALIGN = 4096;
18
  const MB = 1024 * 1024;
 
19
  const GET_SIZES = [524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096];
20
  const FIRST_GET = 131072;
21
  const HOT_HEAD = 524288;
22
  const HOT_TAIL = 1048576;
23
  const BOT_MAX = 24;
24
  const PIPE_MS = 45000;
25
+ const PIPE_MAX_MS = 30 * 60 * 1000;
26
  const IP_STREAM_MAX = 48;
27
  const BODY_MAX = 262144;
28
  const FILE_TTL = 15 * 60 * 1000;
 
715
 
716
  function parseRange(h, size) {
717
  if (!size) return { start: 0, end: 0, partial: false };
718
+ if (!h) return { start: 0, end: size - 1, partial: false };
 
 
 
719
  const m = /^bytes=(\d*)-(\d*)$/.exec(String(h).trim());
720
+ if (!m) return { start: 0, end: size - 1, partial: false };
 
 
 
721
  let start;
722
  let end;
723
  if (m[1] === "") {
 
731
  if (!Number.isFinite(start) || start < 0) start = 0;
732
  if (!Number.isFinite(end) || end >= size) end = size - 1;
733
  if (start > end) start = 0;
734
+ return { start, end, partial: start > 0 || end < size - 1 };
735
+ }
736
+
737
+ function pipeBudget(start, end) {
738
+ const n = Math.max(1, end - start + 1);
739
+ return Math.min(PIPE_MAX_MS, Math.max(PIPE_MS, Math.ceil(n / (128 * 1024)) * 1000));
740
  }
741
 
742
  function pickGetSize(room, want, first) {
 
912
  if (pipeDead(req, res)) return;
913
  res.writeHead(status, headers);
914
  if (typeof res.flushHeaders === "function") res.flushHeaders();
915
+ const budget = pipeBudget(start, end);
916
+ const stop = () => pipeDead(req, res) || Date.now() - t0 > budget;
917
  let pos = start;
918
  const prefix = hotPrefix(msgId, pos, end);
919
  if (prefix && prefix.length) {
 
1006
  }
1007
 
1008
  async function handleStream(req, res, url, msgId) {
1009
+ const o = String(req.headers.origin || "").trim();
1010
+ if (o && o.toLowerCase() !== "null" && !isBlogOrigin(o)) {
1011
  res.writeHead(403);
1012
  return res.end();
1013
  }
1014
+ const ref = String(req.headers.referer || "");
1015
+ if (ref) {
1016
+ try {
1017
+ if (!isBlogOrigin(new URL(ref).origin)) {
1018
+ res.writeHead(403);
1019
+ return res.end();
1020
+ }
1021
+ } catch {
1022
+ res.writeHead(403);
1023
+ return res.end();
1024
+ }
1025
+ }
1026
  const k = url.searchParams.get("k") || "";
1027
  const ticket = openTicket(k);
1028
  if (!ticket || ticket.m !== msgId) {
 
1073
  "Accept-Ranges": "bytes",
1074
  "Cache-Control": "private, no-store, no-transform",
1075
  "Content-Disposition": "inline",
1076
+ "Content-Encoding": "identity",
1077
  "X-Content-Type-Options": "nosniff",
1078
  "X-Accel-Buffering": "no"
1079
  };
1080
+ const partial = rng.partial;
1081
  headers["Content-Length"] = String(rng.end - rng.start + 1);
1082
  if (partial) {
1083
  headers["Content-Range"] = "bytes " + rng.start + "-" + rng.end + "/" + size;