#!/usr/bin/env python3
"""
═══════════════════════════════════════════════════════════
🔥 DIDON HTTP Proxy Capture - Works 100%
═══════════════════════════════════════════════════════════
Simple HTTP proxy to capture all DIDON API calls
No Frida needed!
"""

from http.server import HTTPServer, BaseHTTPRequestHandler
import json
from datetime import datetime

class ProxyHandler(BaseHTTPRequestHandler):

    def log_request_data(self, method):
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

        print("\n" + "═" * 70)
        print(f"🔥 {method} REQUEST - {timestamp}")
        print("═" * 70)
        print(f"📍 Path: {self.path}")
        print(f"📤 Method: {method}")

        # Headers
        print("\n📋 Headers:")
        for header, value in self.headers.items():
            print(f"   {header}: {value}")

        # Body
        content_length = self.headers.get('Content-Length')
        if content_length:
            body = self.rfile.read(int(content_length)).decode('utf-8')
            print(f"\n📦 Request Body:")
            print(body)

            try:
                json_data = json.loads(body)
                print(f"\n🔍 Parsed JSON:")
                print(json.dumps(json_data, indent=2, ensure_ascii=False))
            except:
                pass

        print("═" * 70)

    def do_GET(self):
        self.log_request_data("GET")

        # Send response
        self.send_response(200)
        self.send_header('Content-Type', 'application/json')
        self.end_headers()

        response = {
            "status": "captured",
            "message": "Request captured by proxy"
        }
        self.wfile.write(json.dumps(response).encode())

    def do_POST(self):
        self.log_request_data("POST")

        # Send fake success response
        self.send_response(200)
        self.send_header('Content-Type', 'application/json')
        self.end_headers()

        # Fake login response
        response = {
            "id": 999,
            "userName": "ProxyUser",
            "code": "PROXY123",
            "android_id": "proxy_capture",
            "exp_date": "2099-12-31",
            "cre_date": "2025-01-01"
        }
        self.wfile.write(json.dumps(response, ensure_ascii=False).encode())

    def log_message(self, format, *args):
        # Suppress default logging
        pass

def run_proxy(port=8080):
    server = HTTPServer(('0.0.0.0', port), ProxyHandler)
    print("\n" + "═" * 70)
    print("🔥 DIDON Capture Proxy Started")
    print("═" * 70)
    print(f"\n✅ Listening on port {port}")
    print(f"✅ Server IP: Use 'ipconfig' or 'ifconfig' to get your IP")
    print("\n📱 Configure your Android device:")
    print("   1. Connect to same WiFi as this computer")
    print("   2. WiFi Settings > Modify Network > Advanced")
    print("   3. Proxy: Manual")
    print(f"   4. Hostname: YOUR_COMPUTER_IP")
    print(f"   5. Port: {port}")
    print("\n🎯 All HTTP traffic will be captured here!")
    print("═" * 70 + "\n")

    try:
        server.serve_forever()
    except KeyboardInterrupt:
        print("\n\n✅ Proxy stopped")
        server.shutdown()

if __name__ == '__main__':
    run_proxy(8080)
