source/dev/dev.js
2026-01-03 18:47:53 +00:00

230 lines
7.1 KiB
JavaScript

async function searchResults(keyword) {
try {
const encodedKeyword = encodeURIComponent(keyword);
const ddosInterceptor = new DdosGuardInterceptor();
const responseText = await ddosInterceptor.fetchWithBypass(`https://animepahe.si/api?m=search&q=${encodedKeyword}`);
const dataText = await responseText.text();
console.log(dataText);
const data = JSON.parse(dataText);
const transformedResults = data.data.map(result => {
return {
title: result.title,
image: result.poster,
href: `https://animepahe.si/anime/${result.session}`
};
});
return JSON.stringify(transformedResults);
} catch (error) {
console.log("Fetch error in searchResults: " + error);
return JSON.stringify([{ title: "Error", image: "", href: "" }]);
}
}
async function extractDetails(url) {
try {
const response = await fetchv2(url);
const html = await response.text();
return JSON.stringify([{
description: description,
aliases: "N/A",
airdate: "N/A"
}]);
} catch (err) {
return JSON.stringify([{
description: "Error",
aliases: "Error",
airdate: "Error"
}]);
}
}
async function extractEpisodes(url) {
const results = [];
try {
const response = await fetchv2(url);
const html = await response.text();
let match;
while ((match = regex.exec(html)) !== null) {
results.push({
href: match[1].trim(),
number: parseInt(match[2], 10)
});
}
return JSON.stringify(results);
} catch (err) {
return JSON.stringify([{
href: "Error",
number: "Error"
}]);
}
}
async function extractStreamUrl(url) {
try {
const response = await fetchv2(url);
const html = await response.text();
return "https://error.org/";
} catch (err) {
return "https://error.org/";
}
}
// Fixed DDOS Bypass
class DdosGuardInterceptor {
constructor() {
this.errorCodes = [403];
this.serverCheck = ["ddos-guard"];
this.cookieStore = {};
}
async fetchWithBypass(url, options = {}) {
let response = await this.fetchWithCookies(url, options);
const isBlocked = await this.isBlockedResponse(response);
if (!isBlocked) {
return response;
}
if (this.cookieStore["__ddg2_"]) {
return this.fetchWithCookies(url, options);
}
const newCookie = await this.getNewCookie(url);
if (!newCookie) {
return response;
}
return this.fetchWithCookies(url, options);
}
async fetchWithCookies(url, options) {
const cookieHeader = this.getCookieHeader();
const headers = { ...options.headers, Cookie: cookieHeader };
const response = await fetchv2(url, headers );
const setCookieHeader = response.headers["Set-Cookie"];
if (setCookieHeader) {
this.storeCookies(setCookieHeader);
}
return response;
}
isDdosGuard(response) {
const serverHeader = response.headers["Server"];
return serverHeader && this.serverCheck.includes(serverHeader.toLowerCase());
}
async isBlockedResponse(response) {
if (this.errorCodes.includes(response.status)) {
return true;
}
const clonedResponse = response.clone();
const text = await clonedResponse.text();
if (text.includes('ddos-guard/js-challenge') ||
text.includes('DDoS-Guard') ||
text.includes('data-ddg-origin')) {
return true;
}
return false;
}
storeCookies(setCookieString) {
const cookies = Array.isArray(setCookieString) ? setCookieString : [setCookieString];
cookies.forEach(cookieHeader => {
const parts = cookieHeader.split(";");
if (parts.length > 0) {
const [key, value] = parts[0].split("=");
if (key) {
this.cookieStore[key.trim()] = value?.trim() || "";
}
}
});
}
getCookieHeader() {
return Object.entries(this.cookieStore)
.map(([key, value]) => `${key}=${value}`)
.join("; ");
}
async getNewCookie(targetUrl) {
try {
const wellKnownResponse = await fetchv2("https://check.ddos-guard.net/check.js");
const wellKnownText = await wellKnownResponse.text();
const paths = wellKnownText.match(/['"](\/\.well-known\/ddos-guard\/[^'"]+)['"]/g);
const checkPaths = wellKnownText.match(/['"]https:\/\/check\.ddos-guard\.net\/[^'"]+['"]/g);
if (!paths || paths.length === 0) {
return null;
}
const localPath = paths[0].replace(/['"]/g, '');
const match = targetUrl.match(/^(https?:\/\/[^\/]+)/);
if (!match) {
return null;
}
const baseUrl = match[1];
const localUrl = `${baseUrl}${localPath}`;
const localResponse = await fetchv2(localUrl, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
'Referer': targetUrl
}
});
let setCookie = localResponse.headers["set-cookie"] ||
localResponse.headers["Set-Cookie"] ||
(localResponse.headers.raw && localResponse.headers.raw()['set-cookie']) ||
(localResponse.headers.get && localResponse.headers.get('set-cookie'));
if (setCookie) {
this.storeCookies(setCookie);
}
if (checkPaths && checkPaths.length > 0) {
const checkUrl = checkPaths[0].replace(/['"]/g, '');
const checkResponse = await fetchv2(checkUrl, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
'Referer': targetUrl
}
});
setCookie = checkResponse.headers["set-cookie"] ||
checkResponse.headers["Set-Cookie"] ||
(checkResponse.headers.raw && checkResponse.headers.raw()['set-cookie']) ||
(checkResponse.headers.get && checkResponse.headers.get('set-cookie'));
if (setCookie) {
this.storeCookies(setCookie);
}
}
if (this.cookieStore["__ddg2_"]) {
return this.cookieStore["__ddg2_"];
}
return null;
} catch (error) {
return null;
}
}
}