# sonertari/sslproxy architecture

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

- Interactive diagram: https://gitdiagram.com/sonertari/sslproxy
- Repository: https://github.com/sonertari/sslproxy

## Overview

##### Project Type and Purpose
- SSLproxy is an open-source network proxy tool (not a full-stack web app) written in C.
- It transparently intercepts (diverts) SSL/TLS and TCP traffic, decrypts it, feeds it to external “listening” programs for deep inspection or modification, then re-encrypts and forwards to the original destination.
- It uses NAT engines (pf/ipfw/netfilter/TPROXY) for transparent redirection and supports extensive filtering, certificate forging, user authentication, logging, and protocol validation.

##### File-Structure Analysis
- Top level: build scripts (`Mk/`, `GNUmakefile`, `BSDmakefile`), configuration (`src/sslproxy.conf{.5}`), and documentation (`README.md`, man pages).
- `src/` – C source code:
- `main.c`, `opts.c` (CLI & config parser)
- `proxy*.c` (core loop, connection management, threading)
- `filter.c` (filter rules engine)
- `ssl.c`, `cert.c` (OpenSSL integration, on-the-fly certificate generation)
- `proto*.c` (per-protocol handlers: HTTP, SMTP, POP3, passthrough, TCP)
- `cache*.c` (session/session key caching)
- `privsep.c` (privilege separation)
- `log*.c` (logging and pcap/mirror support)
- `nat.c` (abstraction over OS NAT/divert mechanisms)
- `extra/` – helper scripts; `tests/` – unit & end-to-end test harness; `xnu/` – OS headers for Mac PF compatibility.

##### Key Architectural Insights from README
- Mode of operation: client packets → OS NAT engine → SSLproxy → decrypt → divert to listening program → get back → encrypt → server → response reverse path.
- Core is event-driven (libevent), lock-free threading, modular protocol plug-ins, flexible “proxyspec” and filtering rule engine.
- External dependencies: OpenSSL, libevent, libpcap/libnet (for mirroring), sqlite3 (for user auth).

##### Components
a. Network Entities
- Client Application (e.g., browser, mail client)
- Original Server (e.g., HTTPS server)
- External “Listening” Programs (e.g., UTM services, virus scanner)
- OS Kernel with NAT/Divert subsystem (pf, iptables+TPROXY, ipfw)
b. SSLproxy Process
- CLI & Configuration Parser
- Event Loop & Thread Manager (libevent)
- Packet Redirector (nat abstraction)
- Filter Engine (filter.c, kbtree + aho-corasick)
- Protocol Handlers (proto*.c modules)
- SSL/TLS Handler & Certificate Forge (ssl.c, cert.c)
- Cache Manager (session caching)
- Privilege Separation Module
- Logging Subsystem (connect/content logs, pcap, mirror)
- UserAuth Module (sqlite3 interaction, ARP cache check)
c. External Dependencies (shown as libraries or services)
- OpenSSL
- libevent
- libpcap / libnet (optional)
- sqlite3 (optional)
d. Data Stores and Config
- Configuration File (`sslproxy.conf`) & Command-line options
- SQLite3 Users Database (for UserAuth)
- Log Files / PCAP files

##### Relationships and Data Flows
1. Client → OS NAT Redirect → SSLproxy
2. SSLproxy decrypts (OpenSSL) → Protocol Handler → Filter Engine → Divert
3. Divert → External Program
4. External Program → (unencrypted) SSLproxy → Encrypt → Destination Server
5. Response: reverse order back to Client
- Control-flow interactions:
- SSLproxy ↔ SQLite3 for user authentication
- SSLproxy ↔ file system for config, logs, certificates
- SSLproxy ↔ OS kernel modules via `nat.c` abstraction

##### Architectural Patterns and Principles
- Event-driven, non-blocking I/O (libevent)
- Lock-free core with thread manager (pxythrmgr)
- Modular protocol plug-ins (proto*.c) and filter rules engine
- Privilege separation (privsep) for sensitive operations
- Caching of SSL session data to improve performance
- Separation of concerns: network I/O, SSL processing, divert logic, logging, config parsing

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Network Entities
    ClientApp["Client Application"]:::network
    OSKernel["OS Kernel<br/>NAT/Divert Subsystem"]:::network
    ExternalProg["External Listening Program"]:::network
    Server["Original Server"]:::network

    %% SSLproxy Process Container
    subgraph "SSLproxy Process"
        direction TB
        CLIConfigParser["CLI & Configuration Parser"]:::internal
        MainCoreLoop["Main & Core Loop"]:::internal
        EventLoopThreadMgr["Event Loop &<br/>Thread Manager"]:::internal
        PacketRedirector["Packet Redirector<br/>(NAT Abstraction)"]:::internal
        SSLHandlerCertForge["SSL/TLS Handler &<br/>Certificate Forge"]:::internal
        ProtocolHandlers["Protocol Handlers"]:::internal
        FilterEngine["Filter Engine"]:::internal
        CacheManager["Cache Manager"]:::internal
        PrivSep["Privilege Separation"]:::internal
        LoggingSubsystem["Logging Subsystem"]:::internal
        UserAuthModule["UserAuth Module"]:::internal
    end

    %% External Dependencies
    OpenSSLLib["OpenSSL"]:::external
    LibeventLib["libevent"]:::external
    LibpcapLib["libpcap/libnet"]:::external
    Sqlite3Lib["sqlite3"]:::external

    %% Data Stores & Config
    ConfigFile["Configuration File"]:::datastore
    UsersDB["SQLite3 Users Database"]:::datastore
    LogFiles["Log Files / PCAP Files"]:::datastore

    %% Packet Flow
    ClientApp -->|"encrypted SSL/TLS"| OSKernel
    OSKernel -->|"redirect"| PacketRedirector
    PacketRedirector -->|"decrypt"| SSLHandlerCertForge
    SSLHandlerCertForge -->|"decrypted data"| ProtocolHandlers
    ProtocolHandlers -->|"filtered data"| FilterEngine
    FilterEngine -->|"divert"| ExternalProg
    ExternalProg -->|"processed clear-text"| SSLHandlerCertForge
    SSLHandlerCertForge -->|"encrypt"| PacketRedirector
    PacketRedirector --> OSKernel
    OSKernel --> Server

    %% Control & Data Interactions
    CLIConfigParser --> ConfigFile
    MainCoreLoop --> EventLoopThreadMgr
    EventLoopThreadMgr --> MainCoreLoop
    PacketRedirector --> OSKernel
    UserAuthModule --> Sqlite3Lib
    UserAuthModule --> UsersDB
    LoggingSubsystem --> LogFiles
    LoggingSubsystem --> LibpcapLib

    %% External Libs
    SSLHandlerCertForge --> OpenSSLLib
    EventLoopThreadMgr --> LibeventLib

    %% Click Events
    click CLIConfigParser "https://github.com/sonertari/sslproxy/blob/master/src/opts.c"
    click MainCoreLoop "https://github.com/sonertari/sslproxy/blob/master/src/main.c"
    click MainCoreLoop "https://github.com/sonertari/sslproxy/blob/master/src/proxy.c"
    click EventLoopThreadMgr "https://github.com/sonertari/sslproxy/blob/master/src/pxythr.c"
    click EventLoopThreadMgr "https://github.com/sonertari/sslproxy/blob/master/src/pxythrmgr.c"
    click PacketRedirector "https://github.com/sonertari/sslproxy/blob/master/src/nat.c"
    click FilterEngine "https://github.com/sonertari/sslproxy/blob/master/src/filter.c"
    click ProtocolHandlers "https://github.com/sonertari/sslproxy/blob/master/src/protohttp.c"
    click ProtocolHandlers "https://github.com/sonertari/sslproxy/blob/master/src/protosmtp.c"
    click ProtocolHandlers "https://github.com/sonertari/sslproxy/blob/master/src/protopop3.c"
    click ProtocolHandlers "https://github.com/sonertari/sslproxy/blob/master/src/protopassthrough.c"
    click ProtocolHandlers "https://github.com/sonertari/sslproxy/blob/master/src/protossl.c"
    click ProtocolHandlers "https://github.com/sonertari/sslproxy/blob/master/src/prototcp.c"
    click ProtocolHandlers "https://github.com/sonertari/sslproxy/blob/master/src/protoautossl.c"
    click SSLHandlerCertForge "https://github.com/sonertari/sslproxy/blob/master/src/ssl.c"
    click SSLHandlerCertForge "https://github.com/sonertari/sslproxy/blob/master/src/cert.c"
    click CacheManager "https://github.com/sonertari/sslproxy/blob/master/src/cache.c"
    click PrivSep "https://github.com/sonertari/sslproxy/blob/master/src/privsep.c"
    click LoggingSubsystem "https://github.com/sonertari/sslproxy/blob/master/src/log.c"
    click UserAuthModule "https://github.com/sonertari/sslproxy/blob/master/src/sys.c"
    click ConfigFile "https://github.com/sonertari/sslproxy/blob/master/src/sslproxy.conf"
    click UsersDB "https://github.com/sonertari/sslproxy/blob/master/tests/testproxy/users.db"

    %% Styles
    classDef network fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef internal fill:#FFFACD,stroke:#333,stroke-width:1px
    classDef external fill:#90EE90,stroke:#333,stroke-width:1px
    classDef datastore fill:#D3D3D3,stroke:#333,stroke-width:1px
```
