How shx-dow/lantern works
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: client.py Client · tui.py TUI · server.py Server · peer.py Peer CLI
Components 17
Entrypoints
Launch surfaces and mode selection for the app.
User Interface
Terminal front-ends for interacting with peers and transfers.
- TUI · Textual-based terminal dashboard.
lantern/tui.py
Discovery
LAN peer advertisement and peer list maintenance.
- Discovery · Finds peers and advertises presence on the LAN.
lantern/discovery.py
Transfer Stack
TCP file listing, upload, and download path between peers.
Filesystem Boundary
Local share root and path safety boundary for file access.
- Share Root · Default local shared directory and access boundary.
- Path Safety · Prevents traversal outside the intended share root.
Support
Runtime settings, updates, packaging, and published assets.
- Config · Ports, defaults, and shared runtime constants.
lantern/config.py - Updater · Update-related runtime support.
lantern/updater.py - Styles · CSS styling for the terminal interface.
lantern/styles/lantern.css - Pyproject · Package metadata and build configuration.
pyproject.toml - Requirements · Pinned runtime dependencies.
requirements.txt - Publish CI · Automated publish workflow.
.github/workflows/publish.yml
Tests
Focused correctness checks around protocol and client behavior.
- Client Tests · Client interaction coverage.
tests/test_client.py - Protocol Tests · Wire-format coverage.
tests/test_protocol.py
Connections 21
- 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.