Update index.js
Browse files
index.js
CHANGED
|
@@ -1,143 +1,144 @@
|
|
| 1 |
-
const express = require('express');
|
| 2 |
-
const cors = require('cors');
|
| 3 |
-
const { connect } = require("puppeteer-real-browser");
|
| 4 |
-
const fs = require('fs');
|
| 5 |
-
const path = require('path');
|
| 6 |
-
|
| 7 |
-
const app = express();
|
| 8 |
-
app.use(cors());
|
| 9 |
-
app.use(express.json());
|
| 10 |
-
|
| 11 |
-
const PORT = process.env.PORT || 7860;
|
| 12 |
-
const MAX_CONCURRENT_BROWSERS = Number(process.env.MAX_BROWSERS) || 5;
|
| 13 |
-
let activeBrowsers = 0;
|
| 14 |
-
|
| 15 |
-
const CACHE_DIR = path.join(__dirname, "cache");
|
| 16 |
-
const CACHE_FILE = path.join(CACHE_DIR, "cache.json");
|
| 17 |
-
const CACHE_TTL = 30 * 60 * 1000;
|
| 18 |
-
|
| 19 |
-
function getCache(key) {
|
| 20 |
-
if (!fs.existsSync(CACHE_FILE)) return null;
|
| 21 |
-
try {
|
| 22 |
-
const data = JSON.parse(fs.readFileSync(CACHE_FILE, 'utf-8'));
|
| 23 |
-
if (data[key] && Date.now() - data[key].timestamp < CACHE_TTL) {
|
| 24 |
-
return data[key].value;
|
| 25 |
-
}
|
| 26 |
-
} catch (e) { return null; }
|
| 27 |
-
return null;
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
-
function setCache(key, value) {
|
| 31 |
-
try {
|
| 32 |
-
if (!fs.existsSync(CACHE_DIR)) {
|
| 33 |
-
fs.mkdirSync(CACHE_DIR, { recursive: true });
|
| 34 |
-
}
|
| 35 |
-
|
| 36 |
-
let data = {};
|
| 37 |
-
if (fs.existsSync(CACHE_FILE)) {
|
| 38 |
-
data = JSON.parse(fs.readFileSync(CACHE_FILE, 'utf-8'));
|
| 39 |
-
}
|
| 40 |
-
data[key] = { timestamp: Date.now(), value };
|
| 41 |
-
fs.writeFileSync(CACHE_FILE, JSON.stringify(data, null, 2));
|
| 42 |
-
} catch (e) { console.error("Cache Error : ", e); }
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
async function createBrowser(proxy = null) {
|
| 46 |
-
const options = {
|
| 47 |
-
headless: false,
|
| 48 |
-
turnstile: true,
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
'--
|
| 52 |
-
'--disable-
|
| 53 |
-
'--disable-
|
| 54 |
-
'--
|
| 55 |
-
'--no-
|
| 56 |
-
'--
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
}
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
const
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
const
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
}
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
});
|
| 142 |
-
|
|
|
|
| 143 |
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const cors = require('cors');
|
| 3 |
+
const { connect } = require("puppeteer-real-browser");
|
| 4 |
+
const fs = require('fs');
|
| 5 |
+
const path = require('path');
|
| 6 |
+
|
| 7 |
+
const app = express();
|
| 8 |
+
app.use(cors());
|
| 9 |
+
app.use(express.json());
|
| 10 |
+
|
| 11 |
+
const PORT = process.env.PORT || 7860;
|
| 12 |
+
const MAX_CONCURRENT_BROWSERS = Number(process.env.MAX_BROWSERS) || 5;
|
| 13 |
+
let activeBrowsers = 0;
|
| 14 |
+
|
| 15 |
+
const CACHE_DIR = path.join(__dirname, "cache");
|
| 16 |
+
const CACHE_FILE = path.join(CACHE_DIR, "cache.json");
|
| 17 |
+
const CACHE_TTL = 30 * 60 * 1000;
|
| 18 |
+
|
| 19 |
+
function getCache(key) {
|
| 20 |
+
if (!fs.existsSync(CACHE_FILE)) return null;
|
| 21 |
+
try {
|
| 22 |
+
const data = JSON.parse(fs.readFileSync(CACHE_FILE, 'utf-8'));
|
| 23 |
+
if (data[key] && Date.now() - data[key].timestamp < CACHE_TTL) {
|
| 24 |
+
return data[key].value;
|
| 25 |
+
}
|
| 26 |
+
} catch (e) { return null; }
|
| 27 |
+
return null;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
function setCache(key, value) {
|
| 31 |
+
try {
|
| 32 |
+
if (!fs.existsSync(CACHE_DIR)) {
|
| 33 |
+
fs.mkdirSync(CACHE_DIR, { recursive: true });
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
let data = {};
|
| 37 |
+
if (fs.existsSync(CACHE_FILE)) {
|
| 38 |
+
data = JSON.parse(fs.readFileSync(CACHE_FILE, 'utf-8'));
|
| 39 |
+
}
|
| 40 |
+
data[key] = { timestamp: Date.now(), value };
|
| 41 |
+
fs.writeFileSync(CACHE_FILE, JSON.stringify(data, null, 2));
|
| 42 |
+
} catch (e) { console.error("Cache Error : ", e); }
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
async function createBrowser(proxy = null) {
|
| 46 |
+
const options = {
|
| 47 |
+
headless: false,
|
| 48 |
+
turnstile: true,
|
| 49 |
+
executablePath: process.env.CHROME_PATH,
|
| 50 |
+
args: [
|
| 51 |
+
'--no-sandbox',
|
| 52 |
+
'--disable-setuid-sandbox',
|
| 53 |
+
'--disable-dev-shm-usage',
|
| 54 |
+
'--disable-accelerated-2d-canvas',
|
| 55 |
+
'--no-first-run',
|
| 56 |
+
'--no-zygote',
|
| 57 |
+
'--disable-gpu'
|
| 58 |
+
],
|
| 59 |
+
customConfig: {},
|
| 60 |
+
connectOption: { defaultViewport: null }
|
| 61 |
+
};
|
| 62 |
+
|
| 63 |
+
if (proxy) {
|
| 64 |
+
options.args.push(`--proxy-server=${proxy.hostname}:${proxy.port}`);
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
const { browser, page } = await connect(options);
|
| 68 |
+
|
| 69 |
+
if (proxy && proxy.username && proxy.password) {
|
| 70 |
+
await page.authenticate({ username: proxy.username, password: proxy.password });
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
return { browser, page };
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
const handleCloudflare = require('./endpoints/cloudflare');
|
| 77 |
+
const handleTurnstile = require('./endpoints/turnstile');
|
| 78 |
+
|
| 79 |
+
app.post('/bypass', async (req, res) => {
|
| 80 |
+
const { url, mode, siteKey, proxy } = req.body;
|
| 81 |
+
|
| 82 |
+
if (!url || !mode) {
|
| 83 |
+
return res.status(400).json({ status: "error", message: "URL and Mode are required" });
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
if (activeBrowsers >= MAX_CONCURRENT_BROWSERS) {
|
| 87 |
+
return res.status(429).json({ status: "busy", message: "Server is busy, please try again later" });
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
const cacheKey = JSON.stringify({ url, mode, siteKey });
|
| 91 |
+
const cached = getCache(cacheKey);
|
| 92 |
+
if (cached) return res.json({ status: "success", cached: true, ...cached });
|
| 93 |
+
|
| 94 |
+
activeBrowsers++;
|
| 95 |
+
let browserInstance = null;
|
| 96 |
+
|
| 97 |
+
try {
|
| 98 |
+
const { browser, page } = await createBrowser(proxy);
|
| 99 |
+
browserInstance = browser;
|
| 100 |
+
|
| 101 |
+
let result;
|
| 102 |
+
if (mode === 'cloudflare') {
|
| 103 |
+
result = await handleCloudflare(page, url);
|
| 104 |
+
} else if (mode === 'turnstile') {
|
| 105 |
+
if (!siteKey) throw new Error("Sitekey is required for Turnstile mode");
|
| 106 |
+
result = await handleTurnstile(page, url, siteKey);
|
| 107 |
+
} else {
|
| 108 |
+
throw new Error("Unrecognized mode (use : cloudflare / turnstile)");
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
if (result) setCache(cacheKey, result);
|
| 112 |
+
|
| 113 |
+
res.json({ status: "success", ...result });
|
| 114 |
+
|
| 115 |
+
} catch (error) {
|
| 116 |
+
console.error("Error Processing : ", error.message);
|
| 117 |
+
res.status(500).json({ status: "error", message: error.message });
|
| 118 |
+
} finally {
|
| 119 |
+
if (browserInstance) {
|
| 120 |
+
try { await browserInstance.close(); } catch (e) {}
|
| 121 |
+
}
|
| 122 |
+
activeBrowsers--;
|
| 123 |
+
}
|
| 124 |
+
});
|
| 125 |
+
|
| 126 |
+
app.get('/', (req, res) => {
|
| 127 |
+
res.json({
|
| 128 |
+
status: "Running",
|
| 129 |
+
active_browsers: activeBrowsers,
|
| 130 |
+
max_browsers: MAX_CONCURRENT_BROWSERS,
|
| 131 |
+
usage: {
|
| 132 |
+
method: "POST",
|
| 133 |
+
endpoint: "/bypass",
|
| 134 |
+
body: {
|
| 135 |
+
url: "https://target.com",
|
| 136 |
+
mode: "cloudflare | turnstile",
|
| 137 |
+
siteKey: "0x4AAAA... (only for turnstile)",
|
| 138 |
+
proxy: { hostname: "...", port: "...", username: "...", password: "..." }
|
| 139 |
+
}
|
| 140 |
+
}
|
| 141 |
+
});
|
| 142 |
+
});
|
| 143 |
+
|
| 144 |
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
|