vipsphi commited on
Commit
741c4a4
·
verified ·
1 Parent(s): 959cf19

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +59 -77
server.js CHANGED
@@ -1,29 +1,15 @@
1
- const { app: electronApp, session } = require("electron");
2
-
3
- // Global Proxy Auth Handler
4
- electronApp.on("login", (event, webContents, details, authInfo, callback) => {
5
- if (authInfo.isProxy) {
6
- const ses = webContents.session;
7
- if (ses.proxyCredentials) {
8
- logger.debug(`[Main] Providing credentials for proxy via app.on('login')`);
9
- event.preventDefault();
10
- callback(ses.proxyCredentials.username, ses.proxyCredentials.password);
11
- }
12
- }
13
- });
14
-
15
  const express = require("express");
16
  const bodyParser = require("body-parser");
17
  const cors = require("cors");
18
- const sqlite3 = require("sqlite3");
19
  const { v4: uuidv4 } = require("uuid");
20
  const path = require("path");
21
  const RecaptchaSolver = require("./solver");
22
  const http = require("http");
23
  const { Server } = require("socket.io");
24
- const logger = require("./logger");
25
 
26
- const PORT = 7860;
27
  const app = express();
28
  const server = http.createServer(app);
29
  const io = new Server(server, {
@@ -94,7 +80,7 @@ app.post("/api/register", (req, res) => {
94
 
95
  // Kiểm tra xem HWID này đã được cộng tiền lần nào chưa
96
  db.get("SELECT id FROM users WHERE hwid = ?", [hwid], (err, row) => {
97
- const initialCredits = row ? 0 : 100; // Nếu đã tồn tại HWID thì 0, mới thì 10
98
  const message = row
99
  ? "Đăng ký thành công! (Máy này đã nhận bonus trước đó nên không được cộng thêm)"
100
  : "Đăng ký thành công! Bạn nhận được +100 Credits bonus.";
@@ -220,6 +206,9 @@ app.post("/api/admin/approve", requireAdmin, (req, res) => {
220
  return res.status(400).json({ error: "Giao dịch không hợp lệ" });
221
 
222
  if (action === "reject") {
 
 
 
223
  db.run("UPDATE transactions SET status = 'rejected' WHERE id = ?", [
224
  transId,
225
  ]);
@@ -273,59 +262,10 @@ app.post("/api/admin/update-credits", requireAdmin, (req, res) => {
273
  );
274
  });
275
 
276
- // --- CAPTCHA SOLVE QUEUE ---
277
- const solveQueue = [];
278
- let activeSolves = 0;
279
- const MAX_CONCURRENT_SOLVES = 3; // Tăng lên 3 vì đã tối ưu logging
280
-
281
- async function processQueue() {
282
- if (solveQueue.length === 0 || activeSolves >= MAX_CONCURRENT_SOLVES) {
283
- logger.updateStats(activeSolves, solveQueue.length);
284
- return;
285
- }
286
-
287
- activeSolves++;
288
- const { req, res, user, targetUrl, targetKey, action, proxy } = solveQueue.shift();
289
-
290
- logger.debug(`Processing request for ${user.username}. Active: ${activeSolves}/${MAX_CONCURRENT_SOLVES}`);
291
- logger.updateStats(activeSolves, solveQueue.length);
292
-
293
- const solver = new RecaptchaSolver();
294
- try {
295
- const token = await solver.getRecaptchaToken(
296
- targetUrl,
297
- targetKey,
298
- action || "homepage",
299
- proxy
300
- );
301
-
302
- if (token && !token.startsWith("ERROR")) {
303
- db.run("UPDATE users SET credits = credits - 1 WHERE id = ?", [user.id]);
304
- db.run(
305
- "INSERT INTO usage_logs (user_id, action, details) VALUES (?, ?, ?)",
306
- [user.id, "SOLVE_CAPTCHA", targetUrl]
307
- );
308
-
309
- res.json({ success: true, token: token });
310
- io.emit("user:credit-update");
311
- logger.debug(`Success for ${user.username}`);
312
- } else {
313
- res.status(500).json({ success: false, error: token });
314
- logger.warn(`Solver returned error for ${user.username}: ${token}`);
315
- }
316
- } catch (e) {
317
- res.status(500).json({ success: false, error: e.message });
318
- logger.error(`Catch error for ${user.username}: ${e.message}`);
319
- } finally {
320
- activeSolves--;
321
- logger.updateStats(activeSolves, solveQueue.length);
322
- processQueue();
323
- }
324
- }
325
-
326
- // --- API GIẢI CAPTCHA ---
327
  app.post("/api/solve", async (req, res) => {
328
- const { action, proxy } = req.body;
 
329
  const targetUrl = "https://labs.google";
330
  const targetKey = "6LdsFiUsAAAAAIjVDZcuLhaHiDn5nnHVXVRQGeMV";
331
  const apiKey = req.headers["x-api-key"];
@@ -336,12 +276,54 @@ app.post("/api/solve", async (req, res) => {
336
  "SELECT * FROM users WHERE api_key = ?",
337
  [apiKey],
338
  async (err, user) => {
339
- if (!user) return res.status(403).json({ error: "API Key không hợp lệ" });
340
- if (user.credits < 1) return res.status(402).json({ error: "Hết tiền, nạp thêm đi!" });
341
-
342
- solveQueue.push({ req, res, user, targetUrl, targetKey, action, proxy });
343
- logger.updateStats(activeSolves, solveQueue.length);
344
- processQueue();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
345
  }
346
  );
347
  });
@@ -353,7 +335,7 @@ electronApp.on("window-all-closed", (e) => e.preventDefault());
353
  electronApp.whenReady().then(() => {
354
  // 1. Start Default Port 3000
355
  server.listen(PORT, () =>
356
- console.log(`Server running at: 0.0.0.0:${PORT}`)
357
  );
358
 
359
  // 2. Start HTTP Port 80
 
1
+ const { app: electronApp } = require("electron");
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  const express = require("express");
3
  const bodyParser = require("body-parser");
4
  const cors = require("cors");
5
+ const sqlite3 = require("sqlite3").verbose();
6
  const { v4: uuidv4 } = require("uuid");
7
  const path = require("path");
8
  const RecaptchaSolver = require("./solver");
9
  const http = require("http");
10
  const { Server } = require("socket.io");
 
11
 
12
+ const PORT = 3000;
13
  const app = express();
14
  const server = http.createServer(app);
15
  const io = new Server(server, {
 
80
 
81
  // Kiểm tra xem HWID này đã được cộng tiền lần nào chưa
82
  db.get("SELECT id FROM users WHERE hwid = ?", [hwid], (err, row) => {
83
+ const initialCredits = row ? 0 : 100; // Nếu đã tồn tại HWID thì 0, mới thì 100
84
  const message = row
85
  ? "Đăng ký thành công! (Máy này đã nhận bonus trước đó nên không được cộng thêm)"
86
  : "Đăng ký thành công! Bạn nhận được +100 Credits bonus.";
 
206
  return res.status(400).json({ error: "Giao dịch không hợp lệ" });
207
 
208
  if (action === "reject") {
209
+ db.run("UPDATE transactions SET status = 'rejected' WHERE id = ?", [
210
+ transId,
211
+ ]);
212
  db.run("UPDATE transactions SET status = 'rejected' WHERE id = ?", [
213
  transId,
214
  ]);
 
262
  );
263
  });
264
 
265
+ // --- API GIẢI CAPTCHA (GIỮ NGUYÊN) ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
  app.post("/api/solve", async (req, res) => {
267
+ // const { url, siteKey, action } = req.body;
268
+ const { action } = req.body;
269
  const targetUrl = "https://labs.google";
270
  const targetKey = "6LdsFiUsAAAAAIjVDZcuLhaHiDn5nnHVXVRQGeMV";
271
  const apiKey = req.headers["x-api-key"];
 
276
  "SELECT * FROM users WHERE api_key = ?",
277
  [apiKey],
278
  async (err, user) => {
279
+ if (!user)
280
+ return res.status(403).json({ error: "API Key không tồn tại" });
281
+ if (user.credits <= 0)
282
+ return res.status(402).json({ error: "Hết tiền rồi!" });
283
+
284
+ const solver = new RecaptchaSolver();
285
+ try {
286
+ const token = await solver.getRecaptchaToken(
287
+ targetUrl,
288
+ targetKey,
289
+ action || undefined
290
+ );
291
+ if (token && !token.startsWith("ERROR")) {
292
+ db.run("UPDATE users SET credits = credits - 1 WHERE id = ?", [
293
+ user.id,
294
+ ]);
295
+
296
+ const logDetails = JSON.stringify({
297
+ url: targetUrl,
298
+ status: "Success",
299
+ length: token.length,
300
+ });
301
+
302
+ db.run(
303
+ "INSERT INTO usage_logs (user_id, action, details) VALUES (?, ?, ?)",
304
+ [user.id, "SOLVE_CAPTCHA", logDetails]
305
+ );
306
+
307
+ res.json({ success: true, token: token });
308
+ io.emit("user:credit-update");
309
+ } else {
310
+ // Log Failure
311
+ const logDetails = JSON.stringify({
312
+ url: targetUrl,
313
+ status: "Failed",
314
+ error: token || "Unknown Error",
315
+ length: 0,
316
+ });
317
+ db.run(
318
+ "INSERT INTO usage_logs (user_id, action, details) VALUES (?, ?, ?)",
319
+ [user.id, "SOLVE_CAPTCHA", logDetails]
320
+ );
321
+
322
+ res.status(500).json({ success: false, error: token });
323
+ }
324
+ } catch (e) {
325
+ res.status(500).json({ success: false, error: e.message });
326
+ }
327
  }
328
  );
329
  });
 
335
  electronApp.whenReady().then(() => {
336
  // 1. Start Default Port 3000
337
  server.listen(PORT, () =>
338
+ console.log(`Server running at: http://localhost:${PORT}`)
339
  );
340
 
341
  // 2. Start HTTP Port 80