Skip to content

Latest commit

 

History

History
675 lines (560 loc) · 27.8 KB

File metadata and controls

675 lines (560 loc) · 27.8 KB

Multi-Layered Undetectable Browser Automation System with Integuru + mitmproxy

Executive Summary

This system combines three sophisticated technologies to create an intelligent, self-optimizing automation platform:

  1. Integuru: API reverse-engineering via HAR/network analysis (fastest automation path)
  2. mitmproxy: Real-time network interception & activity recording at multiple levels
  3. CDP MCP Server: Browser state capture, headless replication, and visual debugging

Key Innovation: The system automatically chooses the fastest viable automation modality for each task by analyzing the network traffic you generate, eliminating the speed penalty of traditional browser automation.


Architecture Overview

┌──────────────────────────────────────────────────────────────────────────────┐
│                           USER WORKSTATION                                   │
├──────────────────────────────────────────────────────────────────────────────┤
│                                                                              │
│  ┌────────────────────────────────────────────────────────────────────┐    │
│  │  Visible Browser (with CDP Extension + Stealth)                   │    │
│  │  ├─ User authenticates normally (KlingAI, Gmail, etc.)            │    │
│  │  ├─ Extension captures browser state (cookies, storage, etc.)     │    │
│  │  └─ mitmproxy intercepts all network traffic                      │    │
│  └────────────────────────────────────────────────────────────────────┘    │
│                          ↓                                                    │
│  ┌────────────────────────────────────────────────────────────────────┐    │
│  │  mitmproxy (Active Interception Layer)                             │    │
│  │  ├─ Level 1: Real-time HTTP/HTTPS capture → HAR export            │    │
│  │  ├─ Level 2: Request/response body analysis                        │    │
│  │  ├─ Level 3: WebSocket, Server-Sent Events (SSE) monitoring       │    │
│  │  ├─ Level 4: Performance metrics (latency, size, timing)          │    │
│  │  └─ Output: network_requests.har + metadata.json                  │    │
│  └────────────────────────────────────────────────────────────────────┘    │
│                          ↓                                                    │
│  ┌────────────────────────────────────────────────────────────────────┐    │
│  │  Activity Recording Engine (Layered)                               │    │
│  │  ├─ Automation Record: CDP commands + screenshots                  │    │
│  │  ├─ Network Record: All HTTP(S) requests + responses              │    │
│  │  ├─ State Record: Browser state snapshots (cookies, DOM, etc.)    │    │
│  │  └─ Metadata Record: Timing, performance, user actions           │    │
│  └────────────────────────────────────────────────────────────────────┘    │
│                          ↓                                                    │
│  ┌────────────────────────────────────────────────────────────────────┐    │
│  │  Integuru Agent (API Reverse-Engineering)                         │    │
│  │  ├─ Input: HAR file + user prompt ("Download image from KlingAI") │    │
│  │  ├─ Analysis:                                                      │    │
│  │  │  1. Identify target request (final action)                     │    │
│  │  │  2. Find dynamic parameters (API tokens, IDs, etc.)            │    │
│  │  │  3. Build dependency graph of required requests               │    │
│  │  │  4. Trace back to authentication-only requests                │    │
│  │  └─ Output: Runnable Python code (direct API calls)              │    │
│  └────────────────────────────────────────────────────────────────────┘    │
│                          ↓                                                    │
│  ┌────────────────────────────────────────────────────────────────────┐    │
│  │  Modality Optimizer (Speed Selector)                               │    │
│  │  ├─ If API available + low complexity: USE INTEGURU (FASTEST)     │    │
│  │  │  └─ Direct HTTP calls to internal APIs (seconds)              │    │
│  │  ├─ Else if moderate complexity: USE HEADLESS + CDP               │    │
│  │  │  └─ Replicated state + visual verification (minutes)          │    │
│  │  └─ Else: FALLBACK to visible browser + manual (slowest)         │    │
│  └────────────────────────────────────────────────────────────────────┘    │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘
                                     ↓
┌──────────────────────────────────────────────────────────────────────────────┐
│                      MCP SERVER (Headless Execution)                         │
├──────────────────────────────────────────────────────────────────────────────┤
│                                                                              │
│  ┌─── Tool: execute-via-integuru ────────────────────────────────────┐    │
│  │ Input: Integuru-generated Python code                             │    │
│  │ Output: Response data + screenshots (if errors)                   │    │
│  │ Speed: 2-5 seconds typically                                      │    │
│  └────────────────────────────────────────────────────────────────────┘    │
│                                                                              │
│  ┌─── Tool: execute-via-headless-cdp ────────────────────────────────┐    │
│  │ Input: Browser state dump + action sequence                       │    │
│  │ Process:                                                            │    │
│  │ 1. Spawn headless browser with replicated profile                │    │
│  │ 2. For each action:                                              │    │
│  │    a. Execute CDP command                                        │    │
│  │    b. Screenshot immediately                                     │    │
│  │    c. Analyze with GLM-4.5V                                      │    │
│  │    d. Log results                                                │    │
│  │ Output: execution_log.json + screenshots/                        │    │
│  │ Speed: 10-30 seconds depending on complexity                     │    │
│  └────────────────────────────────────────────────────────────────────┘    │
│                                                                              │
│  ┌─── Tool: fallback-visible-browser ────────────────────────────────┐    │
│  │ Input: Action sequence + timeout                                  │    │
│  │ Output: User gets browser window, performs action manually        │    │
│  │ Speed: Variable (depends on user response)                        │    │
│  └────────────────────────────────────────────────────────────────────┘    │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘
                                     ↓
┌──────────────────────────────────────────────────────────────────────────────┐
│                  OUTPUT & RECORDING LAYER                                    │
├──────────────────────────────────────────────────────────────────────────────┤
│                                                                              │
│ ├─ automation.json        (CDP commands, screenshots, timing)              │
│ ├─ network.har            (Integuru input + execution trace)               │
│ ├─ execution_log.json     (Step-by-step results with GLM analysis)        │
│ ├─ screenshots/           (Per-step visual verification)                   │
│ ├─ reproducible_code.py   (Replay-able Python script)                      │
│ └─ optimization_report    (Which modality was fastest, why, metrics)      │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘

Three-Layer Activity Recording System

Layer 1: Network Activity (mitmproxy)

Purpose: Capture all HTTP(S) traffic to enable API reverse-engineering

# Start mitmproxy with HAR recording
mitmdump --set hardump=network_requests.har \
         --flow-detail=3 \
         --set confdir=~/.mitmproxy \
         -q

# Or with Python addon for custom processing:
mitmdump -s record_addon.py --set record_level=3

What's recorded:

  • Full request/response headers
  • Request/response bodies
  • Timing information
  • Cookie jar state
  • WebSocket frames (if applicable)

Output: network_requests.har (standard HAR format, compatible with Integuru)

Layer 2: State & Browser Activity (CDP MCP)

Purpose: Capture browser automation state separately for debugging & replay

# During automation execution:
recording = {
    "timestamp": datetime.now().isoformat(),
    "actions": [
        {
            "type": "CDP_COMMAND",
            "method": "Input.dispatchMouseEvent",
            "params": {"x": 100, "y": 200, "type": "mousePressed"},
            "screenshot_before": "screenshot_001.png",
            "screenshot_after": "screenshot_002.png",
            "response": {...},
            "duration_ms": 150
        },
        {
            "type": "WAIT",
            "duration_ms": 2000,
            "reason": "waiting for async response"
        },
        {
            "type": "SCREENSHOT_ANALYSIS",
            "screenshot": "screenshot_003.png",
            "glm_analysis": "Login button is visible and clickable",
            "success": True
        }
    ],
    "total_duration_ms": 45000,
    "success": True
}

Output: automation.json (full execution trace with screenshots)

Layer 3: Metadata & Performance (Optimizer)

Purpose: Track which automation modality was used and why

# After automation completes:
optimization_report = {
    "task": "Download generated image from KlingAI",
    "modality_selected": "integuru",
    "modality_reasons": {
        "integuru": {
            "available": True,
            "confidence": 0.95,
            "estimated_time": 3,
            "code_lines": 25
        },
        "headless_cdp": {
            "available": True,
            "confidence": 0.80,
            "estimated_time": 20
        },
        "visible_browser": {
            "available": True,
            "estimated_time": 300  # User waits
        }
    },
    "actual_execution": {
        "modality": "integuru",
        "duration_seconds": 2.8,
        "success": True,
        "api_calls_made": 3,
        "network_requests": 5
    },
    "optimization_metrics": {
        "api_reverse_engineering_quality": 0.98,
        "dependency_graph_accuracy": 0.99,
        "code_generation_success_rate": 1.0
    }
}

Output: optimization_report.json (metrics for continuous improvement)


Integuru Integration: Deep Dive

How It Works in Your System

User Action (visible browser):
  "Download my generated image"
                    ↓
          mitmproxy captures ALL HTTP(S)
                    ↓
        network_requests.har generated
                    ↓
   Integuru analyzes HAR + your prompt
                    ↓
   Identifies final request (e.g., GET /api/v1/image/123/download)
                    ↓
   Builds dependency graph:
   ├─ GET /api/auth/verify
   ├─ GET /api/user/profile
   ├─ GET /api/projects/{projectId}/images
   ├─ POST /api/images/{imageId}/generate
   └─ GET /api/images/{imageId}/download ← TARGET
                    ↓
   Extracts dynamic parameters from responses
                    ↓
   Generates Python code:
   
   ```python
   def download_image(auth_token):
       # Step 1: Verify auth
       headers = {"Authorization": f"Bearer {auth_token}"}
       verify = requests.get("https://api.klingai.com/auth/verify", headers=headers)
       
       # Step 2: Get user profile (to extract projectId from response)
       profile = requests.get("https://api.klingai.com/user/profile", headers=headers)
       project_id = profile.json()['current_project_id']
       
       # Step 3: List images
       images = requests.get(f"https://api.klingai.com/projects/{project_id}/images", headers=headers)
       image_id = images.json()['images'][-1]['id']
       
       # Step 4: Download
       download = requests.get(f"https://api.klingai.com/images/{image_id}/download", headers=headers)
       return download.content

MCP executes code in ~2-3 seconds ↓ Image downloaded successfully


### Key Advantages for Your Use Case

| Aspect | Integuru | Headless CDP | Visible Browser |
|--------|----------|-------------|-----------------|
| **Speed** | 2-5s | 15-30s | 5-10min |
| **Reliability** | 95%+ | 80%+ | 99%+ |
| **Detection Risk** | Very low | Low | Medium |
| **Setup Complexity** | Low (1 HAR capture) | Medium (state duplication) | Zero |
| **Maintenance** | Auto-maintained if APIs stable | Manual if UI changes | Zero |
| **Best For** | API-driven sites (KlingAI, GitHub) | Complex UI interactions | Rare, novel actions |

---

## The "Fastest Modality" Optimizer

This is the **key innovation** that makes your system uniquely efficient:

```python
class ModalityOptimizer:
    def choose_modality(self, task_description, network_har):
        """
        Analyzes HAR and task to choose fastest automation path.
        Returns: ("integuru"|"headless_cdp"|"visible_browser", confidence, metadata)
        """
        
        # Step 1: Try Integuru
        integuru_result = self.test_integuru(network_har, task_description)
        if integuru_result.success and integuru_result.confidence > 0.90:
            return ("integuru", integuru_result.confidence, {
                "estimated_time": 3,
                "code_lines": integuru_result.code_length
            })
        
        # Step 2: Fall back to Headless CDP
        state = self.capture_browser_state()
        headless_result = self.test_headless_replication(state, task_description)
        if headless_result.success and headless_result.confidence > 0.80:
            return ("headless_cdp", headless_result.confidence, {
                "estimated_time": 20,
                "state_size_mb": len(state) / 1024 / 1024
            })
        
        # Step 3: Require human intervention
        return ("visible_browser", 1.0, {
            "estimated_time": "user_dependent",
            "reason": "Task too complex or novel"
        })

mitmproxy Configuration for Your System

Setup Script (.mitmproxy/addon.py)

import json
from mitmproxy import http, ctx
from datetime import datetime
from pathlib import Path

class ActivityRecorder:
    def __init__(self):
        self.flows_log = []
        self.start_time = datetime.now()
        self.session_dir = Path(f"./sessions/{self.start_time.isoformat()}")
        self.session_dir.mkdir(parents=True, exist_ok=True)
    
    def request(self, flow: http.HTTPFlow) -> None:
        """Log every HTTP request."""
        self.flows_log.append({
            "timestamp": datetime.now().isoformat(),
            "type": "request",
            "method": flow.request.method,
            "url": flow.request.url,
            "headers": dict(flow.request.headers),
            "body_size": len(flow.request.content) if flow.request.content else 0,
        })
    
    def response(self, flow: http.HTTPFlow) -> None:
        """Log every HTTP response."""
        self.flows_log.append({
            "timestamp": datetime.now().isoformat(),
            "type": "response",
            "status_code": flow.response.status_code,
            "headers": dict(flow.response.headers),
            "body_size": len(flow.response.content) if flow.response.content else 0,
        })
    
    def done(self) -> None:
        """Save activity log on exit."""
        output_file = self.session_dir / "activity_log.json"
        with open(output_file, "w") as f:
            json.dump(self.flows_log, f, indent=2)
        
        ctx.log.info(f"Activity recorded to {output_file}")
        ctx.log.info(f"Total requests: {len([f for f in self.flows_log if f['type'] == 'request'])}")

addons = [ActivityRecorder()]

Usage:

mitmdump -s .mitmproxy/addon.py \
         --set hardump=./network_requests.har \
         --set confdir=~/.mitmproxy \
         --mode regular \
         -q

MCP Server Tools (Expanded)

Tool 1: capture-and-analyze

@server.call_tool()
async def capture_and_analyze(browser_state):
    """
    Captures browser state, starts mitmproxy, waits for user action, then analyzes.
    """
    # 1. Start mitmproxy
    mitmproxy_process = start_mitmproxy()
    
    # 2. Capture initial state
    initial_state = {
        "cookies": await extension.get_cookies(),
        "localStorage": await extension.get_storage(),
        "indexedDB": await extension.get_indexeddb(),
        "screenshot": await browser.screenshot()
    }
    
    # 3. Wait for user action (with timeout)
    print("Perform action in visible browser. Waiting for network activity...")
    await asyncio.sleep(timeout)
    
    # 4. Export HAR from mitmproxy
    har_data = export_har_from_mitmproxy()
    
    # 5. Send to Integuru for analysis
    integuru_output = await integuru.analyze(
        har=har_data,
        prompt="What did the user do? Generate code to replicate it."
    )
    
    return {
        "har": har_data,
        "integuru_code": integuru_output,
        "initial_state": initial_state,
        "recommended_modality": await modality_optimizer.choose(har_data)
    }

Tool 2: execute-optimally

@server.call_tool()
async def execute_optimally(task_description, har_data):
    """
    Chooses and executes the fastest modality automatically.
    """
    modality, confidence, metadata = modality_optimizer.choose_modality(
        task_description, 
        har_data
    )
    
    if modality == "integuru":
        return await execute_integuru(har_data, task_description)
    
    elif modality == "headless_cdp":
        return await execute_headless_cdp(task_description)
    
    else:
        return {
            "status": "REQUIRES_USER_ACTION",
            "reason": "Task too complex for automation",
            "browser_window_id": open_visible_browser()
        }

Tool 3: record-and-replay

@server.call_tool()
async def record_and_replay(session_id, action_num=None):
    """
    Replays recorded automation from storage.
    """
    # Load execution log
    log = load_execution_log(session_id)
    
    if action_num:
        # Replay specific action
        action = log["actions"][action_num]
    else:
        # Replay entire session
        actions = log["actions"]
    
    for action in actions:
        if action["type"] == "CDP_COMMAND":
            await execute_cdp_command(action["method"], action["params"])
            await verify_with_screenshot(action["screenshot_after"])
        
        elif action["type"] == "WAIT":
            await asyncio.sleep(action["duration_ms"] / 1000)
    
    return {"status": "REPLAY_COMPLETE", "session_id": session_id}

Workflow: Complete Example (KlingAI Image Download)

Phase 1: Setup (One-time)

# Install Integuru
git clone https://github.com/Integuru-AI/Integuru
cd Integuru
poetry install

# Configure mitmproxy
mkdir -p ~/.mitmproxy/sessions
# Copy addon.py to ~/.mitmproxy/

# Install MCP server
pip install mcp-browser-automation

Phase 2: Capture Activity

# Terminal 1: Start mitmproxy
mitmdump --set hardump=./network_requests.har \
         -s ~/.mitmproxy/addon.py \
         --set confdir=~/.mitmproxy

# Terminal 2: User performs action in visible browser
# (Navigate to KlingAI, generate image, download it)
# mitmproxy captures all HTTP(S) traffic

Phase 3: Generate Automation Code

# After mitmproxy captures traffic:
poetry run integuru \
    --prompt "Download the generated image from KlingAI" \
    --model gpt-4o \
    --generate-code \
    --har-path ./network_requests.har

# Output: integuru_code.py

Phase 4: Execute Automatically

# In MCP server:
result = await mcp.execute_optimally(
    task_description="Download image from KlingAI",
    har_data=load_har("network_requests.har")
)

# Output:
# {
#     "modality": "integuru",
#     "execution_time": 2.3,
#     "success": True,
#     "image_path": "./downloaded_image.png",
#     "logs": {...}
# }

Phase 5: Review & Store

// automation_session_20251114_153700.json
{
    "session_id": "20251114_153700",
    "task": "Download image from KlingAI",
    "modality_used": "integuru",
    "execution_time_seconds": 2.3,
    "success": True,
    "recordings": {
        "network_har": "network_requests.har",
        "automation_log": "automation.json",
        "screenshots_dir": "screenshots/",
        "optimization_report": "optimization_report.json"
    },
    "reproducible_code": "integuru_code.py",
    "notes": "API reverse-engineered successfully. Can reuse for future downloads."
}

Performance Characteristics

Speed Comparison (KlingAI Image Download)

Modality Time Method
Integuru 2-5s Direct API calls
Headless CDP 15-30s Full browser automation
Visible Browser 5-10min User performs manually

Network Activity

Integuru (3 API calls):
├─ GET /api/auth/verify         150ms
├─ GET /api/projects/images     200ms
└─ GET /api/images/{id}/download 250ms
Total: 600ms

Headless CDP (5+ requests):
├─ Page load + JS execution      2000ms
├─ API calls                     3000ms
├─ Screenshots + GLM analysis   5000ms
└─ Overhead                     10000ms
Total: 20000ms

Security & Stealth Features

Detection Evasion Layers

  1. Network Layer: mitmproxy is running on localhost → invisible to external observers
  2. API Layer: Integuru-generated code uses real API endpoints → not detectable as automation
  3. Browser Layer: CDP extension hides navigator.webdriver → appears as normal user
  4. Session Layer: Replicated browser state includes full authentication → no re-login needed

Privacy

  • All HAR files stored locally (no cloud upload)
  • mitmproxy addon doesn't log sensitive data by default
  • Execution logs exclude credentials (extension filters them)
  • Screenshots stored encrypted if needed

Continuous Improvement

Feedback Loop

Each execution feeds back into optimization:

1. Record execution time for each modality
2. Track success/failure rates
3. Measure Integuru code generation quality
4. Update modality optimizer weights

→ Next execution automatically chooses faster route based on history

Metrics Dashboard

class MetricsCollector:
    def record_execution(self, session_data):
        """
        Tracks:
        - Modality selection accuracy
        - Execution time vs. estimate
        - Success rate per site
        - API stability (HAR changes)
        - Code generation quality
        """

Getting Started Checklist

  • Install Integuru + dependencies
  • Configure mitmproxy with HAR recording
  • Deploy CDP extension for state capture
  • Set up MCP server with all tools
  • Create mitmproxy addon for activity logging
  • Test on simple site (Wikipedia/GitHub)
  • Test on complex site (KlingAI/Notion)
  • Implement modality optimizer
  • Build metrics collection
  • Document automation workflows
  • Set up CI/CD for continuous improvement

Why This Architecture Excels

  1. Speed: Integuru finds fastest path automatically (2-5s instead of 20-30s)
  2. Reliability: Multiple fallback modalities ensure success
  3. Maintainability: APIs change less than UIs → less maintenance
  4. Traceability: Every step recorded with screenshots + GLM analysis
  5. Reusability: Generated code can be replayed or refactored
  6. Scalability: Each modality independent → can parallelize executions
  7. Optimization: Learns which sites benefit from which modality