# anywherelan/awl architecture

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

- Interactive diagram: https://gitdiagram.com/anywherelan/awl
- Repository: https://github.com/anywherelan/awl

## Overview

##### Project Type and Purpose
- This is a Go‑based, peer‑to‑peer mesh VPN application (awl). It combines a headless daemon (`awl`), a desktop system‑tray client (`awl‑tray`), a web‑based admin UI, and a CLI. Its purpose is to form a fully decentralized, encrypted VPN mesh using virtual network interfaces (tun/wintun) and libp2p (QUIC/TCP+TLS, DHT, bootstrap nodes) to connect peers directly or via relays, and optionally route traffic via SOCKS5 exit nodes.

##### File Structure Analysis
- Top‑level “cmd” contains two user‑facing executables:
- `cmd/awl` – headless daemon with embedded web UI and CLI subcommands
- `cmd/awl‑tray` – desktop system‑tray wrapper that embeds `awl` plus tray UI hooks
- “cli” package – implements the terminal‑based client interface used by both executables.
- “api” package – internal HTTP/RPC API layer (settings, peers, metrics).
- “service” package – business logic for tunnels, SOCKS5 service, auth.
- “p2p”, “protocol” – low‑level libp2p integration and wire‑protocol definitions.
- “vpn” & “awldns” – virtual interface management (platform‑specific) and DNS server.
- “socks5” – SOCKS5 proxy implementation.
- “config”, “entity” – configuration loading and domain models.
- “awlevent” – asynchronous event handling (notifications, status changes).
- “update” – self‑update mechanism.
- Test files in each package indicate unit tests for every component.

##### README Insights
- Libraries: tun/wintun driver, libp2p, QUIC/TCP+TLS, DHT, bootstrap nodes.
- Features: peer discovery via DHT, NAT traversal, relay fallback, built‑in DNS, SOCKS5 proxy, system‑tray and web UI.
- Platforms: Windows, Linux, macOS, Android.
- Interaction flows: user invokes CLI or clicks tray → communicates via local API → orchestrates p2p connections → configures virtual interface → routes traffic.

a. Main Components
- Clients: CLI, Tray UI, Web UI (browser)
- API Layer (http/json RPC)
- Core Daemon (application.go orchestrator)
- Services: VPN Engine, DNS Server, SOCKS5 Proxy, Update Manager, Event Dispatcher
- P2P Module (libp2p adapter, DHT, QUIC/TCP+TLS)
- Virtual Network Interface (tun/wintun driver)
- External Services: Bootstrap Nodes, Other Peers
- Operating System Network Stack
b. Relationships & Data Flows
1. User → CLI/Tray → local API → Core Daemon
2. Core → P2P Module → Bootstrap Nodes (initial registration)
3. Core → P2P Module ↔ Other Peers (peer‑to‑peer encrypted tunnels)
4. Core → VPN Engine → Virtual Interface → OS Network Stack
5. Core → DNS Server → resolves `*.awl` names → traffic via virtual interface
6. Core → SOCKS5 Service → forwards application traffic over P2P tunnels
7. Self‑Update: Core → Update Manager → GitHub Releases
c. Architectural Patterns & Principles
- Layered architecture: UI layer, API layer, core orchestration, adapter modules.
- Modular design: each functional area in its own Go package.
- Dependency injection: Core imports interfaces from service, p2p, vpn, socks5.
- Cross‑platform abstractions: vpn/iface_*.go, os_hacks_*.go under embeds.
- Asynchronous events: awlevent package for decoupling core logic and notifications.
d. Key Technologies
- Language: Go modules (`go.mod`)
- Networking: libp2p (QUIC/TCP + TLS), DHT, tun/wintun driver
- Web UI: embedded HTTP server in `api` + static assets from `embeds`
- CLI: urfave/cli (as seen in README examples)
- Testing: extensive `_test.go` files in every package

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% UI Layer
    subgraph "UI Layer"
        CLI["CLI Client"]:::ui
        Tray["System Tray UI"]:::ui
        Browser["Web UI (Browser)"]:::ui
    end

    %% API Layer
    API["API Layer (HTTP/JSON RPC)"]:::api

    %% Core Orchestrator
    Core["Core Orchestrator"]:::core

    %% Modules / Adapters
    subgraph "Modules & Adapters"
        Service["Service Layer"]:::mod
        P2P["P2P Module"]:::mod
        Protocol["Protocol Definitions"]:::mod
        VPN["VPN Engine"]:::mod
        DNS["DNS Server"]:::mod
        SOCKS5["SOCKS5 Proxy"]:::mod
        Update["Self-Update Manager"]:::mod
        Event["Event Dispatcher"]:::mod
    end

    %% External Systems
    subgraph "External Systems"
        Bootstrap["Bootstrap Nodes"]:::external
        Peers["Other Peers"]:::external
        OSNet["OS Network Stack"]:::external
        GitHub["GitHub Releases"]:::external
        Apps["Client Applications"]:::external
    end

    %% Connections
    CLI -->|"HTTP/JSON"| API
    Tray -->|"HTTP/JSON"| API
    Browser -->|"HTTP"| API

    API -->|"RPC Calls"| Core

    Core -->|"invoke"| Service
    Core -->|"invoke"| P2P
    Core -->|"invoke"| Protocol
    Core -->|"invoke"| VPN
    Core -->|"invoke"| DNS
    Core -->|"invoke"| SOCKS5
    Core -->|"invoke"| Update
    Core -->|"emit events to"| Event

    P2P -->|"initial registration libp2p QUIC/TLS"| Bootstrap
    P2P <-->|"encrypted tunnels"| Peers

    VPN -->|"tun packets"| OSNet
    DNS -->|"DNS queries"| VPN

    Apps -->|"SOCKS5 traffic"| SOCKS5
    SOCKS5 -->|"forward over libp2p"| P2P

    Update -->|"check/releases"| GitHub

    %% Click Events
    click CLI "https://github.com/anywherelan/awl/tree/master/cli"
    click Tray "https://github.com/anywherelan/awl/tree/master/cmd/awl-tray"
    click API "https://github.com/anywherelan/awl/tree/master/api"
    click Core "https://github.com/anywherelan/awl/blob/master/application.go"
    click Service "https://github.com/anywherelan/awl/tree/master/service"
    click P2P "https://github.com/anywherelan/awl/tree/master/p2p"
    click Protocol "https://github.com/anywherelan/awl/tree/master/protocol"
    click VPN "https://github.com/anywherelan/awl/tree/master/vpn"
    click DNS "https://github.com/anywherelan/awl/blob/master/awldns/awldns.go"
    click SOCKS5 "https://github.com/anywherelan/awl/tree/master/socks5"
    click Update "https://github.com/anywherelan/awl/blob/master/update/update.go"
    click Event "https://github.com/anywherelan/awl/blob/master/awlevent/awlevent.go"

    %% Styles
    classDef ui fill:#D0E8FF,stroke:#0366d6,color:#0366d6
    classDef api fill:#D5F5E3,stroke:#27AE60,color:#145A32
    classDef core fill:#A9DFBF,stroke:#28B463,color:#196F3D
    classDef mod fill:#FCF3CF,stroke:#F1C40F,color:#B9770E
    classDef external fill:#E5E7E9,stroke:#7F8C8D,color:#566573
```
