Update plugins/wai.js
Browse files- plugins/wai.js +98 -85
plugins/wai.js
CHANGED
|
@@ -1,86 +1,99 @@
|
|
| 1 |
-
const puppeteer = require('puppeteer');
|
| 2 |
-
const fetch = require('node-fetch');
|
| 3 |
-
|
| 4 |
-
module.exports = {
|
| 5 |
-
path: '/wai',
|
| 6 |
-
method: 'GET',
|
| 7 |
-
description: 'Generate gambar menggunakan model Illustrious-v140 via CroxyProxy bypass.',
|
| 8 |
-
handler: async (request, reply) => {
|
| 9 |
-
const { prompt } = request.query;
|
| 10 |
-
if (!prompt) return { error: "Prompt kosong!" };
|
| 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 |
};
|
|
|
|
| 1 |
+
const puppeteer = require('puppeteer');
|
| 2 |
+
const fetch = require('node-fetch');
|
| 3 |
+
|
| 4 |
+
module.exports = {
|
| 5 |
+
path: '/wai',
|
| 6 |
+
method: 'GET',
|
| 7 |
+
description: 'Generate gambar menggunakan model Illustrious-v140 via CroxyProxy bypass.',
|
| 8 |
+
handler: async (request, reply) => {
|
| 9 |
+
const { prompt } = request.query;
|
| 10 |
+
if (!prompt) return { error: "Prompt kosong!" };
|
| 11 |
+
|
| 12 |
+
const browser = await puppeteer.launch({
|
| 13 |
+
headless: true,
|
| 14 |
+
executablePath: '/usr/bin/google-chrome-stable', // Path default di linux
|
| 15 |
+
args: [
|
| 16 |
+
'--no-sandbox',
|
| 17 |
+
'--disable-setuid-sandbox',
|
| 18 |
+
'--disable-dev-shm-usage',
|
| 19 |
+
'--disable-accelerated-2d-canvas',
|
| 20 |
+
'--no-first-run',
|
| 21 |
+
'--no-zygote',
|
| 22 |
+
'--single-process', // Penting untuk menghemat RAM di Space gratisan
|
| 23 |
+
'--disable-gpu'
|
| 24 |
+
]
|
| 25 |
+
});
|
| 26 |
+
const page = await browser.newPage();
|
| 27 |
+
|
| 28 |
+
return new Promise(async (resolve) => {
|
| 29 |
+
try {
|
| 30 |
+
await page.goto('https://www.croxyproxy.com/', { waitUntil: 'networkidle2' });
|
| 31 |
+
await page.type('#url', 'https://nech-c-wainsfwillustrious-v140.hf.space/');
|
| 32 |
+
await Promise.all([page.click('#requestSubmit'), page.waitForNavigation({ waitUntil: 'networkidle0' })]);
|
| 33 |
+
await new Promise(r => setTimeout(r, 5000));
|
| 34 |
+
|
| 35 |
+
const currentUrl = page.url();
|
| 36 |
+
const urlObj = new URL(currentUrl);
|
| 37 |
+
const proxyBase = `${urlObj.protocol}//${urlObj.host}`;
|
| 38 |
+
const cpoToken = urlObj.searchParams.get('__cpo');
|
| 39 |
+
const cookies = await page.cookies();
|
| 40 |
+
const cookieString = cookies.map(c => `${c.name}=${c.value}`).join('; ');
|
| 41 |
+
const session_hash = Math.random().toString(36).substring(2);
|
| 42 |
+
|
| 43 |
+
const headers = { "content-type": "application/json", "cookie": cookieString, "referer": currentUrl };
|
| 44 |
+
|
| 45 |
+
const joinRes = await fetch(`${proxyBase}/gradio_api/queue/join?__cpo=${cpoToken}`, {
|
| 46 |
+
method: "POST",
|
| 47 |
+
headers,
|
| 48 |
+
body: JSON.stringify({
|
| 49 |
+
"data": ["v160", prompt, "masterpiece, best quality", "low quality", Math.floor(Math.random() * 2000000000), true, 1024, 1024, 6, 30, 1, null, true, null],
|
| 50 |
+
"fn_index": 12,
|
| 51 |
+
"session_hash": session_hash
|
| 52 |
+
})
|
| 53 |
+
});
|
| 54 |
+
|
| 55 |
+
const streamRes = await fetch(`${proxyBase}/gradio_api/queue/data?session_hash=${session_hash}&__cpo=${cpoToken}`, { headers });
|
| 56 |
+
|
| 57 |
+
streamRes.body.on('data', (chunk) => {
|
| 58 |
+
const dataStr = chunk.toString();
|
| 59 |
+
// Pisahkan berdasarkan baris karena satu chunk bisa berisi beberapa event data
|
| 60 |
+
const lines = dataStr.split("\n");
|
| 61 |
+
|
| 62 |
+
for (let line of lines) {
|
| 63 |
+
if (!line.startsWith("data: ")) continue;
|
| 64 |
+
|
| 65 |
+
try {
|
| 66 |
+
// Membersihkan string "data: " untuk mendapatkan JSON murni
|
| 67 |
+
const rawJson = line.replace("data: ", "").trim();
|
| 68 |
+
if (!rawJson || rawJson === "[DONE]") continue;
|
| 69 |
+
|
| 70 |
+
const json = JSON.parse(rawJson);
|
| 71 |
+
|
| 72 |
+
// Pastikan pesan adalah process_completed dan data yang dicari ada
|
| 73 |
+
if (json.msg === "process_completed" && json.output && json.output.data) {
|
| 74 |
+
const gallery = json.output.data.find(item => Array.isArray(item));
|
| 75 |
+
|
| 76 |
+
if (gallery) {
|
| 77 |
+
const results = gallery.map(img => {
|
| 78 |
+
const rawUrl = img.image?.url || img.url;
|
| 79 |
+
const pathOnly = rawUrl.split('gradio_api')[1].split('&')[0];
|
| 80 |
+
return `https://nech-c-wainsfwillustrious-v140.hf.space/gradio_api${pathOnly}`;
|
| 81 |
+
});
|
| 82 |
+
|
| 83 |
+
browser.close();
|
| 84 |
+
resolve({ status: "success", images: results });
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
} catch (e) {
|
| 88 |
+
// Abaikan error parsing jika chunk terpotong di tengah jalan
|
| 89 |
+
continue;
|
| 90 |
+
}
|
| 91 |
+
}
|
| 92 |
+
});
|
| 93 |
+
} catch (e) {
|
| 94 |
+
browser.close();
|
| 95 |
+
resolve({ error: e.message });
|
| 96 |
+
}
|
| 97 |
+
});
|
| 98 |
+
}
|
| 99 |
};
|