darkvibe314 commited on
Commit
2b715e9
·
verified ·
1 Parent(s): 46056ec

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +26 -27
server.js CHANGED
@@ -8,7 +8,6 @@ puppeteer.use(StealthPlugin());
8
  const app = express();
9
  app.use(express.json());
10
 
11
- // Serve the beautiful Silent Tech UI
12
  app.use(express.static(path.join(__dirname, 'public')));
13
 
14
  app.post('/api/generate', async (req, res) => {
@@ -19,72 +18,72 @@ app.post('/api/generate', async (req, res) => {
19
  try {
20
  browser = await puppeteer.launch({
21
  headless: "new",
 
22
  args: [
23
  '--no-sandbox',
24
  '--disable-setuid-sandbox',
25
- '--disable-dev-shm-usage'
 
26
  ]
27
  });
28
 
29
  const page = await browser.newPage();
 
30
  await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36');
31
 
32
  let mediaUrl = null;
33
 
34
- // The Stealth Interceptor: Listens to the network to catch the generated image/video URL
35
  page.on('response', async (response) => {
36
  if (response.url().includes('/nextjs-api/stream/create-evaluation') && response.status() === 200) {
37
  try {
38
  const text = await response.text();
39
  const match = text.match(/https:\/\/[^\s"'<>\\]+\.(mp4|png|webp|jpg|jpeg)/i);
40
  if (match) mediaUrl = match[0];
41
- } catch (e) {
42
- // Ignore stream parsing errors
43
- }
44
  }
45
  });
46
 
47
- // 1. Navigate to Arena
48
  await page.goto('https://arena.ai/', { waitUntil: 'networkidle2' });
49
 
50
- // 2. Wait for the chat box and type the prompt
 
 
 
51
  await page.waitForSelector('textarea', { timeout: 15000 });
52
- await page.type('textarea', prompt);
 
 
53
 
54
- // 3. Find and click the Submit button
55
- await page.evaluate(() => {
56
- const buttons = Array.from(document.querySelectorAll('button'));
57
- const sendBtn = buttons.find(b => b.innerHTML.includes('svg') || b.innerText.toLowerCase().includes('send'));
58
- if (sendBtn) sendBtn.click();
59
- });
60
 
61
- // 4. Wait up to 60 seconds for the image to generate
62
  let attempts = 0;
63
- while (!mediaUrl && attempts < 60) {
64
  await new Promise(r => setTimeout(r, 1000));
65
  attempts++;
66
  }
67
 
68
- // 5. Send result back to the dashboard / bot
69
  if (mediaUrl) {
70
- res.json({
71
- success: true,
72
- creator: "Silent Tech",
73
- prompt: prompt,
74
- url: mediaUrl
75
- });
76
  } else {
77
- res.status(500).json({ success: false, error: "Generation Timeout. Cloudflare or Captcha might have blocked the headless browser." });
 
 
 
 
 
 
78
  }
79
 
80
  } catch (err) {
81
  res.status(500).json({ success: false, error: err.message });
82
  } finally {
83
- if (browser) await browser.close(); // Prevent memory leaks
84
  }
85
  });
86
 
87
- // Hugging Face needs it bound to 0.0.0.0 on port 7860
88
  app.listen(7860, "0.0.0.0", () => {
89
  console.log('Silent Tech API Proxy running on port 7860');
90
  });
 
8
  const app = express();
9
  app.use(express.json());
10
 
 
11
  app.use(express.static(path.join(__dirname, 'public')));
12
 
13
  app.post('/api/generate', async (req, res) => {
 
18
  try {
19
  browser = await puppeteer.launch({
20
  headless: "new",
21
+ executablePath: process.env.PUPPETEER_EXECUTABLE_PATH, // Uses the official container's Chrome
22
  args: [
23
  '--no-sandbox',
24
  '--disable-setuid-sandbox',
25
+ '--disable-dev-shm-usage',
26
+ '--window-size=1280,720'
27
  ]
28
  });
29
 
30
  const page = await browser.newPage();
31
+ await page.setViewport({ width: 1280, height: 720 });
32
  await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36');
33
 
34
  let mediaUrl = null;
35
 
 
36
  page.on('response', async (response) => {
37
  if (response.url().includes('/nextjs-api/stream/create-evaluation') && response.status() === 200) {
38
  try {
39
  const text = await response.text();
40
  const match = text.match(/https:\/\/[^\s"'<>\\]+\.(mp4|png|webp|jpg|jpeg)/i);
41
  if (match) mediaUrl = match[0];
42
+ } catch (e) {}
 
 
43
  }
44
  });
45
 
46
+ // 1. Go to site
47
  await page.goto('https://arena.ai/', { waitUntil: 'networkidle2' });
48
 
49
+ // Give Cloudflare 5 extra seconds to clear
50
+ await new Promise(r => setTimeout(r, 5000));
51
+
52
+ // 2. Wait for textarea and type
53
  await page.waitForSelector('textarea', { timeout: 15000 });
54
+ await page.type('textarea', prompt, { delay: 50 });
55
+
56
+ await new Promise(r => setTimeout(r, 500));
57
 
58
+ // 3. Press Enter
59
+ await page.keyboard.press('Enter');
 
 
 
 
60
 
61
+ // 4. Wait for media (up to 45 seconds)
62
  let attempts = 0;
63
+ while (!mediaUrl && attempts < 45) {
64
  await new Promise(r => setTimeout(r, 1000));
65
  attempts++;
66
  }
67
 
 
68
  if (mediaUrl) {
69
+ res.json({ success: true, creator: "Silent Tech", prompt: prompt, url: mediaUrl });
 
 
 
 
 
70
  } else {
71
+ // 👁️ SERVER VISION: Take screenshot of what blocked the bot
72
+ const base64Screenshot = await page.screenshot({ encoding: "base64" });
73
+ res.status(500).json({
74
+ success: false,
75
+ error: "Generation Timeout. See debug screenshot.",
76
+ debug_image: `data:image/png;base64,${base64Screenshot}`
77
+ });
78
  }
79
 
80
  } catch (err) {
81
  res.status(500).json({ success: false, error: err.message });
82
  } finally {
83
+ if (browser) await browser.close();
84
  }
85
  });
86
 
 
87
  app.listen(7860, "0.0.0.0", () => {
88
  console.log('Silent Tech API Proxy running on port 7860');
89
  });