JOHN SILLVESTOR commited on
Commit
587e108
·
verified ·
1 Parent(s): 4d1c67d

Harden stream tickets: UA binding, tight subnet, anti-jam concurrency limits

Browse files
Files changed (1) hide show
  1. server.js +24 -6
server.js CHANGED
@@ -15,7 +15,7 @@ const API_HASH = process.env.TELEGRAM_API_HASH || "";
15
  const STREAM_SECRET = process.env.STREAM_SECRET || "";
16
  const ROLE = (process.env.NODE_ROLE || "stream").toLowerCase();
17
  const HASH_LEN = 6;
18
- const TOKEN_TTL = 1800;
19
  const MIN_ALIGN = 4096;
20
  const MB = 1024 * 1024;
21
  const GET_SIZES = [1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096];
@@ -26,8 +26,8 @@ const STREAM_WINDOW = 8 * MB;
26
  const BOT_MAX = 12;
27
  const PIPE_MS = 45000;
28
  const PIPE_MAX_MS = 30 * 60 * 1000;
29
- const IP_STREAM_MAX = 10;
30
- const UID_STREAM_MAX = 8;
31
  const GLOBAL_PIPE_MAX = 96;
32
  const HOT_BYTES_MAX = 1536 * MB;
33
  const BODY_MAX = 262144;
@@ -492,10 +492,10 @@ function ipNet(ip) {
492
  let s = String(ip || "").replace(/^::ffff:/i, "");
493
  if (/^\d{1,3}(?:\.\d{1,3}){3}$/.test(s)) {
494
  const p = s.split(".");
495
- return "4." + p[0] + "." + p[1];
496
  }
497
  const parts = s.split(":").filter(Boolean);
498
- return "6." + parts.slice(0, 3).join(":");
499
  }
500
 
501
  function ipTag(ip) {
@@ -503,6 +503,12 @@ function ipTag(ip) {
503
  return crypto.createHmac("sha256", hmacKey).update("net:" + ipNet(ip)).digest("hex").slice(0, 16);
504
  }
505
 
 
 
 
 
 
 
506
  function ticketNetOk(ticket, ip) {
507
  const p = String(ticket && ticket.p || "");
508
  if (!p) return false;
@@ -511,6 +517,14 @@ function ticketNetOk(ticket, ip) {
511
  return timingSafeEq(p, now);
512
  }
513
 
 
 
 
 
 
 
 
 
514
  function payLimited(uid, ip) {
515
  const slot = Math.floor(Date.now() / 60000);
516
  const a = "pay:" + uid + ":" + slot;
@@ -1472,7 +1486,7 @@ async function handleGenerate(req, res, url) {
1472
  const playMime = httpVideoType(rec.mime, rec.name);
1473
  const durSec = Number(rec.duration) > 1 ? Math.floor(Number(rec.duration)) : 0;
1474
  const exp = Math.floor(Date.now() / 1000) + TOKEN_TTL;
1475
- const k = sealTicket({ m: msgId, h: hash, u: uid || "anon", l: lvl, b: bot.idx, e: exp, s: rec.size, n: playMime, p: ipTag(ip), d: durSec });
1476
  const host = publicSpaceOrigin(req);
1477
  const body = {
1478
  url: host + "/stream/" + msgId + playExt(playMime, rec.name) + "?k=" + k,
@@ -1509,6 +1523,10 @@ async function handleStream(req, res, url, msgId) {
1509
  res.writeHead(403);
1510
  return res.end();
1511
  }
 
 
 
 
1512
  if (!takeStream(ip, ticket.u)) {
1513
  res.setHeader("Retry-After", "1");
1514
  res.writeHead(429);
 
15
  const STREAM_SECRET = process.env.STREAM_SECRET || "";
16
  const ROLE = (process.env.NODE_ROLE || "stream").toLowerCase();
17
  const HASH_LEN = 6;
18
+ const TOKEN_TTL = 1200;
19
  const MIN_ALIGN = 4096;
20
  const MB = 1024 * 1024;
21
  const GET_SIZES = [1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096];
 
26
  const BOT_MAX = 12;
27
  const PIPE_MS = 45000;
28
  const PIPE_MAX_MS = 30 * 60 * 1000;
29
+ const IP_STREAM_MAX = 5;
30
+ const UID_STREAM_MAX = 3;
31
  const GLOBAL_PIPE_MAX = 96;
32
  const HOT_BYTES_MAX = 1536 * MB;
33
  const BODY_MAX = 262144;
 
492
  let s = String(ip || "").replace(/^::ffff:/i, "");
493
  if (/^\d{1,3}(?:\.\d{1,3}){3}$/.test(s)) {
494
  const p = s.split(".");
495
+ return "4." + p[0] + "." + p[1] + "." + p[2];
496
  }
497
  const parts = s.split(":").filter(Boolean);
498
+ return "6." + parts.slice(0, 4).join(":");
499
  }
500
 
501
  function ipTag(ip) {
 
503
  return crypto.createHmac("sha256", hmacKey).update("net:" + ipNet(ip)).digest("hex").slice(0, 16);
504
  }
505
 
506
+ function uaTag(ua) {
507
+ if (!hmacKey) return "";
508
+ const s = String(ua || "").toLowerCase().replace(/\s+/g, "").slice(0, 100);
509
+ return crypto.createHmac("sha256", hmacKey).update("ua:" + s).digest("hex").slice(0, 16);
510
+ }
511
+
512
  function ticketNetOk(ticket, ip) {
513
  const p = String(ticket && ticket.p || "");
514
  if (!p) return false;
 
517
  return timingSafeEq(p, now);
518
  }
519
 
520
+ function ticketUaOk(ticket, ua) {
521
+ const a = String(ticket && ticket.a || "");
522
+ if (!a) return true;
523
+ const now = uaTag(ua);
524
+ if (!now) return false;
525
+ return timingSafeEq(a, now);
526
+ }
527
+
528
  function payLimited(uid, ip) {
529
  const slot = Math.floor(Date.now() / 60000);
530
  const a = "pay:" + uid + ":" + slot;
 
1486
  const playMime = httpVideoType(rec.mime, rec.name);
1487
  const durSec = Number(rec.duration) > 1 ? Math.floor(Number(rec.duration)) : 0;
1488
  const exp = Math.floor(Date.now() / 1000) + TOKEN_TTL;
1489
+ const k = sealTicket({ m: msgId, h: hash, u: uid || "anon", l: lvl, b: bot.idx, e: exp, s: rec.size, n: playMime, p: ipTag(ip), a: uaTag(req.headers["user-agent"]), d: durSec });
1490
  const host = publicSpaceOrigin(req);
1491
  const body = {
1492
  url: host + "/stream/" + msgId + playExt(playMime, rec.name) + "?k=" + k,
 
1523
  res.writeHead(403);
1524
  return res.end();
1525
  }
1526
+ if (!ticketUaOk(ticket, req.headers["user-agent"])) {
1527
+ res.writeHead(403);
1528
+ return res.end();
1529
+ }
1530
  if (!takeStream(ip, ticket.u)) {
1531
  res.setHeader("Retry-After", "1");
1532
  res.writeHead(429);