Spaces:
Running
Running
DUBLK 2000 Admin commited on
Commit ·
c1089e0
1
Parent(s): 6971087
Fix payment idempotency, key separation, origin binding, egress budget
Browse files
server.js
CHANGED
|
@@ -45,7 +45,9 @@ const BOT_MAX = 25;
|
|
| 45 |
const PIPE_MS = 30000;
|
| 46 |
const PIPE_MAX_MS = 3 * 60 * 60 * 1000;
|
| 47 |
const RANGE_LOOKAHEAD = 8;
|
| 48 |
-
const WARM_AHEAD =
|
|
|
|
|
|
|
| 49 |
const WARM_BATCH = Math.max(1, Math.min(2, CPU_N));
|
| 50 |
const IP_STREAM_MAX = 5000;
|
| 51 |
const IP_TITLE_MAX = 2500;
|
|
@@ -60,7 +62,8 @@ const HLS_SEG_SEC = 6;
|
|
| 60 |
const HLS_IP_MAX = 24;
|
| 61 |
const HLS_BUCKET = 240;
|
| 62 |
const hlsSegCount = new Map();
|
| 63 |
-
const DISK_MAX_BYTES =
|
|
|
|
| 64 |
const HOT_BYTES_MAX = Math.min(1536 * MB, Math.max(512 * MB, Math.floor(RAM_BYTES * 0.1)));
|
| 65 |
const BODY_MAX = 262144;
|
| 66 |
const FILE_TTL = 20 * 60 * 1000;
|
|
@@ -92,12 +95,15 @@ const HOME_URL = "https://dublk2000.blogspot.com/";
|
|
| 92 |
const BRAND_FILE = path.join(__dirname, "brand.png");
|
| 93 |
const BRAND_URL = (SPACE_URLS[0] || SPACE_FALLBACK[0]) + "/public/brand.png";
|
| 94 |
|
| 95 |
-
const
|
| 96 |
-
? crypto.hkdfSync("sha256", Buffer.from(STREAM_SECRET, "utf8"), Buffer.from("dublk2000"), Buffer.from(
|
| 97 |
-
: null;
|
| 98 |
-
const hmacKey = STREAM_SECRET
|
| 99 |
-
? crypto.hkdfSync("sha256", Buffer.from(STREAM_SECRET, "utf8"), Buffer.from("dublk2000"), Buffer.from("hmac-sha256-ticket"), 32)
|
| 100 |
: null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
const bots = [];
|
| 103 |
const botSlots = [];
|
|
@@ -636,7 +642,7 @@ async function verifyIdToken(raw) {
|
|
| 636 |
}
|
| 637 |
if (header.alg !== "RS256" || !header.kid) return null;
|
| 638 |
const certs = await googleCerts();
|
| 639 |
-
const pem = certs[header.kid];
|
| 640 |
if (!pem) return null;
|
| 641 |
const ok = crypto.createVerify("SHA256").update(parts[0] + "." + parts[1]).verify(pem, parts[2], "base64url");
|
| 642 |
if (!ok) return null;
|
|
@@ -1427,9 +1433,9 @@ function hlsBucket() {
|
|
| 1427 |
}
|
| 1428 |
|
| 1429 |
function hlsSig(msgId, hash, lvl, bucket) {
|
| 1430 |
-
if (!
|
| 1431 |
return crypto
|
| 1432 |
-
.createHmac("sha256",
|
| 1433 |
.update("hls:" + String(msgId) + ":" + String(hash) + ":" + String(lvl) + ":" + String(bucket))
|
| 1434 |
.digest("hex")
|
| 1435 |
.slice(0, 24);
|
|
@@ -1829,6 +1835,14 @@ async function writeDiskSlice(msgId, offset, buffer) {
|
|
| 1829 |
try {
|
| 1830 |
const file = sliceFilePath(msgId, offset);
|
| 1831 |
await fs.promises.mkdir(path.dirname(file), { recursive: true });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1832 |
await fs.promises.writeFile(file, buffer);
|
| 1833 |
diskBytesUsed += buffer.length;
|
| 1834 |
if (diskBytesUsed > DISK_MAX_BYTES) {
|
|
@@ -2041,17 +2055,23 @@ async function pipeDiskRange(msgId, start, end, req, res, headers, size) {
|
|
| 2041 |
}
|
| 2042 |
|
| 2043 |
const warmRun = new Set();
|
| 2044 |
-
function kickWarmAhead(bot, rec, msgId, fromOff, fileSize) {
|
| 2045 |
const id = String(msgId);
|
|
|
|
|
|
|
|
|
|
| 2046 |
const start = Math.floor(Math.max(0, fromOff) / CHUNK_SIZE) * CHUNK_SIZE;
|
| 2047 |
const warmKey = id + ":" + Math.floor(start / (16 * MB));
|
| 2048 |
-
if (!bot || !rec || !id ||
|
|
|
|
|
|
|
| 2049 |
warmRun.add(warmKey);
|
|
|
|
| 2050 |
const cap = Number(fileSize) || 0;
|
| 2051 |
(async () => {
|
| 2052 |
try {
|
| 2053 |
const jobs = [];
|
| 2054 |
-
for (let i = 0; i <
|
| 2055 |
const off = start + i * CHUNK_SIZE;
|
| 2056 |
if (cap && off >= cap) break;
|
| 2057 |
if (getSliceMem(id, off)) continue;
|
|
@@ -2068,6 +2088,7 @@ function kickWarmAhead(bot, rec, msgId, fromOff, fileSize) {
|
|
| 2068 |
} catch { }
|
| 2069 |
finally {
|
| 2070 |
warmRun.delete(warmKey);
|
|
|
|
| 2071 |
}
|
| 2072 |
})();
|
| 2073 |
}
|
|
@@ -2215,7 +2236,7 @@ async function pipeRange(bot, rec, start, end, req, res, status, headers, msgId)
|
|
| 2215 |
return;
|
| 2216 |
}
|
| 2217 |
}
|
| 2218 |
-
kickWarmAhead(bot, rec, msgId, start, rec.size || (end + 1));
|
| 2219 |
if (pipeDead(req, res)) return;
|
| 2220 |
res.writeHead(status, headers);
|
| 2221 |
if (typeof res.flushHeaders === "function") res.flushHeaders();
|
|
@@ -2455,7 +2476,7 @@ async function handleStream(req, res, url, msgId, kHex, ticketObj) {
|
|
| 2455 |
return res.end();
|
| 2456 |
}
|
| 2457 |
} else {
|
| 2458 |
-
if (!netOk
|
| 2459 |
res.writeHead(403);
|
| 2460 |
return res.end();
|
| 2461 |
}
|
|
@@ -2789,18 +2810,21 @@ async function grantVip(uid, planKey) {
|
|
| 2789 |
const plan = PLANS[planKey];
|
| 2790 |
if (!plan || !uidOk(uid)) throw new Error("grant");
|
| 2791 |
const now = Math.floor(Date.now() / 1000);
|
|
|
|
| 2792 |
let start = now;
|
| 2793 |
try {
|
| 2794 |
const old = await rtdb("users/" + uid + "/subscription", "GET");
|
| 2795 |
const oldExp = vipExpirySec(old);
|
| 2796 |
if (vipActive(old) && oldExp > start) start = oldExp;
|
| 2797 |
} catch { }
|
|
|
|
|
|
|
| 2798 |
const sub = {
|
| 2799 |
status: "active",
|
| 2800 |
plan: planKey,
|
| 2801 |
label: plan.label,
|
| 2802 |
price: plan.price,
|
| 2803 |
-
expiry:
|
| 2804 |
updatedAt: now
|
| 2805 |
};
|
| 2806 |
await rtdb("users/" + uid + "/subscription", "PUT", sub);
|
|
@@ -2842,39 +2866,46 @@ async function orderSeen(id) {
|
|
| 2842 |
return false;
|
| 2843 |
}
|
| 2844 |
|
| 2845 |
-
async function markOrder(id, uid, planKey) {
|
| 2846 |
paidOrders.set(id, Date.now());
|
|
|
|
|
|
|
| 2847 |
try {
|
| 2848 |
-
await rtdb("processed_orders/" + id, "PUT",
|
| 2849 |
-
t: Math.floor(Date.now() / 1000),
|
| 2850 |
-
uid: uid,
|
| 2851 |
-
plan: planKey
|
| 2852 |
-
});
|
| 2853 |
} catch { }
|
| 2854 |
}
|
| 2855 |
|
| 2856 |
-
async function claimOrder(id, uid, planKey) {
|
| 2857 |
-
const
|
| 2858 |
-
|
| 2859 |
-
|
| 2860 |
-
|
| 2861 |
-
|
| 2862 |
-
|
| 2863 |
-
|
| 2864 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2865 |
try {
|
| 2866 |
await rtdb("processed_orders/" + id, "PUT", rec);
|
| 2867 |
-
return "claimed";
|
| 2868 |
} catch { }
|
| 2869 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2870 |
}
|
|
|
|
|
|
|
| 2871 |
}
|
| 2872 |
|
| 2873 |
async function fulfillPaid(ses) {
|
| 2874 |
if (!ses || ses.object !== "checkout.session") return false;
|
| 2875 |
if (ses.mode !== "payment" || ses.payment_status !== "paid") return false;
|
| 2876 |
if (ses.status === "expired") return false;
|
| 2877 |
-
if (STRIPE_KEY.indexOf("
|
| 2878 |
const uid = ses.metadata && ses.metadata.uid;
|
| 2879 |
const planKey = ses.metadata && ses.metadata.plan;
|
| 2880 |
const plan = PLANS[planKey];
|
|
@@ -2885,22 +2916,11 @@ async function fulfillPaid(ses) {
|
|
| 2885 |
const sid = String(ses.id || "");
|
| 2886 |
if (!/^cs_[A-Za-z0-9_]+$/.test(sid)) return false;
|
| 2887 |
return withPayLock(sid, async () => {
|
| 2888 |
-
const
|
| 2889 |
-
if (
|
| 2890 |
-
|
| 2891 |
-
|
| 2892 |
-
|
| 2893 |
-
await markOrder(sid, uid, planKey);
|
| 2894 |
-
return true;
|
| 2895 |
-
} catch {
|
| 2896 |
-
try { await rtdb("processed_orders/" + sid, "DELETE"); } catch { }
|
| 2897 |
-
paidOrders.delete(sid);
|
| 2898 |
-
return false;
|
| 2899 |
-
}
|
| 2900 |
-
}
|
| 2901 |
-
if (await orderSeen(sid)) return true;
|
| 2902 |
-
await grantVip(uid, planKey);
|
| 2903 |
-
await markOrder(sid, uid, planKey);
|
| 2904 |
return true;
|
| 2905 |
});
|
| 2906 |
}
|
|
@@ -2908,30 +2928,23 @@ async function fulfillPaid(ses) {
|
|
| 2908 |
async function stripeEventSeen(id) {
|
| 2909 |
if (!id) return true;
|
| 2910 |
if (stripeEvents.has(id)) return true;
|
|
|
|
| 2911 |
try {
|
| 2912 |
-
await rtdb("stripe_events/" + id, "
|
| 2913 |
-
|
| 2914 |
-
|
| 2915 |
-
|
| 2916 |
-
stripeEvents.delete(first);
|
| 2917 |
-
}
|
| 2918 |
-
return false;
|
| 2919 |
-
} catch (e) {
|
| 2920 |
-
if (String(e && e.message || "").indexOf("409") >= 0) {
|
| 2921 |
-
stripeEvents.add(id);
|
| 2922 |
-
return true;
|
| 2923 |
-
}
|
| 2924 |
try {
|
| 2925 |
-
|
| 2926 |
-
|
| 2927 |
-
stripeEvents.add(id);
|
| 2928 |
-
return true;
|
| 2929 |
-
}
|
| 2930 |
-
stripeEvents.add(id);
|
| 2931 |
-
return false;
|
| 2932 |
} catch { }
|
| 2933 |
-
return false;
|
| 2934 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2935 |
}
|
| 2936 |
|
| 2937 |
async function handleStripeWebhook(req, res) {
|
|
@@ -3030,12 +3043,12 @@ function chanDigits(id) {
|
|
| 3030 |
}
|
| 3031 |
|
| 3032 |
function adminKeyOk(req, url) {
|
| 3033 |
-
if (!
|
| 3034 |
const h = String(req.headers.authorization || "").replace(/^Bearer\s+/i, "").trim();
|
| 3035 |
const x = String(req.headers["x-admin-key"] || "").trim();
|
| 3036 |
const got = h || x;
|
| 3037 |
-
if (!got || got.length !==
|
| 3038 |
-
return timingSafeEq(got,
|
| 3039 |
}
|
| 3040 |
|
| 3041 |
function parseTgPosts(text) {
|
|
@@ -3201,6 +3214,11 @@ async function main() {
|
|
| 3201 |
process.stderr.write("missing required settings\n");
|
| 3202 |
process.exit(1);
|
| 3203 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3204 |
const tokens = String(process.env.BOT_TOKENS || "")
|
| 3205 |
.split(/[\s,]+/)
|
| 3206 |
.map((t) => t.trim())
|
|
|
|
| 45 |
const PIPE_MS = 30000;
|
| 46 |
const PIPE_MAX_MS = 3 * 60 * 60 * 1000;
|
| 47 |
const RANGE_LOOKAHEAD = 8;
|
| 48 |
+
const WARM_AHEAD = 8;
|
| 49 |
+
const WARM_BUDGET_BYTES = 64 * MB;
|
| 50 |
+
let warmBytesInFlight = 0;
|
| 51 |
const WARM_BATCH = Math.max(1, Math.min(2, CPU_N));
|
| 52 |
const IP_STREAM_MAX = 5000;
|
| 53 |
const IP_TITLE_MAX = 2500;
|
|
|
|
| 62 |
const HLS_IP_MAX = 24;
|
| 63 |
const HLS_BUCKET = 240;
|
| 64 |
const hlsSegCount = new Map();
|
| 65 |
+
const DISK_MAX_BYTES = 8 * 1024 * 1024 * 1024;
|
| 66 |
+
const DISK_FREE_MIN_BYTES = 3 * 1024 * 1024 * 1024;
|
| 67 |
const HOT_BYTES_MAX = Math.min(1536 * MB, Math.max(512 * MB, Math.floor(RAM_BYTES * 0.1)));
|
| 68 |
const BODY_MAX = 262144;
|
| 69 |
const FILE_TTL = 20 * 60 * 1000;
|
|
|
|
| 95 |
const BRAND_FILE = path.join(__dirname, "brand.png");
|
| 96 |
const BRAND_URL = (SPACE_URLS[0] || SPACE_FALLBACK[0]) + "/public/brand.png";
|
| 97 |
|
| 98 |
+
const deriveKey = STREAM_SECRET
|
| 99 |
+
? (info) => crypto.hkdfSync("sha256", Buffer.from(STREAM_SECRET, "utf8"), Buffer.from("dublk2000"), Buffer.from(info), 32)
|
|
|
|
|
|
|
|
|
|
| 100 |
: null;
|
| 101 |
+
const aesKey = deriveKey ? deriveKey("aes-256-gcm-ticket") : null;
|
| 102 |
+
const hmacKey = deriveKey ? deriveKey("hmac-sha256-ticket") : null;
|
| 103 |
+
const hlsKey = deriveKey ? deriveKey("hls-sig-v1") : null;
|
| 104 |
+
const adminKey = deriveKey ? deriveKey("admin-api-v1").toString("hex") : "";
|
| 105 |
+
const ADMIN_KEY_ENV = String(process.env.ADMIN_API_KEY || "");
|
| 106 |
+
const ADMIN_KEY = ADMIN_KEY_ENV || adminKey;
|
| 107 |
|
| 108 |
const bots = [];
|
| 109 |
const botSlots = [];
|
|
|
|
| 642 |
}
|
| 643 |
if (header.alg !== "RS256" || !header.kid) return null;
|
| 644 |
const certs = await googleCerts();
|
| 645 |
+
const pem = Object.prototype.hasOwnProperty.call(certs, header.kid) ? certs[header.kid] : null;
|
| 646 |
if (!pem) return null;
|
| 647 |
const ok = crypto.createVerify("SHA256").update(parts[0] + "." + parts[1]).verify(pem, parts[2], "base64url");
|
| 648 |
if (!ok) return null;
|
|
|
|
| 1433 |
}
|
| 1434 |
|
| 1435 |
function hlsSig(msgId, hash, lvl, bucket) {
|
| 1436 |
+
if (!hlsKey) return "";
|
| 1437 |
return crypto
|
| 1438 |
+
.createHmac("sha256", hlsKey)
|
| 1439 |
.update("hls:" + String(msgId) + ":" + String(hash) + ":" + String(lvl) + ":" + String(bucket))
|
| 1440 |
.digest("hex")
|
| 1441 |
.slice(0, 24);
|
|
|
|
| 1835 |
try {
|
| 1836 |
const file = sliceFilePath(msgId, offset);
|
| 1837 |
await fs.promises.mkdir(path.dirname(file), { recursive: true });
|
| 1838 |
+
try {
|
| 1839 |
+
const st = await fs.promises.statfs(DISK_SLICE_DIR);
|
| 1840 |
+
if (Number(st.bavail) * Number(st.bsize) < DISK_FREE_MIN_BYTES) {
|
| 1841 |
+
await trimDiskCache();
|
| 1842 |
+
const st2 = await fs.promises.statfs(DISK_SLICE_DIR);
|
| 1843 |
+
if (Number(st2.bavail) * Number(st2.bsize) < DISK_FREE_MIN_BYTES) return;
|
| 1844 |
+
}
|
| 1845 |
+
} catch { }
|
| 1846 |
await fs.promises.writeFile(file, buffer);
|
| 1847 |
diskBytesUsed += buffer.length;
|
| 1848 |
if (diskBytesUsed > DISK_MAX_BYTES) {
|
|
|
|
| 2055 |
}
|
| 2056 |
|
| 2057 |
const warmRun = new Set();
|
| 2058 |
+
function kickWarmAhead(bot, rec, msgId, fromOff, fileSize, wantBytes) {
|
| 2059 |
const id = String(msgId);
|
| 2060 |
+
const need = Number(wantBytes) || 0;
|
| 2061 |
+
const span = Math.max(0, Math.min(CHUNK_SIZE * 4, need));
|
| 2062 |
+
const chunks = Math.max(1, Math.min(WARM_AHEAD, Math.ceil(span / CHUNK_SIZE)));
|
| 2063 |
const start = Math.floor(Math.max(0, fromOff) / CHUNK_SIZE) * CHUNK_SIZE;
|
| 2064 |
const warmKey = id + ":" + Math.floor(start / (16 * MB));
|
| 2065 |
+
if (!bot || !rec || !id || need <= 0) return;
|
| 2066 |
+
if (warmRun.has(warmKey) || warmRun.size >= 8) return;
|
| 2067 |
+
if (warmBytesInFlight + chunks * CHUNK_SIZE > WARM_BUDGET_BYTES) return;
|
| 2068 |
warmRun.add(warmKey);
|
| 2069 |
+
warmBytesInFlight += chunks * CHUNK_SIZE;
|
| 2070 |
const cap = Number(fileSize) || 0;
|
| 2071 |
(async () => {
|
| 2072 |
try {
|
| 2073 |
const jobs = [];
|
| 2074 |
+
for (let i = 0; i < chunks; i++) {
|
| 2075 |
const off = start + i * CHUNK_SIZE;
|
| 2076 |
if (cap && off >= cap) break;
|
| 2077 |
if (getSliceMem(id, off)) continue;
|
|
|
|
| 2088 |
} catch { }
|
| 2089 |
finally {
|
| 2090 |
warmRun.delete(warmKey);
|
| 2091 |
+
warmBytesInFlight = Math.max(0, warmBytesInFlight - chunks * CHUNK_SIZE);
|
| 2092 |
}
|
| 2093 |
})();
|
| 2094 |
}
|
|
|
|
| 2236 |
return;
|
| 2237 |
}
|
| 2238 |
}
|
| 2239 |
+
kickWarmAhead(bot, rec, msgId, start, rec.size || (end + 1), end - start + 1);
|
| 2240 |
if (pipeDead(req, res)) return;
|
| 2241 |
res.writeHead(status, headers);
|
| 2242 |
if (typeof res.flushHeaders === "function") res.flushHeaders();
|
|
|
|
| 2476 |
return res.end();
|
| 2477 |
}
|
| 2478 |
} else {
|
| 2479 |
+
if (!netOk) {
|
| 2480 |
res.writeHead(403);
|
| 2481 |
return res.end();
|
| 2482 |
}
|
|
|
|
| 2810 |
const plan = PLANS[planKey];
|
| 2811 |
if (!plan || !uidOk(uid)) throw new Error("grant");
|
| 2812 |
const now = Math.floor(Date.now() / 1000);
|
| 2813 |
+
const floorExp = Number(arguments.length > 1 ? arguments[1] : 0) || 0;
|
| 2814 |
let start = now;
|
| 2815 |
try {
|
| 2816 |
const old = await rtdb("users/" + uid + "/subscription", "GET");
|
| 2817 |
const oldExp = vipExpirySec(old);
|
| 2818 |
if (vipActive(old) && oldExp > start) start = oldExp;
|
| 2819 |
} catch { }
|
| 2820 |
+
const target = floorExp > 0 ? floorExp : (start + plan.sec);
|
| 2821 |
+
const expiry = target > start ? target : (start + plan.sec);
|
| 2822 |
const sub = {
|
| 2823 |
status: "active",
|
| 2824 |
plan: planKey,
|
| 2825 |
label: plan.label,
|
| 2826 |
price: plan.price,
|
| 2827 |
+
expiry: expiry,
|
| 2828 |
updatedAt: now
|
| 2829 |
};
|
| 2830 |
await rtdb("users/" + uid + "/subscription", "PUT", sub);
|
|
|
|
| 2866 |
return false;
|
| 2867 |
}
|
| 2868 |
|
| 2869 |
+
async function markOrder(id, uid, planKey, exp) {
|
| 2870 |
paidOrders.set(id, Date.now());
|
| 2871 |
+
const rec = { t: Math.floor(Date.now() / 1000), uid: uid, plan: planKey };
|
| 2872 |
+
if (Number(exp) > 0) rec.exp = Number(exp);
|
| 2873 |
try {
|
| 2874 |
+
await rtdb("processed_orders/" + id, "PUT", rec);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2875 |
} catch { }
|
| 2876 |
}
|
| 2877 |
|
| 2878 |
+
async function claimOrder(id, uid, planKey, planSec) {
|
| 2879 |
+
const now = Math.floor(Date.now() / 1000);
|
| 2880 |
+
const readBack = async () => {
|
| 2881 |
+
try {
|
| 2882 |
+
const v = await rtdb("processed_orders/" + id, "GET");
|
| 2883 |
+
return (v && typeof v === "object" && !Array.isArray(v)) ? v : null;
|
| 2884 |
+
} catch {
|
| 2885 |
+
return null;
|
| 2886 |
+
}
|
| 2887 |
+
};
|
| 2888 |
+
let back = await readBack();
|
| 2889 |
+
if (!back) {
|
| 2890 |
+
const rec = { t: now, uid: uid, plan: planKey, exp: now + planSec };
|
| 2891 |
try {
|
| 2892 |
await rtdb("processed_orders/" + id, "PUT", rec);
|
|
|
|
| 2893 |
} catch { }
|
| 2894 |
+
back = await readBack();
|
| 2895 |
+
}
|
| 2896 |
+
if (!back) return { state: "unknown", exp: 0 };
|
| 2897 |
+
if (String(back.uid || "") !== String(uid) || String(back.plan || "") !== String(planKey)) {
|
| 2898 |
+
return { state: "conflict", exp: 0 };
|
| 2899 |
}
|
| 2900 |
+
const exp = Number(back.exp) || 0;
|
| 2901 |
+
return { state: exp > 0 ? "seen" : "claimed", exp: exp };
|
| 2902 |
}
|
| 2903 |
|
| 2904 |
async function fulfillPaid(ses) {
|
| 2905 |
if (!ses || ses.object !== "checkout.session") return false;
|
| 2906 |
if (ses.mode !== "payment" || ses.payment_status !== "paid") return false;
|
| 2907 |
if (ses.status === "expired") return false;
|
| 2908 |
+
if (STRIPE_KEY.indexOf("sk_test_") !== 0 && ses.livemode !== true) return false;
|
| 2909 |
const uid = ses.metadata && ses.metadata.uid;
|
| 2910 |
const planKey = ses.metadata && ses.metadata.plan;
|
| 2911 |
const plan = PLANS[planKey];
|
|
|
|
| 2916 |
const sid = String(ses.id || "");
|
| 2917 |
if (!/^cs_[A-Za-z0-9_]+$/.test(sid)) return false;
|
| 2918 |
return withPayLock(sid, async () => {
|
| 2919 |
+
const c = await claimOrder(sid, uid, planKey, plan.sec);
|
| 2920 |
+
if (c.state === "conflict") return false;
|
| 2921 |
+
paidOrders.set(sid, Date.now());
|
| 2922 |
+
await grantVip(uid, planKey, c.exp);
|
| 2923 |
+
await markOrder(sid, uid, planKey, c.exp);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2924 |
return true;
|
| 2925 |
});
|
| 2926 |
}
|
|
|
|
| 2928 |
async function stripeEventSeen(id) {
|
| 2929 |
if (!id) return true;
|
| 2930 |
if (stripeEvents.has(id)) return true;
|
| 2931 |
+
let back = null;
|
| 2932 |
try {
|
| 2933 |
+
const v = await rtdb("stripe_events/" + id, "GET");
|
| 2934 |
+
back = (v && typeof v === "object") ? v : null;
|
| 2935 |
+
} catch { }
|
| 2936 |
+
if (!back) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2937 |
try {
|
| 2938 |
+
await rtdb("stripe_events/" + id, "PUT", { t: Math.floor(Date.now() / 1000) });
|
| 2939 |
+
back = await rtdb("stripe_events/" + id, "GET");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2940 |
} catch { }
|
|
|
|
| 2941 |
}
|
| 2942 |
+
stripeEvents.add(id);
|
| 2943 |
+
if (stripeEvents.size > 4000) {
|
| 2944 |
+
const first = stripeEvents.values().next().value;
|
| 2945 |
+
stripeEvents.delete(first);
|
| 2946 |
+
}
|
| 2947 |
+
return !!(back && typeof back === "object");
|
| 2948 |
}
|
| 2949 |
|
| 2950 |
async function handleStripeWebhook(req, res) {
|
|
|
|
| 3043 |
}
|
| 3044 |
|
| 3045 |
function adminKeyOk(req, url) {
|
| 3046 |
+
if (!ADMIN_KEY) return false;
|
| 3047 |
const h = String(req.headers.authorization || "").replace(/^Bearer\s+/i, "").trim();
|
| 3048 |
const x = String(req.headers["x-admin-key"] || "").trim();
|
| 3049 |
const got = h || x;
|
| 3050 |
+
if (!got || got.length !== ADMIN_KEY.length) return false;
|
| 3051 |
+
return timingSafeEq(got, ADMIN_KEY);
|
| 3052 |
}
|
| 3053 |
|
| 3054 |
function parseTgPosts(text) {
|
|
|
|
| 3214 |
process.stderr.write("missing required settings\n");
|
| 3215 |
process.exit(1);
|
| 3216 |
}
|
| 3217 |
+
try {
|
| 3218 |
+
await trimDiskCache();
|
| 3219 |
+
} catch { }
|
| 3220 |
+
const _diskSweep = setInterval(() => { trimDiskCache().catch(() => { }); }, 60000);
|
| 3221 |
+
if (typeof _diskSweep.unref === "function") _diskSweep.unref();
|
| 3222 |
const tokens = String(process.env.BOT_TOKENS || "")
|
| 3223 |
.split(/[\s,]+/)
|
| 3224 |
.map((t) => t.trim())
|