How produck/mitm works
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.