anacrolix/torrent

repository·master·Indexed 27 days ago

https://github.com/anacrolix/torrent

A Go implementation of BitTorrent-related packages and command-line utilities designed primarily as a library. It supports protocol encryption, DHT, PEX, uTP, WebTorrent, and BitTorrent v2, with an emphasis on streaming data via idiomatic Go io interfaces. The project includes a bencode encoding/decoding package and torrentfs, which allows exposing a torrent client as a read-only FUSE filesystem.

Tokens
19.3K
Snippets
29
Records
182
Agent score
91%

What's inside anacrolix/torrent

  1. Use torrentfs as a read-only filesystem

    master

    The torrentfs package provides a way to expose a torrent client as a read-only filesystem. The core logic is FUSE-library-agnostic, providing the fundamental types and helpers. To actually mount the filesystem, you must use a concrete FUSE backend implementation.

    Available backends:

    • anacrolix/og-torrentfs: Uses the anacrolix/fuse library. Supports macFUSE and fuse-t on macOS, and fusermount on Linux.
    • anacrolix/hanwen-torrentfs: Uses the hanwen/go-fuse/v2 library. Uses the macFUSE socket protocol on macOS (note: this is incompatible with fuse-t).
  2. Install the torrent library or CLI tools

    master

    You can install the BitTorrent library package for use in your Go projects, or install the provided command-line utilities.

    To install the library:

    go get github.com/anacrolix/torrent

    To install the command-line tools (cmds):

    go install github.com/anacrolix/torrent/cmd/...@latest

    Note: Installing via import path uses Go's module-aware mode and ignores the repository's go.work file, meaning it works without needing to check out git submodules.

    go get github.com/anacrolix/torrent
  3. Build from a repository checkout

    master

    The repository uses a Go workspace (go.work) that includes the possum storage backend as a git submodule. If you are building from a local clone (e.g., using go build ./...), you must ensure submodules are initialized, otherwise the build will fail because it cannot find the possum module.

    To clone the repository with all necessary submodules:

    git clone --recurse-submodules https://github.com/anacrolix/torrent

    To update submodules in an existing clone:

    git submodule update --init --recursive

    Alternatively, you can disable the workspace by setting GOWORK=off, but the possum storage backend will not be available.

    git clone --recurse-submodules https://github.com/anacrolix/torrent
  4. Use the bencode encoding/decoding package

    master
    The bencode package provides encoding and decoding functionality with an API design similar to Go's standard encoding/json package. For concrete implementation details and code patterns, refer to the official package examples in the Go documentation.
  5. Specify torrent sources for download

    master

    The torrent positional argument accepts several formats to identify the torrent to be downloaded:

    1. Magnet URIs: Prefixed with magnet:
    2. HTTP/HTTPS URLs: Direct links to .torrent files (e.g., http://example.com/file.torrent)
    3. Infohashes: Prefixed with infohash: followed by the hex string
    4. Local Files: A path to a .torrent file on the filesystem
  6. Use the WebSeed Client to request torrent data

    master

    The webseed.Client allows you to fetch data from a webserver (webseed) to satisfy torrent piece requirements. You initialize a client with an http.Client and a base Url, then use StartNewRequest to obtain a Request object that provides an io.Reader for the requested data.

    To use the client:

    1. Create a webseed.Client.
    2. Call SetInfo with the torrent's metainfo.Info and a segments.Index to map files to URLs.
    3. Call StartNewRequest with a segments.Extent (RequestSpec) to begin downloading a specific range of data.
    4. Read from the returned Request.Body.
    5. Call Request.Close() or Request.Cancel() when finished.
  7. Configure Local Service Discovery (LPD)

    master

    To enable BEP-14 Local Service Discovery, provide a LocalServiceDiscoveryConfig to the LocalServiceDiscovery field in ClientConfig:

    FieldDescription
    IfiThe network interface on which to multicast announce messages.
    Ip6Whether to use IPv6 for discovery.
  8. Configure Rate Limiting for Upload and Download

    master

    You can control the bandwidth usage of the client using golang.org/x/time/rate limiters within ClientConfig:

    • UploadRateLimiter: Limits bytes sent to peers. If the burst is set to 0, the implementation automatically sets it to MaxAllocPeerRequestDataPerConn to ensure it can fit a full chunk.
    • DownloadRateLimiter: Limits bytes read from peer connections. If nil, no rate limiting is applied.

    To check the effective download limit, use EffectiveDownloadRateLimit(l *rate.Limiter), which returns rate.Inf if the limiter is nil.