Spaces:
Running
Running
DUBLK 2000 Admin commited on
Commit ·
33d48fb
1
Parent(s): e3ef07a
Add optimized WebP poster hosting, payment idempotency, key separation
Browse files- Dockerfile +1 -0
- server.js +73 -14
Dockerfile
CHANGED
|
@@ -6,6 +6,7 @@ WORKDIR /app
|
|
| 6 |
COPY --chown=node:node package.json ./
|
| 7 |
RUN npm install --omit=dev --no-audit --no-fund
|
| 8 |
COPY --chown=node:node server.js brand.png ./
|
|
|
|
| 9 |
USER node
|
| 10 |
ENV HOME=/home/node
|
| 11 |
ENV NODE_ENV=production
|
|
|
|
| 6 |
COPY --chown=node:node package.json ./
|
| 7 |
RUN npm install --omit=dev --no-audit --no-fund
|
| 8 |
COPY --chown=node:node server.js brand.png ./
|
| 9 |
+
COPY --chown=node:node posters ./posters
|
| 10 |
USER node
|
| 11 |
ENV HOME=/home/node
|
| 12 |
ENV NODE_ENV=production
|
server.js
CHANGED
|
@@ -105,6 +105,7 @@ const inflightFile = new Map();
|
|
| 105 |
const hotMap = new Map();
|
| 106 |
const inflightHot = new Map();
|
| 107 |
const durCache = new Map();
|
|
|
|
| 108 |
const inflightDur = new Map();
|
| 109 |
const lvlCache = new Map();
|
| 110 |
const rateMap = new Map();
|
|
@@ -425,6 +426,10 @@ function sweepMaps() {
|
|
| 425 |
const ids = [...durCache.keys()].slice(0, durCache.size - 35000);
|
| 426 |
for (const id of ids) durCache.delete(id);
|
| 427 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 428 |
if (rateMap.size > 20000) {
|
| 429 |
const slot = Math.floor(now / 60000);
|
| 430 |
for (const k of rateMap.keys()) {
|
|
@@ -1209,10 +1214,52 @@ function ticketLvl(ticket) {
|
|
| 1209 |
return "anonymous";
|
| 1210 |
}
|
| 1211 |
|
| 1212 |
-
function previewMaxEnd(size, l) {
|
| 1213 |
const pct = lvlPct(l);
|
| 1214 |
if (!size || pct >= 100) return size - 1;
|
| 1215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1216 |
}
|
| 1217 |
|
| 1218 |
function mp4IndexOff(size, msgId) {
|
|
@@ -1247,7 +1294,9 @@ function applyPreview(rng, size, l, msgId, ticket) {
|
|
| 1247 |
partial: rng.start > 0 || endCap < size - 1
|
| 1248 |
};
|
| 1249 |
}
|
| 1250 |
-
const
|
|
|
|
|
|
|
| 1251 |
const tailAllowed = size >= TAIL_MIN_SIZE;
|
| 1252 |
const tailWin = tailAllowed ? Math.max(0, size - Math.min(TAIL_MAX, Math.floor(size * 0.01))) : Infinity;
|
| 1253 |
if (tailAllowed && rng.start >= tailWin) {
|
|
@@ -2592,7 +2641,7 @@ async function openPay(uid, planKey, email, auto) {
|
|
| 2592 |
if (payTypes.indexOf("link") >= 0) need.push("link");
|
| 2593 |
if (paypalLive) need.push("paypal");
|
| 2594 |
if (need.some((t) => types.indexOf(t) < 0)) return null;
|
| 2595 |
-
if (meta.uid !== uid || meta.plan !== planKey || String(meta.email || "") !== email) return null;
|
| 2596 |
if ((meta.auto === "1") !== !!auto) return null;
|
| 2597 |
return { sid: ses.id, url: ses.url };
|
| 2598 |
}
|
|
@@ -2611,7 +2660,7 @@ async function handleVipStart(req, res) {
|
|
| 2611 |
} catch {
|
| 2612 |
return json(res, 400, { error: "body" });
|
| 2613 |
}
|
| 2614 |
-
const planKey = String(body.plan || "");
|
| 2615 |
const plan = PLANS[planKey];
|
| 2616 |
const email = payEmail(idp);
|
| 2617 |
if (!plan || !email) return json(res, 400, { error: "plan" });
|
|
@@ -2666,20 +2715,20 @@ async function handleVipStart(req, res) {
|
|
| 2666 |
}
|
| 2667 |
const useTypes = (wallets && wallets.types && wallets.types.length ? wallets.types : ["card"]).filter((t) => !(auto && t === "samsung_pay"));
|
| 2668 |
useTypes.forEach((t, i) => { fields["payment_method_types[" + i + "]"] = t; });
|
| 2669 |
-
async function createSes(body) {
|
| 2670 |
const r = await fetch("https://api.stripe.com/v1/checkout/sessions", {
|
| 2671 |
method: "POST",
|
| 2672 |
headers: {
|
| 2673 |
Authorization: "Bearer " + STRIPE_KEY,
|
| 2674 |
"Content-Type": "application/x-www-form-urlencoded",
|
| 2675 |
-
"Idempotency-Key":
|
| 2676 |
},
|
| 2677 |
body: new URLSearchParams(body).toString(),
|
| 2678 |
signal: AbortSignal.timeout(12000)
|
| 2679 |
});
|
| 2680 |
return { r, d: await r.json() };
|
| 2681 |
}
|
| 2682 |
-
let { r, d } = await createSes(fields);
|
| 2683 |
const firstErr = d && (d.error && d.error.message || d.message || null);
|
| 2684 |
if (!r.ok) {
|
| 2685 |
const slim = Object.assign({}, fields);
|
|
@@ -2691,7 +2740,7 @@ async function handleVipStart(req, res) {
|
|
| 2691 |
"branding_settings[background_color]",
|
| 2692 |
"line_items[0][price_data][product_data][images][0]"
|
| 2693 |
].forEach((k) => { delete slim[k]; });
|
| 2694 |
-
({ r, d } = await createSes(slim));
|
| 2695 |
if (!r.ok) {
|
| 2696 |
errText(new Error("stripe: " + (firstErr || "unknown") + " | slim: " + ((d && d.error && d.error.message) || "unknown")));
|
| 2697 |
}
|
|
@@ -2721,7 +2770,7 @@ async function handleVipGo(req, res) {
|
|
| 2721 |
const meta = d && d.metadata || {};
|
| 2722 |
const email = payEmail(idp);
|
| 2723 |
if (!d || !d.url || meta.uid !== idp.sub || d.status === "expired" || d.payment_status === "paid") return json(res, 400, { error: "Payment failed" });
|
| 2724 |
-
if (meta.email && (!email || String(meta.email) !== email)) return json(res, 400, { error: "Payment failed" });
|
| 2725 |
json(res, 200, { url: d.url });
|
| 2726 |
}
|
| 2727 |
|
|
@@ -2746,14 +2795,17 @@ async function handleVipConfirm(req, res) {
|
|
| 2746 |
if (!d || meta.uid !== idp.sub || d.client_reference_id !== idp.sub) {
|
| 2747 |
return json(res, 400, { error: "Payment failed", paid: false });
|
| 2748 |
}
|
| 2749 |
-
if (sem && (!email || sem !== email)) return json(res, 400, { error: "Payment failed", paid: false });
|
| 2750 |
if (d.payment_status !== "paid" || d.status !== "complete") return json(res, 402, { error: "unpaid", paid: false });
|
| 2751 |
let granted = false;
|
| 2752 |
try { granted = await fulfillPaid(d); } catch { granted = false; }
|
| 2753 |
lvlCache.delete(idp.sub);
|
| 2754 |
let sub = null;
|
| 2755 |
try { sub = await rtdb("users/" + idp.sub + "/subscription", "GET"); } catch { }
|
| 2756 |
-
if (!granted || !vipActive(sub))
|
|
|
|
|
|
|
|
|
|
| 2757 |
json(res, 200, { ok: true, paid: true, accessLevel: "subscribed", subscription: publicSub(sub) });
|
| 2758 |
}
|
| 2759 |
|
|
@@ -2857,6 +2909,13 @@ async function claimOrder(id, uid, planKey, planSec) {
|
|
| 2857 |
return { state: exp > 0 ? "seen" : "claimed", exp: exp };
|
| 2858 |
}
|
| 2859 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2860 |
async function fulfillPaid(ses) {
|
| 2861 |
if (!ses || ses.object !== "checkout.session") return false;
|
| 2862 |
if (ses.mode !== "payment" && ses.mode !== "subscription") return false;
|
|
@@ -2872,7 +2931,7 @@ async function fulfillPaid(ses) {
|
|
| 2872 |
if (ses.status !== "complete") return false;
|
| 2873 |
const email = String(ses.metadata && ses.metadata.email || "").toLowerCase();
|
| 2874 |
if (email && (email.length > 200 || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email))) return false;
|
| 2875 |
-
if (
|
| 2876 |
if (String(ses.currency || "").toLowerCase() !== "usd") return false;
|
| 2877 |
const sid = String(ses.id || "");
|
| 2878 |
if (!/^cs_[A-Za-z0-9_]+$/.test(sid)) return false;
|
|
@@ -2934,7 +2993,7 @@ async function fulfillInvoice(inv) {
|
|
| 2934 |
const planKey = sub && sub.metadata && sub.metadata.plan;
|
| 2935 |
const plan = PLANS[planKey];
|
| 2936 |
if (!uidOk(uid) || planKey !== "month" || !plan) return false;
|
| 2937 |
-
if (
|
| 2938 |
if (String(inv.currency || "").toLowerCase() !== "usd") return false;
|
| 2939 |
return withPayLock(invId, async () => {
|
| 2940 |
const c = await claimOrder(invId, uid, planKey, plan.sec);
|
|
|
|
| 105 |
const hotMap = new Map();
|
| 106 |
const inflightHot = new Map();
|
| 107 |
const durCache = new Map();
|
| 108 |
+
const mdatCache = new Map();
|
| 109 |
const inflightDur = new Map();
|
| 110 |
const lvlCache = new Map();
|
| 111 |
const rateMap = new Map();
|
|
|
|
| 426 |
const ids = [...durCache.keys()].slice(0, durCache.size - 35000);
|
| 427 |
for (const id of ids) durCache.delete(id);
|
| 428 |
}
|
| 429 |
+
if (mdatCache.size > 50000) {
|
| 430 |
+
const ids = [...mdatCache.keys()].slice(0, mdatCache.size - 35000);
|
| 431 |
+
for (const id of ids) mdatCache.delete(id);
|
| 432 |
+
}
|
| 433 |
if (rateMap.size > 20000) {
|
| 434 |
const slot = Math.floor(now / 60000);
|
| 435 |
for (const k of rateMap.keys()) {
|
|
|
|
| 1214 |
return "anonymous";
|
| 1215 |
}
|
| 1216 |
|
| 1217 |
+
function previewMaxEnd(size, l, msgId, dur) {
|
| 1218 |
const pct = lvlPct(l);
|
| 1219 |
if (!size || pct >= 100) return size - 1;
|
| 1220 |
+
let base = Math.floor(Number(size) * pct / 100);
|
| 1221 |
+
const d = Number(dur);
|
| 1222 |
+
if (d > 1 && d < 86400) {
|
| 1223 |
+
const bitrate = Number(size) * 8 / d;
|
| 1224 |
+
if (bitrate > 16000 && bitrate < 40000000) {
|
| 1225 |
+
const ms0 = mdatStartOff(size, msgId);
|
| 1226 |
+
const span = Math.max(0, Number(size) - ms0);
|
| 1227 |
+
const tCut = ms0 + Math.floor(span * pct / 100);
|
| 1228 |
+
if (tCut > base) base = tCut;
|
| 1229 |
+
base += Math.min(Math.floor(base * 0.04), Math.floor(size * 0.04));
|
| 1230 |
+
}
|
| 1231 |
+
}
|
| 1232 |
+
const head = Math.min(48 * MB, Math.max(4 * MB, Math.floor(size * 0.06)));
|
| 1233 |
+
return Math.max(0, Math.min(size - 1, base + head - 1));
|
| 1234 |
+
}
|
| 1235 |
+
|
| 1236 |
+
function mdatStartOff(size, msgId) {
|
| 1237 |
+
const id = String(msgId || "");
|
| 1238 |
+
const cached = mdatCache.get(id);
|
| 1239 |
+
if (cached !== undefined) return cached;
|
| 1240 |
+
const h = hotMap.get(id);
|
| 1241 |
+
const buf = h && h.headOff === 0 ? h.head : null;
|
| 1242 |
+
let out = 0;
|
| 1243 |
+
if (buf && buf.length >= 16 && size) {
|
| 1244 |
+
let i = 0;
|
| 1245 |
+
for (let n = 0; i + 8 <= buf.length && n < 16; n++) {
|
| 1246 |
+
let sz = buf.readUInt32BE(i);
|
| 1247 |
+
const typ = buf.toString("ascii", i + 4, i + 8);
|
| 1248 |
+
let big = false;
|
| 1249 |
+
if (sz === 1) {
|
| 1250 |
+
if (i + 16 > buf.length) break;
|
| 1251 |
+
sz = Number(buf.readBigUInt64BE(i + 8));
|
| 1252 |
+
big = true;
|
| 1253 |
+
}
|
| 1254 |
+
if (!sz || sz < 8) break;
|
| 1255 |
+
if (typ === "mdat") { out = i + (big ? 16 : 8); break; }
|
| 1256 |
+
if (typ === "moov") { out = i + sz; break; }
|
| 1257 |
+
if (i + sz > size) break;
|
| 1258 |
+
i += sz;
|
| 1259 |
+
}
|
| 1260 |
+
}
|
| 1261 |
+
mdatCache.set(id, out);
|
| 1262 |
+
return out;
|
| 1263 |
}
|
| 1264 |
|
| 1265 |
function mp4IndexOff(size, msgId) {
|
|
|
|
| 1294 |
partial: rng.start > 0 || endCap < size - 1
|
| 1295 |
};
|
| 1296 |
}
|
| 1297 |
+
const td = Number(ticket && ticket.d);
|
| 1298 |
+
const dur = td > 1 ? td : (durCache.get(String(msgId || "")) || 0);
|
| 1299 |
+
const maxEnd = previewMaxEnd(size, l, msgId, dur);
|
| 1300 |
const tailAllowed = size >= TAIL_MIN_SIZE;
|
| 1301 |
const tailWin = tailAllowed ? Math.max(0, size - Math.min(TAIL_MAX, Math.floor(size * 0.01))) : Infinity;
|
| 1302 |
if (tailAllowed && rng.start >= tailWin) {
|
|
|
|
| 2641 |
if (payTypes.indexOf("link") >= 0) need.push("link");
|
| 2642 |
if (paypalLive) need.push("paypal");
|
| 2643 |
if (need.some((t) => types.indexOf(t) < 0)) return null;
|
| 2644 |
+
if (meta.uid !== uid || String(meta.plan || "").toLowerCase() !== planKey || String(meta.email || "").toLowerCase() !== email) return null;
|
| 2645 |
if ((meta.auto === "1") !== !!auto) return null;
|
| 2646 |
return { sid: ses.id, url: ses.url };
|
| 2647 |
}
|
|
|
|
| 2660 |
} catch {
|
| 2661 |
return json(res, 400, { error: "body" });
|
| 2662 |
}
|
| 2663 |
+
const planKey = String(body.plan || "").trim().toLowerCase();
|
| 2664 |
const plan = PLANS[planKey];
|
| 2665 |
const email = payEmail(idp);
|
| 2666 |
if (!plan || !email) return json(res, 400, { error: "plan" });
|
|
|
|
| 2715 |
}
|
| 2716 |
const useTypes = (wallets && wallets.types && wallets.types.length ? wallets.types : ["card"]).filter((t) => !(auto && t === "samsung_pay"));
|
| 2717 |
useTypes.forEach((t, i) => { fields["payment_method_types[" + i + "]"] = t; });
|
| 2718 |
+
async function createSes(body, idemKey) {
|
| 2719 |
const r = await fetch("https://api.stripe.com/v1/checkout/sessions", {
|
| 2720 |
method: "POST",
|
| 2721 |
headers: {
|
| 2722 |
Authorization: "Bearer " + STRIPE_KEY,
|
| 2723 |
"Content-Type": "application/x-www-form-urlencoded",
|
| 2724 |
+
"Idempotency-Key": idemKey
|
| 2725 |
},
|
| 2726 |
body: new URLSearchParams(body).toString(),
|
| 2727 |
signal: AbortSignal.timeout(12000)
|
| 2728 |
});
|
| 2729 |
return { r, d: await r.json() };
|
| 2730 |
}
|
| 2731 |
+
let { r, d } = await createSes(fields, idem);
|
| 2732 |
const firstErr = d && (d.error && d.error.message || d.message || null);
|
| 2733 |
if (!r.ok) {
|
| 2734 |
const slim = Object.assign({}, fields);
|
|
|
|
| 2740 |
"branding_settings[background_color]",
|
| 2741 |
"line_items[0][price_data][product_data][images][0]"
|
| 2742 |
].forEach((k) => { delete slim[k]; });
|
| 2743 |
+
({ r, d } = await createSes(slim, idem + "s"));
|
| 2744 |
if (!r.ok) {
|
| 2745 |
errText(new Error("stripe: " + (firstErr || "unknown") + " | slim: " + ((d && d.error && d.error.message) || "unknown")));
|
| 2746 |
}
|
|
|
|
| 2770 |
const meta = d && d.metadata || {};
|
| 2771 |
const email = payEmail(idp);
|
| 2772 |
if (!d || !d.url || meta.uid !== idp.sub || d.status === "expired" || d.payment_status === "paid") return json(res, 400, { error: "Payment failed" });
|
| 2773 |
+
if (meta.email && (!email || String(meta.email).toLowerCase() !== email)) return json(res, 400, { error: "Payment failed" });
|
| 2774 |
json(res, 200, { url: d.url });
|
| 2775 |
}
|
| 2776 |
|
|
|
|
| 2795 |
if (!d || meta.uid !== idp.sub || d.client_reference_id !== idp.sub) {
|
| 2796 |
return json(res, 400, { error: "Payment failed", paid: false });
|
| 2797 |
}
|
| 2798 |
+
if (sem && (!email || sem.toLowerCase() !== email)) return json(res, 400, { error: "Payment failed", paid: false });
|
| 2799 |
if (d.payment_status !== "paid" || d.status !== "complete") return json(res, 402, { error: "unpaid", paid: false });
|
| 2800 |
let granted = false;
|
| 2801 |
try { granted = await fulfillPaid(d); } catch { granted = false; }
|
| 2802 |
lvlCache.delete(idp.sub);
|
| 2803 |
let sub = null;
|
| 2804 |
try { sub = await rtdb("users/" + idp.sub + "/subscription", "GET"); } catch { }
|
| 2805 |
+
if (!granted || !vipActive(sub)) {
|
| 2806 |
+
if (d.payment_status === "paid" && d.status === "complete") return json(res, 402, { error: "order", paid: false });
|
| 2807 |
+
return json(res, 402, { error: "unpaid", paid: false });
|
| 2808 |
+
}
|
| 2809 |
json(res, 200, { ok: true, paid: true, accessLevel: "subscribed", subscription: publicSub(sub) });
|
| 2810 |
}
|
| 2811 |
|
|
|
|
| 2909 |
return { state: exp > 0 ? "seen" : "claimed", exp: exp };
|
| 2910 |
}
|
| 2911 |
|
| 2912 |
+
function priceOk(paid, subtotal, expect) {
|
| 2913 |
+
const total = Number(paid);
|
| 2914 |
+
const sub = Number(subtotal);
|
| 2915 |
+
if (!Number.isFinite(total) || total < expect) return false;
|
| 2916 |
+
return sub === expect || total === expect;
|
| 2917 |
+
}
|
| 2918 |
+
|
| 2919 |
async function fulfillPaid(ses) {
|
| 2920 |
if (!ses || ses.object !== "checkout.session") return false;
|
| 2921 |
if (ses.mode !== "payment" && ses.mode !== "subscription") return false;
|
|
|
|
| 2931 |
if (ses.status !== "complete") return false;
|
| 2932 |
const email = String(ses.metadata && ses.metadata.email || "").toLowerCase();
|
| 2933 |
if (email && (email.length > 200 || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email))) return false;
|
| 2934 |
+
if (!priceOk(ses.amount_total, ses.amount_subtotal, plan.amount)) return false;
|
| 2935 |
if (String(ses.currency || "").toLowerCase() !== "usd") return false;
|
| 2936 |
const sid = String(ses.id || "");
|
| 2937 |
if (!/^cs_[A-Za-z0-9_]+$/.test(sid)) return false;
|
|
|
|
| 2993 |
const planKey = sub && sub.metadata && sub.metadata.plan;
|
| 2994 |
const plan = PLANS[planKey];
|
| 2995 |
if (!uidOk(uid) || planKey !== "month" || !plan) return false;
|
| 2996 |
+
if (!priceOk(inv.amount_paid, inv.subtotal, plan.amount)) return false;
|
| 2997 |
if (String(inv.currency || "").toLowerCase() !== "usd") return false;
|
| 2998 |
return withPayLock(invId, async () => {
|
| 2999 |
const c = await claimOrder(invId, uid, planKey, plan.sec);
|