# jpillora/chisel architecture

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

- Interactive diagram: https://gitdiagram.com/jpillora/chisel
- Repository: https://github.com/jpillora/chisel

## Overview

##### Project Overview and Purpose
- Chisel is a tunneling tool that creates secure TCP/UDP tunnels over HTTP using SSH for encryption and authentication.
- It is distributed as a single executable that contains both client and server capabilities.
- The README emphasizes features such as encrypted and authenticated connections, reverse tunneling, SOCKS5 support, and additional proxy assistance.

##### File Structure
- Top-level directories include:
- client: Contains client-side logic (client.go, client_connect.go, client_test.go)
- server: Contains server-side logic (server.go, server_handler.go, server_listen.go)
- share: Holds shared packages and utilities. Subdirectories here include:
▪ ccrypto: Cryptography routines (key generation, deterministic randomness, key helpers)
▪ cio: IO utilities like logging, pipe management, stdio connections (important for SSH and ProxyCommand)
▪ cnet: Networking utilities for handling TCP/UDP connections, WebSockets, HTTP server helpers, and connection stats
▪ cos: Common functions including pprof and signal handling
▪ settings: Handles configuration from environments, files, and remote settings (also contains authentication setup)
▪ tunnel: Contains tunnel logic (tunnel creation, both input and output handling, including UDP and SSH tunnels)
- test: Contains benchmarks and end-to-end tests, showing that performance and network interactions are important.
- example: Provides example configuration files (e.g., users.json, Flyfile, fly.toml) that may also be highlighted.

##### Main Components
a. Client Component:
- Represented by the “client” directory and its related files.
- Responsible for initiating connections to the server, maintaining tunnels, and handling local port forwarding, reverse tunnels, and SOCKS connections.
- Uses shared libraries for network connections (cnet), tunnel management (tunnel), and cryptography (ccrypto).

b. Server Component:
- Represented by the “server” directory.
- Listens for incoming connections, establishes tunnels, and manages HTTP and SSH-handled traffic.
- Implements authentication (via settings and users files) and also supports reverse proxy and SOCKS5 modes.
- Uses shared libraries similar to the client for core networking and cryptographic functions.

c. Shared Libraries/Modules:
- “share” acts as the library of common functions, covering:
- Encryption & key management (ccrypto)
- I/O handling and logging (cio)
- Network connections and WebSocket support (cnet)
- Signal handling and common utilities (cos)
- Tunnel setup and management (tunnel)
- Configuration and settings management (settings)
- These should be depicted as “supporting modules” that both client and server rely on.

d. External Interfaces and Flows:
- How the client connects to the server over HTTP/HTTPS and how the tunnel transport uses SSH under the hood.
- External networks: firewalls, destination endpoints (e.g., reverse proxy target, SOCKS5 connections), and, optionally, integration with Docker deployments (as hinted by .github and Dockerfile).

##### Relationships and Interactions
- Within both components, include internal flows such as:
- From client: CLI → tunnel configuration → network connection setup → encrypted SSH handshake.
- From server: Incoming network requests → authentication (using users.json or inline --auth) → session management → tunnel routing.
- Bidirectional communication for reverse tunneling (server listening and client proxying connections).

##### Architectural Patterns and Design Principles
- Single-executable design that encapsulates both client and server logic.
- A modular approach (separation of concerns) is evident in the distribution of common functionality into the “share” directory.
- Use of Go interfaces and dependency injection (common in Go projects) to facilitate testing (see extensive testing directories) and clear responsibilities.
- Emphasis on performance and concurrency (supported by benchmark tests and the use of efficient Go libraries).

##### Relevant Technologies and Frameworks
- Go Programming Language (with its standard libraries for networking, crypto, etc.)
- SSH protocol (for encryption and authentication)
- HTTP/HTTPS for tunnel transport
- WebSockets as an alternative connection mechanism when traversing proxies
- Docker (as indicated by Dockerfile and CI workflows)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Entities
    UserCLI["User/CLI Interface"]:::external

    %% Application Layer
    subgraph "Application Layer"
        ClientApp["Client: Manages_tunnel_requests,_local_port_forwarding"]:::client
        ServerApp["Server: Accepts_connections,_auth_verification,_tunnel_routing"]:::server
    end

    %% Shared Modules
    subgraph "Shared Modules"
        cc["Cryptography (ccrypto)"]:::shared
        cio["IO Utilities (cio)"]:::shared
        cnet["Networking (cnet)"]:::shared
        cos["Common Functions (cos)"]:::shared
        settings["Configuration (settings)"]:::shared
        tunnel["Tunnel Logic (tunnel)"]:::shared
    end

    %% Project Tools
    subgraph "Project Tools"
        examples["Example Configurations"]:::tool
        tests["Testing and Benchmarking"]:::tool
    end

    %% External Network
    External["Remote Endpoints_(Reverse_Proxy,_SOCKS5)"]:::external

    %% Relationships & Flows
    UserCLI -->|"initiates_connection"| ClientApp
    ClientApp -->|"establishes_tunnel"| ServerApp
    ServerApp -->|"sends_data_to"| External
    ServerApp -->|"receives_reverse_tunnel"| ClientApp

    %% Client Dependencies on Shared Modules
    ClientApp -->|"uses"| cc
    ClientApp -->|"uses"| cio
    ClientApp -->|"uses"| cnet
    ClientApp -->|"uses"| tunnel
    ClientApp -->|"uses"| settings

    %% Server Dependencies on Shared Modules
    ServerApp -->|"uses"| cc
    ServerApp -->|"uses"| cio
    ServerApp -->|"uses"| cnet
    ServerApp -->|"uses"| cos
    ServerApp -->|"uses"| tunnel
    ServerApp -->|"uses"| settings

    %% Styling
    classDef client fill:#AED6F1,stroke:#3498DB,stroke-width:2px;
    classDef server fill:#F9E79F,stroke:#F1C40F,stroke-width:2px;
    classDef shared fill:#D5F5E3,stroke:#28B463,stroke-width:2px;
    classDef external fill:#F1948A,stroke:#E74C3C,stroke-width:2px,stroke-dasharray: 5, 5;
    classDef tool fill:#E8DAEF,stroke:#8E44AD,stroke-width:2px;

    %% Click Events
    click ClientApp "https://github.com/jpillora/chisel/tree/master/client/"
    click ServerApp "https://github.com/jpillora/chisel/tree/master/server/"
    click cc "https://github.com/jpillora/chisel/tree/master/share/ccrypto/"
    click cio "https://github.com/jpillora/chisel/tree/master/share/cio/"
    click cnet "https://github.com/jpillora/chisel/tree/master/share/cnet/"
    click cos "https://github.com/jpillora/chisel/tree/master/share/cos/"
    click settings "https://github.com/jpillora/chisel/tree/master/share/settings/"
    click tunnel "https://github.com/jpillora/chisel/tree/master/share/tunnel/"
    click examples "https://github.com/jpillora/chisel/tree/master/example/"
    click tests "https://github.com/jpillora/chisel/tree/master/test/"
```
