JOHN SILLVESTOR commited on
Commit
a1893ab
·
verified ·
1 Parent(s): e5f89d4

Improve stream range lookahead and pipe error resilience

Browse files
Files changed (1) hide show
  1. server.js +20 -2
server.js CHANGED
@@ -67,6 +67,7 @@ const FB_DB = String(process.env.FIREBASE_DB_URL || "").replace(/\/?$/, "/");
67
  const FB_SECRET = process.env.FIREBASE_DB_SECRET || "";
68
  const STRIPE_KEY = process.env.STRIPE_SECRET_KEY || "";
69
  const STRIPE_WH = process.env.STRIPE_WEBHOOK_SECRET || "";
 
70
  const SPACE_FALLBACK = [
71
  "https://dublk2000-dublk-s1.hf.space",
72
  "https://dublk2000-dublk-s2.hf.space",
@@ -760,8 +761,18 @@ function uidOk(uid) {
760
  return typeof uid === "string" && /^[A-Za-z0-9_-]{8,128}$/.test(uid);
761
  }
762
 
763
- async function stripeSession(id) {
764
- if (!STRIPE_KEY || !id || !/^cs_[A-Za-z0-9_]+$/.test(String(id))) return null;
 
 
 
 
 
 
 
 
 
 
765
  const r = await fetch("https://api.stripe.com/v1/checkout/sessions/" + encodeURIComponent(id), {
766
  headers: { Authorization: "Bearer " + STRIPE_KEY },
767
  signal: AbortSignal.timeout(12000)
@@ -2314,6 +2325,10 @@ async function handleStream(req, res, url, msgId, kHex, ticketObj) {
2314
  return res.end();
2315
  }
2316
  const ip = clientIp(req);
 
 
 
 
2317
  const vipUntil = Number(ticket.x) || 0;
2318
  if (ticketLvl(ticket) === "subscribed" && vipUntil > 0 && vipUntil <= Math.floor(Date.now() / 1000)) ticket.l = "registered";
2319
  const isVip = ticketLvl(ticket) === "subscribed";
@@ -2672,6 +2687,9 @@ async function handleVipStart(req, res) {
2672
  const plan = PLANS[planKey];
2673
  const email = payEmail(idp);
2674
  if (!plan || !email) return json(res, 400, { error: "plan" });
 
 
 
2675
  autoAllowEmail(email).catch(() => { });
2676
  const auto = planKey === "month" && body.auto === true;
2677
  const urls = payUrls(body.returnUrl);
 
67
  const FB_SECRET = process.env.FIREBASE_DB_SECRET || "";
68
  const STRIPE_KEY = process.env.STRIPE_SECRET_KEY || "";
69
  const STRIPE_WH = process.env.STRIPE_WEBHOOK_SECRET || "";
70
+ const RC_SIGN = process.env.RC_SIGN_KEY || "";
71
  const SPACE_FALLBACK = [
72
  "https://dublk2000-dublk-s1.hf.space",
73
  "https://dublk2000-dublk-s2.hf.space",
 
761
  return typeof uid === "string" && /^[A-Za-z0-9_-]{8,128}$/.test(uid);
762
  }
763
 
764
+ function verifyProof(proof, ip) {
765
+ if (!RC_SIGN) return true;
766
+ const m = /^(\d{10})\.([0-9a-f]{64})$/.exec(String(proof || ""));
767
+ if (!m) return false;
768
+ const exp = Number(m[1]);
769
+ const now = Math.floor(Date.now() / 1000);
770
+ if (exp < now || exp > now + 600) return false;
771
+ const mac = crypto.createHmac("sha256", RC_SIGN).update("vip:" + ip + ":" + exp).digest("hex");
772
+ return timingSafeEq(mac, m[2]);
773
+ }
774
+
775
+ async function stripeSession(id) { if (!STRIPE_KEY || !id || !/^cs_[A-Za-z0-9_]+$/.test(String(id))) return null;
776
  const r = await fetch("https://api.stripe.com/v1/checkout/sessions/" + encodeURIComponent(id), {
777
  headers: { Authorization: "Bearer " + STRIPE_KEY },
778
  signal: AbortSignal.timeout(12000)
 
2325
  return res.end();
2326
  }
2327
  const ip = clientIp(req);
2328
+ if (!ticketNetOk(ticket, ip) || !ticketUaOk(ticket, req.headers["user-agent"])) {
2329
+ res.writeHead(403);
2330
+ return res.end();
2331
+ }
2332
  const vipUntil = Number(ticket.x) || 0;
2333
  if (ticketLvl(ticket) === "subscribed" && vipUntil > 0 && vipUntil <= Math.floor(Date.now() / 1000)) ticket.l = "registered";
2334
  const isVip = ticketLvl(ticket) === "subscribed";
 
2687
  const plan = PLANS[planKey];
2688
  const email = payEmail(idp);
2689
  if (!plan || !email) return json(res, 400, { error: "plan" });
2690
+ if (!verifyProof(body.proof, clientIp(req))) {
2691
+ return json(res, 400, { error: "captcha" });
2692
+ }
2693
  autoAllowEmail(email).catch(() => { });
2694
  const auto = planKey === "month" && body.auto === true;
2695
  const urls = payUrls(body.returnUrl);