/** * ============================================================================ * ULTIMATE AI BROWSER ENGINE (GOD-MODE) - ESM VERSION * Status: Fixed ReferenceError: require is not defined * ============================================================================ */ import express from 'express'; import http from 'http'; import { WebSocketServer } from 'ws'; import puppeteer from 'puppeteer-extra'; import StealthPlugin from 'puppeteer-extra-plugin-stealth'; import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; import { execSync } from 'child_process'; // ESM-এ __dirname এবং __filename ডিফাইন করার প্রফেশনাল উপায় const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); // Apply Stealth Plugin puppeteer.use(StealthPlugin()); const app = express(); const server = http.createServer(app); const wss = new WebSocketServer({ server }); const PORT = process.env.PORT || 3000; const DATASET_REPO = "Senku21230/Browser-Extensions"; const EXTENSION_PATH = '/tmp/extensions'; // Global State Management let browser = null; let pages = []; let activePageIndex = 0; let cdpSession = null; /** * ============================================================================ * 1. MAGIC DATASET SYNC MECHANISM * ============================================================================ */ function syncExtensionsFromDataset() { console.log("[SYSTEM] Initializing Persistent Extension Storage..."); const hfToken = process.env.HF_TOKEN; if (!hfToken) { console.error("[CRITICAL WARNING] HF_TOKEN missing!"); if (!fs.existsSync(EXTENSION_PATH)) fs.mkdirSync(EXTENSION_PATH, { recursive: true }); return; } try { if (fs.existsSync(EXTENSION_PATH)) { fs.rmSync(EXTENSION_PATH, { recursive: true, force: true }); } console.log(`[STORAGE] Syncing: ${DATASET_REPO}...`); const cloneCmd = `git clone https://oauth2:${hfToken}@huggingface.co/datasets/${DATASET_REPO} ${EXTENSION_PATH}`; execSync(cloneCmd, { stdio: 'ignore' }); console.log("[STORAGE] Sync Complete!"); } catch (error) { console.error("[ERROR] Sync Failed."); if (!fs.existsSync(EXTENSION_PATH)) fs.mkdirSync(EXTENSION_PATH, { recursive: true }); } } /** * ============================================================================ * 2. AI TOOL REGISTRY (THE MASTER BRAIN - PART A) * ============================================================================ */ const ai_tools = [ // --- 1. CORE MASTER TOOL --- { name: "basic_navigation_controller", description: "CORE INTERACTION MASTER: Handles navigate, click, type, scroll, and extract actions. Parameters: 'action_type', 'url', 'selector', 'text_to_type', 'direction'. Returns fully rendered DOM converted to clean markdown for 'extract'." }, // --- 2. CYBER & HACKING TOOLS --- { name: "deep_network_v8_interceptor", description: "CRITICAL NETWORK HACK: Hooks into V8 memory via CDP to intercept encrypted payloads (Authorization Bearer Tokens, Refresh Cookies) before hitting the cloud. Returns a ready-to-execute cURL command." }, { name: "visual_ocr_canvas_piercer", description: "ADVANCED VISION ENGINE: Captures 4K screenshots, runs deep OCR to calculate X/Y coordinates of text hidden in WebGL/Canvas, and executes hardware-level mouse clicks." }, { name: "deep_shadow_dom_piercer", description: "DOM PENETRATION PROTOCOL: Overwrites 'attachShadow' to force all Closed Shadow Roots open. Recursively extracts deeply nested HTML and JSON data invisible to standard scrapers." }, { name: "auto_api_reverse_engineering", description: "BACKGROUND API SCANNER: Passively monitors XHR/Fetch calls, deduces pagination logic, and returns a JSON map of unauthenticated backend API endpoints." }, { name: "network_payload_modifier", description: "REAL-TIME TRAFFIC MUTATION: Intercepts and rewrites HTTP responses before rendering. Parameter: 'target_url_pattern' and 'mutation_json'. Useful for unlocking client-side premium states." }, { name: "custom_extension_loader", description: "PERSISTENT STORAGE INJECTOR: Dynamically loads extensions from /tmp/extensions and relaunches the browser engine seamlessly." }, { name: "start_recording", description: "MEDIA ENGINE (START): Initializes an FFMPEG stream bound to the CDP screencast to record a 1080p .mp4 video of the browser session." }, { name: "stop_recording", description: "MEDIA ENGINE (STOP): Safely terminates the FFMPEG stream and returns the absolute download URL of the recorded session." }, { name: "auto_captcha_solver", description: "SECURITY BYPASS: Autonomous bypass sequence for Cloudflare Turnstile, hCaptcha, or reCAPTCHA using audio-payloads and vision models." }, { name: "file_manager_io", description: "STORAGE I/O: Intercepts native downloads to save files to the server, or uses CDP to force local server files into secure file upload inputs." }, { name: "media_inventory", description: "SYSTEM MANAGEMENT: Recursively scans the server's output directories and returns a JSON inventory of all generated .mp4 videos and .png screenshots." }, { name: "smart_cleanup", description: "SYSTEM MANAGEMENT: Executes permanent deletion of server files based on URL or file name commands." }, // --- 3. CORE EXTRACTION & SCRIPTING --- { name: "evaluate_javascript", description: "BROWSER CONSOLE: Injects and executes raw JavaScript code within the active page's context. Parameter: 'js_code_string'." }, { name: "inject_custom_cookies", description: "SESSION HIJACKING: Injects predefined session cookies into the browser context to bypass login screens. Parameter: 'cookie_json_array'." }, { name: "take_screenshot", description: "VISUAL CAPTURE: Captures a high-resolution .png screenshot of the current tab. Parameter optional: 'full_page' (boolean)." }, { name: "hover_element", description: "UI INTERACTION: Simulates physical mouse hover over an element to trigger dropdowns or lazy-loads. Parameter: 'selector'." }, { name: "wait_for_selector", description: "EXECUTION DELAY: Pauses AI execution until a specific DOM element appears. Parameters: 'selector' and optional 'timeout_ms'." }, { name: "get_element_attributes", description: "DATA EXTRACTION: Retrieves all HTML attributes (href, src, class, id) of a specific element without extracting inner text. Parameter: 'selector'." }, { name: "switch_to_iframe", description: "CONTEXT SWITCHING: Shifts Puppeteer execution context into a nested iframe to interact with embedded widgets. Parameter: 'iframe_selector'." }, { name: "exit_iframe", description: "CONTEXT SWITCHING: Returns the execution context from a nested iframe back to the main document." }, { name: "emulate_device_viewport", description: "DEVICE SPOOFING: Alters viewport dimensions and User-Agent to emulate a specific device. Parameter: 'device_name'." }, { name: "set_geolocation", description: "HARDWARE SPOOFING: Overrides HTML5 Geolocation API with fake GPS coordinates. Parameters: 'latitude', 'longitude'." }, // --- 4. ADVANCED DOM & EXTRACTION --- { name: "extract_table_data", description: "DATA EXTRACTION: Extracts an HTML