/** * AI Face Swap Scraper & Generator (aifaceswap.io) * Reverse-engineered by Vamz Spectre / VamxAPI * * Capabilities: * - High-speed AI Face Swap without registration / free quota bypass * - Automatic dynamic session handshake & RSA PKCS#1 v1.5 key exchange * - Payload encryption using AES-256-GCM + dynamic theme-version SHA256 key * - Aliyun OSS direct pre-signed upload integration * - Supports Local File Paths, Remote Image URLs, and Base64 Data URIs * * Usage: * node aifaceswap-scraper.js "" "" * * Example: * node aifaceswap-scraper.js "https://temp.aifaceswap.io/aifaceswap/static_img/204de792a133b31edd255f7bb0d2e76f.webp" "https://temp.aifaceswap.io/aifaceswap/static_img/8a1bce5ea303791589165a5f607e7399.webp" */ const fs = require("fs"); const crypto = require("crypto"); const BASE_URL = "https://aifaceswap.io"; const CDN_PREFIX = "https://art-global.ezmaker.ai/"; const APP_ID_V1 = "aifaceswap_v1"; const SECRET = "1H5tRtzsBkqXcaJ"; const RSA_PUBLIC_KEY = `-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwlO+boC6cwRo3UfXVBadaYwcX 0zKS2fuVNY2qZ0dgwb1NJ+/Q9FeAosL4ONiosD71on3PVYqRUlL5045mvH2K9i8b AFVMEip7E6RMK6tKAAif7xzZrXnP1GZ5Rijtqdgwh+YmzTo39cuBCsZqK9oEoeQ3 r/myG9S+9cR5huTuFQIDAQAB -----END PUBLIC KEY-----`; function makeid(length = 16) { let result = ""; const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for (let i = 0; i < length; i++) { result += chars.charAt(Math.floor(Math.random() * chars.length)); } return result; } function encryptAesCbc(text, key, iv) { const cipher = crypto.createCipheriv("aes-128-cbc", Buffer.from(key, "utf8"), Buffer.from(iv, "utf8")); let encrypted = cipher.update(text, "utf8", "base64"); encrypted += cipher.final("base64"); return encrypted; } function encryptRsa(text, publicKey) { return crypto.publicEncrypt( { key: publicKey, padding: crypto.constants.RSA_PKCS1_PADDING }, Buffer.from(text, "utf8") ).toString("base64"); } function signux() { const aesSecret = makeid(16); const nonce = crypto.randomUUID(); const secret_key = encryptRsa(aesSecret, RSA_PUBLIC_KEY); const sign = encryptAesCbc(`${APP_ID_V1}:${nonce}:${secret_key}`, aesSecret, aesSecret); return { nonce, secret_key, sign, aesSecret }; } function signx() { const t = Math.floor(Date.now() / 1000); const nonce = crypto.randomUUID(); const aesSecret = makeid(16); const secret_key = encryptRsa(aesSecret, RSA_PUBLIC_KEY); const sign = encryptAesCbc(`${APP_ID_V1}:${SECRET}:${t}:${nonce}:${secret_key}`, aesSecret, aesSecret); return { t, nonce, secret_key, sign, aesSecret }; } function getFpEncrypt(fp, aesSecret) { return encryptAesCbc(`${APP_ID_V1}:${fp}`, aesSecret, aesSecret); } function encryptGcm(dataObj, themeVersion) { const jsonStr = JSON.stringify(dataObj); const keyHash = crypto.createHash("sha256").update(themeVersion).digest(); const iv = crypto.randomBytes(12); const cipher = crypto.createCipheriv("aes-256-gcm", keyHash, iv); const encrypted = Buffer.concat([cipher.update(jsonStr, "utf8"), cipher.final()]); const tag = cipher.getAuthTag(); const combined = Buffer.concat([iv, encrypted, tag]); return combined.toString("base64"); } async function resolveImageBuffer(input) { if (Buffer.isBuffer(input)) { return { buffer: input, ext: "jpg" }; } if (typeof input !== "string") { throw new Error("Input gambar tidak valid (harus URL, Path File, atau Base64)."); } // Remote URL if (input.startsWith("http://") || input.startsWith("https://")) { const res = await fetch(input, { headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36" } }); if (!res.ok) throw new Error(`Gagal mengunduh gambar dari URL: ${input} (${res.status})`); const arrayBuffer = await res.arrayBuffer(); const contentType = res.headers.get("content-type") || ""; let ext = "jpg"; if (contentType.includes("png") || input.endsWith(".png")) ext = "png"; else if (contentType.includes("webp") || input.endsWith(".webp")) ext = "webp"; return { buffer: Buffer.from(arrayBuffer), ext }; } // Base64 Data URI if (input.startsWith("data:image/")) { const match = input.match(/^data:image\/([a-zA-Z0-9_\-]+);base64,(.+)$/); if (match) { return { buffer: Buffer.from(match[2], "base64"), ext: match[1] === "jpeg" ? "jpg" : match[1] }; } } // Local file path if (fs.existsSync(input)) { const buffer = fs.readFileSync(input); const extMatch = input.match(/\.([a-zA-Z0-9]+)$/); const ext = extMatch ? extMatch[1].toLowerCase() : "jpg"; return { buffer, ext: ext === "jpeg" ? "jpg" : ext }; } throw new Error(`File atau URL gambar tidak ditemukan: ${input}`); } async function aiFaceSwap(targetImageInput, faceImageInput, options = {}) { const timeoutMs = options.timeoutMs || 90000; const pollIntervalMs = options.pollIntervalMs || 2500; // 1. Dapatkan Session & Dynamic Theme Version const initRes = await fetch(`${BASE_URL}/id/`, { headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36" } }); if (!initRes.ok) throw new Error(`Gagal menghubungi server aifaceswap.io: ${initRes.status}`); const cookies = initRes.headers.getSetCookie ? initRes.headers.getSetCookie() : [initRes.headers.get("set-cookie") || ""]; const cookieHeader = cookies.filter(Boolean).map(c => c.split(";")[0]).join("; "); const html = await initRes.text(); const themeVersionMatch = html.match(/data-kt-theme-version=["\x27]([^"\x27]+)["\x27]/i); if (!themeVersionMatch) throw new Error("Gagal mengekstrak data-kt-theme-version dari aifaceswap.io"); const themeVersion = themeVersionMatch[1]; // Helper OSS Upload async function uploadToOss(resolvedImg) { const { buffer, ext } = resolvedImg; const md5 = crypto.createHash("md5").update(buffer).digest("hex"); const fileName = `${md5}.${ext}`; const s = signux(); const upReq = await fetch(`${BASE_URL}/api/upload_file?batch=1`, { method: "POST", headers: { "Content-Type": "application/json", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36", "Origin": BASE_URL, "Referer": `${BASE_URL}/id/`, "Cookie": cookieHeader, "x-guide": s.secret_key, "x-sign": s.sign, "nonce": s.nonce, "x-code": String(Date.now()), "theme-version": themeVersion }, body: JSON.stringify({ file_name: fileName, type: "image" }) }); const upData = await upReq.json(); if (upData.code !== 200 || !upData.data?.url) { throw new Error(`Inisialisasi upload OSS gagal: ${JSON.stringify(upData)}`); } const ossUrl = upData.data.url; const mime = ext === "png" ? "image/png" : ext === "webp" ? "image/webp" : "image/jpeg"; const putReq = await fetch(ossUrl, { method: "PUT", headers: { "Content-Type": mime, "x-oss-storage-class": "Standard" }, body: buffer }); if (!putReq.ok) { throw new Error(`Upload binary ke OSS gagal dengan status: ${putReq.status}`); } const cdnUrl = ossUrl.split("?")[0].split("https://yimeta-ai-face-swap.oss-us-west-1.aliyuncs.com/")[1]; return { cdnUrl, extra_data: upData.extra_data || {} }; } // 2. Upload kedua gambar const [targetImg, faceImg] = await Promise.all([ resolveImageBuffer(targetImageInput).then(uploadToOss), resolveImageBuffer(faceImageInput).then(uploadToOss) ]); // 3. Buat handshake signature & payload const targetBase = targetImg.cdnUrl.split("/").pop().split(".")[0]; const faceBase = faceImg.cdnUrl.split("/").pop().split(".")[0]; const nonceN = crypto.createHash("md5").update(`${targetBase}:${faceBase}`).digest("hex"); const sx = signx(); const fp = crypto.randomBytes(16).toString("hex"); const fp1 = getFpEncrypt(fp, sx.aesSecret); const encData = encryptGcm( { source_image: targetImg.cdnUrl, face_image: faceImg.cdnUrl, type_1: targetImg.extra_data.type_1 || 0, type_2: faceImg.extra_data.type_2 || 0 }, themeVersion ); // 4. Submit Face Swap Task const genReq = await fetch(`${BASE_URL}/api/generate_face_v1`, { method: "POST", headers: { "Content-Type": "application/json", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36", "Origin": BASE_URL, "Referer": `${BASE_URL}/id/`, "Cookie": cookieHeader, "theme-version": themeVersion, "X-code": String(Date.now()), "x-guide": sx.secret_key, "x-sign": sx.sign, "nonce": nonceN, "fp": fp, "fp1": fp1 }, body: JSON.stringify({ request_type: 2, data: encData }) }); const genData = await genReq.json(); if (genData.code !== 200 || !genData.data) { throw new Error(`Generate Face gagal: ${JSON.stringify(genData)}`); } // Jika hasil langsung keluar tanpa queue if (genData.data.result_image) { const finalUrl = genData.data.result_image.startsWith("http") ? genData.data.result_image : `${CDN_PREFIX}${genData.data.result_image}`; return { status: true, creator: "Vamz Spectre", data: { task_id: genData.data.task_id || null, result_url: finalUrl, source_image: `${CDN_PREFIX}${targetImg.cdnUrl}`, face_image: `${CDN_PREFIX}${faceImg.cdnUrl}` } }; } const taskId = genData.data.task_id; if (!taskId) { throw new Error("Task ID tidak ditemukan dalam response server."); } // 5. Polling Task Status const startTime = Date.now(); while (Date.now() - startTime < timeoutMs) { await new Promise(r => setTimeout(r, pollIntervalMs)); const chkReq = await fetch(`${BASE_URL}/api/check_status`, { method: "POST", headers: { "Content-Type": "application/json", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36", "Origin": BASE_URL, "Referer": `${BASE_URL}/id/`, "Cookie": cookieHeader, "theme-version": themeVersion, "X-code": String(Date.now()) }, body: JSON.stringify({ task_id: taskId, nonce: nonceN }) }); const chk = await chkReq.json(); if (chk.code === 200 && chk.data) { if (chk.data.status === 2 && chk.data.result_image) { const finalUrl = chk.data.result_image.startsWith("http") ? chk.data.result_image : `${CDN_PREFIX}${chk.data.result_image}`; return { status: true, creator: "Vamz Spectre", data: { task_id: taskId, result_url: finalUrl, source_image: `${CDN_PREFIX}${targetImg.cdnUrl}`, face_image: `${CDN_PREFIX}${faceImg.cdnUrl}` } }; } if (chk.data.status === -1) { throw new Error(`Proses swap gagal dari sisi server: ${chk.data.error || "Task Failed"}`); } } } throw new Error("Waktu tunggu swap melebihi batas (Timeout)."); } module.exports = { aiFaceSwap }; // CLI Execution Support if (require.main === module) { const targetArg = process.argv[2]; const faceArg = process.argv[3]; if (!targetArg || !faceArg) { console.log( JSON.stringify( { status: false, creator: "Vamz Spectre", message: "Harap masukkan target_image dan face_image.", usage: 'node aifaceswap-scraper.js "" ""' }, null, 2 ) ); process.exit(1); } aiFaceSwap(targetArg, faceArg) .then(res => { console.log(JSON.stringify(res, null, 2)); }) .catch(err => { console.error( JSON.stringify( { status: false, creator: "Vamz Spectre", error: err.message || String(err) }, null, 2 ) ); process.exit(1); }); }