# aristochen/usb-proxy architecture

> Architecture diagram of the GitHub repository aristochen/usb-proxy, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aristochen/usb-proxy
- Repository: https://github.com/aristochen/usb-proxy

## Overview

usb-proxy is a USB man-in-the-middle: it presents a connected physical USB device to a separate USB host through raw-gadget, forwarding control and endpoint traffic between them. The executable discovers the device, builds the host-visible descriptors, initializes the gadget, and enters the control-event loop; proxy code handles control requests and endpoint transfers. Optional capabilities include injection rules (pattern replacement, byte operations, Lua transforms), endpoint remapping, and isochronous batching. The excerpts show core transport and transformation behavior; some setup and endpoint wiring is documented in the README but not fully excerpted.

Main entry points: [`usb-proxy.cpp`](https://github.com/aristochen/usb-proxy/blob/main/usb-proxy.cpp) (Proxy executable), [`host-raw-gadget.cpp`](https://github.com/aristochen/usb-proxy/blob/main/host-raw-gadget.cpp) (Raw-gadget I/O), [`proxy.cpp`](https://github.com/aristochen/usb-proxy/blob/main/proxy.cpp) (Control event loop), [`device-libusb.cpp`](https://github.com/aristochen/usb-proxy/blob/main/device-libusb.cpp) (libusb transfers)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Runtime entry"]
  node_main["Proxy executable<br/>[usb-proxy.cpp]"]
  node_descriptor["Descriptor setup<br/>[usb-proxy.cpp]"]
  node_remap["Endpoint remapping<br/>[usb-proxy.cpp]"]
  node_shared["Shared runtime state<br/>[misc.h]"]
  node_helpers["Hex conversion<br/>[misc.cpp]"]
end

subgraph group_device["Device transport"]
  node_discovery["Device discovery<br/>[device-libusb.cpp]"]
  node_device_io["libusb transfers<br/>[device-libusb.cpp]"]
  node_iso["Isochronous batching<br/>[device-libusb.cpp]"]
end

subgraph group_gadget["Gadget transport"]
  node_gadget_api["Raw-gadget I/O"]
end

subgraph group_features["Proxy features"]
  node_control_loop["Control event loop<br/>[proxy.cpp]"]
  node_endpoint_proxy["Endpoint forwarding<br/>[proxy.cpp]"]
  node_injection["Injection rules<br/>[injection.json]"]
  node_transforms["Payload transforms<br/>[proxy.cpp]"]
  node_lua_scripts["Lua transforms"]
end

node_host(("USB host"))
node_device_actor(("USB device"))

node_host -->|"initiates proxying"| node_main
node_device_actor -->|"connects over USB"| node_discovery
node_main -->|"selects device"| node_discovery
node_main -->|"builds descriptors"| node_descriptor
node_main -.->|"starts gadget"| node_gadget_api
node_descriptor -.->|"supplies descriptors"| node_gadget_api
node_host -->|"sends USB traffic"| node_gadget_api
node_gadget_api -.->|"delivers control events"| node_control_loop
node_control_loop -->|"forwards control requests"| node_device_io
node_control_loop -->|"replies or stalls"| node_gadget_api
node_control_loop -->|"uses configured rules"| node_injection
node_control_loop -->|"applies transforms"| node_transforms
node_endpoint_proxy -->|"transfers endpoint data"| node_device_io
node_endpoint_proxy -.->|"reads and writes"| node_gadget_api
node_endpoint_proxy -->|"uses endpoint rules"| node_injection
node_transforms -->|"loads scripts"| node_lua_scripts
node_main -->|"optionally remaps"| node_remap
node_remap -->|"rewrites descriptors"| node_descriptor
node_device_io -->|"uses batch transfers"| node_iso
node_main -.->|"configures runtime"| node_shared
node_transforms -.->|"decodes hex"| node_helpers

click node_main "https://github.com/aristochen/usb-proxy/blob/main/usb-proxy.cpp"
click node_discovery "https://github.com/aristochen/usb-proxy/blob/main/device-libusb.cpp"
click node_device_io "https://github.com/aristochen/usb-proxy/blob/main/device-libusb.cpp"
click node_descriptor "https://github.com/aristochen/usb-proxy/blob/main/usb-proxy.cpp"
click node_gadget_api "https://github.com/aristochen/usb-proxy/blob/main/host-raw-gadget.cpp"
click node_control_loop "https://github.com/aristochen/usb-proxy/blob/main/proxy.cpp"
click node_endpoint_proxy "https://github.com/aristochen/usb-proxy/blob/main/proxy.cpp"
click node_injection "https://github.com/aristochen/usb-proxy/blob/main/injection.json"
click node_transforms "https://github.com/aristochen/usb-proxy/blob/main/proxy.cpp"
click node_lua_scripts "https://github.com/aristochen/usb-proxy/tree/main/scripts"
click node_remap "https://github.com/aristochen/usb-proxy/blob/main/usb-proxy.cpp"
click node_iso "https://github.com/aristochen/usb-proxy/blob/main/device-libusb.cpp"
click node_shared "https://github.com/aristochen/usb-proxy/blob/main/misc.h"
click node_helpers "https://github.com/aristochen/usb-proxy/blob/main/misc.cpp"

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_main,node_descriptor,node_remap,node_shared,node_helpers toneBlue
class node_discovery,node_device_io,node_iso toneAmber
class node_gadget_api toneMint
class node_control_loop,node_endpoint_proxy,node_injection,node_transforms,node_lua_scripts toneRose
class node_host,node_device_actor toneIndigo
```

## Components

### Runtime entry

- [Proxy executable](https://github.com/aristochen/usb-proxy/blob/main/usb-proxy.cpp): `usb-proxy.cpp`
- [Descriptor setup](https://github.com/aristochen/usb-proxy/blob/main/usb-proxy.cpp): `usb-proxy.cpp`
- [Endpoint remapping](https://github.com/aristochen/usb-proxy/blob/main/usb-proxy.cpp): `usb-proxy.cpp`
- [Shared runtime state](https://github.com/aristochen/usb-proxy/blob/main/misc.h): `misc.h`
- [Hex conversion](https://github.com/aristochen/usb-proxy/blob/main/misc.cpp): `misc.cpp`

### Device transport

- [Device discovery](https://github.com/aristochen/usb-proxy/blob/main/device-libusb.cpp): `device-libusb.cpp`
- [libusb transfers](https://github.com/aristochen/usb-proxy/blob/main/device-libusb.cpp): `device-libusb.cpp`
- [Isochronous batching](https://github.com/aristochen/usb-proxy/blob/main/device-libusb.cpp): `device-libusb.cpp`

### Gadget transport

- [Raw-gadget I/O](https://github.com/aristochen/usb-proxy/blob/main/host-raw-gadget.cpp): `host-raw-gadget.cpp`

### Proxy features

- [Control event loop](https://github.com/aristochen/usb-proxy/blob/main/proxy.cpp): `proxy.cpp`
- [Endpoint forwarding](https://github.com/aristochen/usb-proxy/blob/main/proxy.cpp): `proxy.cpp`
- [Injection rules](https://github.com/aristochen/usb-proxy/blob/main/injection.json): `injection.json`
- [Payload transforms](https://github.com/aristochen/usb-proxy/blob/main/proxy.cpp): `proxy.cpp`
- [Lua transforms](https://github.com/aristochen/usb-proxy/tree/main/scripts): `scripts`

### Other components

- **USB host**
- **USB device**

## Connections

- USB host → Proxy executable: initiates proxying (evidence: `README.md`)
- USB device → Device discovery: connects over USB (evidence: `README.md`)
- Proxy executable → Device discovery: selects device (evidence: `README.md`)
- Proxy executable → Descriptor setup: builds descriptors (evidence: `usb-proxy.cpp`)
- Proxy executable → Raw-gadget I/O: starts gadget
- Descriptor setup → Raw-gadget I/O: supplies descriptors
- USB host → Raw-gadget I/O: sends USB traffic (evidence: `README.md`)
- Raw-gadget I/O → Control event loop: delivers control events
- Control event loop → libusb transfers: forwards control requests (evidence: `proxy.cpp`)
- Control event loop → Raw-gadget I/O: replies or stalls (evidence: `proxy.cpp`)
- Control event loop → Injection rules: uses configured rules (evidence: `README.md`)
- Control event loop → Payload transforms: applies transforms (evidence: `proxy.cpp`)
- Endpoint forwarding → libusb transfers: transfers endpoint data (evidence: `proxy.cpp`)
- Endpoint forwarding → Raw-gadget I/O: reads and writes
- Endpoint forwarding → Injection rules: uses endpoint rules (evidence: `README.md`)
- Payload transforms → Lua transforms: loads scripts (evidence: `README.md`)
- Proxy executable → Endpoint remapping: optionally remaps (evidence: `usb-proxy.cpp`)
- Endpoint remapping → Descriptor setup: rewrites descriptors (evidence: `usb-proxy.cpp`)
- libusb transfers → Isochronous batching: uses batch transfers (evidence: `device-libusb.cpp`)
- Proxy executable → Shared runtime state: configures runtime (evidence: `usb-proxy.cpp`)
- Payload transforms → Hex conversion: decodes hex
