|
| 1 | +// api/proxy.js - GUNA SISTEM AES JOHAN! |
| 2 | + |
| 3 | +// Data dari IPFS (rahsia) |
| 4 | +const IPFS_CONFIG = { |
| 5 | + cid: "QmduEbAZhbtdt37ijGkM8U5cteD3Lpfp9Y22VtoQv2UYZv", |
| 6 | + aesKey: "GA/6NIB5TbUKAPdw22D64e/OPE5TheEdCcDtIs8CG8A=", |
| 7 | + gateway: "https://gateway.pinata.cloud/ipfs/" |
| 8 | +}; |
| 9 | + |
| 10 | +let API_KEY = null; |
| 11 | + |
| 12 | +// DECRYPT FUNCTION |
| 13 | +async function decryptAESGCM(combined, keyBase64) { |
| 14 | + const keyBuffer = Uint8Array.from(atob(keyBase64), c => c.charCodeAt(0)); |
| 15 | + const key = await crypto.subtle.importKey('raw', keyBuffer, { name: 'AES-GCM' }, false, ['decrypt']); |
| 16 | + const iv = combined.slice(0, 12); |
| 17 | + const encryptedData = combined.slice(12); |
| 18 | + const decrypted = await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, key, encryptedData); |
| 19 | + return decrypted; |
| 20 | +} |
| 21 | + |
| 22 | +// DAPAT API KEY DARI IPFS |
| 23 | +async function getAPIKey() { |
| 24 | + if (API_KEY) return API_KEY; |
| 25 | + |
| 26 | + const response = await fetch(IPFS_CONFIG.gateway + IPFS_CONFIG.cid); |
| 27 | + const encryptedData = new Uint8Array(await response.arrayBuffer()); |
| 28 | + const decryptedData = await decryptAESGCM(encryptedData, IPFS_CONFIG.aesKey); |
| 29 | + const data = JSON.parse(new TextDecoder().decode(decryptedData)); |
| 30 | + API_KEY = data.API_KEY; |
| 31 | + return API_KEY; |
| 32 | +} |
| 33 | + |
| 34 | +// PROXY HANDLER |
| 35 | +export default async function handler(req, res) { |
| 36 | + // Set CORS headers |
| 37 | + res.setHeader('Access-Control-Allow-Origin', '*'); |
| 38 | + res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS'); |
| 39 | + res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); |
| 40 | + |
| 41 | + // Handle preflight |
| 42 | + if (req.method === 'OPTIONS') { |
| 43 | + res.status(200).end(); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + // Only POST allowed |
| 48 | + if (req.method !== 'POST') { |
| 49 | + return res.status(405).json({ error: 'Method not allowed' }); |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + const API_KEY = await getAPIKey(); // 🔥 DAPAT DARI IPFS! |
| 54 | + const { prompt } = req.body; |
| 55 | + |
| 56 | + const response = await fetch( |
| 57 | + `https://generativelanguage.googleapis.com/v1beta/models/gemma-3-27b-it:generateContent?key=${API_KEY}`, |
| 58 | + { |
| 59 | + method: 'POST', |
| 60 | + headers: { 'Content-Type': 'application/json' }, |
| 61 | + body: JSON.stringify({ |
| 62 | + contents: [{ parts: [{ text: prompt }] }] |
| 63 | + }) |
| 64 | + } |
| 65 | + ); |
| 66 | + |
| 67 | + const data = await response.json(); |
| 68 | + res.status(200).json(data); |
| 69 | + |
| 70 | + } catch (error) { |
| 71 | + console.error('Proxy error:', error); |
| 72 | + res.status(500).json({ error: error.message }); |
| 73 | + } |
| 74 | +} |
0 commit comments