JOHN SILLVESTOR commited on
Commit
ea87f37
·
verified ·
1 Parent(s): e66cdaf

Update Node HTTP service

Browse files
Files changed (1) hide show
  1. server.js +31 -10
server.js CHANGED
@@ -13,7 +13,7 @@ const API_HASH = process.env.TELEGRAM_API_HASH || "";
13
  const STREAM_SECRET = process.env.STREAM_SECRET || "";
14
  const ROLE = (process.env.NODE_ROLE || "stream").toLowerCase();
15
  const HASH_LEN = 6;
16
- const TOKEN_TTL = 900;
17
  const MIN_ALIGN = 4096;
18
  const MB = 1024 * 1024;
19
  const HTTP_WINDOW = MB;
@@ -112,7 +112,7 @@ function openTicket(hex) {
112
 
113
  function cors(req, res) {
114
  const o = req.headers.origin || "";
115
- const allow = o === ORIGIN || o.endsWith(".blogspot.com") && o.startsWith("https://");
116
  res.setHeader("Access-Control-Allow-Origin", allow ? o : ORIGIN);
117
  res.setHeader("Vary", "Origin");
118
  res.setHeader("Access-Control-Allow-Methods", "GET,POST,HEAD,OPTIONS");
@@ -122,6 +122,7 @@ function cors(req, res) {
122
  res.setHeader("X-Content-Type-Options", "nosniff");
123
  res.setHeader("Referrer-Policy", "strict-origin-when-cross-origin");
124
  res.setHeader("X-Frame-Options", "DENY");
 
125
  }
126
 
127
  function json(res, code, body) {
@@ -261,13 +262,13 @@ function limited(ip) {
261
  const n = (rateMap.get(key) || 0) + 1;
262
  rateMap.set(key, n);
263
  if (rateMap.size > 4000) sweepMaps();
264
- return n > 800;
265
  }
266
 
267
  function isBlogOrigin(o) {
268
- o = String(o || "");
269
  if (o === ORIGIN) return true;
270
- return o.startsWith("https://") && (o.endsWith(".blogspot.com") || o.endsWith(".blogger.com"));
271
  }
272
 
273
  function fromBlog(req) {
@@ -288,6 +289,21 @@ function denyHot(req, res) {
288
  return true;
289
  }
290
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
291
  function payLimited(uid, ip) {
292
  const slot = Math.floor(Date.now() / 60000);
293
  const a = "pay:" + uid + ":" + slot;
@@ -334,6 +350,8 @@ async function verifyIdToken(raw) {
334
  if (!ok) return null;
335
  const now = Math.floor(Date.now() / 1000);
336
  if (payload.exp <= now || payload.iat > now + 60) return null;
 
 
337
  if (payload.aud !== FB_PROJECT) return null;
338
  if (payload.iss !== "https://securetoken.google.com/" + FB_PROJECT) return null;
339
  if (!payload.sub || payload.sub.length > 128) return null;
@@ -761,13 +779,12 @@ async function handleGenerate(req, res, url) {
761
  if (!(rec.duration > 1)) fillDuration(bot, rec, msgId).catch(function () {});
762
  const playMime = httpVideoType(rec.mime, rec.name);
763
  const exp = Math.floor(Date.now() / 1000) + TOKEN_TTL;
764
- const k = sealTicket({ m: msgId, h: hash, u: uid || "anon", l: lvl, b: bot.idx, e: exp, s: rec.size, n: playMime });
765
  const host = "https://" + (req.headers.host || "localhost");
766
  json(res, 200, {
767
  url: host + "/stream/" + msgId + "?k=" + k,
768
  fileSize: rec.size,
769
  mime: playMime,
770
- name: rec.name,
771
  duration: rec.duration || 0,
772
  accessLevel: lvl,
773
  expiry: exp
@@ -775,8 +792,7 @@ async function handleGenerate(req, res, url) {
775
  }
776
 
777
  async function handleStream(req, res, url, msgId) {
778
- const o = String(req.headers.origin || "");
779
- if (o && !isBlogOrigin(o)) {
780
  res.writeHead(403);
781
  return res.end();
782
  }
@@ -791,6 +807,10 @@ async function handleStream(req, res, url, msgId) {
791
  return res.end();
792
  }
793
  const ip = clientIp(req);
 
 
 
 
794
  if (!takeStream(ip)) {
795
  res.setHeader("Retry-After", "1");
796
  res.writeHead(429);
@@ -828,7 +848,7 @@ async function handleStream(req, res, url, msgId) {
828
  const headers = {
829
  "Content-Type": httpVideoType(rec.mime, rec.name),
830
  "Accept-Ranges": "bytes",
831
- "Cache-Control": "private, max-age=30, no-transform",
832
  "Content-Disposition": "inline",
833
  "X-Content-Type-Options": "nosniff"
834
  };
@@ -1052,6 +1072,7 @@ async function handleStripeWebhook(req, res) {
1052
  }
1053
 
1054
  async function handleBootstrap(req, res, url) {
 
1055
  json(res, 200, {
1056
  worker: SPACE_URLS[0] || "",
1057
  spaces: SPACE_URLS,
 
13
  const STREAM_SECRET = process.env.STREAM_SECRET || "";
14
  const ROLE = (process.env.NODE_ROLE || "stream").toLowerCase();
15
  const HASH_LEN = 6;
16
+ const TOKEN_TTL = 600;
17
  const MIN_ALIGN = 4096;
18
  const MB = 1024 * 1024;
19
  const HTTP_WINDOW = MB;
 
112
 
113
  function cors(req, res) {
114
  const o = req.headers.origin || "";
115
+ const allow = isBlogOrigin(o);
116
  res.setHeader("Access-Control-Allow-Origin", allow ? o : ORIGIN);
117
  res.setHeader("Vary", "Origin");
118
  res.setHeader("Access-Control-Allow-Methods", "GET,POST,HEAD,OPTIONS");
 
122
  res.setHeader("X-Content-Type-Options", "nosniff");
123
  res.setHeader("Referrer-Policy", "strict-origin-when-cross-origin");
124
  res.setHeader("X-Frame-Options", "DENY");
125
+ res.setHeader("X-Robots-Tag", "noindex, nofollow");
126
  }
127
 
128
  function json(res, code, body) {
 
262
  const n = (rateMap.get(key) || 0) + 1;
263
  rateMap.set(key, n);
264
  if (rateMap.size > 4000) sweepMaps();
265
+ return n > 90;
266
  }
267
 
268
  function isBlogOrigin(o) {
269
+ o = String(o || "").replace(/\/$/, "");
270
  if (o === ORIGIN) return true;
271
+ return o === "https://dublk2000.blogspot.com" || o === "https://www.dublk2000.blogspot.com";
272
  }
273
 
274
  function fromBlog(req) {
 
289
  return true;
290
  }
291
 
292
+ function ipNet(ip) {
293
+ let s = String(ip || "").replace(/^::ffff:/i, "");
294
+ if (/^\d{1,3}(?:\.\d{1,3}){3}$/.test(s)) {
295
+ const p = s.split(".");
296
+ return "4." + p[0] + "." + p[1];
297
+ }
298
+ const parts = s.split(":").filter(Boolean);
299
+ return "6." + parts.slice(0, 3).join(":");
300
+ }
301
+
302
+ function ipTag(ip) {
303
+ if (!hmacKey) return "";
304
+ return crypto.createHmac("sha256", hmacKey).update("net:" + ipNet(ip)).digest("hex").slice(0, 16);
305
+ }
306
+
307
  function payLimited(uid, ip) {
308
  const slot = Math.floor(Date.now() / 60000);
309
  const a = "pay:" + uid + ":" + slot;
 
350
  if (!ok) return null;
351
  const now = Math.floor(Date.now() / 1000);
352
  if (payload.exp <= now || payload.iat > now + 60) return null;
353
+ if (payload.auth_time && payload.auth_time > now + 60) return null;
354
+ if (payload.nbf && payload.nbf > now + 60) return null;
355
  if (payload.aud !== FB_PROJECT) return null;
356
  if (payload.iss !== "https://securetoken.google.com/" + FB_PROJECT) return null;
357
  if (!payload.sub || payload.sub.length > 128) return null;
 
779
  if (!(rec.duration > 1)) fillDuration(bot, rec, msgId).catch(function () {});
780
  const playMime = httpVideoType(rec.mime, rec.name);
781
  const exp = Math.floor(Date.now() / 1000) + TOKEN_TTL;
782
+ 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) });
783
  const host = "https://" + (req.headers.host || "localhost");
784
  json(res, 200, {
785
  url: host + "/stream/" + msgId + "?k=" + k,
786
  fileSize: rec.size,
787
  mime: playMime,
 
788
  duration: rec.duration || 0,
789
  accessLevel: lvl,
790
  expiry: exp
 
792
  }
793
 
794
  async function handleStream(req, res, url, msgId) {
795
+ if (!fromBlog(req)) {
 
796
  res.writeHead(403);
797
  return res.end();
798
  }
 
807
  return res.end();
808
  }
809
  const ip = clientIp(req);
810
+ if (!ticket.p || ticket.p !== ipTag(ip)) {
811
+ res.writeHead(403);
812
+ return res.end();
813
+ }
814
  if (!takeStream(ip)) {
815
  res.setHeader("Retry-After", "1");
816
  res.writeHead(429);
 
848
  const headers = {
849
  "Content-Type": httpVideoType(rec.mime, rec.name),
850
  "Accept-Ranges": "bytes",
851
+ "Cache-Control": "private, no-store, no-transform",
852
  "Content-Disposition": "inline",
853
  "X-Content-Type-Options": "nosniff"
854
  };
 
1072
  }
1073
 
1074
  async function handleBootstrap(req, res, url) {
1075
+ if (denyHot(req, res)) return;
1076
  json(res, 200, {
1077
  worker: SPACE_URLS[0] || "",
1078
  spaces: SPACE_URLS,