torrent

package module
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: MPL-2.0 Imports: 33 Imported by: ? 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.

README

torrent

Go Reference Build

An implementation of the BitTorrent protocol: http://www.bittorrent.org/beps/bep_0003.html

Implemented:

  • BEP-3 The BitTorrent Protocol Specification
  • BEP-23 Tracker Returns Compact Peer Lists
  • BEP-27 Private Torrents

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConcurrentDownloads

func ConcurrentDownloads(concurrent int) func(*Client) error

ConcurrentDownloads sets the number of peers to actively download from at the same time. Peers beyond this many are still connected to, and still uploaded to, but are not asked for pieces until one of the peers downloading finishes or gives up its piece. Default is 4.

What the cap governs is how many peers hold a fresh claim on a piece. In the endgame, once every piece still missing has already been claimed, further peers may take a duplicate of a claim regardless of the cap — the peers holding it are the ones being backed up — with at most maxPieceOwners peers fetching any one piece.

func DownloadCap

func DownloadCap(bytesPerSecond int) func(*Client) error

DownloadCap sets the maximum download speed of the client, subject to the dispatcher's overall limit. Must be at least dispatcher.MinimumRateCap. Default is no limit.

func NotifyWhenDownloadComplete

func NotifyWhenDownloadComplete(notifier chan *Client) func(*Client) error

NotifyWhenDownloadComplete sets a channel to notify when the download is verified and completes.

func NotifyWhenStopped

func NotifyWhenStopped(notifier chan *Client) func(*Client) error

NotifyWhenStopped sets a channel to notify when the client is stopped.

func PeersWanted

func PeersWanted(wanted int) func(*Client) error

PeersWanted sets the number of peers to ask the tracker for. Default is 32.

func SeedDuration

func SeedDuration(duration time.Duration) func(*Client) error

SeedDuration sets the maximum amount of time to seed. Default is 4 days.

func SetTrackerUserAgent added in v1.7.0

func SetTrackerUserAgent(agent string)

SetTrackerUserAgent sets the user agent header sent with tracker announces. An empty string, which is the default, sends none. It is safe to call at any time, including while clients are announcing.

func TrackerUserAgent

func TrackerUserAgent() string

TrackerUserAgent returns the user agent header sent with tracker announces. An empty string means none is sent.

func UploadCap

func UploadCap(bytesPerSecond int) func(*Client) error

UploadCap sets the maximum upload speed of the client, subject to the dispatcher's overall limit. Must be at least dispatcher.MinimumRateCap. Default is no limit.

Types

type Client

type Client struct {
	InRate  rate.Limiter
	OutRate rate.Limiter
	// contains filtered or unexported fields
}

Client provides the ability to download and/or seed a torrent.

func NewClient

func NewClient(d *dispatcher.Dispatcher, torrentFile *tfs.File, options ...func(*Client) error) (*Client, error)

NewClient creates and starts a new client for a torrent.

func (*Client) ExternalIP added in v1.0.3

func (c *Client) ExternalIP() net.IP

ExternalIP returns our external IP address.

func (*Client) HandleConnection

func (c *Client) HandleConnection(conn net.Conn, logger *slog.Logger, _ dispatcher.ProtocolExtensions,
	infoHash tfs.InfoHash, sendHandshake bool, handshakeDone func(),
)

HandleConnection is called by the dispatcher for new connections. handshakeDone, when it isn't nil, is the dispatcher's notification that the pending handshake slot it took for this connection may be released, and is called as soon as the handshake this finishes is over.

func (*Client) Logger

func (c *Client) Logger() *slog.Logger

Logger returns the client's logger.

func (*Client) Status

func (c *Client) Status() *Status

Status returns the current status of the torrent.

func (*Client) Stop

func (c *Client) Stop(timeout time.Duration)

Stop the torrent. Does not return until the torrent has stopped or the timeout has been hit.

func (*Client) TorrentFile

func (c *Client) TorrentFile() *tfs.File

TorrentFile returns the client's torrent file.

type State

type State int

State holds the enumeration of possible states.

const (
	Initializing State = iota
	Downloading
	Seeding
	Stopping
	Done
	Errored
)

Possible states.

type Status

type Status struct {
	SeedingStopsAt         time.Time
	State                  State
	PercentComplete        float64
	TotalBytes             int64
	RemainingBytes         int64
	UploadBytesPerSecond   int
	DownloadBytesPerSecond int
	// PeersDownloading counts the distinct peers we are currently downloading a piece from, not the pieces being
	// downloaded: a peer answers for itself once however many pieces it has been asked for.
	PeersDownloading int
	PeersConnected   int
	Leechers         int
	Seeders          int
}

Status holds the status information for a torrent.

func (*Status) String

func (s *Status) String() string

Directories

Path Synopsis
cmd
torrent command
container

Jump to

Keyboard shortcuts

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