123 lines
5.4 KiB
JavaScript
123 lines
5.4 KiB
JavaScript
"use strict";
|
|
|
|
const { normalizeWatchUrl, cookieToken } = require("./jobValidation");
|
|
|
|
const PROBE_TIMEOUT_MS = 12_000;
|
|
const PROBE_MAX_BYTES = 1024 * 1024;
|
|
|
|
// This is a read-only download-access check, not an account-login assertion.
|
|
// The configured cookie may be a Joomla session or a verification cookie.
|
|
// scripts/lib/proxer_utils.py resolves the same exact `"code":"` marker;
|
|
// HTTP 200 alone, a missing marker, and a generic 403 prove neither login nor
|
|
// expiry. No protected library or media-transfer code is involved here.
|
|
function classifyWatchResponse(status, html, contentType = "text/html") {
|
|
const isHtml = typeof html === "string"
|
|
&& /^\s*(?:text\/html|application\/xhtml\+xml)(?:\s*;|\s*$)/i.test(contentType);
|
|
const title = isHtml ? html.match(/<title\b[^>]*>([\s\S]*?)<\/title\s*>/i)?.[1] || "" : "";
|
|
const challenge = isHtml && (/(?:Just a moment|Attention Required|Checking your browser)/i.test(title)
|
|
|| /\bid\s*=\s*["'](?:challenge-form|cf-challenge-running|cf-error-details)["']/i.test(html));
|
|
if (challenge) return { state: "verification" };
|
|
// These HTTP outcomes remain meaningful when an upstream error is JSON or
|
|
// plain text. An observed verification challenge still takes precedence.
|
|
if (status === 401) return { state: "invalid" };
|
|
if (status === 429 || status >= 500) return { state: "unavailable" };
|
|
if (status === 404 || status === 410) return { state: "source" };
|
|
if (status !== 200 || !isHtml) return { state: "unverifiable" };
|
|
|
|
// Restrict the marker's value to a plausible embed identifier; never return
|
|
// it or any upstream HTML, user information, signed URL, or response header.
|
|
if (/"code":"[A-Za-z0-9_-]{1,256}"/.test(html)) return { state: "valid" };
|
|
|
|
const visibleText = html.replace(/<!--[\s\S]*?-->/g, " ")
|
|
.replace(/<(script|style)\b[^>]*>[\s\S]*?<\/\1\s*>/gi, " ")
|
|
.replace(/<[^>]*>/g, " ").replace(/\s+/g, " ");
|
|
// Proxer's public logged-out pages use this explicit access-denied wording.
|
|
// Observed primary source: https://proxer.me/manga/doujin/clicks/S
|
|
// A login form in the navigation alone is not treated as an invalid cookie.
|
|
if (/Bitte logge dich ein, um diesen Bereich betreten zu k(?:ö|ö)nnen\./i.test(visibleText)) {
|
|
return { state: "invalid" };
|
|
}
|
|
if (/\bcaptcha\b/i.test(visibleText)) return { state: "verification" };
|
|
return { state: "unverifiable" };
|
|
}
|
|
|
|
async function boundedHtml(response, maxBytes) {
|
|
const declaredLength = Number(response.headers.get("content-length"));
|
|
if (Number.isFinite(declaredLength) && declaredLength > maxBytes) {
|
|
await response.body?.cancel().catch(() => {});
|
|
return null;
|
|
}
|
|
if (!response.body) return null;
|
|
const reader = response.body.getReader();
|
|
let size = 0;
|
|
const parts = [];
|
|
try {
|
|
while (true) {
|
|
const { done, value } = await reader.read();
|
|
if (done) return Buffer.concat(parts, size).toString("utf8");
|
|
size += value.byteLength;
|
|
if (size > maxBytes) {
|
|
await reader.cancel().catch(() => {});
|
|
return null;
|
|
}
|
|
parts.push(Buffer.from(value));
|
|
}
|
|
} finally {
|
|
reader.releaseLock();
|
|
}
|
|
}
|
|
|
|
function createProxerSessionProbe({ fetch: fetchImpl = globalThis.fetch, timeoutMs = PROBE_TIMEOUT_MS, maxBytes = PROBE_MAX_BYTES } = {}) {
|
|
return async function probe(cookie, { url } = {}) {
|
|
// Canonicalization forbids arbitrary hosts, ports, credentials, queries and
|
|
// non-watch paths. Redirects are never followed with the credential.
|
|
const target = normalizeWatchUrl(url).url;
|
|
const equals = typeof cookie === "string" ? cookie.indexOf("=") : -1;
|
|
if (equals < 1) throw new Error("A complete validated cookie is required for the access check.");
|
|
cookieToken(cookie.slice(equals + 1), cookie.slice(0, equals + 1));
|
|
const controller = new AbortController();
|
|
let timer;
|
|
try {
|
|
const inspect = (async () => {
|
|
const response = await fetchImpl(target, {
|
|
method: "GET", redirect: "manual", credentials: "omit", signal: controller.signal,
|
|
headers: {
|
|
"User-Agent": "Mozilla/5.0", Accept: "text/html,application/xhtml+xml",
|
|
"Cache-Control": "no-cache", Cookie: `${cookie}; stream_choose=proxer-stream`,
|
|
},
|
|
});
|
|
if (response.headers.get("cf-mitigated") === "challenge") {
|
|
await response.body?.cancel().catch(() => {});
|
|
return { state: "verification" };
|
|
}
|
|
if (response.status >= 300 && response.status < 400) {
|
|
await response.body?.cancel().catch(() => {});
|
|
return { state: "unverifiable" };
|
|
}
|
|
const html = await boundedHtml(response, maxBytes);
|
|
// An absent or oversized body cannot prove success. Preserve explicit
|
|
// HTTP rejection/source/availability outcomes; challenge headers were
|
|
// already handled before reading any body.
|
|
return classifyWatchResponse(response.status, html ?? "", response.headers.get("content-type") || "");
|
|
})();
|
|
const timeout = new Promise(resolve => {
|
|
timer = setTimeout(() => {
|
|
controller.abort();
|
|
resolve({ state: "unavailable" });
|
|
}, timeoutMs);
|
|
});
|
|
return await Promise.race([inspect, timeout]);
|
|
} catch {
|
|
return { state: "unavailable" };
|
|
} finally {
|
|
clearTimeout(timer);
|
|
controller.abort();
|
|
}
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
PROBE_TIMEOUT_MS, PROBE_MAX_BYTES, classifyWatchResponse, createProxerSessionProbe,
|
|
probeSessionToken: createProxerSessionProbe(),
|
|
};
|