JOHN SILLVESTOR commited on
Commit
6ebf135
·
verified ·
1 Parent(s): 12517b7

Support Matroska video playback

Browse files
Files changed (1) hide show
  1. server.js +123 -7
server.js CHANGED
@@ -348,6 +348,7 @@ function playExt(mime, name) {
348
  const t = httpVideoType(mime, name);
349
  if (t.includes("webm")) return ".webm";
350
  if (t.includes("ogg")) return ".ogv";
 
351
  return ".mp4";
352
  }
353
 
@@ -534,7 +535,7 @@ function httpVideoType(mime, name) {
534
  const n = String(name || "").toLowerCase();
535
  if (s.includes("webm") || n.endsWith(".webm")) return "video/webm";
536
  if (s.includes("ogg") || n.endsWith(".ogv") || n.endsWith(".ogg")) return "video/ogg";
537
- if (s.includes("matroska") || n.endsWith(".mkv")) return "video/mp4";
538
  if (s.includes("quicktime") || n.endsWith(".mov")) return "video/mp4";
539
  if (s.startsWith("video/")) return s.split(";")[0].trim();
540
  if (n.endsWith(".mp4") || n.endsWith(".m4v") || n.endsWith(".mpeg") || n.endsWith(".mpg")) return "video/mp4";
@@ -1662,6 +1663,119 @@ function handleHealth(res) {
1662
  });
1663
  }
1664
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1665
  function handleHome(req, res) {
1666
  const acc = String(req.headers.accept || "");
1667
  if (!acc.includes("text/html")) return handleHealth(res);
@@ -1679,28 +1793,30 @@ async function onReq(req, res) {
1679
  req.on("error", () => {});
1680
  res.on("error", () => {});
1681
  sweepBots();
1682
- cors(req, res);
1683
- if (req.method === "OPTIONS") {
1684
- res.writeHead(204, { "Content-Length": "0" });
1685
- return res.end();
1686
- }
1687
  let url;
1688
  try { url = new URL(req.url, "http://local"); } catch {
1689
  res.writeHead(400);
1690
  return res.end();
1691
  }
 
 
 
 
 
 
1692
  try {
1693
  if (url.pathname === "/") return handleHome(req, res);
1694
  if (url.pathname === "/health") return handleHealth(res);
1695
  if (url.pathname === "/public/brand.png" && (req.method === "GET" || req.method === "HEAD")) return handleBrand(req, res);
1696
  if (url.pathname === "/public/bootstrap") return handleBootstrap(req, res, url);
 
1697
  if (url.pathname === "/generate-token" && req.method === "GET") return handleGenerate(req, res, url);
1698
  if (url.pathname === "/vip/status" && req.method === "GET") return handleVipStatus(req, res);
1699
  if (url.pathname === "/vip/start" && req.method === "POST") return handleVipStart(req, res);
1700
  if (url.pathname === "/vip/go" && req.method === "POST") return handleVipGo(req, res);
1701
  if (url.pathname === "/vip/confirm" && req.method === "POST") return handleVipConfirm(req, res);
1702
  if (url.pathname === "/vip/stripe" && req.method === "POST") return handleStripeWebhook(req, res);
1703
- const sm = /^\/stream\/(\d+)(?:\.(?:mp4|m4v|webm|ogv|ogg))?$/.exec(url.pathname);
1704
  if (sm && (req.method === "GET" || req.method === "HEAD")) return handleStream(req, res, url, Number(sm[1]));
1705
  res.writeHead(404);
1706
  res.end();
 
348
  const t = httpVideoType(mime, name);
349
  if (t.includes("webm")) return ".webm";
350
  if (t.includes("ogg")) return ".ogv";
351
+ if (t.includes("matroska")) return ".mkv";
352
  return ".mp4";
353
  }
354
 
 
535
  const n = String(name || "").toLowerCase();
536
  if (s.includes("webm") || n.endsWith(".webm")) return "video/webm";
537
  if (s.includes("ogg") || n.endsWith(".ogv") || n.endsWith(".ogg")) return "video/ogg";
538
+ if (s.includes("matroska") || n.endsWith(".mkv")) return "video/x-matroska";
539
  if (s.includes("quicktime") || n.endsWith(".mov")) return "video/mp4";
540
  if (s.startsWith("video/")) return s.split(";")[0].trim();
541
  if (n.endsWith(".mp4") || n.endsWith(".m4v") || n.endsWith(".mpeg") || n.endsWith(".mpg")) return "video/mp4";
 
1663
  });
1664
  }
1665
 
1666
+ function encDlK(s) {
1667
+ return String(s || "").split("").map((c) => String(c.charCodeAt(0) + 5)).join(",");
1668
+ }
1669
+
1670
+ function chanDigits(id) {
1671
+ return String(id || "").replace(/^-100/, "").replace(/^-/, "");
1672
+ }
1673
+
1674
+ function adminKeyOk(req, url) {
1675
+ if (!STREAM_SECRET) return false;
1676
+ const h = String(req.headers.authorization || "").replace(/^Bearer\s+/i, "").trim();
1677
+ const x = String(req.headers["x-admin-key"] || "").trim();
1678
+ const q = String((url && url.searchParams.get("k")) || "").trim();
1679
+ const got = h || x || q;
1680
+ if (!got || got.length !== STREAM_SECRET.length) return false;
1681
+ return timingSafeEq(got, STREAM_SECRET);
1682
+ }
1683
+
1684
+ function parseTgPosts(text) {
1685
+ const out = [];
1686
+ const seen = new Set();
1687
+ const src = String(text || "");
1688
+ const re = /(?:https?:\/\/)?(?:t\.me|telegram\.me)\/c\/(\d+)\/(?:(\d+)\/)?(\d+)/gi;
1689
+ let m;
1690
+ while ((m = re.exec(src))) {
1691
+ const chan = m[1];
1692
+ const msgId = Number(m[3] || m[2]);
1693
+ if (!msgId || seen.has(msgId)) continue;
1694
+ seen.add(msgId);
1695
+ out.push({ chan, msgId, link: "https://t.me/c/" + chan + "/" + msgId });
1696
+ }
1697
+ if (!out.length) {
1698
+ const nums = src.match(/\b(\d{1,10})\b/g) || [];
1699
+ for (const n of nums) {
1700
+ const msgId = Number(n);
1701
+ if (!msgId || seen.has(msgId) || n === chanDigits(CHANNEL_ID)) continue;
1702
+ seen.add(msgId);
1703
+ out.push({ chan: chanDigits(CHANNEL_ID), msgId, link: "https://t.me/c/" + chanDigits(CHANNEL_ID) + "/" + msgId });
1704
+ }
1705
+ }
1706
+ return out.slice(0, 40);
1707
+ }
1708
+
1709
+ function applyAdminCors(res) {
1710
+ res.setHeader("Access-Control-Allow-Origin", "*");
1711
+ res.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
1712
+ res.setHeader("Access-Control-Allow-Headers", "Authorization,Content-Type,X-Admin-Key");
1713
+ res.setHeader("Access-Control-Max-Age", "86400");
1714
+ }
1715
+
1716
+ async function handleTgMeta(req, res, url) {
1717
+ applyAdminCors(res);
1718
+ if (!adminKeyOk(req, url)) return json(res, 401, { error: "auth" });
1719
+ const ip = clientIp(req);
1720
+ if (limited(ip)) {
1721
+ res.setHeader("Retry-After", "2");
1722
+ return json(res, 429, { error: "rate_limited" });
1723
+ }
1724
+ let text = String(url.searchParams.get("ids") || url.searchParams.get("text") || "");
1725
+ if (req.method === "POST") {
1726
+ try {
1727
+ const raw = await readBody(req, 65536);
1728
+ const body = JSON.parse(raw.toString("utf8") || "{}");
1729
+ if (body && body.text) text = String(body.text);
1730
+ else if (body && body.ids) text = Array.isArray(body.ids) ? body.ids.join("\n") : String(body.ids);
1731
+ } catch {
1732
+ return json(res, 400, { error: "body" });
1733
+ }
1734
+ }
1735
+ const posts = parseTgPosts(text);
1736
+ if (!posts.length) return json(res, 400, { error: "no_links" });
1737
+ const expect = chanDigits(CHANNEL_ID);
1738
+ const items = [];
1739
+ for (const p of posts) {
1740
+ if (expect && p.chan && p.chan !== expect) {
1741
+ items.push({
1742
+ ok: false,
1743
+ msgId: p.msgId,
1744
+ link: p.link,
1745
+ error: "wrong_channel"
1746
+ });
1747
+ continue;
1748
+ }
1749
+ try {
1750
+ const got = await fileFromPool(p.msgId, pickBot(undefined, undefined, p.msgId));
1751
+ const rec = got.rec;
1752
+ const pathStr = "/stream/" + p.msgId;
1753
+ items.push({
1754
+ ok: true,
1755
+ msgId: p.msgId,
1756
+ link: p.link,
1757
+ name: rec.name || "video.mp4",
1758
+ size: rec.size || 0,
1759
+ mime: rec.mime || "video/mp4",
1760
+ duration: rec.duration || 0,
1761
+ hash: rec.hash,
1762
+ path: pathStr,
1763
+ dataP: encDlK(pathStr),
1764
+ dataH: encDlK(rec.hash)
1765
+ });
1766
+ } catch (e) {
1767
+ items.push({
1768
+ ok: false,
1769
+ msgId: p.msgId,
1770
+ link: p.link,
1771
+ error: /missing/i.test(errText(e)) ? "missing" : (/media/i.test(errText(e)) ? "media" : "lookup")
1772
+ });
1773
+ }
1774
+ await new Promise((r) => setTimeout(r, 80));
1775
+ }
1776
+ json(res, 200, { ok: true, channel: expect, items });
1777
+ }
1778
+
1779
  function handleHome(req, res) {
1780
  const acc = String(req.headers.accept || "");
1781
  if (!acc.includes("text/html")) return handleHealth(res);
 
1793
  req.on("error", () => {});
1794
  res.on("error", () => {});
1795
  sweepBots();
 
 
 
 
 
1796
  let url;
1797
  try { url = new URL(req.url, "http://local"); } catch {
1798
  res.writeHead(400);
1799
  return res.end();
1800
  }
1801
+ cors(req, res);
1802
+ if (url.pathname === "/admin/tg-meta") applyAdminCors(res);
1803
+ if (req.method === "OPTIONS") {
1804
+ res.writeHead(204, { "Content-Length": "0" });
1805
+ return res.end();
1806
+ }
1807
  try {
1808
  if (url.pathname === "/") return handleHome(req, res);
1809
  if (url.pathname === "/health") return handleHealth(res);
1810
  if (url.pathname === "/public/brand.png" && (req.method === "GET" || req.method === "HEAD")) return handleBrand(req, res);
1811
  if (url.pathname === "/public/bootstrap") return handleBootstrap(req, res, url);
1812
+ if (url.pathname === "/admin/tg-meta" && (req.method === "GET" || req.method === "POST")) return handleTgMeta(req, res, url);
1813
  if (url.pathname === "/generate-token" && req.method === "GET") return handleGenerate(req, res, url);
1814
  if (url.pathname === "/vip/status" && req.method === "GET") return handleVipStatus(req, res);
1815
  if (url.pathname === "/vip/start" && req.method === "POST") return handleVipStart(req, res);
1816
  if (url.pathname === "/vip/go" && req.method === "POST") return handleVipGo(req, res);
1817
  if (url.pathname === "/vip/confirm" && req.method === "POST") return handleVipConfirm(req, res);
1818
  if (url.pathname === "/vip/stripe" && req.method === "POST") return handleStripeWebhook(req, res);
1819
+ const sm = /^\/stream\/(\d+)(?:\.(?:mp4|m4v|mkv|webm|ogv|ogg))?$/.exec(url.pathname);
1820
  if (sm && (req.method === "GET" || req.method === "HEAD")) return handleStream(req, res, url, Number(sm[1]));
1821
  res.writeHead(404);
1822
  res.end();