This application is a web server that acts as a torrent client, providing an HTTP API for streaming, file listing, metadata retrieval, and status checking of torrents.
thanks to https://github.com/anacrolix/torrent
- Torrent Streaming: Stream video/audio content directly from torrents without fully downloading them.
- File Listing: Get a list of all files within a torrent.
- Metadata Retrieval: Fetch torrent metadata like name, info hash, total size, and file count.
- Torrent Status: Monitor the download progress, speed, and connected peers for active torrents.
- In-Memory Caching (LRU): Recently accessed torrents are kept in a fast in-memory cache.
- Persistent Metadata Storage (LotusDB): Torrent metadata is persisted to disk using LotusDB, reducing the need to re-fetch info for known torrents.
- Automatic Cleanup: Inactive torrents are automatically dropped from memory and cache after a configurable duration.
- Restart Capability: The server can be gracefully restarted via an API endpoint.
- CORS Enabled: All API endpoints support Cross-Origin Resource Sharing.
All API endpoints require a url query parameter, which should be a magnet link.
- Endpoint:
/stream - Method:
GET - Description: Streams the largest file (or a specific file by index) from a given torrent. Supports
Rangerequests for seeking. - Query Parameters:
url(required): The magnet link of the torrent.index(optional): The 0-based index of the file to stream. If omitted, the largest file in the torrent will be streamed.
- Response Headers:
Content-Type: Determined by file extension (e.g.,video/mp4,video/x-matroska,application/octet-stream).Content-Disposition:inline; filename="..."X-Filename: Original filename.X-Filesize: Size of the file in bytes.X-Content-Type: Content type of the file.
- Example Usage (Browser/VLC):
http://localhost:3000/stream?url=magnet:?xt=urn:btih:YOUR_INFO_HASH&dn=YourTorrentName
- Endpoint:
/files - Method:
GET - Description: Retrieves a list of all files within a torrent, including their paths and sizes.
- Query Parameters:
url(required): The magnet link of the torrent.
- Response (JSON):
{ "InfoHash": "string", "Files": [ { "Path": "string", "Size": 0, "SizeHuman": "string" } ] } - Example Usage (cURL):
curl "http://localhost:3000/files?url=magnet:?xt=urn:btih:YOUR_INFO_HASH&dn=YourTorrentName"
- Endpoint:
/metadata - Method:
GET - Description: Fetches essential metadata about a torrent.
- Query Parameters:
url(required): The magnet link of the torrent.
- Response (JSON):
{ "Name": "string", "InfoHash": "string", "TotalSize": 0, "TotalSizeHuman": "string", "FileCount": 0 } - Example Usage (cURL):
curl "http://localhost:3000/metadata?url=magnet:?xt=urn:btih:YOUR_INFO_HASH&dn=YourTorrentName"
- Endpoint:
/status - Method:
GET - Description: Provides real-time status updates for an active torrent, including download progress, speed, and connected peers.
- Query Parameters:
url(required): The magnet link of the torrent.
- Response (JSON):
{ "InfoHash": "string", "Name": "string", "TotalBytes": 0, "BytesCompleted": 0, "PercentageCompleted": 0.0, "DownloadSpeedBps": 0.0, "DownloadSpeedHuman": "string", "ConnectedPeers": 0, "Files": [ { "Path": "string", "Size": 0, "BytesCompleted": 0, "PercentageCompleted": 0.0 } ] } - Example Usage (cURL):
curl "http://localhost:3000/status?url=magnet:?xt=urn:btih:YOUR_INFO_HASH&dn=YourTorrentName"
- Endpoint:
/restart - Method:
GET - Description: Triggers a graceful restart of the server. Useful for applying configuration changes or recovering from issues.
- Query Parameters: None.
- Response:
The server has been restarted.(HTTP 200 OK) - Example Usage (cURL):
curl "http://localhost:3000/restart"
Run the application with the following command-line flags:
-port <number>:- Description: Port to listen on.
- Default:
3000
-download-dir <path>:- Description: Directory to save downloaded files.
- Default:
/downloads
-cleanup-inactive-after <duration>:- Description: Duration after which to clean up inactive torrents (e.g.,
30m,2h). Set to0to disable. - Default:
30m
- Description: Duration after which to clean up inactive torrents (e.g.,
Example:
./rsd9 -port 8080 -download-dir /mnt/torrent_data -cleanup-inactive-after 1hTo build the application, navigate to the project root and run:
go build -o rsd9Then, run the executable:
./rsd9Or with custom options:
./rsd9 -port 8080 -download-dir /mnt/torrent_data