JOHN SILLVESTOR commited on
Commit
b8737a3
·
verified ·
1 Parent(s): 2ecf90c

Clamp stream range to ticket access level

Browse files
Files changed (1) hide show
  1. server.js +42 -2
server.js CHANGED
@@ -755,6 +755,41 @@ async function fillDuration(bot, rec, msgId) {
755
  return d;
756
  }
757
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
758
  function parseRange(h, size) {
759
  if (!size) return { start: 0, end: 0, partial: false };
760
  const firstEnd = Math.min(size - 1, HOT_HEAD - 1);
@@ -1115,8 +1150,11 @@ async function handleStream(req, res, url, msgId) {
1115
  const sizeHint = Number(ticket.s) || 0;
1116
  const mimeHint = String(ticket.n || "video/mp4");
1117
  const typeHint = mimeHint.indexOf("/") > 0 ? mimeHint : "video/mp4";
 
1118
  if (sizeHint > 0) {
1119
- const rngHot = parseRange(req.headers.range, sizeHint);
 
 
1120
  const needHot = rngHot.end - rngHot.start + 1;
1121
  const headersHot = mediaHeaders(typeHint, msgId, sizeHint, ticket.h, rngHot);
1122
  if (req.method === "HEAD") {
@@ -1157,7 +1195,9 @@ async function handleStream(req, res, url, msgId) {
1157
  return res.end();
1158
  }
1159
  const size = rec.size;
1160
- const rng = parseRange(req.headers.range, size);
 
 
1161
  const headers = mediaHeaders(httpVideoType(rec.mime, rec.name), msgId, size, rec.hash, rng);
1162
  if (req.method === "HEAD") {
1163
  res.writeHead(206, headers);
 
755
  return d;
756
  }
757
 
758
+ const LVL_PCT = { anonymous: 30, registered: 60, subscribed: 100 };
759
+
760
+ function lvlPct(l) {
761
+ const p = LVL_PCT[String(l || "")];
762
+ return p == null ? 30 : p;
763
+ }
764
+
765
+ function previewMaxEnd(size, l) {
766
+ const pct = lvlPct(l);
767
+ if (!size || pct >= 100) return size - 1;
768
+ const n = Math.floor(Number(size) * pct / 100);
769
+ return Math.max(0, Math.min(size - 1, Math.max(0, n) - 1));
770
+ }
771
+
772
+ function applyPreview(rng, size, l) {
773
+ const maxEnd = previewMaxEnd(size, l);
774
+ if (!rng || rng.start > maxEnd) return null;
775
+ const end = Math.min(rng.end, maxEnd);
776
+ return {
777
+ start: rng.start,
778
+ end,
779
+ partial: rng.start > 0 || end < size - 1
780
+ };
781
+ }
782
+
783
+ function denyRange(res, size) {
784
+ res.writeHead(416, Object.assign(streamLock(), {
785
+ "Content-Range": "bytes */" + size,
786
+ "Content-Type": "video/mp4",
787
+ "Cache-Control": "private, no-store, no-transform",
788
+ "Accept-Ranges": "bytes"
789
+ }));
790
+ return res.end();
791
+ }
792
+
793
  function parseRange(h, size) {
794
  if (!size) return { start: 0, end: 0, partial: false };
795
  const firstEnd = Math.min(size - 1, HOT_HEAD - 1);
 
1150
  const sizeHint = Number(ticket.s) || 0;
1151
  const mimeHint = String(ticket.n || "video/mp4");
1152
  const typeHint = mimeHint.indexOf("/") > 0 ? mimeHint : "video/mp4";
1153
+ const lvl = String(ticket.l || "anonymous");
1154
  if (sizeHint > 0) {
1155
+ const rngHot0 = parseRange(req.headers.range, sizeHint);
1156
+ const rngHot = applyPreview(rngHot0, sizeHint, lvl);
1157
+ if (!rngHot) return denyRange(res, sizeHint);
1158
  const needHot = rngHot.end - rngHot.start + 1;
1159
  const headersHot = mediaHeaders(typeHint, msgId, sizeHint, ticket.h, rngHot);
1160
  if (req.method === "HEAD") {
 
1195
  return res.end();
1196
  }
1197
  const size = rec.size;
1198
+ const rng0 = parseRange(req.headers.range, size);
1199
+ const rng = applyPreview(rng0, size, lvl);
1200
+ if (!rng) return denyRange(res, size);
1201
  const headers = mediaHeaders(httpVideoType(rec.mime, rec.name), msgId, size, rec.hash, rng);
1202
  if (req.method === "HEAD") {
1203
  res.writeHead(206, headers);