# produck/mitm architecture

> Architecture diagram of the GitHub repository produck/mitm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/produck/mitm
- Repository: https://github.com/produck/mitm

## Overview

##### Project Type and Purpose
- This is a Node.js-based proxy server, designed to intercept and handle HTTP, HTTPS, WS, and WSS requests.
- The README shows that it supports configurable interception strategies for different types of requests, including SSL/TLS interception (via its certificate management) and support for WebSocket upgrades.
- The project's functionality is made available as an npm package, suggesting that users can integrate it or run it as a standalone server.

##### File Structure to Identify Main Components
- Top-level files (index.js, package.json, LICENSE, README.md, etc.) indicate a standard Node.js module.
- The src directory contains key modules:
- server.js: Likely the central module that sets up and starts the proxy server.
- certificate.js: Handles root and dynamic certificate generation; crucial for HTTPS interception.
- normalize.js and shadow.js: Likely support preprocessing of requests or response manipulation.
- strategy folder: Contains a subfolder “handler” with context.js, request.js, upgrade.js and an index.js for orchestrating strategy logic. These files form the strategy layer that dictates how different protocols (HTTP, HTTPS, WS, WSS) are processed.
- The assets folder hosts the CA certificate (node-mitmproxy.ca.crt) used for SSL interception.
- The test folder includes unit and integration tests, illustrating how each component and strategy is verified.

##### Extract Architecture Insights from the README
- The README shows detailed API options, including:
- strategy object (with functions for handling request, response, sslConnect, and websocket).
- socket configuration (defining path and naming of the socket store).
- certificate information (including functions for certificate storage and retrieval).
- It provides sample code for using the module to create a MitmServer instance and instructing behavior for HTTP, HTTPS, and WebSocket connections.

a. Main Components to Include:
- Proxy Server (Server Module): Central component that listens for connections.
- Strategy Layer: Contains subcomponents (handlers) for various operations:
- Request Handler
- Response Handler
- Upgrade (WebSocket) Handler
- SSL Connect Handler (to decide on SSL interception)
- Certificate Manager: Uses certificate.js and related functions to generate, retrieve, and store SSL certificates.
- Socket Manager: Manages the socket connection path and naming, as specified under the socket options.
- Client and External Services: Represent incoming client requests and target servers being proxied.

b. Relationships and Data Flow:
- Client requests (HTTP/HTTPS/WS/WSS) enter the Proxy Server.
- The Proxy Server delegates the processing to the Strategy Layer:
- For HTTP/HTTPS: The request flows to the Request Handler and/or Response Handler.
- For HTTPS: The SSL Connect Handler collaborates with the Certificate Manager to generate/load certificates for secure interception.
- For WebSocket connections: The Upgrade Handler in the Strategy Layer facilitates bidirectional proxying.
- The Socket Manager ensures that connection metadata (such as protocol and hostname) is formatted and stored correctly.
- After applying the custom strategies, the differentiated traffic is either forwarded to the destination or responded to directly.

c. Architectural Patterns and Design Principles:
- Modular design: The separation between core server functionality and strategy modules illustrates a clear separation of concerns.
- Extensibility: Customizable strategy options allow users to override default behavior easily.
- Dynamic certificate management: A dynamic module (Certificate Manager) handles certificate generation and caching, supporting HTTPS interception without browser complaints.
- Emphasis on dependency injection via options – promoting flexible integration and testing.

d. Technologies and Frameworks:
- Node.js as the runtime environment.
- Standard Node modules for HTTP/S and possibly net modules for socket handling.
- Use of certificates (handled via modules like certificate.js) for MITM interception.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Entities
    Client["Client (HTTP/HTTPS/WS/WSS)"]:::external
    Target["External Target Server"]:::external

    %% Core Components Subgraph
    subgraph "Core Components"
        ProxyServer["Proxy Server (server.js)"]:::core
        CertManager["Certificate Manager (certificate.js)"]:::core
        SocketManager["Socket Manager (Config Based)"]:::core
    end

    %% Strategy Layer Subgraph
    subgraph "Strategy Layer"
        StrategyOrch["Strategy Orchestrator (strategy/index.js)"]:::strategy
        ReqHandler["Request Handler (request.js)"]:::strategy
        UpgHandler["Upgrade Handler (upgrade.js)"]:::strategy
        CtxHandler["Context Handler (context.js)"]:::strategy
    end

    %% Preprocessing Modules Subgraph
    subgraph "Preprocessing Modules"
        Normalize["Normalization Module (normalize.js)"]:::support
        Shadow["Shadow Module (shadow.js)"]:::support
    end

    %% Assets
    CA["CA Certificate (node-mitmproxy.ca.crt)"]:::asset

    %% Connections
    Client -->|"sends_request"| ProxyServer
    ProxyServer -->|"delegates_to"| StrategyOrch
    ProxyServer -->|"invokes_cert"| CertManager
    ProxyServer -->|"applies"| Normalize
    ProxyServer -->|"applies"| Shadow
    ProxyServer -->|"configures"| SocketManager
    CertManager -->|"retrieves_CA"| CA
    StrategyOrch -->|"handles_HTTP"| ReqHandler
    StrategyOrch -->|"handles_WS"| UpgHandler
    StrategyOrch -->|"manages_context"| CtxHandler
    ProxyServer -->|"forwards_to"| Target

    %% Click Events
    click ProxyServer "https://github.com/produck/mitm/blob/master/src/server.js"
    click CertManager "https://github.com/produck/mitm/blob/master/src/certificate.js"
    click ReqHandler "https://github.com/produck/mitm/blob/master/src/strategy/handler/request.js"
    click UpgHandler "https://github.com/produck/mitm/blob/master/src/strategy/handler/upgrade.js"
    click CtxHandler "https://github.com/produck/mitm/blob/master/src/strategy/handler/context.js"
    click StrategyOrch "https://github.com/produck/mitm/blob/master/src/strategy/index.js"
    click Normalize "https://github.com/produck/mitm/blob/master/src/normalize.js"
    click Shadow "https://github.com/produck/mitm/blob/master/src/shadow.js"
    click CA "https://github.com/produck/mitm/blob/master/assets/node-mitmproxy.ca.crt"

    %% Styles
    classDef core fill:#f9d,stroke:#000,stroke-width:2px;
    classDef strategy fill:#cff,stroke:#000,stroke-width:2px;
    classDef asset fill:#fcf,stroke:#000,stroke-width:2px;
    classDef support fill:#efe,stroke:#000,stroke-width:2px;
    classDef external fill:#ff9,stroke:#000,stroke-width:2px;
```
