-
Notifications
You must be signed in to change notification settings - Fork 10
Architecture
MangoFetch is engineered as a modern, modular download ecosystem written in Rust. It utilizes a multi-crate workspace to decouple core downloading logic from the terminal and graphical interfaces.
The codebase is organized into four core crates:
-
mangofetch-core: The central download engine.- UI-agnostic and fully asynchronous (built on
tokio). - Contains the Download Queue Manager (
queue.rs) which schedules active downloads, manages execution concurrency slots, and saves recovery states torecovery.jsonfor crash safety. - Implements native high-performance engines:
- Direct Downloader: Multi-segment parallel HTTP downloader with zero-copy buffer pools.
-
HLS Downloader: Custom M3U8 parser supporting AES-128 segment decryption (using
aesandcbcblock ciphers) and parallel chunk fetching. -
BitTorrent Engine: Integrated via
librqbitfor magnet links and torrent files. - P2P Transfer Engine: A secure TCP node-to-node protocol utilizing multi-threaded streaming.
- Handles external subprocess orchestration for FFmpeg and yt-dlp.
- Includes the Dependency Engine which auto-checks, updates, and cryptographically verifies external binaries via SHA256 checksums (
tool_hashes.json). - Houses the Plugin Manager that dynamically loads custom plugins at runtime.
- UI-agnostic and fully asynchronous (built on
-
mangofetch-plugin-sdk: The development SDK.- Defines the standard Application Binary Interface (ABI) and traits for writing dynamic plugins.
- Exposes serialization models for plugin manifests, events, and configuration settings.
-
mangofetch-cli: The command-line entrypoint.- Implements CLI commands and option parsing via
clap. - Implements the interactive Terminal User Interface (TUI) powered by
ratatuiwith support for custom color themes, scrolling, log streaming, and mouse interaction.
- Implements CLI commands and option parsing via
-
mangofetch-gui: The desktop graphical application.- Built on
eguiandeframe(v0.31) for a lightweight, cross-platform visual interface that communicates natively withmangofetch-core.
- Built on
When a URL or file resource is queued, it goes through the following lifecycle:
graph TD
A["Resource URL / Magnet / P2P Code"] --> B["PlatformRegistry::from_url()"]
B --> C{Platform Detected?}
C -->|Native Extractor| D["Extract Metadata (Rust native)"]
C -->|Generic / fallback| E["Invoke yt-dlp Metadata Extraction"]
C -->|P2P / Magnet| F["Connect Peer / Swarm"]
D & E & F --> G["DownloadQueue::enqueue()"]
G --> H["Pending State (in recovery.json)"]
H -->|Queue slot opens| I["Active State (Downloading)"]
I -->|HTTP / HLS / P2P / Torrent| J["Download Chunks & Decrypt (if HLS)"]
J --> K["Subprocess Post-Processing (FFmpeg Merge)"]
K --> L["Final Checksum & Verify"]
L --> M["Completed State"]
-
Resolution & Classification:
-
Platform::from_urldetermines the handling engine (e.g. native parsers, BitTorrent, P2P, oryt-dlpfallback).
-
-
Metadata Extraction:
- The engine retrieves stream URLs, titles, and resolutions without pulling media payloads.
-
Task Queueing:
- The task enters the
DownloadQueuewith states:Pending,Downloading,Paused,Completed, orFailed. - The session state is serialized to
recovery.jsonafter every state change.
- The task enters the
-
Segment Fetching & I/O:
- HTTP segments are fetched concurrently and written to disk.
- For HLS streams, the native engine handles decryption of segments using dynamic keys.
-
Post-Processing:
- If audio and video are separate streams (e.g. VP9 + Opus),
ffmpegis launched with hardware acceleration overrides (automatically detecting CUDA, VAAPI, or VideoToolbox) to mux streams without re-encoding.
- If audio and video are separate streams (e.g. VP9 + Opus),
-
Persistence & Finalization:
- Completed files are placed in the output directory and session logs are finalized.
Unlike basic downloaders that spawn Python wrapper processes, MangoFetch has a native HLS engine in Rust. It fetches the .m3u8 playlist, resolves relative segment URIs, decrypts segments on-the-fly using cbc-aes, and merges them with minimal overhead.
Subprocess invocations to external binaries like yt-dlp or ffmpeg sanitize arguments strictly, preventing shell-injection vectors. Path lengths and filenames are normalized using the sanitize-filename crate.
The plugin engine loads compiled shared libraries (.so, .dll, .dylib) at runtime using libloading. An ABI version check ensures compatibility before any function pointers are invoked.