# shx-dow/lantern architecture

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

- Interactive diagram: https://gitdiagram.com/shx-dow/lantern
- Repository: https://github.com/shx-dow/lantern

## Overview

1. **Overall shape**
   Lantern is a **decentralized peer-to-peer file sharing app**. There is **no central backend**: each machine runs the same package, advertises itself on the LAN, serves files over TCP, and can act as a client to other peers.

2. **Entry points and mode selection**
   The package is launched through the `lantern` app entrypoint, with `main.py` and `peer.py` acting as the startup surface. The app supports two front-ends over the same backend: **default TUI mode** and **CLI mode** via `--cli`.

3. **User interface layer**
   `tui.py` implements the Textual-based terminal dashboard, including theme toggling, progress feedback, and action shortcuts. This layer is presentation-only: it calls into the same discovery and transfer logic used by the CLI.

4. **Command-line interaction**
   `peer.py` provides the text-command interface for operations like listing peers, listing remote files, downloading, uploading, and showing local shared files. This is the simplest path to the backend and is useful for non-interactive environments.

5. **Discovery subsystem**
   `discovery.py` handles **LAN peer discovery over UDP broadcast**. Its job is to find other Lantern instances on the local network and publish the local peer’s presence. The discovery layer feeds the current peer list to both UI modes.

6. **Transfer subsystem**
   `server.py` and `client.py` split the file-sharing responsibilities:
   - `server.py` listens for inbound TCP connections and serves file listings/transfers.
   - `client.py` initiates outbound TCP connections to other peers to request lists, upload files, or download files.
   This is the core data path for actual file movement.

7. **Wire protocol boundary**
   `protocol.py` defines the message framing and transfer protocol used by both client and server. It is the compatibility contract between peers, keeping the transport logic separate from UI and discovery.

8. **Filesystem and safety boundary**
   The shared content lives in a local directory under `~/Downloads/Lantern/` by default. That directory is the main filesystem boundary for the app, and the README notes **path safety protections** against traversal attacks so peers cannot escape the intended share root.

9. **Configuration and constants**
   `config.py` centralizes runtime settings such as ports, default share paths, and other constants used across discovery, server, client, and UI. This keeps network and filesystem assumptions consistent across modules.

10. **Runtime dependencies that shape the architecture**
    The project is Python 3.10+ and depends on **Textual** for the TUI and **psutil** for system/process-related functionality. These are not standalone services, but they materially define the interactive terminal experience and runtime requirements.

11. **Packaging, distribution, and maintenance**
    `pyproject.toml`, `requirements.txt`, and `.github/workflows/publish.yml` show that the project is packaged for installation and published through CI. The `styles/lantern.css` file is a presentation asset for the TUI, not part of the networking core.

12. **Testing surface**
    The tests focus on `protocol.py` and `client.py`, which is consistent with the architecture: the most critical correctness boundaries are the on-the-wire message format and the outbound peer interaction logic.

Main entry points: [`lantern/client.py`](https://github.com/shx-dow/lantern/blob/main/lantern/client.py) (Client), [`lantern/tui.py`](https://github.com/shx-dow/lantern/blob/main/lantern/tui.py) (TUI), [`lantern/server.py`](https://github.com/shx-dow/lantern/blob/main/lantern/server.py) (Server), [`lantern/peer.py`](https://github.com/shx-dow/lantern/blob/main/lantern/peer.py) (Peer CLI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_entry["Entrypoints"]
  node_node_main["Main<br/>entrypoint<br/>[main.py]"]
  node_node_peer["Peer CLI<br/>entrypoint<br/>[peer.py]"]
end

subgraph group_group_ui["User Interface"]
  node_node_tui["TUI<br/>frontend<br/>[tui.py]"]
end

subgraph group_group_discovery["Discovery"]
  node_node_discovery(("Discovery<br/>udp broadcast<br/>[discovery.py]"))
end

subgraph group_group_transfer["Transfer Stack"]
  node_node_server[("Server<br/>[server.py]")]
  node_node_client[("Client<br/>[client.py]")]
  node_node_protocol{{"Protocol<br/>[protocol.py]"}}
end

subgraph group_group_fs["Filesystem Boundary"]
  node_node_share["Share Root<br/>filesystem root"]
  node_node_safety{{"Path Safety<br/>security guard"}}
end

subgraph group_group_support["Support"]
  node_node_config["Config<br/>settings<br/>[config.py]"]
  node_node_updater["Updater<br/>maintenance<br/>[updater.py]"]
  node_node_styles["Styles<br/>ui asset<br/>[lantern.css]"]
  node_node_pyproject["Pyproject<br/>packaging<br/>[pyproject.toml]"]
  node_node_requirements["Requirements<br/>dependencies<br/>[requirements.txt]"]
  node_node_workflow["Publish CI<br/>ci pipeline<br/>[publish.yml]"]
end

subgraph group_group_tests["Tests"]
  node_node_test_client["Client Tests<br/>[test_client.py]"]
  node_node_test_protocol["Protocol Tests<br/>[test_protocol.py]"]
end

node_node_main -->|"default mode"| node_node_tui
node_node_main -->|"--cli"| node_node_peer
node_node_tui -->|"peers"| node_node_discovery
node_node_tui -->|"actions"| node_node_client
node_node_peer -->|"peers"| node_node_discovery
node_node_peer -->|"commands"| node_node_client
node_node_discovery -->|"settings"| node_node_config
node_node_server -->|"speaks"| node_node_protocol
node_node_client -->|"speaks"| node_node_protocol
node_node_server -->|"serves"| node_node_share
node_node_client -->|"reads/writes"| node_node_share
node_node_client -->|"checks"| node_node_safety
node_node_server -->|"checks"| node_node_safety
node_node_server -->|"settings"| node_node_config
node_node_client -->|"settings"| node_node_config
node_node_tui -->|"theme"| node_node_styles
node_node_tui -->|"update"| node_node_updater
node_node_pyproject -.->|"deps"| node_node_requirements
node_node_workflow -.->|"publish"| node_node_pyproject
node_node_test_client -.->|"covers"| node_node_client
node_node_test_protocol -.->|"covers"| node_node_protocol

click node_node_main "https://github.com/shx-dow/lantern/blob/main/lantern/main.py"
click node_node_peer "https://github.com/shx-dow/lantern/blob/main/lantern/peer.py"
click node_node_tui "https://github.com/shx-dow/lantern/blob/main/lantern/tui.py"
click node_node_discovery "https://github.com/shx-dow/lantern/blob/main/lantern/discovery.py"
click node_node_server "https://github.com/shx-dow/lantern/blob/main/lantern/server.py"
click node_node_client "https://github.com/shx-dow/lantern/blob/main/lantern/client.py"
click node_node_protocol "https://github.com/shx-dow/lantern/blob/main/lantern/protocol.py"
click node_node_config "https://github.com/shx-dow/lantern/blob/main/lantern/config.py"
click node_node_updater "https://github.com/shx-dow/lantern/blob/main/lantern/updater.py"
click node_node_styles "https://github.com/shx-dow/lantern/blob/main/lantern/styles/lantern.css"
click node_node_pyproject "https://github.com/shx-dow/lantern/blob/main/pyproject.toml"
click node_node_requirements "https://github.com/shx-dow/lantern/blob/main/requirements.txt"
click node_node_workflow "https://github.com/shx-dow/lantern/blob/main/.github/workflows/publish.yml"
click node_node_test_client "https://github.com/shx-dow/lantern/blob/main/tests/test_client.py"
click node_node_test_protocol "https://github.com/shx-dow/lantern/blob/main/tests/test_protocol.py"

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_node_main,node_node_peer,node_node_test_client,node_node_test_protocol toneBlue
class node_node_tui toneAmber
class node_node_discovery toneMint
class node_node_server,node_node_client,node_node_protocol toneRose
class node_node_share,node_node_safety toneIndigo
class node_node_config,node_node_updater,node_node_styles,node_node_pyproject,node_node_requirements,node_node_workflow toneTeal
```

## Components

### Entrypoints

Launch surfaces and mode selection for the app.

- [Main](https://github.com/shx-dow/lantern/blob/main/lantern/main.py): entrypoint · App startup and mode selection. · `lantern/main.py`
- [Peer CLI](https://github.com/shx-dow/lantern/blob/main/lantern/peer.py): entrypoint · Command-line peer interaction surface. · `lantern/peer.py`

### User Interface

Terminal front-ends for interacting with peers and transfers.

- [TUI](https://github.com/shx-dow/lantern/blob/main/lantern/tui.py): frontend · Textual-based terminal dashboard. · `lantern/tui.py`

### Discovery

LAN peer advertisement and peer list maintenance.

- [Discovery](https://github.com/shx-dow/lantern/blob/main/lantern/discovery.py): udp broadcast · Finds peers and advertises presence on the LAN. · `lantern/discovery.py`

### Transfer Stack

TCP file listing, upload, and download path between peers.

- [Server](https://github.com/shx-dow/lantern/blob/main/lantern/server.py): tcp server · Accepts inbound connections and serves listings/transfers. · `lantern/server.py`
- [Client](https://github.com/shx-dow/lantern/blob/main/lantern/client.py): tcp client · Connects to peers for lists, uploads, and downloads. · `lantern/client.py`
- [Protocol](https://github.com/shx-dow/lantern/blob/main/lantern/protocol.py): wire protocol · Message framing and transfer contract between peers. · `lantern/protocol.py`

### Filesystem Boundary

Local share root and path safety boundary for file access.

- **Share Root**: filesystem root · Default local shared directory and access boundary.
- **Path Safety**: security guard · Prevents traversal outside the intended share root.

### Support

Runtime settings, updates, packaging, and published assets.

- [Config](https://github.com/shx-dow/lantern/blob/main/lantern/config.py): settings · Ports, defaults, and shared runtime constants. · `lantern/config.py`
- [Updater](https://github.com/shx-dow/lantern/blob/main/lantern/updater.py): maintenance · Update-related runtime support. · `lantern/updater.py`
- [Styles](https://github.com/shx-dow/lantern/blob/main/lantern/styles/lantern.css): ui asset · CSS styling for the terminal interface. · `lantern/styles/lantern.css`
- [Pyproject](https://github.com/shx-dow/lantern/blob/main/pyproject.toml): packaging · Package metadata and build configuration. · `pyproject.toml`
- [Requirements](https://github.com/shx-dow/lantern/blob/main/requirements.txt): dependencies · Pinned runtime dependencies. · `requirements.txt`
- [Publish CI](https://github.com/shx-dow/lantern/blob/main/.github/workflows/publish.yml): ci pipeline · Automated publish workflow. · `.github/workflows/publish.yml`

### Tests

Focused correctness checks around protocol and client behavior.

- [Client Tests](https://github.com/shx-dow/lantern/blob/main/tests/test_client.py): test · Client interaction coverage. · `tests/test_client.py`
- [Protocol Tests](https://github.com/shx-dow/lantern/blob/main/tests/test_protocol.py): test · Wire-format coverage. · `tests/test_protocol.py`

## Connections

- Main → TUI: default mode · Launches the terminal dashboard by default.
- Main → Peer CLI: --cli · Switches to command-line interaction mode.
- TUI → Discovery: peers · Consumes discovered peers for display and actions.
- TUI → Client: actions · Triggers remote list, upload, and download operations.
- Peer CLI → Discovery: peers · Uses peer discovery for listing and selection.
- Peer CLI → Client: commands · Invokes outbound peer operations.
- Discovery → Config: settings · Uses configured broadcast ports and runtime parameters.
- Server → Protocol: speaks · Frames inbound messages using the shared protocol.
- Client → Protocol: speaks · Frames outbound requests using the shared protocol.
- Server → Share Root: serves · Reads local shared files for remote peers.
- Client → Share Root: reads/writes · Downloads to and uploads from the local share boundary.
- Client → Path Safety: checks · Enforces path constraints before file access.
- Server → Path Safety: checks · Protects file serving against traversal outside the share root.
- Server → Config: settings · Uses configured TCP ports and defaults.
- Client → Config: settings · Uses configured peer connection settings.
- TUI → Styles: theme · Loads presentation styling for the dashboard.
- TUI → Updater: update · Hooks into update-related runtime support.
- Pyproject → Requirements: deps · Packaging and dependency definitions support the runtime.
- Publish CI → Pyproject: publish · CI publishes the packaged project.
- Client Tests → Client: covers · Exercises outbound peer interactions.
- Protocol Tests → Protocol: covers · Exercises the wire-format contract.
