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
- Variables
- func Download(ctx context.Context, t *torrent.Torrent, onProgress ProgressFunc) error
- func SeedUntilGoal(ctx context.Context, t *torrent.Torrent, startedAt time.Time, goal SeedGoal, ...) bool
- type Client
- func (c *Client) AddMagnet(ctx context.Context, magnetURI string) (*torrent.Torrent, error)
- func (c *Client) AddMagnetIn(ctx context.Context, magnetURI, dir string) (*torrent.Torrent, error)
- func (c *Client) AddTorrentFile(ctx context.Context, path string) (*torrent.Torrent, error)
- func (c *Client) AddTorrentFileIn(ctx context.Context, path, dir string) (*torrent.Torrent, error)
- func (c *Client) Close()
- func (c *Client) LastIncoming() time.Time
- func (c *Client) ListenAddrs() []net.Addr
- func (c *Client) ListenPort() int
- func (c *Client) Remove(t *torrent.Torrent)
- func (c *Client) Torrents() int
- type Config
- type ProgressFunc
- type SeedGoal
- type TunnelDialer
Constants ¶
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 ¶
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.
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 ¶
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 ¶
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 ¶
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 ¶
AddMagnetIn is AddMagnet saving the torrent's files in dir.
func (*Client) AddTorrentFile ¶
AddTorrentFile adds a torrent from a local .torrent file, saved in Config.DataDir.
func (*Client) AddTorrentFileIn ¶
AddTorrentFileIn is AddTorrentFile saving the torrent's files in dir.
func (*Client) LastIncoming ¶
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 ¶
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 ¶
ListenPort is the port actually listened on (0 when nothing is, as in tunnel-only mode).
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 ¶
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.
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.