Spaces:
Running
Running
JOHN SILLVESTOR commited on
Tune multi-user stream on CPU Basic
Browse files- .gitattributes +1 -0
- Dockerfile +6 -5
- brand.png +3 -0
- server.js +184 -64
.gitattributes
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
brand.png filter=lfs diff=lfs merge=lfs -text
|
Dockerfile
CHANGED
|
@@ -1,13 +1,14 @@
|
|
| 1 |
FROM node:22-bookworm-slim
|
| 2 |
WORKDIR /app
|
| 3 |
-
COPY package.json ./
|
| 4 |
RUN npm install --omit=dev --no-audit --no-fund
|
| 5 |
-
COPY server.js ./
|
| 6 |
-
RUN chown -R node:node /app
|
| 7 |
USER node
|
| 8 |
ENV HOME=/home/node
|
| 9 |
ENV NODE_ENV=production
|
| 10 |
ENV PORT=7860
|
|
|
|
|
|
|
| 11 |
EXPOSE 7860
|
| 12 |
-
HEALTHCHECK --interval=
|
| 13 |
-
CMD ["node",
|
|
|
|
| 1 |
FROM node:22-bookworm-slim
|
| 2 |
WORKDIR /app
|
| 3 |
+
COPY --chown=node:node package.json ./
|
| 4 |
RUN npm install --omit=dev --no-audit --no-fund
|
| 5 |
+
COPY --chown=node:node server.js brand.png ./
|
|
|
|
| 6 |
USER node
|
| 7 |
ENV HOME=/home/node
|
| 8 |
ENV NODE_ENV=production
|
| 9 |
ENV PORT=7860
|
| 10 |
+
ENV UV_THREADPOOL_SIZE=8
|
| 11 |
+
ENV NODE_OPTIONS=--max-old-space-size=6144
|
| 12 |
EXPOSE 7860
|
| 13 |
+
HEALTHCHECK --interval=45s --timeout=5s --start-period=50s --retries=3 CMD node -e "fetch('http://127.0.0.1:7860/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"
|
| 14 |
+
CMD ["node","--max-old-space-size=6144","server.js"]
|
brand.png
ADDED
|
Git LFS Details
|
server.js
CHANGED
|
@@ -15,21 +15,24 @@ 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 =
|
| 19 |
const MIN_ALIGN = 4096;
|
| 20 |
const MB = 1024 * 1024;
|
| 21 |
const GET_SIZES = [1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096];
|
| 22 |
const FIRST_GET = 1048576;
|
| 23 |
-
const HOT_HEAD =
|
| 24 |
-
const HOT_TAIL =
|
| 25 |
-
const STREAM_WINDOW =
|
| 26 |
-
const BOT_MAX =
|
| 27 |
const PIPE_MS = 45000;
|
| 28 |
const PIPE_MAX_MS = 30 * 60 * 1000;
|
| 29 |
-
const IP_STREAM_MAX =
|
|
|
|
|
|
|
|
|
|
| 30 |
const BODY_MAX = 262144;
|
| 31 |
-
const FILE_TTL =
|
| 32 |
-
const LVL_TTL =
|
| 33 |
const FB_PROJECT = process.env.FIREBASE_PROJECT_ID || "";
|
| 34 |
const FB_DB = String(process.env.FIREBASE_DB_URL || "").replace(/\/?$/, "/");
|
| 35 |
const FB_SECRET = process.env.FIREBASE_DB_SECRET || "";
|
|
@@ -75,8 +78,10 @@ const inflightDur = new Map();
|
|
| 75 |
const lvlCache = new Map();
|
| 76 |
const rateMap = new Map();
|
| 77 |
const ipStreams = new Map();
|
|
|
|
| 78 |
const paidOrders = new Map();
|
| 79 |
const payLocks = new Map();
|
|
|
|
| 80 |
let fbKeys = { certs: null, exp: 0 };
|
| 81 |
let rr = 0;
|
| 82 |
|
|
@@ -245,17 +250,32 @@ function waitDrain(req, res) {
|
|
| 245 |
});
|
| 246 |
}
|
| 247 |
|
| 248 |
-
function takeStream(ip) {
|
|
|
|
| 249 |
const n = (ipStreams.get(ip) || 0) + 1;
|
| 250 |
if (n > IP_STREAM_MAX) return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
ipStreams.set(ip, n);
|
|
|
|
| 252 |
return true;
|
| 253 |
}
|
| 254 |
|
| 255 |
-
function freeStream(ip) {
|
|
|
|
| 256 |
const n = (ipStreams.get(ip) || 1) - 1;
|
| 257 |
if (n <= 0) ipStreams.delete(ip);
|
| 258 |
else ipStreams.set(ip, n);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
}
|
| 260 |
|
| 261 |
function sweepMaps() {
|
|
@@ -273,16 +293,17 @@ function sweepMaps() {
|
|
| 273 |
for (const [k, v] of hotMap) {
|
| 274 |
if (!v || !v.ts || now - v.ts > FILE_TTL) hotMap.delete(k);
|
| 275 |
}
|
| 276 |
-
|
| 277 |
-
const arr = [...hotMap.entries()].sort((a, b) => (a[1].ts || 0) - (b[1].ts || 0));
|
| 278 |
-
for (let i = 0; i < arr.length - 32; i++) hotMap.delete(arr[i][0]);
|
| 279 |
-
}
|
| 280 |
}
|
| 281 |
if (lvlCache.size > 2000) {
|
| 282 |
for (const [k, v] of lvlCache) {
|
| 283 |
if (!v || now - v.ts > LVL_TTL * 4) lvlCache.delete(k);
|
| 284 |
}
|
| 285 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
if (rateMap.size > 4000) {
|
| 287 |
const slot = Math.floor(now / 60000);
|
| 288 |
for (const k of rateMap.keys()) {
|
|
@@ -297,6 +318,29 @@ function sweepMaps() {
|
|
| 297 |
}
|
| 298 |
}
|
| 299 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
function withTime(p, ms, fallback) {
|
| 301 |
let t;
|
| 302 |
return Promise.race([
|
|
@@ -629,12 +673,10 @@ function dropFile(msgId, bot) {
|
|
| 629 |
}
|
| 630 |
}
|
| 631 |
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
const msg = msgs && msgs[0];
|
| 635 |
-
if (!msg || msg.deleted) throw new Error("missing");
|
| 636 |
const doc = msg.video || msg.document;
|
| 637 |
-
if (!doc)
|
| 638 |
let name = "video.mp4";
|
| 639 |
let duration = 0;
|
| 640 |
const attrs = doc.attributes || [];
|
|
@@ -643,16 +685,15 @@ async function loadFile(bot, msgId) {
|
|
| 643 |
const n = Number(a && a.duration);
|
| 644 |
if (Number.isFinite(n) && n > duration) duration = n;
|
| 645 |
}
|
| 646 |
-
const cached = durCache.get(String(
|
| 647 |
if (cached > 1) duration = cached;
|
| 648 |
-
const loc = new Api.InputDocumentFileLocation({
|
| 649 |
-
id: doc.id,
|
| 650 |
-
accessHash: doc.accessHash,
|
| 651 |
-
fileReference: doc.fileReference,
|
| 652 |
-
thumbSize: ""
|
| 653 |
-
});
|
| 654 |
const rec = {
|
| 655 |
-
loc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 656 |
size: Number(doc.size || 0),
|
| 657 |
mime: doc.mimeType || "video/mp4",
|
| 658 |
name,
|
|
@@ -663,11 +704,78 @@ async function loadFile(bot, msgId) {
|
|
| 663 |
doc,
|
| 664 |
ts: Date.now()
|
| 665 |
};
|
| 666 |
-
if (duration > 1) durCache.set(String(
|
| 667 |
-
fileCache.set(String(
|
| 668 |
return rec;
|
| 669 |
}
|
| 670 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 671 |
async function fileFromMsg(bot, msgId) {
|
| 672 |
const key = String(msgId) + ":" + bot.idx;
|
| 673 |
const hit = fileCache.get(key);
|
|
@@ -809,7 +917,7 @@ async function fillDuration(bot, rec, msgId) {
|
|
| 809 |
}
|
| 810 |
|
| 811 |
const LVL_PCT = { anonymous: 30, registered: 60, subscribed: 100 };
|
| 812 |
-
const INDEX_TAIL =
|
| 813 |
|
| 814 |
function lvlPct(l) {
|
| 815 |
const p = LVL_PCT[String(l || "")];
|
|
@@ -833,10 +941,9 @@ function previewMaxEnd(size, l) {
|
|
| 833 |
function previewTailLen(size, maxEnd) {
|
| 834 |
const unpaid = size - (maxEnd + 1);
|
| 835 |
if (unpaid <= 0) return 0;
|
| 836 |
-
const
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
return Math.min(want, unpaid - minGap);
|
| 840 |
}
|
| 841 |
|
| 842 |
function applyPreview(rng, size, l) {
|
|
@@ -924,7 +1031,6 @@ function mediaHeaders(type, msgId, size, hash, rng) {
|
|
| 924 |
"Content-Length": String(rng.end - rng.start + 1),
|
| 925 |
"Content-Range": "bytes " + rng.start + "-" + rng.end + "/" + size
|
| 926 |
});
|
| 927 |
-
if (rng.partial) headers.Connection = "close";
|
| 928 |
return headers;
|
| 929 |
}
|
| 930 |
|
|
@@ -1001,6 +1107,7 @@ function hotFill(msgId, start, buf, size) {
|
|
| 1001 |
h.tailOff = from;
|
| 1002 |
}
|
| 1003 |
}
|
|
|
|
| 1004 |
}
|
| 1005 |
|
| 1006 |
function hotHeadLen(msgId) {
|
|
@@ -1206,19 +1313,22 @@ async function handleGenerate(req, res, url) {
|
|
| 1206 |
const rec = got.rec;
|
| 1207 |
if (rec.hash && rec.hash !== hash) return json(res, 400, { error: "hash" });
|
| 1208 |
warmHot(bot, rec, msgId);
|
| 1209 |
-
|
| 1210 |
-
|
|
|
|
| 1211 |
const cachedDur = durCache.get(String(msgId));
|
| 1212 |
if (cachedDur > 1) rec.duration = cachedDur;
|
| 1213 |
const playMime = httpVideoType(rec.mime, rec.name);
|
|
|
|
| 1214 |
const exp = Math.floor(Date.now() / 1000) + TOKEN_TTL;
|
| 1215 |
-
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) });
|
| 1216 |
const host = "https://" + (req.headers.host || "localhost");
|
| 1217 |
json(res, 200, {
|
| 1218 |
url: host + "/stream/" + msgId + playExt(playMime, rec.name) + "?k=" + k,
|
| 1219 |
fileSize: rec.size,
|
| 1220 |
mime: playMime,
|
| 1221 |
-
duration:
|
|
|
|
| 1222 |
accessLevel: lvl,
|
| 1223 |
expiry: exp
|
| 1224 |
});
|
|
@@ -1240,7 +1350,7 @@ async function handleStream(req, res, url, msgId) {
|
|
| 1240 |
return res.end();
|
| 1241 |
}
|
| 1242 |
const ip = clientIp(req);
|
| 1243 |
-
if (!takeStream(ip)) {
|
| 1244 |
res.setHeader("Retry-After", "1");
|
| 1245 |
res.writeHead(429);
|
| 1246 |
return res.end();
|
|
@@ -1336,7 +1446,7 @@ async function handleStream(req, res, url, msgId) {
|
|
| 1336 |
}
|
| 1337 |
}
|
| 1338 |
} finally {
|
| 1339 |
-
freeStream(ip);
|
| 1340 |
}
|
| 1341 |
}
|
| 1342 |
|
|
@@ -1659,7 +1769,8 @@ function handleHealth(res) {
|
|
| 1659 |
role: ROLE,
|
| 1660 |
bots: bots.filter((b) => botUsable(b) || (b.ready && !(b.coolUntil && Date.now() < b.coolUntil))).length,
|
| 1661 |
load: bots.reduce((n, b) => n + b.load, 0),
|
| 1662 |
-
cap: bots.filter((b) => b.ready).length * BOT_MAX
|
|
|
|
| 1663 |
});
|
| 1664 |
}
|
| 1665 |
|
|
@@ -1735,30 +1846,40 @@ async function handleTgMeta(req, res, url) {
|
|
| 1735 |
const posts = parseTgPosts(text);
|
| 1736 |
if (!posts.length) return json(res, 400, { error: "no_links" });
|
| 1737 |
const expect = chanDigits(CHANNEL_ID);
|
| 1738 |
-
|
| 1739 |
-
|
| 1740 |
-
|
| 1741 |
-
|
| 1742 |
-
|
| 1743 |
-
|
| 1744 |
-
|
| 1745 |
-
|
| 1746 |
-
|
| 1747 |
-
|
|
|
|
| 1748 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1749 |
try {
|
| 1750 |
-
|
| 1751 |
-
|
| 1752 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1753 |
items.push({
|
| 1754 |
ok: true,
|
| 1755 |
-
msgId:
|
| 1756 |
-
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)
|
|
@@ -1766,12 +1887,11 @@ async function handleTgMeta(req, res, url) {
|
|
| 1766 |
} catch (e) {
|
| 1767 |
items.push({
|
| 1768 |
ok: false,
|
| 1769 |
-
msgId:
|
| 1770 |
-
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 |
}
|
|
@@ -1854,12 +1974,12 @@ async function main() {
|
|
| 1854 |
setInterval(() => { reviveBots().catch(() => {}); }, 20000);
|
| 1855 |
const srv = http.createServer({
|
| 1856 |
keepAlive: true,
|
| 1857 |
-
keepAliveTimeout:
|
| 1858 |
-
headersTimeout:
|
| 1859 |
requestTimeout: 0,
|
| 1860 |
-
connectionsCheckingInterval:
|
| 1861 |
}, onReq);
|
| 1862 |
-
srv.maxConnections =
|
| 1863 |
srv.on("clientError", (err, sock) => {
|
| 1864 |
try {
|
| 1865 |
if (sock.writable) sock.end("HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n");
|
|
|
|
| 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];
|
| 22 |
const FIRST_GET = 1048576;
|
| 23 |
+
const HOT_HEAD = 2 * MB;
|
| 24 |
+
const HOT_TAIL = MB;
|
| 25 |
+
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;
|
| 34 |
+
const FILE_TTL = 20 * 60 * 1000;
|
| 35 |
+
const LVL_TTL = 60000;
|
| 36 |
const FB_PROJECT = process.env.FIREBASE_PROJECT_ID || "";
|
| 37 |
const FB_DB = String(process.env.FIREBASE_DB_URL || "").replace(/\/?$/, "/");
|
| 38 |
const FB_SECRET = process.env.FIREBASE_DB_SECRET || "";
|
|
|
|
| 78 |
const lvlCache = new Map();
|
| 79 |
const rateMap = new Map();
|
| 80 |
const ipStreams = new Map();
|
| 81 |
+
const uidStreams = new Map();
|
| 82 |
const paidOrders = new Map();
|
| 83 |
const payLocks = new Map();
|
| 84 |
+
let livePipes = 0;
|
| 85 |
let fbKeys = { certs: null, exp: 0 };
|
| 86 |
let rr = 0;
|
| 87 |
|
|
|
|
| 250 |
});
|
| 251 |
}
|
| 252 |
|
| 253 |
+
function takeStream(ip, uid) {
|
| 254 |
+
if (livePipes >= GLOBAL_PIPE_MAX) return false;
|
| 255 |
const n = (ipStreams.get(ip) || 0) + 1;
|
| 256 |
if (n > IP_STREAM_MAX) return false;
|
| 257 |
+
const u = uid && uid !== "anon" ? String(uid) : "";
|
| 258 |
+
if (u) {
|
| 259 |
+
const un = (uidStreams.get(u) || 0) + 1;
|
| 260 |
+
if (un > UID_STREAM_MAX) return false;
|
| 261 |
+
uidStreams.set(u, un);
|
| 262 |
+
}
|
| 263 |
ipStreams.set(ip, n);
|
| 264 |
+
livePipes += 1;
|
| 265 |
return true;
|
| 266 |
}
|
| 267 |
|
| 268 |
+
function freeStream(ip, uid) {
|
| 269 |
+
livePipes = Math.max(0, livePipes - 1);
|
| 270 |
const n = (ipStreams.get(ip) || 1) - 1;
|
| 271 |
if (n <= 0) ipStreams.delete(ip);
|
| 272 |
else ipStreams.set(ip, n);
|
| 273 |
+
const u = uid && uid !== "anon" ? String(uid) : "";
|
| 274 |
+
if (u) {
|
| 275 |
+
const un = (uidStreams.get(u) || 1) - 1;
|
| 276 |
+
if (un <= 0) uidStreams.delete(u);
|
| 277 |
+
else uidStreams.set(u, un);
|
| 278 |
+
}
|
| 279 |
}
|
| 280 |
|
| 281 |
function sweepMaps() {
|
|
|
|
| 293 |
for (const [k, v] of hotMap) {
|
| 294 |
if (!v || !v.ts || now - v.ts > FILE_TTL) hotMap.delete(k);
|
| 295 |
}
|
| 296 |
+
trimHot();
|
|
|
|
|
|
|
|
|
|
| 297 |
}
|
| 298 |
if (lvlCache.size > 2000) {
|
| 299 |
for (const [k, v] of lvlCache) {
|
| 300 |
if (!v || now - v.ts > LVL_TTL * 4) lvlCache.delete(k);
|
| 301 |
}
|
| 302 |
}
|
| 303 |
+
if (durCache.size > 800) {
|
| 304 |
+
const ids = [...durCache.keys()].slice(0, durCache.size - 600);
|
| 305 |
+
for (const id of ids) durCache.delete(id);
|
| 306 |
+
}
|
| 307 |
if (rateMap.size > 4000) {
|
| 308 |
const slot = Math.floor(now / 60000);
|
| 309 |
for (const k of rateMap.keys()) {
|
|
|
|
| 318 |
}
|
| 319 |
}
|
| 320 |
|
| 321 |
+
function hotBytes() {
|
| 322 |
+
let n = 0;
|
| 323 |
+
for (const h of hotMap.values()) {
|
| 324 |
+
if (h.head) n += h.head.length;
|
| 325 |
+
if (h.tail) n += h.tail.length;
|
| 326 |
+
}
|
| 327 |
+
return n;
|
| 328 |
+
}
|
| 329 |
+
|
| 330 |
+
function trimHot() {
|
| 331 |
+
if (hotMap.size <= 8) return;
|
| 332 |
+
let bytes = hotBytes();
|
| 333 |
+
if (bytes <= HOT_BYTES_MAX && hotMap.size <= 96) return;
|
| 334 |
+
const arr = [...hotMap.entries()].sort((a, b) => (a[1].ts || 0) - (b[1].ts || 0));
|
| 335 |
+
for (let i = 0; i < arr.length - 8; i++) {
|
| 336 |
+
if (bytes <= HOT_BYTES_MAX && hotMap.size <= 64) break;
|
| 337 |
+
const h = arr[i][1];
|
| 338 |
+
if (h.head) bytes -= h.head.length;
|
| 339 |
+
if (h.tail) bytes -= h.tail.length;
|
| 340 |
+
hotMap.delete(arr[i][0]);
|
| 341 |
+
}
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
function withTime(p, ms, fallback) {
|
| 345 |
let t;
|
| 346 |
return Promise.race([
|
|
|
|
| 673 |
}
|
| 674 |
}
|
| 675 |
|
| 676 |
+
function recFromMsg(msg, botIdx) {
|
| 677 |
+
if (!msg || msg.deleted) return null;
|
|
|
|
|
|
|
| 678 |
const doc = msg.video || msg.document;
|
| 679 |
+
if (!doc) return null;
|
| 680 |
let name = "video.mp4";
|
| 681 |
let duration = 0;
|
| 682 |
const attrs = doc.attributes || [];
|
|
|
|
| 685 |
const n = Number(a && a.duration);
|
| 686 |
if (Number.isFinite(n) && n > duration) duration = n;
|
| 687 |
}
|
| 688 |
+
const cached = durCache.get(String(msg.id));
|
| 689 |
if (cached > 1) duration = cached;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 690 |
const rec = {
|
| 691 |
+
loc: new Api.InputDocumentFileLocation({
|
| 692 |
+
id: doc.id,
|
| 693 |
+
accessHash: doc.accessHash,
|
| 694 |
+
fileReference: doc.fileReference,
|
| 695 |
+
thumbSize: ""
|
| 696 |
+
}),
|
| 697 |
size: Number(doc.size || 0),
|
| 698 |
mime: doc.mimeType || "video/mp4",
|
| 699 |
name,
|
|
|
|
| 704 |
doc,
|
| 705 |
ts: Date.now()
|
| 706 |
};
|
| 707 |
+
if (duration > 1) durCache.set(String(msg.id), duration);
|
| 708 |
+
if (botIdx != null) fileCache.set(String(msg.id) + ":" + botIdx, rec);
|
| 709 |
return rec;
|
| 710 |
}
|
| 711 |
|
| 712 |
+
function normFile(name) {
|
| 713 |
+
return String(name || "")
|
| 714 |
+
.replace(/\.[a-z0-9]{2,5}$/i, "")
|
| 715 |
+
.toLowerCase()
|
| 716 |
+
.replace(/[._]+/g, " ")
|
| 717 |
+
.replace(/\s+/g, " ")
|
| 718 |
+
.trim();
|
| 719 |
+
}
|
| 720 |
+
|
| 721 |
+
function seriesFamily(name) {
|
| 722 |
+
const n = normFile(name);
|
| 723 |
+
const seasonEp = n.match(/^(.*?s\s*\d+)\s*e\s*\d+\b/);
|
| 724 |
+
if (seasonEp) return seasonEp[1].replace(/\s+/g, " ").trim();
|
| 725 |
+
const ep = n.replace(/\b(?:e|ep|episode)\s*\d+\b/g, " ");
|
| 726 |
+
return ep.replace(/\b(?:1080p|720p|480p|2160p|4k|full\s*hd|hd)\b/g, " ").replace(/\s+/g, " ").trim();
|
| 727 |
+
}
|
| 728 |
+
|
| 729 |
+
function episodeNum(name) {
|
| 730 |
+
const n = normFile(name);
|
| 731 |
+
const m = n.match(/\be\s*(\d+)\b/) || n.match(/\b(?:ep|episode)\s*(\d+)\b/);
|
| 732 |
+
return m ? Number(m[1]) : 0;
|
| 733 |
+
}
|
| 734 |
+
|
| 735 |
+
async function loadFile(bot, msgId) {
|
| 736 |
+
const msgs = await bot.client.getMessages(CHANNEL_ID, { ids: msgId });
|
| 737 |
+
const rec = recFromMsg(msgs && msgs[0], bot.idx);
|
| 738 |
+
if (!rec) {
|
| 739 |
+
const msg = msgs && msgs[0];
|
| 740 |
+
if (!msg || msg.deleted) throw new Error("missing");
|
| 741 |
+
throw new Error("media");
|
| 742 |
+
}
|
| 743 |
+
return rec;
|
| 744 |
+
}
|
| 745 |
+
|
| 746 |
+
async function expandFamily(bot, seedId) {
|
| 747 |
+
const seed = await fileFromMsg(bot, seedId);
|
| 748 |
+
const fam = seriesFamily(seed.name);
|
| 749 |
+
const lo = Math.max(1, seedId - 30);
|
| 750 |
+
const hi = seedId + 40;
|
| 751 |
+
const ids = [];
|
| 752 |
+
for (let i = lo; i <= hi; i++) ids.push(i);
|
| 753 |
+
let msgs = [];
|
| 754 |
+
try {
|
| 755 |
+
msgs = await bot.client.getMessages(CHANNEL_ID, { ids });
|
| 756 |
+
} catch {
|
| 757 |
+
return [{ msgId: seedId, rec: seed }];
|
| 758 |
+
}
|
| 759 |
+
const hits = [];
|
| 760 |
+
const seen = new Set();
|
| 761 |
+
for (const msg of msgs || []) {
|
| 762 |
+
const rec = recFromMsg(msg, bot.idx);
|
| 763 |
+
if (!rec || !msg) continue;
|
| 764 |
+
if (fam && seriesFamily(rec.name) !== fam) continue;
|
| 765 |
+
if (seen.has(msg.id)) continue;
|
| 766 |
+
seen.add(msg.id);
|
| 767 |
+
hits.push({ msgId: msg.id, rec });
|
| 768 |
+
}
|
| 769 |
+
if (!hits.some((h) => h.msgId === seedId)) hits.push({ msgId: seedId, rec: seed });
|
| 770 |
+
hits.sort((a, b) => {
|
| 771 |
+
const ea = episodeNum(a.rec.name);
|
| 772 |
+
const eb = episodeNum(b.rec.name);
|
| 773 |
+
if (ea && eb && ea !== eb) return ea - eb;
|
| 774 |
+
return a.msgId - b.msgId;
|
| 775 |
+
});
|
| 776 |
+
return hits.slice(0, 40);
|
| 777 |
+
}
|
| 778 |
+
|
| 779 |
async function fileFromMsg(bot, msgId) {
|
| 780 |
const key = String(msgId) + ":" + bot.idx;
|
| 781 |
const hit = fileCache.get(key);
|
|
|
|
| 917 |
}
|
| 918 |
|
| 919 |
const LVL_PCT = { anonymous: 30, registered: 60, subscribed: 100 };
|
| 920 |
+
const INDEX_TAIL = 512 * 1024;
|
| 921 |
|
| 922 |
function lvlPct(l) {
|
| 923 |
const p = LVL_PCT[String(l || "")];
|
|
|
|
| 941 |
function previewTailLen(size, maxEnd) {
|
| 942 |
const unpaid = size - (maxEnd + 1);
|
| 943 |
if (unpaid <= 0) return 0;
|
| 944 |
+
const keep = Math.max(Math.floor(size * 0.12), 768 * 1024);
|
| 945 |
+
if (unpaid <= keep) return 0;
|
| 946 |
+
return Math.min(INDEX_TAIL, unpaid - keep);
|
|
|
|
| 947 |
}
|
| 948 |
|
| 949 |
function applyPreview(rng, size, l) {
|
|
|
|
| 1031 |
"Content-Length": String(rng.end - rng.start + 1),
|
| 1032 |
"Content-Range": "bytes " + rng.start + "-" + rng.end + "/" + size
|
| 1033 |
});
|
|
|
|
| 1034 |
return headers;
|
| 1035 |
}
|
| 1036 |
|
|
|
|
| 1107 |
h.tailOff = from;
|
| 1108 |
}
|
| 1109 |
}
|
| 1110 |
+
trimHot();
|
| 1111 |
}
|
| 1112 |
|
| 1113 |
function hotHeadLen(msgId) {
|
|
|
|
| 1313 |
const rec = got.rec;
|
| 1314 |
if (rec.hash && rec.hash !== hash) return json(res, 400, { error: "hash" });
|
| 1315 |
warmHot(bot, rec, msgId);
|
| 1316 |
+
fillDuration(bot, rec, msgId).catch(() => {});
|
| 1317 |
+
const wantHead = Math.min(131072, rec.size || 0);
|
| 1318 |
+
if (wantHead > 0) await waitHotHead(msgId, wantHead, 80);
|
| 1319 |
const cachedDur = durCache.get(String(msgId));
|
| 1320 |
if (cachedDur > 1) rec.duration = cachedDur;
|
| 1321 |
const playMime = httpVideoType(rec.mime, rec.name);
|
| 1322 |
+
const durSec = Number(rec.duration) > 1 ? Math.floor(Number(rec.duration)) : 0;
|
| 1323 |
const exp = Math.floor(Date.now() / 1000) + TOKEN_TTL;
|
| 1324 |
+
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 });
|
| 1325 |
const host = "https://" + (req.headers.host || "localhost");
|
| 1326 |
json(res, 200, {
|
| 1327 |
url: host + "/stream/" + msgId + playExt(playMime, rec.name) + "?k=" + k,
|
| 1328 |
fileSize: rec.size,
|
| 1329 |
mime: playMime,
|
| 1330 |
+
duration: durSec,
|
| 1331 |
+
previewPct: lvlPct(lvl),
|
| 1332 |
accessLevel: lvl,
|
| 1333 |
expiry: exp
|
| 1334 |
});
|
|
|
|
| 1350 |
return res.end();
|
| 1351 |
}
|
| 1352 |
const ip = clientIp(req);
|
| 1353 |
+
if (!takeStream(ip, ticket.u)) {
|
| 1354 |
res.setHeader("Retry-After", "1");
|
| 1355 |
res.writeHead(429);
|
| 1356 |
return res.end();
|
|
|
|
| 1446 |
}
|
| 1447 |
}
|
| 1448 |
} finally {
|
| 1449 |
+
freeStream(ip, ticket && ticket.u);
|
| 1450 |
}
|
| 1451 |
}
|
| 1452 |
|
|
|
|
| 1769 |
role: ROLE,
|
| 1770 |
bots: bots.filter((b) => botUsable(b) || (b.ready && !(b.coolUntil && Date.now() < b.coolUntil))).length,
|
| 1771 |
load: bots.reduce((n, b) => n + b.load, 0),
|
| 1772 |
+
cap: bots.filter((b) => b.ready).length * BOT_MAX,
|
| 1773 |
+
pipes: livePipes
|
| 1774 |
});
|
| 1775 |
}
|
| 1776 |
|
|
|
|
| 1846 |
const posts = parseTgPosts(text);
|
| 1847 |
if (!posts.length) return json(res, 400, { error: "no_links" });
|
| 1848 |
const expect = chanDigits(CHANNEL_ID);
|
| 1849 |
+
if (expect && posts.some((p) => p.chan && p.chan !== expect)) {
|
| 1850 |
+
return json(res, 400, { error: "wrong_channel" });
|
| 1851 |
+
}
|
| 1852 |
+
let jobs = posts.map((p) => ({ msgId: p.msgId, rec: null }));
|
| 1853 |
+
if (posts.length === 1) {
|
| 1854 |
+
try {
|
| 1855 |
+
const bot = pickBot(undefined, undefined, posts[0].msgId);
|
| 1856 |
+
if (!bot) throw new Error("file");
|
| 1857 |
+
jobs = await expandFamily(bot, posts[0].msgId);
|
| 1858 |
+
} catch {
|
| 1859 |
+
jobs = [{ msgId: posts[0].msgId, rec: null }];
|
| 1860 |
}
|
| 1861 |
+
}
|
| 1862 |
+
const items = [];
|
| 1863 |
+
for (const job of jobs) {
|
| 1864 |
+
const link = "https://t.me/c/" + expect + "/" + job.msgId;
|
| 1865 |
try {
|
| 1866 |
+
let rec = job.rec;
|
| 1867 |
+
if (!rec) {
|
| 1868 |
+
const got = await fileFromPool(job.msgId, pickBot(undefined, undefined, job.msgId));
|
| 1869 |
+
rec = got.rec;
|
| 1870 |
+
}
|
| 1871 |
+
const pathStr = "/stream/" + job.msgId;
|
| 1872 |
+
const ep = episodeNum(rec.name);
|
| 1873 |
items.push({
|
| 1874 |
ok: true,
|
| 1875 |
+
msgId: job.msgId,
|
| 1876 |
+
link,
|
| 1877 |
name: rec.name || "video.mp4",
|
| 1878 |
size: rec.size || 0,
|
| 1879 |
mime: rec.mime || "video/mp4",
|
| 1880 |
duration: rec.duration || 0,
|
| 1881 |
hash: rec.hash,
|
| 1882 |
+
ep,
|
| 1883 |
path: pathStr,
|
| 1884 |
dataP: encDlK(pathStr),
|
| 1885 |
dataH: encDlK(rec.hash)
|
|
|
|
| 1887 |
} catch (e) {
|
| 1888 |
items.push({
|
| 1889 |
ok: false,
|
| 1890 |
+
msgId: job.msgId,
|
| 1891 |
+
link,
|
| 1892 |
error: /missing/i.test(errText(e)) ? "missing" : (/media/i.test(errText(e)) ? "media" : "lookup")
|
| 1893 |
});
|
| 1894 |
}
|
|
|
|
| 1895 |
}
|
| 1896 |
json(res, 200, { ok: true, channel: expect, items });
|
| 1897 |
}
|
|
|
|
| 1974 |
setInterval(() => { reviveBots().catch(() => {}); }, 20000);
|
| 1975 |
const srv = http.createServer({
|
| 1976 |
keepAlive: true,
|
| 1977 |
+
keepAliveTimeout: 25000,
|
| 1978 |
+
headersTimeout: 28000,
|
| 1979 |
requestTimeout: 0,
|
| 1980 |
+
connectionsCheckingInterval: 15000
|
| 1981 |
}, onReq);
|
| 1982 |
+
srv.maxConnections = 200;
|
| 1983 |
srv.on("clientError", (err, sock) => {
|
| 1984 |
try {
|
| 1985 |
if (sock.writable) sock.end("HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n");
|