Spaces:
Running
Running
JOHN SILLVESTOR commited on
fix stream bots duration origin
Browse files
server.js
CHANGED
|
@@ -16,7 +16,7 @@ const HASH_LEN = 6;
|
|
| 16 |
const TOKEN_TTL = 600;
|
| 17 |
const MIN_ALIGN = 4096;
|
| 18 |
const MB = 1024 * 1024;
|
| 19 |
-
const HTTP_WINDOW = MB;
|
| 20 |
const GET_SIZES = [524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096];
|
| 21 |
const FIRST_GET = 131072;
|
| 22 |
const HOT_HEAD = 524288;
|
|
@@ -32,10 +32,18 @@ const FB_DB = String(process.env.FIREBASE_DB_URL || "").replace(/\/?$/, "/");
|
|
| 32 |
const FB_SECRET = process.env.FIREBASE_DB_SECRET || "";
|
| 33 |
const STRIPE_KEY = process.env.STRIPE_SECRET_KEY || "";
|
| 34 |
const STRIPE_WH = process.env.STRIPE_WEBHOOK_SECRET || "";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
const SPACE_URLS = String(process.env.SPACE_URLS || "")
|
| 36 |
.split(",")
|
| 37 |
.map((s) => s.trim())
|
| 38 |
.filter(Boolean);
|
|
|
|
| 39 |
|
| 40 |
const PLANS = {
|
| 41 |
day: { amount: 99, label: "1 Day VIP", sec: 86400, price: "0.99" },
|
|
@@ -51,6 +59,7 @@ const hmacKey = STREAM_SECRET
|
|
| 51 |
: null;
|
| 52 |
|
| 53 |
const bots = [];
|
|
|
|
| 54 |
const fileCache = new Map();
|
| 55 |
const inflightFile = new Map();
|
| 56 |
const hotMap = new Map();
|
|
@@ -286,8 +295,8 @@ function isBlogOrigin(o) {
|
|
| 286 |
}
|
| 287 |
|
| 288 |
function fromBlog(req) {
|
| 289 |
-
const o = String(req.headers.origin || "");
|
| 290 |
-
if (o) return isBlogOrigin(o);
|
| 291 |
const r = String(req.headers.referer || "");
|
| 292 |
if (!r) return false;
|
| 293 |
try {
|
|
@@ -446,24 +455,56 @@ function pickBot(preferIdx, skipIdx, msgId) {
|
|
| 446 |
return bot;
|
| 447 |
}
|
| 448 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 449 |
async function startBot(token, idx) {
|
|
|
|
|
|
|
| 450 |
const client = new TelegramClient(new StringSession(""), API_ID, API_HASH, {
|
| 451 |
-
connectionRetries:
|
| 452 |
reconnectRetries: Infinity,
|
| 453 |
retryDelay: 1500,
|
| 454 |
autoReconnect: true,
|
| 455 |
useWSS: true,
|
| 456 |
-
floodSleepThreshold:
|
| 457 |
requestRetries: 3,
|
| 458 |
downloadRetries: 3,
|
| 459 |
maxConcurrentDownloads: 16,
|
| 460 |
-
downloadPool: { poolSize: 4, workers: 8, requestDeadlineMs:
|
| 461 |
});
|
| 462 |
client.setLogLevel("error");
|
| 463 |
-
const bot = { client, token, idx, ready: false, load: 0, coolUntil: 0 };
|
| 464 |
await client.start({ botAuthToken: token });
|
| 465 |
-
|
| 466 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
}
|
| 468 |
|
| 469 |
function dropFile(msgId, bot) {
|
|
@@ -538,7 +579,7 @@ async function fileFromPool(msgId, prefer, skipIdx) {
|
|
| 538 |
const first = prefer && botUsable(prefer) && prefer.idx !== skipIdx ? prefer : pickBot(prefer && prefer.idx, skipIdx, msgId);
|
| 539 |
if (first) order.push(first);
|
| 540 |
for (const b of bots) {
|
| 541 |
-
if (order.length >=
|
| 542 |
if (order.includes(b) || !botUsable(b) || b.idx === skipIdx) continue;
|
| 543 |
order.push(b);
|
| 544 |
}
|
|
@@ -816,7 +857,7 @@ async function* rangeChunks(bot, rec, start, end, stop) {
|
|
| 816 |
first = false;
|
| 817 |
const take = Math.min(remain, reqSize - skip);
|
| 818 |
if (take <= 0) break;
|
| 819 |
-
const it = bot.client.iterDownload(rec.
|
| 820 |
offset: aligned,
|
| 821 |
limit: skip + take,
|
| 822 |
requestSize: reqSize,
|
|
@@ -931,7 +972,10 @@ async function handleGenerate(req, res, url) {
|
|
| 931 |
const cachedDur = durCache.get(String(msgId));
|
| 932 |
if (cachedDur > 1) rec.duration = cachedDur;
|
| 933 |
warmHot(bot, rec, msgId).catch(function () {});
|
| 934 |
-
if (!(rec.duration > 1))
|
|
|
|
|
|
|
|
|
|
| 935 |
const playMime = httpVideoType(rec.mime, rec.name);
|
| 936 |
const exp = Math.floor(Date.now() / 1000) + TOKEN_TTL;
|
| 937 |
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) });
|
|
@@ -1236,10 +1280,7 @@ async function handleBootstrap(req, res, url) {
|
|
| 1236 |
|
| 1237 |
function handleHealth(res) {
|
| 1238 |
sweepMaps();
|
| 1239 |
-
|
| 1240 |
-
if (b.client && b.client.connected === false) b.ready = false;
|
| 1241 |
-
else if (b.client && b.client.connected !== false && !b.ready && !(b.coolUntil && Date.now() < b.coolUntil)) b.ready = true;
|
| 1242 |
-
}
|
| 1243 |
json(res, 200, {
|
| 1244 |
ok: true,
|
| 1245 |
role: ROLE,
|
|
@@ -1264,6 +1305,7 @@ function handleHome(req, res) {
|
|
| 1264 |
async function onReq(req, res) {
|
| 1265 |
req.on("error", () => {});
|
| 1266 |
res.on("error", () => {});
|
|
|
|
| 1267 |
cors(req, res);
|
| 1268 |
if (req.method === "OPTIONS") {
|
| 1269 |
res.writeHead(204);
|
|
@@ -1307,6 +1349,7 @@ async function main() {
|
|
| 1307 |
process.exit(1);
|
| 1308 |
}
|
| 1309 |
for (let i = 0; i < tokens.length; i++) {
|
|
|
|
| 1310 |
try {
|
| 1311 |
await startBot(tokens[i], i);
|
| 1312 |
} catch (e) {
|
|
@@ -1317,6 +1360,7 @@ async function main() {
|
|
| 1317 |
process.stderr.write("no bots online\n");
|
| 1318 |
process.exit(1);
|
| 1319 |
}
|
|
|
|
| 1320 |
const srv = http.createServer({
|
| 1321 |
keepAlive: true,
|
| 1322 |
keepAliveTimeout: 65000,
|
|
|
|
| 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;
|
|
|
|
| 32 |
const FB_SECRET = process.env.FIREBASE_DB_SECRET || "";
|
| 33 |
const STRIPE_KEY = process.env.STRIPE_SECRET_KEY || "";
|
| 34 |
const STRIPE_WH = process.env.STRIPE_WEBHOOK_SECRET || "";
|
| 35 |
+
const SPACE_FALLBACK = [
|
| 36 |
+
"https://dublk2000-dublk-s1.hf.space",
|
| 37 |
+
"https://dublk2000-dublk-s2.hf.space",
|
| 38 |
+
"https://activenodes-dublk-s3.hf.space",
|
| 39 |
+
"https://activenodes-dublk-s4.hf.space",
|
| 40 |
+
"https://activenodes-dublk-s5.hf.space"
|
| 41 |
+
];
|
| 42 |
const SPACE_URLS = String(process.env.SPACE_URLS || "")
|
| 43 |
.split(",")
|
| 44 |
.map((s) => s.trim())
|
| 45 |
.filter(Boolean);
|
| 46 |
+
if (!SPACE_URLS.length) SPACE_URLS.push(...SPACE_FALLBACK);
|
| 47 |
|
| 48 |
const PLANS = {
|
| 49 |
day: { amount: 99, label: "1 Day VIP", sec: 86400, price: "0.99" },
|
|
|
|
| 59 |
: null;
|
| 60 |
|
| 61 |
const bots = [];
|
| 62 |
+
const botSlots = [];
|
| 63 |
const fileCache = new Map();
|
| 64 |
const inflightFile = new Map();
|
| 65 |
const hotMap = new Map();
|
|
|
|
| 295 |
}
|
| 296 |
|
| 297 |
function fromBlog(req) {
|
| 298 |
+
const o = String(req.headers.origin || "").trim();
|
| 299 |
+
if (o && o.toLowerCase() !== "null") return isBlogOrigin(o);
|
| 300 |
const r = String(req.headers.referer || "");
|
| 301 |
if (!r) return false;
|
| 302 |
try {
|
|
|
|
| 455 |
return bot;
|
| 456 |
}
|
| 457 |
|
| 458 |
+
function sweepBots() {
|
| 459 |
+
for (const b of bots) {
|
| 460 |
+
if (b.client && b.client.connected === false) b.ready = false;
|
| 461 |
+
else if (b.client && b.client.connected !== false && !b.ready && !(b.coolUntil && Date.now() < b.coolUntil)) b.ready = true;
|
| 462 |
+
}
|
| 463 |
+
}
|
| 464 |
+
|
| 465 |
async function startBot(token, idx) {
|
| 466 |
+
const existing = bots.find((b) => b.idx === idx);
|
| 467 |
+
if (existing && existing.ready && existing.client && existing.client.connected !== false) return;
|
| 468 |
const client = new TelegramClient(new StringSession(""), API_ID, API_HASH, {
|
| 469 |
+
connectionRetries: 8,
|
| 470 |
reconnectRetries: Infinity,
|
| 471 |
retryDelay: 1500,
|
| 472 |
autoReconnect: true,
|
| 473 |
useWSS: true,
|
| 474 |
+
floodSleepThreshold: 2,
|
| 475 |
requestRetries: 3,
|
| 476 |
downloadRetries: 3,
|
| 477 |
maxConcurrentDownloads: 16,
|
| 478 |
+
downloadPool: { poolSize: 4, workers: 8, requestDeadlineMs: 28000 }
|
| 479 |
});
|
| 480 |
client.setLogLevel("error");
|
|
|
|
| 481 |
await client.start({ botAuthToken: token });
|
| 482 |
+
if (existing) {
|
| 483 |
+
const old = existing.client;
|
| 484 |
+
existing.client = client;
|
| 485 |
+
existing.token = token;
|
| 486 |
+
existing.ready = true;
|
| 487 |
+
existing.load = 0;
|
| 488 |
+
existing.coolUntil = 0;
|
| 489 |
+
if (old && old !== client) {
|
| 490 |
+
try { old.disconnect(); } catch {}
|
| 491 |
+
}
|
| 492 |
+
return;
|
| 493 |
+
}
|
| 494 |
+
bots.push({ client, token, idx, ready: true, load: 0, coolUntil: 0 });
|
| 495 |
+
}
|
| 496 |
+
|
| 497 |
+
async function reviveBots() {
|
| 498 |
+
sweepBots();
|
| 499 |
+
for (const slot of botSlots) {
|
| 500 |
+
const b = bots.find((x) => x.idx === slot.idx);
|
| 501 |
+
if (b && b.ready && b.client && b.client.connected !== false) continue;
|
| 502 |
+
try {
|
| 503 |
+
await startBot(slot.token, slot.idx);
|
| 504 |
+
} catch (e) {
|
| 505 |
+
process.stderr.write("bot " + slot.idx + " revive: " + (e && e.message) + "\n");
|
| 506 |
+
}
|
| 507 |
+
}
|
| 508 |
}
|
| 509 |
|
| 510 |
function dropFile(msgId, bot) {
|
|
|
|
| 579 |
const first = prefer && botUsable(prefer) && prefer.idx !== skipIdx ? prefer : pickBot(prefer && prefer.idx, skipIdx, msgId);
|
| 580 |
if (first) order.push(first);
|
| 581 |
for (const b of bots) {
|
| 582 |
+
if (order.length >= Math.min(8, bots.length || 8)) break;
|
| 583 |
if (order.includes(b) || !botUsable(b) || b.idx === skipIdx) continue;
|
| 584 |
order.push(b);
|
| 585 |
}
|
|
|
|
| 857 |
first = false;
|
| 858 |
const take = Math.min(remain, reqSize - skip);
|
| 859 |
if (take <= 0) break;
|
| 860 |
+
const it = bot.client.iterDownload(rec.loc || rec.doc, {
|
| 861 |
offset: aligned,
|
| 862 |
limit: skip + take,
|
| 863 |
requestSize: reqSize,
|
|
|
|
| 972 |
const cachedDur = durCache.get(String(msgId));
|
| 973 |
if (cachedDur > 1) rec.duration = cachedDur;
|
| 974 |
warmHot(bot, rec, msgId).catch(function () {});
|
| 975 |
+
if (!(rec.duration > 1)) {
|
| 976 |
+
const d = await withTime(fillDuration(bot, rec, msgId), 400, 0);
|
| 977 |
+
if (d > 1) rec.duration = d;
|
| 978 |
+
}
|
| 979 |
const playMime = httpVideoType(rec.mime, rec.name);
|
| 980 |
const exp = Math.floor(Date.now() / 1000) + TOKEN_TTL;
|
| 981 |
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) });
|
|
|
|
| 1280 |
|
| 1281 |
function handleHealth(res) {
|
| 1282 |
sweepMaps();
|
| 1283 |
+
sweepBots();
|
|
|
|
|
|
|
|
|
|
| 1284 |
json(res, 200, {
|
| 1285 |
ok: true,
|
| 1286 |
role: ROLE,
|
|
|
|
| 1305 |
async function onReq(req, res) {
|
| 1306 |
req.on("error", () => {});
|
| 1307 |
res.on("error", () => {});
|
| 1308 |
+
sweepBots();
|
| 1309 |
cors(req, res);
|
| 1310 |
if (req.method === "OPTIONS") {
|
| 1311 |
res.writeHead(204);
|
|
|
|
| 1349 |
process.exit(1);
|
| 1350 |
}
|
| 1351 |
for (let i = 0; i < tokens.length; i++) {
|
| 1352 |
+
botSlots.push({ token: tokens[i], idx: i });
|
| 1353 |
try {
|
| 1354 |
await startBot(tokens[i], i);
|
| 1355 |
} catch (e) {
|
|
|
|
| 1360 |
process.stderr.write("no bots online\n");
|
| 1361 |
process.exit(1);
|
| 1362 |
}
|
| 1363 |
+
setInterval(() => { reviveBots().catch(() => {}); }, 20000);
|
| 1364 |
const srv = http.createServer({
|
| 1365 |
keepAlive: true,
|
| 1366 |
keepAliveTimeout: 65000,
|