File size: 3,922 Bytes
22f71cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
568802f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const puppeteer = require('puppeteer');
const fetch = require('node-fetch');

module.exports = {
  path: '/wai',
  method: 'GET',
  description: 'Generate gambar menggunakan model Illustrious-v140 via CroxyProxy bypass.',
  handler: async (request, reply) => {
    const { prompt } = request.query;
    if (!prompt) return { error: "Prompt kosong!" };

  const browser = await puppeteer.launch({
    headless: true,
    executablePath: '/usr/bin/google-chrome-stable', // Path default di linux
    args: [
      '--no-sandbox',
      '--disable-setuid-sandbox',
      '--disable-dev-shm-usage',
      '--disable-accelerated-2d-canvas',
      '--no-first-run',
      '--no-zygote',
      '--single-process', // Penting untuk menghemat RAM di Space gratisan
      '--disable-gpu'
    ]
  });
    const page = await browser.newPage();

    return new Promise(async (resolve) => {
      try {
        await page.goto('https://www.croxyproxy.com/', { waitUntil: 'networkidle2' });
        await page.type('#url', 'https://nech-c-wainsfwillustrious-v140.hf.space/');
        await Promise.all([page.click('#requestSubmit'), page.waitForNavigation({ waitUntil: 'networkidle0' })]);
        await new Promise(r => setTimeout(r, 5000));

        const currentUrl = page.url();
        const urlObj = new URL(currentUrl);
        const proxyBase = `${urlObj.protocol}//${urlObj.host}`;
        const cpoToken = urlObj.searchParams.get('__cpo');
        const cookies = await page.cookies();
        const cookieString = cookies.map(c => `${c.name}=${c.value}`).join('; ');
        const session_hash = Math.random().toString(36).substring(2);

        const headers = { "content-type": "application/json", "cookie": cookieString, "referer": currentUrl };

        const joinRes = await fetch(`${proxyBase}/gradio_api/queue/join?__cpo=${cpoToken}`, {
          method: "POST",
          headers,
          body: JSON.stringify({
            "data": ["v160", prompt, "masterpiece, best quality", "low quality", Math.floor(Math.random() * 2000000000), true, 1024, 1024, 6, 30, 1, null, true, null],
            "fn_index": 12,
            "session_hash": session_hash
          })
        });

        const streamRes = await fetch(`${proxyBase}/gradio_api/queue/data?session_hash=${session_hash}&__cpo=${cpoToken}`, { headers });

        streamRes.body.on('data', (chunk) => {
          const dataStr = chunk.toString();
          // Pisahkan berdasarkan baris karena satu chunk bisa berisi beberapa event data
          const lines = dataStr.split("\n");

          for (let line of lines) {
            if (!line.startsWith("data: ")) continue;

            try {
              // Membersihkan string "data: " untuk mendapatkan JSON murni
              const rawJson = line.replace("data: ", "").trim();
              if (!rawJson || rawJson === "[DONE]") continue;

              const json = JSON.parse(rawJson);

              // Pastikan pesan adalah process_completed dan data yang dicari ada
              if (json.msg === "process_completed" && json.output && json.output.data) {
                const gallery = json.output.data.find(item => Array.isArray(item));

                if (gallery) {
                  const results = gallery.map(img => {
                    const rawUrl = img.image?.url || img.url;
                    const pathOnly = rawUrl.split('gradio_api')[1].split('&')[0];
                    return `https://nech-c-wainsfwillustrious-v140.hf.space/gradio_api${pathOnly}`;
                  });

                  browser.close();
                  resolve({ status: "success", images: results });
                }
              }
            } catch (e) {
              // Abaikan error parsing jika chunk terpotong di tengah jalan
              continue;
            }
          }
        });
      } catch (e) {
        browser.close();
        resolve({ error: e.message });
      }
    });
  }
};