Update index.js
Browse files
index.js
CHANGED
|
@@ -2,10 +2,10 @@ 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-Indrabot.hf.space`;
|
| 10 |
|
| 11 |
global.browserLimit = Number(process.env.browserLimit) || 100;
|
|
@@ -17,29 +17,18 @@ 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 };
|
|
@@ -49,19 +38,22 @@ function writeCache(key, value) {
|
|
| 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 |
-
|
| 58 |
-
antibot: `${domain}/antibot`
|
| 59 |
-
},
|
| 60 |
-
status: {
|
| 61 |
-
browserLimit: global.browserLimit,
|
| 62 |
-
timeOut: global.timeOut,
|
| 63 |
-
authRequired: authToken !== null
|
| 64 |
-
}
|
| 65 |
});
|
| 66 |
});
|
| 67 |
|
|
@@ -71,9 +63,7 @@ if (process.env.NODE_ENV !== 'development') {
|
|
| 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) {
|
|
@@ -84,10 +74,8 @@ async function createBrowser(proxyServer = 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) => {
|
|
@@ -95,7 +83,6 @@ async function createBrowser(proxyServer = null) {
|
|
| 95 |
if (["image", "stylesheet", "font", "media"].includes(type)) req.abort();
|
| 96 |
else req.continue();
|
| 97 |
});
|
| 98 |
-
|
| 99 |
return { browser, page };
|
| 100 |
}
|
| 101 |
|
|
@@ -103,13 +90,11 @@ const turnstile = require('./endpoints/turnstile');
|
|
| 103 |
const cloudflare = require('./endpoints/cloudflare');
|
| 104 |
const antibot = require('./endpoints/antibot');
|
| 105 |
|
| 106 |
-
|
|
|
|
| 107 |
const data = req.body;
|
| 108 |
-
if (!data || typeof data.mode !== 'string')
|
| 109 |
-
|
| 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") {
|
|
@@ -120,29 +105,24 @@ app.post('/cloudflare', async (req, res) => {
|
|
| 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 |
}
|
|
@@ -152,16 +132,12 @@ app.post('/cloudflare', async (req, res) => {
|
|
| 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);
|
|
|
|
| 2 |
const { connect } = require("puppeteer-real-browser");
|
| 3 |
const fs = require('fs');
|
| 4 |
const path = require('path');
|
|
|
|
| 5 |
const app = express();
|
| 6 |
+
|
| 7 |
const port = process.env.PORT || 7860;
|
| 8 |
+
const authToken = process.env.AUTH_TOKEN || process.env.authToken || null; // <- env name AUTH_TOKEN
|
| 9 |
const domain = process.env.DOMAIN || `https://forgets-Indrabot.hf.space`;
|
| 10 |
|
| 11 |
global.browserLimit = Number(process.env.browserLimit) || 100;
|
|
|
|
| 17 |
|
| 18 |
function loadCache() {
|
| 19 |
if (!fs.existsSync(CACHE_FILE)) return {};
|
| 20 |
+
try { return JSON.parse(fs.readFileSync(CACHE_FILE, 'utf-8')); } catch { return {}; }
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
}
|
|
|
|
| 22 |
function saveCache(cache) {
|
| 23 |
+
if (!fs.existsSync(CACHE_DIR)) fs.mkdirSync(CACHE_DIR, { recursive: true });
|
|
|
|
|
|
|
| 24 |
fs.writeFileSync(CACHE_FILE, JSON.stringify(cache, null, 2), 'utf-8');
|
| 25 |
}
|
|
|
|
| 26 |
function readCache(key) {
|
| 27 |
const cache = loadCache();
|
| 28 |
const entry = cache[key];
|
| 29 |
+
if (entry && Date.now() - entry.timestamp < CACHE_TTL) return entry.value;
|
|
|
|
|
|
|
| 30 |
return null;
|
| 31 |
}
|
|
|
|
| 32 |
function writeCache(key, value) {
|
| 33 |
const cache = loadCache();
|
| 34 |
cache[key] = { timestamp: Date.now(), value };
|
|
|
|
| 38 |
app.use(express.json({ limit: "50mb" }));
|
| 39 |
app.use(express.urlencoded({ extended: true, limit: "50mb" }));
|
| 40 |
|
| 41 |
+
// --- Auth middleware ---
|
| 42 |
+
function requireAuth(req, res, next) {
|
| 43 |
+
if (!authToken) return next(); // jika env tidak di-set, izinkan (backwards compat)
|
| 44 |
+
const h = req.headers.authorization || req.headers.Authorization || '';
|
| 45 |
+
if (!h.startsWith('Bearer ')) return res.status(401).json({ message: 'Unauthorized' });
|
| 46 |
+
const token = h.slice(7).trim();
|
| 47 |
+
if (token !== authToken) return res.status(403).json({ message: 'Forbidden' });
|
| 48 |
+
next();
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
app.get("/", (req, res) => {
|
| 52 |
res.json({
|
| 53 |
message: "Server is running!",
|
| 54 |
domain: domain,
|
| 55 |
+
endpoints: { cloudflare: `${domain}/cloudflare`, antibot: `${domain}/antibot` },
|
| 56 |
+
status: { browserLimit: global.browserLimit, timeOut: global.timeOut, authRequired: authToken !== null }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
});
|
| 58 |
});
|
| 59 |
|
|
|
|
| 63 |
console.log(`Domain: ${domain}`);
|
| 64 |
console.log(`Auth required: ${authToken !== null}`);
|
| 65 |
});
|
| 66 |
+
try { server.timeout = global.timeOut; } catch {}
|
|
|
|
|
|
|
| 67 |
}
|
| 68 |
|
| 69 |
async function createBrowser(proxyServer = null) {
|
|
|
|
| 74 |
disableXvfb: false,
|
| 75 |
};
|
| 76 |
if (proxyServer) connectOptions.args = [`--proxy-server=${proxyServer}`];
|
|
|
|
| 77 |
const { browser } = await connect(connectOptions);
|
| 78 |
const [page] = await browser.pages();
|
|
|
|
| 79 |
await page.goto('about:blank');
|
| 80 |
await page.setRequestInterception(true);
|
| 81 |
page.on('request', (req) => {
|
|
|
|
| 83 |
if (["image", "stylesheet", "font", "media"].includes(type)) req.abort();
|
| 84 |
else req.continue();
|
| 85 |
});
|
|
|
|
| 86 |
return { browser, page };
|
| 87 |
}
|
| 88 |
|
|
|
|
| 90 |
const cloudflare = require('./endpoints/cloudflare');
|
| 91 |
const antibot = require('./endpoints/antibot');
|
| 92 |
|
| 93 |
+
// gunakan requireAuth pada route yang butuh proteksi
|
| 94 |
+
app.post('/cloudflare', requireAuth, async (req, res) => {
|
| 95 |
const data = req.body;
|
| 96 |
+
if (!data || typeof data.mode !== 'string') return res.status(400).json({ message: 'Bad Request: missing or invalid mode' });
|
| 97 |
+
if (global.browserLimit <= 0) return res.status(429).json({ message: 'Too Many Requests' });
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
let cacheKey, cached;
|
| 100 |
if (data.mode === "iuam") {
|
|
|
|
| 105 |
|
| 106 |
global.browserLimit--;
|
| 107 |
let result, browser;
|
|
|
|
| 108 |
try {
|
| 109 |
const proxyServer = data.proxy ? `${data.proxy.hostname}:${data.proxy.port}` : null;
|
| 110 |
const ctx = await createBrowser(proxyServer);
|
| 111 |
browser = ctx.browser;
|
| 112 |
const page = ctx.page;
|
|
|
|
| 113 |
await page.goto('about:blank');
|
| 114 |
|
| 115 |
switch (data.mode) {
|
| 116 |
case "turnstile":
|
| 117 |
result = await turnstile(data, page).then(t => ({ token: t }));
|
| 118 |
break;
|
|
|
|
| 119 |
case "iuam":
|
| 120 |
result = await cloudflare(data, page).then(r => ({ ...r }));
|
| 121 |
writeCache(cacheKey, result);
|
| 122 |
break;
|
|
|
|
| 123 |
case "antibot":
|
| 124 |
result = await antibot(data);
|
| 125 |
break;
|
|
|
|
| 126 |
default:
|
| 127 |
result = { code: 400, message: 'Invalid mode' };
|
| 128 |
}
|
|
|
|
| 132 |
if (browser) try { await browser.close(); } catch {}
|
| 133 |
global.browserLimit++;
|
| 134 |
}
|
|
|
|
| 135 |
res.status(result.code ?? 200).json(result);
|
| 136 |
});
|
| 137 |
|
| 138 |
+
app.post("/antibot", requireAuth, async (req, res) => {
|
| 139 |
const data = req.body;
|
| 140 |
+
if (!data || !data.main || !Array.isArray(data.bots)) return res.status(400).json({ message: "Invalid body" });
|
|
|
|
|
|
|
|
|
|
| 141 |
try {
|
| 142 |
const result = await antibot(data);
|
| 143 |
res.json(result);
|