# denuitt1/mhr-cfw architecture

> Architecture diagram of the GitHub repository denuitt1/mhr-cfw, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/denuitt1/mhr-cfw
- Repository: https://github.com/denuitt1/mhr-cfw

## Overview

1. **What this repository is**
   This is a layered proxy/relay system for outbound web traffic. The local Python process acts as a client-side HTTP(S) proxy with MITM capabilities, then tunnels requests through Google-facing infrastructure and Cloudflare Worker relays to an external exit point.

2. **Primary traffic path**
   The default path is: client/browser or system proxy → local relay on `127.0.0.1:8085` → Google/CDN fronting layer → Google Apps Script relay → Cloudflare Worker → destination site. The external observer is meant to see only benign-looking traffic to allowed front domains.

3. **Local proxy subsystem**
   The core runtime lives in Python (`main.py` + `src/`). `src/proxy_server.py` is the network entrypoint, while `src/mitm.py` handles SSL interception so HTTPS traffic can be relayed. `src/cert_installer.py` supports installing the local CA certificate needed for MITM on the client device.

4. **Request shaping and transport**
   `src/codec.py` and `src/h2_transport.py` are the payload/transport layer for moving requests through the relay chain, including HTTP/2-related behavior. This layer is the main boundary between normal client HTTP semantics and the encoded relay protocol carried to the remote endpoints.

5. **Fronting and target selection**
   `src/domain_fronter.py` and `src/google_ip_scanner.py` support selecting or validating front domains and Google-facing endpoints used to hide the real destination. These pieces are what make the relay traffic look like requests to acceptable domains rather than direct outbound connections.

6. **Local environment and binding utilities**
   `src/lan_utils.py` helps the proxy be reachable from other devices on the same LAN or from VMs, while `src/constants.py` centralizes runtime parameters. `src/logging_utils.py` provides consistent operational logging across the proxy pipeline.

7. **Cloud-side relay boundary**
   The Google Apps Script deployment (`deploy/gas/Code.gs`) is the first remote relay endpoint. It authenticates requests using a shared secret, then forwards them to the Cloudflare Worker. This is a key trust boundary: the local proxy only talks to the GAS web app, not directly to the destination.

8. **Cloudflare Worker exit layer**
   `deploy/cloudflare-worker/worker.js` is the Worker-side relay that receives requests from GAS and performs the actual outbound `fetch()`. In the base design, this is the exit point visible to destination sites, with the Worker’s egress IPs being the effective source address.

9. **Optional stable-egress upstream forwarder**
   For sites that bind sessions or CAPTCHA tokens to a stable IP, there is an optional Node-based upstream forwarder in `deploy/upstream_forwarder/`. In that mode, the Worker sends outbound fetches to a VPS-hosted forwarder so the final exit IP becomes stable and under your control.

10. **Configuration and runtime orchestration**
   `config.example.json`, `run.sh`, `run.bat`, and `setup.py` define startup and configuration for the local relay. The local process expects deployment-specific secrets/IDs for the GAS layer, and can also be configured with optional forwarder host routing.

11. **Packaging and deployment artifacts**
   The repo includes Docker and Compose files for repeatable local or VPS deployment, plus a `traefik.yml` in the forwarder setup for TLS termination. These artifacts matter because the architecture spans desktop client, cloud relay, and optional VPS exit infrastructure.

12. **High-level diagram boundaries**
   An accurate architecture diagram should show four distinct zones: the local client machine, Google Apps Script relay, Cloudflare Worker relay, and optional VPS forwarder. The important data flow is request encapsulation through the relays, certificate-based MITM on the client side, and optional upstream egress stabilization for CAPTCHA-sensitive destinations.

Main entry points: [`src/proxy_server.py`](https://github.com/denuitt1/mhr-cfw/blob/main/src/proxy_server.py) (Proxy server), [`deploy/cloudflare-worker/worker.js`](https://github.com/denuitt1/mhr-cfw/blob/main/deploy/cloudflare-worker/worker.js) (Worker exit), [`deploy/upstream_forwarder/upstream_forwarder.js`](https://github.com/denuitt1/mhr-cfw/blob/main/deploy/upstream_forwarder/upstream_forwarder.js) (Upstream forwarder), [`docker-compose.yml`](https://github.com/denuitt1/mhr-cfw/blob/main/docker-compose.yml) (Compose)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_external["External"]
  node_client(("Client<br/>browser/system proxy"))
  node_destination(("Site"))
end

subgraph group_local["Local runtime"]
  node_main_py["Python app<br/>entrypoint<br/>[main.py]"]
  node_proxy_server["Proxy server<br/>HTTP proxy<br/>[proxy_server.py]"]
  node_mitm["MITM<br/>TLS interception<br/>[mitm.py]"]
  node_codec["Relay codec<br/>payload encoder<br/>[codec.py]"]
  node_h2_transport["H2 transport<br/>transport layer<br/>[h2_transport.py]"]
  node_domain_fronter["Fronting<br/>domain selection<br/>[domain_fronter.py]"]
  node_google_ip_scanner["IP scan<br/>endpoint discovery"]
  node_cert_installer["Cert install<br/>CA setup<br/>[cert_installer.py]"]
  node_lan_utils["LAN helpers<br/>network utility<br/>[lan_utils.py]"]
  node_constants["Config consts<br/>runtime parameters<br/>[constants.py]"]
  node_logging_utils["Logging<br/>observability<br/>[logging_utils.py]"]
end

subgraph group_google["Google relay"]
  node_gas["GAS relay<br/>Apps Script web app<br/>[Code.gs]"]
end

subgraph group_worker["Worker exit"]
  node_worker["Worker exit<br/>Cloudflare Worker<br/>[worker.js]"]
  node_upstream_forwarder["Upstream forwarder<br/>VPS relay"]
end

subgraph group_deploy["Deployment"]
  node_run_scripts["Startup<br/>launch scripts<br/>[run.sh]"]
  node_compose["Compose<br/>container orchestration<br/>[docker-compose.yml]"]
end

node_client -->|"HTTP(S)"| node_proxy_server
node_main_py -->|"starts"| node_proxy_server
node_proxy_server -->|"intercepts"| node_mitm
node_proxy_server -->|"encodes"| node_codec
node_codec -->|"rides on"| node_h2_transport
node_proxy_server -->|"fronts through"| node_domain_fronter
node_domain_fronter -->|"validates"| node_google_ip_scanner
node_proxy_server -->|"installs CA"| node_cert_installer
node_proxy_server -->|"binds"| node_lan_utils
node_proxy_server -->|"reads"| node_constants
node_proxy_server -->|"logs"| node_logging_utils
node_h2_transport -->|"forwards to"| node_gas
node_gas -->|"relays"| node_worker
node_worker -->|"fetches"| node_destination
node_worker -.->|"optional egress"| node_upstream_forwarder
node_upstream_forwarder -->|"exits to"| node_destination
node_run_scripts -->|"launches"| node_main_py
node_compose -->|"deploys"| node_upstream_forwarder
node_compose -.->|"deploys"| node_worker

click node_main_py "https://github.com/denuitt1/mhr-cfw/blob/main/main.py"
click node_proxy_server "https://github.com/denuitt1/mhr-cfw/blob/main/src/proxy_server.py"
click node_mitm "https://github.com/denuitt1/mhr-cfw/blob/main/src/mitm.py"
click node_codec "https://github.com/denuitt1/mhr-cfw/blob/main/src/codec.py"
click node_h2_transport "https://github.com/denuitt1/mhr-cfw/blob/main/src/h2_transport.py"
click node_domain_fronter "https://github.com/denuitt1/mhr-cfw/blob/main/src/domain_fronter.py"
click node_google_ip_scanner "https://github.com/denuitt1/mhr-cfw/blob/main/src/google_ip_scanner.py"
click node_cert_installer "https://github.com/denuitt1/mhr-cfw/blob/main/src/cert_installer.py"
click node_lan_utils "https://github.com/denuitt1/mhr-cfw/blob/main/src/lan_utils.py"
click node_constants "https://github.com/denuitt1/mhr-cfw/blob/main/src/constants.py"
click node_logging_utils "https://github.com/denuitt1/mhr-cfw/blob/main/src/logging_utils.py"
click node_gas "https://github.com/denuitt1/mhr-cfw/blob/main/deploy/gas/Code.gs"
click node_worker "https://github.com/denuitt1/mhr-cfw/blob/main/deploy/cloudflare-worker/worker.js"
click node_upstream_forwarder "https://github.com/denuitt1/mhr-cfw/blob/main/deploy/upstream_forwarder/upstream_forwarder.js"
click node_run_scripts "https://github.com/denuitt1/mhr-cfw/blob/main/run.sh"
click node_compose "https://github.com/denuitt1/mhr-cfw/blob/main/docker-compose.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_client,node_destination toneBlue
class node_main_py,node_proxy_server,node_mitm,node_codec,node_h2_transport,node_domain_fronter,node_google_ip_scanner,node_cert_installer,node_lan_utils,node_constants,node_logging_utils toneAmber
class node_gas toneMint
class node_worker,node_upstream_forwarder toneRose
class node_run_scripts,node_compose toneIndigo
```

## Components

### External

User device and destination sites outside the repo

- **Client**: browser/system proxy · Browser or system traffic source
- **Site**: target site · Final upstream destination seen by the relay chain

### Local runtime

Python proxy and client-side helpers

- [Python app](https://github.com/denuitt1/mhr-cfw/blob/main/main.py): entrypoint · Top-level local process launcher · `main.py`
- [Proxy server](https://github.com/denuitt1/mhr-cfw/blob/main/src/proxy_server.py): HTTP proxy · Local network entrypoint for intercepted requests · `src/proxy_server.py`
- [MITM](https://github.com/denuitt1/mhr-cfw/blob/main/src/mitm.py): TLS interception · SSL interception for HTTPS relaying · `src/mitm.py`
- [Relay codec](https://github.com/denuitt1/mhr-cfw/blob/main/src/codec.py): payload encoder · Encodes requests for the relay chain · `src/codec.py`
- [H2 transport](https://github.com/denuitt1/mhr-cfw/blob/main/src/h2_transport.py): transport layer · HTTP/2-oriented relay transport · `src/h2_transport.py`
- [Fronting](https://github.com/denuitt1/mhr-cfw/blob/main/src/domain_fronter.py): domain selection · Chooses benign-looking front domains and endpoints · `src/domain_fronter.py`
- [IP scan](https://github.com/denuitt1/mhr-cfw/blob/main/src/google_ip_scanner.py): endpoint discovery · Validates or selects Google-facing endpoints · `src/google_ip_scanner.py`
- [Cert install](https://github.com/denuitt1/mhr-cfw/blob/main/src/cert_installer.py): CA setup · Installs the local CA used for MITM · `src/cert_installer.py`
- [LAN helpers](https://github.com/denuitt1/mhr-cfw/blob/main/src/lan_utils.py): network utility · Makes the proxy reachable across local networks or VMs · `src/lan_utils.py`
- [Config consts](https://github.com/denuitt1/mhr-cfw/blob/main/src/constants.py): runtime parameters · Shared runtime parameters and defaults · `src/constants.py`
- [Logging](https://github.com/denuitt1/mhr-cfw/blob/main/src/logging_utils.py): observability · Shared operational logging helpers · `src/logging_utils.py`

### Google relay

Apps Script boundary that forwards relay traffic

- [GAS relay](https://github.com/denuitt1/mhr-cfw/blob/main/deploy/gas/Code.gs): Apps Script web app · Trusted cloud relay that forwards to the worker · `deploy/gas/Code.gs`

### Worker exit

Cloudflare Worker and optional upstream egress

- [Worker exit](https://github.com/denuitt1/mhr-cfw/blob/main/deploy/cloudflare-worker/worker.js): Cloudflare Worker · Relay exit layer that performs outbound fetches · `deploy/cloudflare-worker/worker.js`
- [Upstream forwarder](https://github.com/denuitt1/mhr-cfw/blob/main/deploy/upstream_forwarder/upstream_forwarder.js): VPS relay · Optional stable-egress forwarder for sensitive sessions · `deploy/upstream_forwarder/upstream_forwarder.js`

### Deployment

Packaging and orchestration artifacts

- [Startup](https://github.com/denuitt1/mhr-cfw/blob/main/run.sh): launch scripts · Local startup and configuration entrypoints · `run.sh`
- [Compose](https://github.com/denuitt1/mhr-cfw/blob/main/docker-compose.yml): container orchestration · Container deployment for local or server-side components · `docker-compose.yml`

## Connections

- Client → Proxy server: HTTP(S) · Client traffic enters the local proxy
- Python app → Proxy server: starts · Main process launches the proxy server
- Proxy server → MITM: intercepts · HTTPS traffic is decrypted and reissued
- Proxy server → Relay codec: encodes · Requests are packaged for relay transport
- Relay codec → H2 transport: rides on · Encoded payloads are carried over the transport layer
- Proxy server → Fronting: fronts through · Selects benign-looking front domains and endpoints
- Fronting → IP scan: validates · Fronting candidates are checked against Google-facing infrastructure
- Proxy server → Cert install: installs CA · Local trust setup for MITM interception
- Proxy server → LAN helpers: binds · Exposes the proxy to LAN or VM clients
- Proxy server → Config consts: reads · Runtime settings and defaults
- Proxy server → Logging: logs · Operational events are recorded consistently
- H2 transport → GAS relay: forwards to · Transport carries requests to the Apps Script relay
- GAS relay → Worker exit: relays · Apps Script forwards authenticated requests to the Worker
- Worker exit → Site: fetches · Worker performs the outbound request to the target site
- Worker exit → Upstream forwarder: optional egress · Worker can send traffic to a stable upstream forwarder
- Upstream forwarder → Site: exits to · Stable VPS-controlled exit reaches the target site
- Startup → Python app: launches · Shell/batch startup scripts start the local Python runtime
- Compose → Upstream forwarder: deploys · Container orchestration for the optional forwarder stack
- Compose → Worker exit: deploys · Containerized deployment artifacts for relay components
