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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const { connect } = require("puppeteer-real-browser");
|
| 3 |
+
const fs = require('fs');
|
| 4 |
+
const path = require('path');
|
| 5 |
+
|
| 6 |
+
const app = express();
|
| 7 |
+
const port = process.env.PORT || 7860;
|
| 8 |
+
const authToken = process.env.authToken || null;
|
| 9 |
+
const domain = process.env.DOMAIN || `https://forgets-Antiv3.hf.space`;
|
| 10 |
+
|
| 11 |
+
global.browserLimit = Number(process.env.browserLimit) || 25;
|
| 12 |
+
global.timeOut = Number(process.env.timeOut) || 60000;
|
| 13 |
+
|
| 14 |
+
const CACHE_DIR = path.join(__dirname, "cache");
|
| 15 |
+
const CACHE_FILE = path.join(CACHE_DIR, "cache.json");
|
| 16 |
+
const CACHE_TTL = 5 * 60 * 1000;
|
| 17 |
+
|
| 18 |
+
function loadCache() {
|
| 19 |
+
if (!fs.existsSync(CACHE_FILE)) return {};
|
| 20 |
+
try {
|
| 21 |
+
return JSON.parse(fs.readFileSync(CACHE_FILE, 'utf-8'));
|
| 22 |
+
} catch {
|
| 23 |
+
return {};
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
function saveCache(cache) {
|
| 28 |
+
if (!fs.existsSync(CACHE_DIR)) {
|
| 29 |
+
fs.mkdirSync(CACHE_DIR, { recursive: true });
|
| 30 |
+
}
|
| 31 |
+
fs.writeFileSync(CACHE_FILE, JSON.stringify(cache, null, 2), 'utf-8');
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
function readCache(key) {
|
| 35 |
+
const cache = loadCache();
|
| 36 |
+
const entry = cache[key];
|
| 37 |
+
if (entry && Date.now() - entry.timestamp < CACHE_TTL) {
|
| 38 |
+
return entry.value;
|
| 39 |
+
}
|
| 40 |
+
return null;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
function writeCache(key, value) {
|
| 44 |
+
const cache = loadCache();
|
| 45 |
+
cache[key] = { timestamp: Date.now(), value };
|
| 46 |
+
saveCache(cache);
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
app.use(express.json({ limit: "50mb" }));
|
| 50 |
+
app.use(express.urlencoded({ extended: true, limit: "50mb" }));
|
| 51 |
+
|
| 52 |
+
app.get("/", (req, res) => {
|
| 53 |
+
res.json({
|
| 54 |
+
message: "Server is running!",
|
| 55 |
+
domain: domain,
|
| 56 |
+
endpoints: {
|
| 57 |
+
cloudflare: `${domain}/cloudflare`,
|
| 58 |
+
antibot: `${domain}/antibot`
|
| 59 |
+
},
|
| 60 |
+
status: {
|
| 61 |
+
browserLimit: global.browserLimit,
|
| 62 |
+
timeOut: global.timeOut,
|
| 63 |
+
authRequired: authToken !== null
|
| 64 |
+
}
|
| 65 |
+
});
|
| 66 |
+
});
|
| 67 |
+
|
| 68 |
+
if (process.env.NODE_ENV !== 'development') {
|
| 69 |
+
let server = app.listen(port, () => {
|
| 70 |
+
console.log(`Server running on port ${port}`);
|
| 71 |
+
console.log(`Domain: ${domain}`);
|
| 72 |
+
console.log(`Auth required: ${authToken !== null}`);
|
| 73 |
+
});
|
| 74 |
+
try {
|
| 75 |
+
server.timeout = global.timeOut;
|
| 76 |
+
} catch {}
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
async function createBrowser(proxyServer = null) {
|
| 80 |
+
const connectOptions = {
|
| 81 |
+
headless: false,
|
| 82 |
+
turnstile: true,
|
| 83 |
+
connectOption: { defaultViewport: null },
|
| 84 |
+
disableXvfb: false,
|
| 85 |
+
};
|
| 86 |
+
if (proxyServer) connectOptions.args = [`--proxy-server=${proxyServer}`];
|
| 87 |
+
|
| 88 |
+
const { browser } = await connect(connectOptions);
|
| 89 |
+
const [page] = await browser.pages();
|
| 90 |
+
|
| 91 |
+
await page.goto('about:blank');
|
| 92 |
+
await page.setRequestInterception(true);
|
| 93 |
+
page.on('request', (req) => {
|
| 94 |
+
const type = req.resourceType();
|
| 95 |
+
if (["image", "stylesheet", "font", "media"].includes(type)) req.abort();
|
| 96 |
+
else req.continue();
|
| 97 |
+
});
|
| 98 |
+
|
| 99 |
+
return { browser, page };
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
const turnstile = require('./endpoints/turnstile');
|
| 103 |
+
const cloudflare = require('./endpoints/cloudflare');
|
| 104 |
+
const antibot = require('./endpoints/antibot');
|
| 105 |
+
|
| 106 |
+
app.post('/cloudflare', async (req, res) => {
|
| 107 |
+
const data = req.body;
|
| 108 |
+
if (!data || typeof data.mode !== 'string')
|
| 109 |
+
return res.status(400).json({ message: 'Bad Request: missing or invalid mode' });
|
| 110 |
+
|
| 111 |
+
if (global.browserLimit <= 0)
|
| 112 |
+
return res.status(429).json({ message: 'Too Many Requests' });
|
| 113 |
+
|
| 114 |
+
let cacheKey, cached;
|
| 115 |
+
if (data.mode === "iuam") {
|
| 116 |
+
cacheKey = JSON.stringify(data);
|
| 117 |
+
cached = readCache(cacheKey);
|
| 118 |
+
if (cached) return res.status(200).json({ ...cached, cached: true });
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
global.browserLimit--;
|
| 122 |
+
let result, browser;
|
| 123 |
+
|
| 124 |
+
try {
|
| 125 |
+
const proxyServer = data.proxy ? `${data.proxy.hostname}:${data.proxy.port}` : null;
|
| 126 |
+
const ctx = await createBrowser(proxyServer);
|
| 127 |
+
browser = ctx.browser;
|
| 128 |
+
const page = ctx.page;
|
| 129 |
+
|
| 130 |
+
await page.goto('about:blank');
|
| 131 |
+
|
| 132 |
+
switch (data.mode) {
|
| 133 |
+
case "turnstile":
|
| 134 |
+
result = await turnstile(data, page).then(t => ({ token: t }));
|
| 135 |
+
break;
|
| 136 |
+
|
| 137 |
+
case "iuam":
|
| 138 |
+
result = await cloudflare(data, page).then(r => ({ ...r }));
|
| 139 |
+
writeCache(cacheKey, result);
|
| 140 |
+
break;
|
| 141 |
+
|
| 142 |
+
case "antibot":
|
| 143 |
+
result = await antibot(data);
|
| 144 |
+
break;
|
| 145 |
+
|
| 146 |
+
default:
|
| 147 |
+
result = { code: 400, message: 'Invalid mode' };
|
| 148 |
+
}
|
| 149 |
+
} catch (err) {
|
| 150 |
+
result = { code: 500, message: err.message };
|
| 151 |
+
} finally {
|
| 152 |
+
if (browser) try { await browser.close(); } catch {}
|
| 153 |
+
global.browserLimit++;
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
res.status(result.code ?? 200).json(result);
|
| 157 |
+
});
|
| 158 |
+
|
| 159 |
+
app.post("/antibot", async (req, res) => {
|
| 160 |
+
const data = req.body;
|
| 161 |
+
|
| 162 |
+
if (!data || !data.main || !Array.isArray(data.bots))
|
| 163 |
+
return res.status(400).json({ message: "Invalid body" });
|
| 164 |
+
|
| 165 |
+
try {
|
| 166 |
+
const result = await antibot(data);
|
| 167 |
+
res.json(result);
|
| 168 |
+
} catch (err) {
|
| 169 |
+
res.status(500).json({ message: err.message });
|
| 170 |
+
}
|
| 171 |
+
});
|
| 172 |
+
|
| 173 |
+
app.use((req, res) => {
|
| 174 |
+
res.status(404).json({ message: 'Not Found' });
|
| 175 |
+
});
|
| 176 |
+
|
| 177 |
+
if (process.env.NODE_ENV === 'development') {
|
| 178 |
+
module.exports = app;
|
| 179 |
+
}
|