Spaces:
Running
Running
JOHN SILLVESTOR commited on
play buffer window and fetch-metadata locks
Browse files
server.js
CHANGED
|
@@ -20,7 +20,7 @@ const GET_SIZES = [1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4
|
|
| 20 |
const FIRST_GET = 1048576;
|
| 21 |
const HOT_HEAD = 1048576;
|
| 22 |
const HOT_TAIL = 1048576;
|
| 23 |
-
const STREAM_WINDOW =
|
| 24 |
const BOT_MAX = 24;
|
| 25 |
const PIPE_MS = 45000;
|
| 26 |
const PIPE_MAX_MS = 30 * 60 * 1000;
|
|
@@ -137,16 +137,35 @@ function cors(req, res) {
|
|
| 137 |
res.setHeader("X-Content-Type-Options", "nosniff");
|
| 138 |
res.setHeader("Referrer-Policy", "strict-origin-when-cross-origin");
|
| 139 |
res.setHeader("X-Frame-Options", "DENY");
|
| 140 |
-
res.setHeader("X-Robots-Tag", "noindex, nofollow");
|
|
|
|
| 141 |
}
|
| 142 |
|
| 143 |
function json(res, code, body) {
|
| 144 |
if (res.headersSent || res.writableEnded) return;
|
| 145 |
const s = JSON.stringify(body);
|
|
|
|
| 146 |
res.writeHead(code, { "Content-Type": "application/json; charset=utf-8", "Cache-Control": "no-store" });
|
| 147 |
res.end(s);
|
| 148 |
}
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
function clientIp(req) {
|
| 151 |
const xf = req.headers["x-forwarded-for"];
|
| 152 |
if (xf) return String(xf).split(",")[0].trim() || "0";
|
|
@@ -961,6 +980,7 @@ async function readBody(req, max) {
|
|
| 961 |
|
| 962 |
async function handleGenerate(req, res, url) {
|
| 963 |
if (!aesKey) return json(res, 503, { error: "misconfigured" });
|
|
|
|
| 964 |
if (denyHot(req, res)) return;
|
| 965 |
const ip = clientIp(req);
|
| 966 |
if (limited(ip)) {
|
|
@@ -1010,6 +1030,10 @@ async function handleGenerate(req, res, url) {
|
|
| 1010 |
}
|
| 1011 |
|
| 1012 |
async function handleStream(req, res, url, msgId) {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1013 |
const o = String(req.headers.origin || "").trim();
|
| 1014 |
if (o && o.toLowerCase() !== "null" && !isBlogOrigin(o)) {
|
| 1015 |
res.writeHead(403);
|
|
@@ -1058,17 +1082,16 @@ async function handleStream(req, res, url, msgId) {
|
|
| 1058 |
const rngHot = parseRange(req.headers.range, sizeHint);
|
| 1059 |
const needHot = rngHot.end - rngHot.start + 1;
|
| 1060 |
const rangedHot = !!req.headers.range;
|
| 1061 |
-
const headersHot = {
|
| 1062 |
"Content-Type": typeHint,
|
| 1063 |
"Accept-Ranges": "bytes",
|
| 1064 |
"Cache-Control": "private, no-store, no-transform",
|
| 1065 |
"Content-Disposition": "inline",
|
| 1066 |
"Content-Encoding": "identity",
|
| 1067 |
"ETag": '"' + msgId + "-" + sizeHint + "-" + (ticket.h || "x") + '"',
|
| 1068 |
-
"X-Content-Type-Options": "nosniff",
|
| 1069 |
"X-Accel-Buffering": "no",
|
| 1070 |
"Content-Length": String(needHot)
|
| 1071 |
-
};
|
| 1072 |
if (rangedHot) {
|
| 1073 |
headersHot["Content-Range"] = "bytes " + rngHot.start + "-" + rngHot.end + "/" + sizeHint;
|
| 1074 |
}
|
|
@@ -1105,16 +1128,15 @@ async function handleStream(req, res, url, msgId) {
|
|
| 1105 |
const size = rec.size;
|
| 1106 |
const rng = parseRange(req.headers.range, size);
|
| 1107 |
const ranged = !!req.headers.range;
|
| 1108 |
-
const headers = {
|
| 1109 |
"Content-Type": httpVideoType(rec.mime, rec.name),
|
| 1110 |
"Accept-Ranges": "bytes",
|
| 1111 |
"Cache-Control": "private, no-store, no-transform",
|
| 1112 |
"Content-Disposition": "inline",
|
| 1113 |
"Content-Encoding": "identity",
|
| 1114 |
"ETag": '"' + msgId + "-" + size + "-" + (rec.hash || "x") + '"',
|
| 1115 |
-
"X-Content-Type-Options": "nosniff",
|
| 1116 |
"X-Accel-Buffering": "no"
|
| 1117 |
-
};
|
| 1118 |
headers["Content-Length"] = String(rng.end - rng.start + 1);
|
| 1119 |
if (ranged) {
|
| 1120 |
headers["Content-Range"] = "bytes " + rng.start + "-" + rng.end + "/" + size;
|
|
@@ -1155,6 +1177,7 @@ async function handleStream(req, res, url, msgId) {
|
|
| 1155 |
}
|
| 1156 |
|
| 1157 |
async function handleVipStatus(req, res) {
|
|
|
|
| 1158 |
if (denyHot(req, res)) return;
|
| 1159 |
let lvl = "anonymous";
|
| 1160 |
try {
|
|
@@ -1172,6 +1195,7 @@ async function handleVipStatus(req, res) {
|
|
| 1172 |
async function handleVipStart(req, res) {
|
| 1173 |
if (ROLE !== "indexer") return json(res, 409, { error: "use_s1" });
|
| 1174 |
if (!STRIPE_KEY) return json(res, 503, { error: "stripe_not_configured" });
|
|
|
|
| 1175 |
if (denyHot(req, res)) return;
|
| 1176 |
const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 1200, null);
|
| 1177 |
if (!idp) return json(res, 401, { error: "login" });
|
|
@@ -1220,6 +1244,7 @@ async function handleVipStart(req, res) {
|
|
| 1220 |
async function handleVipGo(req, res) {
|
| 1221 |
if (ROLE !== "indexer") return json(res, 409, { error: "use_s1" });
|
| 1222 |
if (!STRIPE_KEY) return json(res, 503, { error: "stripe_not_configured" });
|
|
|
|
| 1223 |
if (denyHot(req, res)) return;
|
| 1224 |
const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 1200, null);
|
| 1225 |
if (!idp) return json(res, 401, { error: "login" });
|
|
@@ -1334,6 +1359,7 @@ async function handleStripeWebhook(req, res) {
|
|
| 1334 |
}
|
| 1335 |
|
| 1336 |
async function handleBootstrap(req, res, url) {
|
|
|
|
| 1337 |
if (denyHot(req, res)) return;
|
| 1338 |
json(res, 200, {
|
| 1339 |
worker: SPACE_URLS[0] || "",
|
|
@@ -1359,10 +1385,11 @@ function handleHealth(res) {
|
|
| 1359 |
function handleHome(req, res) {
|
| 1360 |
const acc = String(req.headers.accept || "");
|
| 1361 |
if (!acc.includes("text/html")) return handleHealth(res);
|
| 1362 |
-
res.writeHead(200, {
|
| 1363 |
"Content-Type": "text/html; charset=utf-8",
|
| 1364 |
-
"Cache-Control": "no-store"
|
| 1365 |
-
|
|
|
|
| 1366 |
res.end(
|
| 1367 |
"<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\"><title>DUBLK Studio</title><style>body{font-family:system-ui,sans-serif;margin:2.5rem auto;max-width:36rem;line-height:1.55;color:#111827}a{color:#4f46e5}</style></head><body><h1>DUBLK Studio</h1><p>Node.js HTTP API for the public DUBLK blog demo.</p><p><a href=\"/health\">Health status</a></p></body></html>"
|
| 1368 |
);
|
|
|
|
| 20 |
const FIRST_GET = 1048576;
|
| 21 |
const HOT_HEAD = 1048576;
|
| 22 |
const HOT_TAIL = 1048576;
|
| 23 |
+
const STREAM_WINDOW = 4 * MB;
|
| 24 |
const BOT_MAX = 24;
|
| 25 |
const PIPE_MS = 45000;
|
| 26 |
const PIPE_MAX_MS = 30 * 60 * 1000;
|
|
|
|
| 137 |
res.setHeader("X-Content-Type-Options", "nosniff");
|
| 138 |
res.setHeader("Referrer-Policy", "strict-origin-when-cross-origin");
|
| 139 |
res.setHeader("X-Frame-Options", "DENY");
|
| 140 |
+
res.setHeader("X-Robots-Tag", "noindex, nofollow, noarchive");
|
| 141 |
+
res.setHeader("Permissions-Policy", "camera=(), microphone=(), geolocation=(), usb=(), payment=(), browsing-topics=()");
|
| 142 |
}
|
| 143 |
|
| 144 |
function json(res, code, body) {
|
| 145 |
if (res.headersSent || res.writableEnded) return;
|
| 146 |
const s = JSON.stringify(body);
|
| 147 |
+
res.setHeader("Cross-Origin-Resource-Policy", "same-origin");
|
| 148 |
res.writeHead(code, { "Content-Type": "application/json; charset=utf-8", "Cache-Control": "no-store" });
|
| 149 |
res.end(s);
|
| 150 |
}
|
| 151 |
|
| 152 |
+
function destOk(req, allow) {
|
| 153 |
+
const d = String(req.headers["sec-fetch-dest"] || "").toLowerCase();
|
| 154 |
+
if (!d) return true;
|
| 155 |
+
return allow.indexOf(d) >= 0;
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
function streamLock() {
|
| 159 |
+
return {
|
| 160 |
+
"X-Content-Type-Options": "nosniff",
|
| 161 |
+
"X-Frame-Options": "DENY",
|
| 162 |
+
"Referrer-Policy": "strict-origin-when-cross-origin",
|
| 163 |
+
"X-Robots-Tag": "noindex, nofollow, noarchive",
|
| 164 |
+
"Permissions-Policy": "camera=(), microphone=(), geolocation=(), usb=(), payment=(), browsing-topics=()",
|
| 165 |
+
"Cross-Origin-Resource-Policy": "cross-origin"
|
| 166 |
+
};
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
function clientIp(req) {
|
| 170 |
const xf = req.headers["x-forwarded-for"];
|
| 171 |
if (xf) return String(xf).split(",")[0].trim() || "0";
|
|
|
|
| 980 |
|
| 981 |
async function handleGenerate(req, res, url) {
|
| 982 |
if (!aesKey) return json(res, 503, { error: "misconfigured" });
|
| 983 |
+
if (!destOk(req, ["empty"])) return json(res, 403, { error: "forbidden" });
|
| 984 |
if (denyHot(req, res)) return;
|
| 985 |
const ip = clientIp(req);
|
| 986 |
if (limited(ip)) {
|
|
|
|
| 1030 |
}
|
| 1031 |
|
| 1032 |
async function handleStream(req, res, url, msgId) {
|
| 1033 |
+
if (!destOk(req, ["video", "audio", "empty"])) {
|
| 1034 |
+
res.writeHead(403);
|
| 1035 |
+
return res.end();
|
| 1036 |
+
}
|
| 1037 |
const o = String(req.headers.origin || "").trim();
|
| 1038 |
if (o && o.toLowerCase() !== "null" && !isBlogOrigin(o)) {
|
| 1039 |
res.writeHead(403);
|
|
|
|
| 1082 |
const rngHot = parseRange(req.headers.range, sizeHint);
|
| 1083 |
const needHot = rngHot.end - rngHot.start + 1;
|
| 1084 |
const rangedHot = !!req.headers.range;
|
| 1085 |
+
const headersHot = Object.assign(streamLock(), {
|
| 1086 |
"Content-Type": typeHint,
|
| 1087 |
"Accept-Ranges": "bytes",
|
| 1088 |
"Cache-Control": "private, no-store, no-transform",
|
| 1089 |
"Content-Disposition": "inline",
|
| 1090 |
"Content-Encoding": "identity",
|
| 1091 |
"ETag": '"' + msgId + "-" + sizeHint + "-" + (ticket.h || "x") + '"',
|
|
|
|
| 1092 |
"X-Accel-Buffering": "no",
|
| 1093 |
"Content-Length": String(needHot)
|
| 1094 |
+
});
|
| 1095 |
if (rangedHot) {
|
| 1096 |
headersHot["Content-Range"] = "bytes " + rngHot.start + "-" + rngHot.end + "/" + sizeHint;
|
| 1097 |
}
|
|
|
|
| 1128 |
const size = rec.size;
|
| 1129 |
const rng = parseRange(req.headers.range, size);
|
| 1130 |
const ranged = !!req.headers.range;
|
| 1131 |
+
const headers = Object.assign(streamLock(), {
|
| 1132 |
"Content-Type": httpVideoType(rec.mime, rec.name),
|
| 1133 |
"Accept-Ranges": "bytes",
|
| 1134 |
"Cache-Control": "private, no-store, no-transform",
|
| 1135 |
"Content-Disposition": "inline",
|
| 1136 |
"Content-Encoding": "identity",
|
| 1137 |
"ETag": '"' + msgId + "-" + size + "-" + (rec.hash || "x") + '"',
|
|
|
|
| 1138 |
"X-Accel-Buffering": "no"
|
| 1139 |
+
});
|
| 1140 |
headers["Content-Length"] = String(rng.end - rng.start + 1);
|
| 1141 |
if (ranged) {
|
| 1142 |
headers["Content-Range"] = "bytes " + rng.start + "-" + rng.end + "/" + size;
|
|
|
|
| 1177 |
}
|
| 1178 |
|
| 1179 |
async function handleVipStatus(req, res) {
|
| 1180 |
+
if (!destOk(req, ["empty"])) return json(res, 403, { error: "forbidden" });
|
| 1181 |
if (denyHot(req, res)) return;
|
| 1182 |
let lvl = "anonymous";
|
| 1183 |
try {
|
|
|
|
| 1195 |
async function handleVipStart(req, res) {
|
| 1196 |
if (ROLE !== "indexer") return json(res, 409, { error: "use_s1" });
|
| 1197 |
if (!STRIPE_KEY) return json(res, 503, { error: "stripe_not_configured" });
|
| 1198 |
+
if (!destOk(req, ["empty"])) return json(res, 403, { error: "forbidden" });
|
| 1199 |
if (denyHot(req, res)) return;
|
| 1200 |
const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 1200, null);
|
| 1201 |
if (!idp) return json(res, 401, { error: "login" });
|
|
|
|
| 1244 |
async function handleVipGo(req, res) {
|
| 1245 |
if (ROLE !== "indexer") return json(res, 409, { error: "use_s1" });
|
| 1246 |
if (!STRIPE_KEY) return json(res, 503, { error: "stripe_not_configured" });
|
| 1247 |
+
if (!destOk(req, ["empty"])) return json(res, 403, { error: "forbidden" });
|
| 1248 |
if (denyHot(req, res)) return;
|
| 1249 |
const idp = await withTime(verifyIdToken(req.headers.authorization || ""), 1200, null);
|
| 1250 |
if (!idp) return json(res, 401, { error: "login" });
|
|
|
|
| 1359 |
}
|
| 1360 |
|
| 1361 |
async function handleBootstrap(req, res, url) {
|
| 1362 |
+
if (!destOk(req, ["empty"])) return json(res, 403, { error: "forbidden" });
|
| 1363 |
if (denyHot(req, res)) return;
|
| 1364 |
json(res, 200, {
|
| 1365 |
worker: SPACE_URLS[0] || "",
|
|
|
|
| 1385 |
function handleHome(req, res) {
|
| 1386 |
const acc = String(req.headers.accept || "");
|
| 1387 |
if (!acc.includes("text/html")) return handleHealth(res);
|
| 1388 |
+
res.writeHead(200, Object.assign(streamLock(), {
|
| 1389 |
"Content-Type": "text/html; charset=utf-8",
|
| 1390 |
+
"Cache-Control": "no-store",
|
| 1391 |
+
"Cross-Origin-Resource-Policy": "same-origin"
|
| 1392 |
+
}));
|
| 1393 |
res.end(
|
| 1394 |
"<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\"><title>DUBLK Studio</title><style>body{font-family:system-ui,sans-serif;margin:2.5rem auto;max-width:36rem;line-height:1.55;color:#111827}a{color:#4f46e5}</style></head><body><h1>DUBLK Studio</h1><p>Node.js HTTP API for the public DUBLK blog demo.</p><p><a href=\"/health\">Health status</a></p></body></html>"
|
| 1395 |
);
|