XORE21 commited on
Commit
d8ea8ce
·
verified ·
1 Parent(s): 677da87

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +143 -142
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
- args: [
50
- '--no-sandbox',
51
- '--disable-setuid-sandbox',
52
- '--disable-dev-shm-usage',
53
- '--disable-accelerated-2d-canvas',
54
- '--no-first-run',
55
- '--no-zygote',
56
- '--disable-gpu'
57
- ],
58
- customConfig: {},
59
- connectOption: { defaultViewport: null }
60
- };
61
-
62
- if (proxy) {
63
- options.args.push(`--proxy-server=${proxy.hostname}:${proxy.port}`);
64
- }
65
-
66
- const { browser, page } = await connect(options);
67
-
68
- if (proxy && proxy.username && proxy.password) {
69
- await page.authenticate({ username: proxy.username, password: proxy.password });
70
- }
71
-
72
- return { browser, page };
73
- }
74
-
75
- const handleCloudflare = require('./endpoints/cloudflare');
76
- const handleTurnstile = require('./endpoints/turnstile');
77
-
78
- app.post('/bypass', async (req, res) => {
79
- const { url, mode, siteKey, proxy } = req.body;
80
-
81
- if (!url || !mode) {
82
- return res.status(400).json({ status: "error", message: "URL and Mode are required" });
83
- }
84
-
85
- if (activeBrowsers >= MAX_CONCURRENT_BROWSERS) {
86
- return res.status(429).json({ status: "busy", message: "Server is busy, please try again later" });
87
- }
88
-
89
- const cacheKey = JSON.stringify({ url, mode, siteKey });
90
- const cached = getCache(cacheKey);
91
- if (cached) return res.json({ status: "success", cached: true, ...cached });
92
-
93
- activeBrowsers++;
94
- let browserInstance = null;
95
-
96
- try {
97
- const { browser, page } = await createBrowser(proxy);
98
- browserInstance = browser;
99
-
100
- let result;
101
- if (mode === 'cloudflare') {
102
- result = await handleCloudflare(page, url);
103
- } else if (mode === 'turnstile') {
104
- if (!siteKey) throw new Error("Sitekey is required for Turnstile mode");
105
- result = await handleTurnstile(page, url, siteKey);
106
- } else {
107
- throw new Error("Unrecognized mode (use : cloudflare / turnstile)");
108
- }
109
-
110
- if (result) setCache(cacheKey, result);
111
-
112
- res.json({ status: "success", ...result });
113
-
114
- } catch (error) {
115
- console.error("Error Processing : ", error.message);
116
- res.status(500).json({ status: "error", message: error.message });
117
- } finally {
118
- if (browserInstance) {
119
- try { await browserInstance.close(); } catch (e) {}
120
- }
121
- activeBrowsers--;
122
- }
123
- });
124
-
125
- app.get('/', (req, res) => {
126
- res.json({
127
- status: "Running",
128
- active_browsers: activeBrowsers,
129
- max_browsers: MAX_CONCURRENT_BROWSERS,
130
- usage: {
131
- method: "POST",
132
- endpoint: "/bypass",
133
- body: {
134
- url: "https://target.com",
135
- mode: "cloudflare | turnstile",
136
- siteKey: "0x4AAAA... (only for turnstile)",
137
- proxy: { hostname: "...", port: "...", username: "...", password: "..." }
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}`));