torrentclient

package
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2026 License: AGPL-3.0 Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package torrentclient wraps anacrolix/torrent ("torrent via anacrolix/torrent ... rather than reimplementing the BitTorrent wire protocol"). Unlike internal/download's NNTP client, there's no remote server to configure: the torrent engine runs embedded in this process, so "download client" settings here are local (listen port, seed limits, save path) rather than host/port/credentials.

Index

Constants

View Source
const DefaultListenPort = 58264

DefaultListenPort is the port the engine listens on for incoming peer connections (TCP and UDP) unless Settings say otherwise. It sits in the dynamic range (49152-65535) that no service is ever assigned, and echoes the web interface's 8264.

Variables

View Source
var ErrStalled = errors.New("the torrent got no data for 2 hours: no one seems to be sharing it")

ErrStalled is returned by Download for a torrent that stopped getting data.

View Source
var StallAfter = 2 * time.Hour

StallAfter is how long a torrent may go without receiving a single byte before it counts as stalled (no one is sharing it). A variable so tests can shorten it.

Functions

func Download

func Download(ctx context.Context, t *torrent.Torrent, onProgress ProgressFunc) error

Download starts downloading every file in t and blocks until complete, reporting progress as it goes. It does not stop seeding afterward — that's SeedUntilGoal's job, run separately so a caller can return control to the pipeline (import, etc.) as soon as the download itself finishes.

func SeedUntilGoal

func SeedUntilGoal(ctx context.Context, t *torrent.Torrent, startedAt time.Time, goal SeedGoal, interval time.Duration) bool

SeedUntilGoal keeps t seeding until goal is met, checking every interval, and reports true when it was; it returns false as soon as ctx is done or t is removed. It does not stop t: the caller does that (Remove).

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is one running torrent engine. Many torrents share it, each saved in its own folder (AddMagnetIn, AddTorrentFileIn), so the whole app listens on one port however many torrents are active.

func New

func New(cfg Config) (*Client, error)

New starts the embedded torrent engine. When cfg.Tunnel is set, this is a real kill switch, not just "prefer the tunnel": DisableTCP/DisableUTP/ NoDHT/NoDefaultPortForwarding/AcceptPeerConnections are all forced off so the client never binds a real host socket for peer traffic at all — there is no direct-connection path to silently fall back to if the tunnel goes down. The only dialer added is cfg.Tunnel itself, so a dropped tunnel simply fails every new dial rather than reverting to a direct one.

DHT and uTP are casualties of this: both are UDP-based and need a real socket bound to arbitrary remote peers (uTP a shared local PacketConn per socket, DHT its own routable UDP endpoint) rather than anacrolix/torrent's per-connection Dialer interface, which only really suits stream protocols. Tunneling them would mean binding a real host UDP socket — exactly the leak this mode exists to prevent. Peer discovery instead relies on tracker announces and PEX over the tunneled TCP connections.

func (*Client) AddMagnet

func (c *Client) AddMagnet(ctx context.Context, magnetURI string) (*torrent.Torrent, error)

AddMagnet adds a torrent from a magnet URI, saved in Config.DataDir, and blocks until its metadata (piece layout, file list) is available.

func (*Client) AddMagnetIn

func (c *Client) AddMagnetIn(ctx context.Context, magnetURI, dir string) (*torrent.Torrent, error)

AddMagnetIn is AddMagnet saving the torrent's files in dir.

func (*Client) AddTorrentFile

func (c *Client) AddTorrentFile(ctx context.Context, path string) (*torrent.Torrent, error)

AddTorrentFile adds a torrent from a local .torrent file, saved in Config.DataDir.

func (*Client) AddTorrentFileIn

func (c *Client) AddTorrentFileIn(ctx context.Context, path, dir string) (*torrent.Torrent, error)

AddTorrentFileIn is AddTorrentFile saving the torrent's files in dir.

func (*Client) Close

func (c *Client) Close()

Close stops the engine and every torrent in it.

func (*Client) LastIncoming

func (c *Client) LastIncoming() time.Time

LastIncoming is when another peer last connected to this engine on its own initiative (zero if never): proof the listen port can be reached from the internet.

func (*Client) ListenAddrs

func (c *Client) ListenAddrs() []net.Addr

ListenAddrs reports the client's real host listen addresses, if any — empty when the client is running in tunnel-only mode (see New).

func (*Client) ListenPort

func (c *Client) ListenPort() int

ListenPort is the port actually listened on (0 when nothing is, as in tunnel-only mode).

func (*Client) Remove

func (c *Client) Remove(t *torrent.Torrent)

Remove stops t (downloading and seeding) and releases its files, which stay on disk.

func (*Client) Torrents

func (c *Client) Torrents() int

Torrents is how many torrents the engine is running.

type Config

type Config struct {
	// DataDir is where torrents added without a folder of their own
	// (AddMagnet, AddTorrentFile) are saved.
	DataDir    string
	ListenPort int // 0 = let the OS pick

	// Tunnel, when set, routes every peer connection through it exclusively
	// instead of the host network. See New's
	// doc comment for exactly what that does and doesn't cover.
	Tunnel TunnelDialer
}

Config are the local engine settings ("seed ratio/time limits, category-based save paths").

type ProgressFunc

type ProgressFunc func(bytesDone, bytesTotal int64)

ProgressFunc mirrors internal/download's callback shape so the API pipeline can treat both protocols uniformly.

type SeedGoal

type SeedGoal struct {
	Ratio float64
	Time  time.Duration
}

SeedGoal is when seeding a finished torrent may stop: once it has uploaded Ratio times its size, or after Time of seeding, whichever comes first. Zero values mean no limit of that kind; with neither set a torrent seeds until it is stopped.

func (SeedGoal) Met

func (g SeedGoal) Met(uploaded, size int64, seeding time.Duration) bool

Met reports whether a torrent of size bytes that has uploaded uploaded bytes over seeding time has reached the goal.

func (SeedGoal) Unlimited

func (g SeedGoal) Unlimited() bool

Unlimited reports whether the goal can never be met.

type TunnelDialer

type TunnelDialer interface {
	DialContext(ctx context.Context, network, addr string) (net.Conn, error)
}

TunnelDialer is satisfied by *vpn.Tunnel. Declared narrowly here rather than importing internal/vpn, per the project's one-way package dependency rule (feature packages shouldn't import sideways into each other) — api is the only package that needs to know about both and wires them together.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL