Forgets commited on
Commit
7358211
·
verified ·
1 Parent(s): 161f857

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +47 -85
index.js CHANGED
@@ -6,76 +6,53 @@ const path = require('path');
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-Limit2.hf.space`;
10
 
 
11
  global.browserLimit = Number(process.env.browserLimit) || 30;
12
  global.timeOut = Number(process.env.timeOut) || 120000;
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,
@@ -91,32 +68,41 @@ async function createBrowser(proxyServer = null) {
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;
@@ -127,53 +113,29 @@ app.post('/cloudflare', async (req, res) => {
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
- }
 
6
  const app = express();
7
  const port = process.env.PORT || 7860;
8
  const authToken = process.env.authToken || null;
9
+ const domain = process.env.DOMAIN || `Limit2`;
10
 
11
+ // Konfigurasi Global
12
  global.browserLimit = Number(process.env.browserLimit) || 30;
13
  global.timeOut = Number(process.env.timeOut) || 120000;
14
 
15
+ // Cache System (Pikeun Cloudflare/Turnstile)
16
  const CACHE_DIR = path.join(__dirname, "cache");
17
  const CACHE_FILE = path.join(CACHE_DIR, "cache.json");
18
  const CACHE_TTL = 5 * 60 * 1000;
19
 
20
  function loadCache() {
21
  if (!fs.existsSync(CACHE_FILE)) return {};
22
+ try { return JSON.parse(fs.readFileSync(CACHE_FILE, 'utf-8')); } catch { return {}; }
 
 
 
 
23
  }
24
 
25
  function saveCache(cache) {
26
+ if (!fs.existsSync(CACHE_DIR)) fs.mkdirSync(CACHE_DIR, { recursive: true });
 
 
27
  fs.writeFileSync(CACHE_FILE, JSON.stringify(cache, null, 2), 'utf-8');
28
  }
29
 
30
+ // Middleware
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  app.use(express.json({ limit: "50mb" }));
32
  app.use(express.urlencoded({ extended: true, limit: "50mb" }));
33
 
34
+ // --- IMPORT ENDPOINTS ---
35
+ // Ngaran variabel dijieun béda (solveAntibot) meh teu bentrok jeung route /antibot
36
+ const solveTurnstile = require('./endpoints/turnstile');
37
+ const solveCloudflare = require('./endpoints/cloudflare');
38
+ const solveAntibot = require('./endpoints/antibot');
39
+
40
+ // Dashboard Status
41
  app.get("/", (req, res) => {
42
  res.json({
43
  message: "Server is running!",
 
44
  endpoints: {
45
  cloudflare: `${domain}/cloudflare`,
46
  antibot: `${domain}/antibot`
47
  },
48
  status: {
49
  browserLimit: global.browserLimit,
 
50
  authRequired: authToken !== null
51
  }
52
  });
53
  });
54
 
55
+ // Browser Creator Utility
 
 
 
 
 
 
 
 
 
 
56
  async function createBrowser(proxyServer = null) {
57
  const connectOptions = {
58
  headless: false,
 
68
  await page.goto('about:blank');
69
  await page.setRequestInterception(true);
70
  page.on('request', (req) => {
71
+ if (["image", "stylesheet", "font", "media"].includes(req.resourceType())) req.abort();
 
72
  else req.continue();
73
  });
74
 
75
  return { browser, page };
76
  }
77
 
78
+ // --- ROUTE: ANTIBOT (KHUSUS PHP BOT) ---
79
+ app.post("/antibot", async (req, res) => {
 
 
 
80
  const data = req.body;
 
 
81
 
82
+ // Validasi data ti PHP
83
+ if (!data || !data.main || !Array.isArray(data.bots)) {
84
+ return res.status(400).json({ message: "Body format salah (kudu aya main & bots array)" });
85
+ }
86
 
87
+ try {
88
+ console.log("Menerima tantangan Antibot...");
89
+ // Manggil fungsi solveAntibot tina folder endpoints/
90
+ const result = await solveAntibot(data);
91
+
92
+ // Kirim hasilna balik ka PHP
93
+ res.json(result);
94
+ } catch (err) {
95
+ console.error("Error Antibot:", err.message);
96
+ res.status(500).json({ message: err.message });
97
  }
98
+ });
99
+
100
+ // --- ROUTE: CLOUDFLARE/TURNSTILE ---
101
+ app.post('/cloudflare', async (req, res) => {
102
+ const data = req.body;
103
+ if (!data || !data.mode) return res.status(400).json({ message: 'Missing mode' });
104
+
105
+ if (global.browserLimit <= 0) return res.status(429).json({ message: 'Server Busy' });
106
 
107
  global.browserLimit--;
108
  let result, browser;
 
113
  browser = ctx.browser;
114
  const page = ctx.page;
115
 
 
 
116
  switch (data.mode) {
117
  case "turnstile":
118
+ const token = await solveTurnstile(data, page);
119
+ result = { token };
120
  break;
 
121
  case "iuam":
122
+ result = await solveCloudflare(data, page);
 
123
  break;
 
 
 
 
 
124
  default:
125
  result = { code: 400, message: 'Invalid mode' };
126
  }
127
  } catch (err) {
128
  result = { code: 500, message: err.message };
129
  } finally {
130
+ if (browser) await browser.close();
131
  global.browserLimit++;
132
  }
133
 
134
  res.status(result.code ?? 200).json(result);
135
  });
136
 
137
+ // Start Server
138
+ const server = app.listen(port, () => {
139
+ console.log(`API Server jalan dina port ${port}`);
 
 
 
 
 
 
 
 
 
140
  });
141
+ server.timeout = global.timeOut;