torrent

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2024 License: MIT Imports: 81 Imported by: 11

Documentation

Overview

Package torrent provides a BitTorrent client implementation.

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = Config{

	Database:                               "~/rain/session.db",
	DataDir:                                "~/rain/data",
	DataDirIncludesTorrentID:               true,
	Host:                                   "0.0.0.0",
	PortBegin:                              20000,
	PortEnd:                                30000,
	MaxOpenFiles:                           10240,
	PEXEnabled:                             true,
	ResumeWriteInterval:                    30 * time.Second,
	PrivatePeerIDPrefix:                    "-RN" + Version + "-",
	PrivateExtensionHandshakeClientVersion: "Rain " + Version,
	BlocklistUpdateInterval:                24 * time.Hour,
	BlocklistUpdateTimeout:                 10 * time.Minute,
	BlocklistEnabledForTrackers:            true,
	BlocklistEnabledForOutgoingConnections: true,
	BlocklistEnabledForIncomingConnections: true,
	BlocklistMaxResponseSize:               100 << 20,
	TorrentAddHTTPTimeout:                  30 * time.Second,
	MaxMetadataSize:                        30 << 20,
	MaxTorrentSize:                         10 << 20,
	MaxPieces:                              64 << 10,
	DNSResolveTimeout:                      5 * time.Second,
	ResumeOnStartup:                        true,
	HealthCheckInterval:                    10 * time.Second,
	HealthCheckTimeout:                     60 * time.Second,
	FilePermissions:                        0o750,

	RPCEnabled:         true,
	RPCHost:            "127.0.0.1",
	RPCPort:            7246,
	RPCShutdownTimeout: 5 * time.Second,

	TrackerNumWant:              200,
	TrackerStopTimeout:          5 * time.Second,
	TrackerMinAnnounceInterval:  time.Minute,
	TrackerHTTPTimeout:          10 * time.Second,
	TrackerHTTPPrivateUserAgent: "Rain/" + Version,
	TrackerHTTPMaxResponseSize:  2 << 20,
	TrackerHTTPVerifyTLS:        true,

	DHTEnabled:             true,
	DHTHost:                "0.0.0.0",
	DHTPort:                7246,
	DHTAnnounceInterval:    30 * time.Minute,
	DHTMinAnnounceInterval: time.Minute,
	DHTBootstrapNodes: []string{
		"router.bittorrent.com:6881",
		"dht.transmissionbt.com:6881",
		"router.utorrent.com:6881",
		"dht.libtorrent.org:25401",
		"dht.aelitis.com:6881",
	},

	UnchokedPeers:                3,
	OptimisticUnchokedPeers:      1,
	MaxRequestsIn:                250,
	MaxRequestsOut:               250,
	DefaultRequestsOut:           50,
	RequestTimeout:               20 * time.Second,
	EndgameMaxDuplicateDownloads: 20,
	MaxPeerDial:                  80,
	MaxPeerAccept:                20,
	ParallelMetadataDownloads:    2,
	PeerConnectTimeout:           5 * time.Second,
	PeerHandshakeTimeout:         10 * time.Second,
	PieceReadTimeout:             30 * time.Second,
	MaxPeerAddresses:             2000,
	AllowedFastSet:               10,

	ReadCacheBlockSize: 128 << 10,
	ReadCacheSize:      256 << 20,
	ReadCacheTTL:       1 * time.Minute,
	ParallelReads:      1,
	ParallelWrites:     1,
	WriteCacheSize:     1 << 30,

	WebseedDialTimeout:             10 * time.Second,
	WebseedTLSHandshakeTimeout:     10 * time.Second,
	WebseedResponseHeaderTimeout:   10 * time.Second,
	WebseedResponseBodyReadTimeout: 10 * time.Second,
	WebseedRetryInterval:           time.Minute,
	WebseedVerifyTLS:               true,
	WebseedMaxSources:              10,
	WebseedMaxDownloads:            4,
}

DefaultConfig for Session. Do not pass zero value Config to NewSession. Copy this struct and modify instead.

View Source
var Version = "0.0.0"

Version of client. Set during build. "0.0.0" is the development version.

Functions

func DisableLogging added in v1.6.1

func DisableLogging()

DisableLogging disables all log messages printed to console. This function needs to be called before creating a Session.

Types

type AddTorrentOptions added in v0.8.7

type AddTorrentOptions struct {
	// ID uniquely identifies the torrent in Session.
	// If empty, a random ID is generated.
	ID string
	// Do not start torrent automatically after adding.
	Stopped bool
	// Stop torrent after all pieces are downloaded.
	StopAfterDownload bool
	// Stop torrent after metadata is downloaded from magnet links.
	StopAfterMetadata bool
}

AddTorrentOptions contains options for adding a new torrent.

type AnnounceError added in v0.9.0

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

AnnounceError is the error returned from announce response to a tracker.

func (*AnnounceError) Error added in v0.9.0

func (e *AnnounceError) Error() string

Contains the humanized version of error.

func (*AnnounceError) Unknown added in v0.9.0

func (e *AnnounceError) Unknown() bool

Unknown returns true if the error is unexpected. Expected errors are tracker errors, network errors and DNS errors.

func (*AnnounceError) Unwrap added in v0.9.0

func (e *AnnounceError) Unwrap() error

Unwrap returns the underlying error object.

type Config

type Config struct {
	// Database file to save resume data.
	Database string
	// DataDir is where files are downloaded.
	DataDir string
	// If true, torrent files are saved into <data_dir>/<torrent_id>/<torrent_name>.
	// Useful if downloading the same torrent from multiple sources.
	DataDirIncludesTorrentID bool
	// Host to listen for TCP Acceptor. Port is computed automatically
	Host string
	// New torrents will be listened at selected port in this range.
	PortBegin, PortEnd uint16
	// At start, client will set max open files limit to this number. (like "ulimit -n" command)
	MaxOpenFiles uint64
	// Enable peer exchange protocol.
	PEXEnabled bool
	// Resume data (bitfield & stats) are saved to disk at interval to keep IO lower.
	ResumeWriteInterval time.Duration
	// Peer id is prefixed with this string. See BEP 20. Remaining bytes of peer id will be randomized.
	// Only applies to private torrents.
	PrivatePeerIDPrefix string
	// Client version that is sent in BEP 10 handshake message.
	// Only applies to private torrents.
	PrivateExtensionHandshakeClientVersion string
	// URL to the blocklist file in CIDR format.
	BlocklistURL string
	// When to refresh blocklist
	BlocklistUpdateInterval time.Duration
	// HTTP timeout for downloading blocklist
	BlocklistUpdateTimeout time.Duration
	// Do not contact tracker if it's IP is blocked
	BlocklistEnabledForTrackers bool
	// Do not connect to peer if it's IP is blocked
	BlocklistEnabledForOutgoingConnections bool
	// Do not accept connections from peer if it's IP is blocked
	BlocklistEnabledForIncomingConnections bool
	// Do not accept response larger than this size
	BlocklistMaxResponseSize int64
	// Time to wait when adding torrent with AddURI().
	TorrentAddHTTPTimeout time.Duration
	// Maximum allowed size to be received by metadata extension.
	MaxMetadataSize uint
	// Maximum allowed size to be read when adding torrent.
	MaxTorrentSize uint
	// Maximum allowed number of pieces in a torrent.
	MaxPieces uint32
	// Time to wait when resolving host names for trackers and peers.
	DNSResolveTimeout time.Duration
	// Global download speed limit in KB/s.
	SpeedLimitDownload int64
	// Global upload speed limit in KB/s.
	SpeedLimitUpload int64
	// Start torrent automatically if it was running when previous session was closed.
	ResumeOnStartup bool
	// Check each torrent loop for aliveness. Helps to detect bugs earlier.
	HealthCheckInterval time.Duration
	// If torrent loop is stuck for more than this duration. Program crashes with stacktrace.
	HealthCheckTimeout time.Duration
	// The unix permission of created files, execute bit is removed for files
	FilePermissions fs.FileMode

	// Enable RPC server
	RPCEnabled bool
	// Host to listen for RPC server
	RPCHost string
	// Listen port for RPC server
	RPCPort int
	// Time to wait for ongoing requests before shutting down RPC HTTP server.
	RPCShutdownTimeout time.Duration

	// Enable DHT node.
	DHTEnabled bool
	// DHT node will listen on this IP.
	DHTHost string
	// DHT node will listen on this UDP port.
	DHTPort uint16
	// DHT announce interval
	DHTAnnounceInterval time.Duration
	// Minimum announce interval when announcing to DHT.
	DHTMinAnnounceInterval time.Duration
	// Known routers to bootstrap local DHT node.
	DHTBootstrapNodes []string

	// Number of peer addresses to request in announce request.
	TrackerNumWant int
	// Time to wait for announcing stopped event.
	// Stopped event is sent to the tracker when torrent is stopped.
	TrackerStopTimeout time.Duration
	// When the client needs new peer addresses to connect, it ask to the tracker.
	// To prevent spamming the tracker an interval is set to wait before the next announce.
	TrackerMinAnnounceInterval time.Duration
	// Total time to wait for response to be read.
	// This includes ConnectTimeout and TLSHandshakeTimeout.
	TrackerHTTPTimeout time.Duration
	// User agent sent when communicating with HTTP trackers.
	// Only applies to private torrents.
	TrackerHTTPPrivateUserAgent string
	// Max number of bytes in a tracker response.
	TrackerHTTPMaxResponseSize uint
	// Check and validate TLS ceritificates.
	TrackerHTTPVerifyTLS bool

	// Number of unchoked peers.
	UnchokedPeers int
	// Number of optimistic unchoked peers.
	OptimisticUnchokedPeers int
	// Max number of blocks allowed to be queued without dropping any.
	MaxRequestsIn int
	// Max number of blocks requested from a peer but not received yet.
	// `rreq` value from extended handshake cannot exceed this limit.
	MaxRequestsOut int
	// Number of bloks requested from peer if it does not send `rreq` value in extended handshake.
	DefaultRequestsOut int
	// Time to wait for a requested block to be received before marking peer as snubbed
	RequestTimeout time.Duration
	// Max number of running downloads on piece in endgame mode, snubbed and choed peers don't count
	EndgameMaxDuplicateDownloads int
	// Max number of outgoing connections to dial
	MaxPeerDial int
	// Max number of incoming connections to accept
	MaxPeerAccept int
	// Running metadata downloads, snubbed peers don't count
	ParallelMetadataDownloads int
	// Time to wait for TCP connection to open.
	PeerConnectTimeout time.Duration
	// Time to wait for BitTorrent handshake to complete.
	PeerHandshakeTimeout time.Duration
	// When peer has started to send piece block, if it does not send any bytes in PieceReadTimeout, the connection is closed.
	PieceReadTimeout time.Duration
	// Max number of peer addresses to keep in connect queue.
	MaxPeerAddresses int
	// Number of allowed-fast messages to send after handshake.
	AllowedFastSet int

	// Number of bytes to read when a piece is requested by a peer.
	ReadCacheBlockSize int64
	// Number of cached bytes for piece read requests.
	ReadCacheSize int64
	// Read bytes for a piece part expires after duration.
	ReadCacheTTL time.Duration
	// Number of read operations to do in parallel.
	ParallelReads uint
	// Number of write operations to do in parallel.
	ParallelWrites uint
	// Number of bytes allocated in memory for downloading piece data.
	WriteCacheSize int64

	// When the client want to connect a peer, first it tries to do encrypted handshake.
	// If it does not work, it connects to same peer again and does unencrypted handshake.
	// This behavior can be changed via this variable.
	DisableOutgoingEncryption bool
	// Dial only encrypted connections.
	ForceOutgoingEncryption bool
	// Do not accept unencrypted connections.
	ForceIncomingEncryption bool

	// TCP connect timeout for WebSeed sources
	WebseedDialTimeout time.Duration
	// TLS handshake timeout for WebSeed sources
	WebseedTLSHandshakeTimeout time.Duration
	// HTTP header timeout for WebSeed sources
	WebseedResponseHeaderTimeout time.Duration
	// HTTP body read timeout for Webseed sources
	WebseedResponseBodyReadTimeout time.Duration
	// Retry interval for restarting failed downloads
	WebseedRetryInterval time.Duration
	// Verify TLS certificate for WebSeed URLs
	WebseedVerifyTLS bool
	// Limit the number of WebSeed sources in torrent.
	WebseedMaxSources int
	// Number of maximum simulateous downloads from WebSeed sources.
	WebseedMaxDownloads int

	// Shell command to execute on torrent completion.
	OnCompleteCmd []string

	// Replace default log handler
	CustomLogHandler log.Handler
	// Enable debugging
	Debug bool
}

Config for Session.

type File added in v1.12.6

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

func (File) Length added in v1.13.0

func (f File) Length() int64

func (File) Path added in v1.12.6

func (f File) Path() string

type FileStats added in v1.12.6

type FileStats struct {
	File
	BytesCompleted int64
}

type InfoHash

type InfoHash [20]byte

InfoHash is the unique value that represents the files in a torrent.

func (InfoHash) String

func (h InfoHash) String() string

String encodes info hash in hex as 40 characters.

type InputError added in v0.17.11

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

InputError is returned from Session.AddTorrent and Session.AddURI methods when there is problem with the input.

func (*InputError) Error added in v0.17.11

func (e *InputError) Error() string

Error implements error interface.

func (*InputError) Unwrap added in v0.17.11

func (e *InputError) Unwrap() error

Unwrap returns the underlying error.

type Peer

type Peer struct {
	ID                 [20]byte
	Client             string
	Addr               net.Addr
	Source             PeerSource
	ConnectedAt        time.Time
	Downloading        bool
	ClientInterested   bool
	ClientChoking      bool
	PeerInterested     bool
	PeerChoking        bool
	OptimisticUnchoked bool
	Snubbed            bool
	EncryptedHandshake bool
	EncryptedStream    bool
	DownloadSpeed      int
	UploadSpeed        int
}

Peer is a remote peer that is connected and completed protocol handshake.

type PeerSource

type PeerSource int

PeerSource indicates that how the peer is found.

const (
	// SourceTracker indicates that the peer is found from one of the trackers.
	SourceTracker PeerSource = iota
	// SourceDHT indicates that the peer is found from Decentralised Hash Table.
	SourceDHT
	// SourcePEX indicates that the peer is found from another peer.
	SourcePEX
	// SourceIncoming indicates that the peer found us.
	SourceIncoming
	// SourceManual indicates that the peer is added manually via AddPeer method.
	SourceManual
)

type Session

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

Session contains torrents, DHT node, caches and other data structures shared by multiple torrents.

func NewSession added in v0.7.3

func NewSession(cfg Config) (*Session, error)

NewSession creates a new Session for downloading and seeding torrents. Returned session must be closed after use.

func (*Session) AddTorrent

func (s *Session) AddTorrent(r io.Reader, opt *AddTorrentOptions) (*Torrent, error)

AddTorrent adds a new torrent to the session by reading .torrent metainfo from reader. Nil value can be passed as opt for default options.

func (*Session) AddURI

func (s *Session) AddURI(uri string, opt *AddTorrentOptions) (*Torrent, error)

AddURI adds a new torrent to the session from a URI. URI may be a magnet link or a HTTP URL. In case of a HTTP address, a torrent is tried to be downloaded from that URL. Nil value can be passed as opt for default options.

func (*Session) CleanDatabase added in v1.1.0

func (s *Session) CleanDatabase() error

CleanDatabase removes invalid records in the database. Normally you don't need to call this.

func (*Session) Close

func (s *Session) Close() error

Close stops all torrents and release the resources.

func (*Session) CompactDatabase added in v1.6.0

func (s *Session) CompactDatabase(output string) error

CompactDatabase rewrites the database using existing torrent records to a new file. Normally you don't need to call this.

func (*Session) GetTorrent

func (s *Session) GetTorrent(id string) *Torrent

GetTorrent by its id. Returns nil if torrent with id is not found.

func (*Session) ListTorrents

func (s *Session) ListTorrents() []*Torrent

ListTorrents returns all torrents in session as a slice. The order of the torrents returned is different on each call.

func (*Session) RemoveTorrent

func (s *Session) RemoveTorrent(id string) error

RemoveTorrent removes the torrent from the session and delete its files.

func (*Session) StartAll added in v0.4.0

func (s *Session) StartAll() error

StartAll starts all torrents in session.

func (*Session) Stats

func (s *Session) Stats() SessionStats

Stats returns current statistics about the Session.

func (*Session) StopAll added in v0.4.0

func (s *Session) StopAll() error

StopAll stops all torrents in session.

type SessionStats

type SessionStats struct {
	// Time elapsed after creation of the Session object.
	Uptime time.Duration
	// Number of torrents in Session.
	Torrents int
	// Total number of connected peers.
	Peers int
	// Number of available ports for new torrents.
	PortsAvailable int

	// Number of rules in blocklist.
	BlockListRules int
	// Time elapsed after the last successful update of blocklist.
	BlockListRecency time.Duration

	// Number of objects in piece read cache.
	// Each object is a block whose size is defined in Config.ReadCacheBlockSize.
	ReadCacheObjects int
	// Current size of read cache.
	ReadCacheSize int64
	// Hit ratio of read cache.
	ReadCacheUtilization int

	// Number of reads per second from disk.
	ReadsPerSecond int
	// Number of active read requests from disk.
	ReadsActive int
	// Number of pending read requests from disk.
	ReadsPending int

	// Number of objects in piece write cache.
	// Objects are complete pieces.
	// Piece size differs among torrents.
	WriteCacheObjects int
	// Current size of write cache.
	WriteCacheSize int64
	// Number of pending torrents that is waiting for write cache.
	WriteCachePendingKeys int

	// Number of writes per second to disk.
	// Each write is a complete piece.
	WritesPerSecond int
	// Number of active write requests to disk.
	WritesActive int
	// Number of pending write requests to disk.
	WritesPending int

	// Download speed from peers in bytes/s.
	SpeedDownload int
	// Upload speed to peers in bytes/s.
	SpeedUpload int
	// Read speed from disk in bytes/s.
	SpeedRead int
	// Write speed to disk in bytes/s.
	SpeedWrite int

	// Number of bytes downloaded from peers.
	BytesDownloaded int64
	// Number of bytes uploaded to peers.
	BytesUploaded int64
	// Number of bytes read from disk.
	BytesRead int64
	// Number of bytes written to disk.
	BytesWritten int64
}

SessionStats contains statistics about Session.

type Stats

type Stats struct {
	// Info hash of torrent.
	InfoHash InfoHash
	// Listening port number.
	Port int
	// Status of the torrent.
	Status Status
	// Contains the error message if torrent is stopped unexpectedly.
	Error  error
	Pieces struct {
		// Number of pieces that are checked when torrent is in "Verifying" state.
		Checked uint32
		// Number of pieces that we are downloaded successfully and verivied by hash check.
		Have uint32
		// Number of pieces that need to be downloaded. Some of them may be being downloaded.
		// Pieces that are being downloaded may counted as missing until they are downloaded and passed hash check.
		Missing uint32
		// Number of unique pieces available on swarm.
		// If this number is less then the number of total pieces, the download may never finish.
		Available uint32
		// Number of total pieces in torrent.
		Total uint32
	}
	Bytes struct {
		// Bytes that are downloaded and passed hash check.
		Completed int64
		// The number of bytes that is needed to complete all missing pieces.
		Incomplete int64
		// The number of total bytes of files in torrent.  Total = Completed + Incomplete
		Total int64
		// Downloaded is the number of bytes downloaded from swarm.
		// Because some pieces may be downloaded more than once, this number may be greater than completed bytes.
		Downloaded int64
		// BytesUploaded is the number of bytes uploaded to the swarm.
		Uploaded int64
		// Bytes downloaded due to duplicate/non-requested pieces.
		Wasted int64
		// Bytes allocated on storage.
		Allocated int64
	}
	Peers struct {
		// Number of peers that are connected, handshaked and ready to send and receive messages.
		Total int
		// Number of peers that have connected to us.
		Incoming int
		// Number of peers that we have connected to.
		Outgoing int
	}
	Handshakes struct {
		// Number of peers that are not handshaked yet.
		Total int
		// Number of incoming peers in handshake state.
		Incoming int
		// Number of outgoing peers in handshake state.
		Outgoing int
	}
	Addresses struct {
		// Total number of peer addresses that are ready to be connected.
		Total int
		// Peers found via trackers.
		Tracker int
		// Peers found via DHT node.
		DHT int
		// Peers found via peer exchange.
		PEX int
	}
	Downloads struct {
		// Number of active piece downloads.
		Total int
		// Number of pieces that are being downloaded normally.
		Running int
		// Number of pieces that are being downloaded too slow.
		Snubbed int
		// Number of piece downloads in choked state.
		Choked int
	}
	MetadataDownloads struct {
		// Number of active metadata downloads.
		Total int
		// Number of peers that uploading too slow.
		Snubbed int
		// Number of peers that are being downloaded normally.
		Running int
	}
	// Name can change after metadata is downloaded.
	Name string
	// Is private torrent?
	Private bool
	// Number of files.
	FileCount int
	// Length of a single piece.
	PieceLength uint32
	// Duration while the torrent is in Seeding status.
	SeededFor time.Duration
	// Speed is calculated as 1-minute moving average.
	Speed struct {
		// Downloaded bytes per second.
		Download int
		// Uploaded bytes per second.
		Upload int
	}
	// Time remaining to complete download. nil value means infinity.
	ETA *time.Duration
}

Stats contains statistics about Torrent.

type Status

type Status int

Status of a Torrent

const (
	// Stopped indicates that the torrent is not running.
	// No peers are connected and files are not open.
	Stopped Status = iota
	// DownloadingMetadata indicates that the torrent is in the process of downloadin metadata.
	// When torrent is added via magnet link, torrent has no metadata and it needs to be downloaded from peers before starting to download files.
	DownloadingMetadata
	// Allocating indicates that the torrent is in the process of creating/opening files on the disk.
	Allocating
	// Verifying indicates that the torrent has already some files on disk and it is checking the validity of pieces by comparing hashes.
	Verifying
	// Downloading the torrent's files from peers.
	Downloading
	// Seeding the torrent. All pieces/files are downloaded.
	Seeding
	// Stopping the torrent. This is the status after Stop() is called. All peers are disconnected and files are closed. A stop event sent to all trackers. After trackers responded the torrent switches into Stopped state.
	Stopping
)

func (Status) String added in v0.16.0

func (s Status) String() string

type Torrent

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

Torrent is created from a torrent file or a magnet link.

func (*Torrent) AddPeer added in v0.4.0

func (t *Torrent) AddPeer(addr string) error

AddPeer adds a new peer to the torrent. Does nothing if torrent is stopped.

func (*Torrent) AddTracker added in v0.4.0

func (t *Torrent) AddTracker(uri string) error

AddTracker adds a new tracker to the torrent.

func (*Torrent) AddedAt

func (t *Torrent) AddedAt() time.Time

AddedAt returns the time that the torrent is added.

func (*Torrent) Announce added in v0.10.0

func (t *Torrent) Announce()

Announce the torrent to all trackers and DHT. It does not overrides the minimum interval value sent by the trackers or set in Config.

func (*Torrent) Dir added in v1.13.0

func (t *Torrent) Dir() string

Dir returns the directory that contains the files in the torrent.

func (*Torrent) FileStats added in v1.13.0

func (t *Torrent) FileStats() ([]FileStats, error)

FileStats returns statistics about each file in the torrent. An error is returned when torrent is not running.

func (*Torrent) Files added in v1.12.6

func (t *Torrent) Files() ([]File, error)

Files in the torrent. An error is returned when metainfo isn't ready.

func (*Torrent) ID

func (t *Torrent) ID() string

ID is a unique identifier in the Session.

func (*Torrent) InfoHash

func (t *Torrent) InfoHash() InfoHash

InfoHash returns the hash of the info dictionary of torrent file. Two different torrents may have the same info hash.

func (*Torrent) Magnet added in v0.16.0

func (t *Torrent) Magnet() (string, error)

Magnet returns the magnet link. Returns error if torrent is private.

func (*Torrent) Move added in v0.15.0

func (t *Torrent) Move(target string) error

Move torrent to another Session. target must be the RPC server address in host:port form.

func (*Torrent) Name

func (t *Torrent) Name() string

Name of the torrent. For magnet downloads name can change after metadata is downloaded but this method still returns the initial name. Use Stats() method to get name in info dictionary.

func (*Torrent) NotifyClose added in v1.12.6

func (t *Torrent) NotifyClose() <-chan struct{}

NotifyClose returns a channel for notifying removal/closure. The channel is closed once RemoveTorrent() is called, dropping it from the session.

func (*Torrent) NotifyComplete added in v1.0.0

func (t *Torrent) NotifyComplete() <-chan struct{}

NotifyComplete returns a channel for notifying completion. The channel is closed once all torrent pieces are downloaded successfully. NotifyComplete must be called after calling Start().

func (*Torrent) NotifyMetadata added in v1.8.0

func (t *Torrent) NotifyMetadata() <-chan struct{}

NotifyMetadata returns a channel for notifying completion of metadata download from magnet links. The channel is closed once all metadata pieces are downloaded successfully. NotifyMetadata must be called after calling Start().

func (*Torrent) NotifyStop added in v1.0.0

func (t *Torrent) NotifyStop() <-chan error

NotifyStop returns a new channel for notifying stop event. Value from the channel contains the error if there is any, otherwise the value is nil. NotifyStop must be called after calling Start().

func (*Torrent) Peers

func (t *Torrent) Peers() []Peer

Peers returns the list of connected (handshake completed) peers of the torrent.

func (*Torrent) Port

func (t *Torrent) Port() int

Port returns the TCP port number that the torrent is listening peers.

func (*Torrent) Start

func (t *Torrent) Start() error

Start downloading the torrent. If all pieces are completed, starts seeding them.

func (*Torrent) Stats

func (t *Torrent) Stats() Stats

Stats returns statistics about the torrent.

func (*Torrent) Stop

func (t *Torrent) Stop() error

Stop the torrent. Does not block. After Stop is called, the torrent switches into Stopping state. During Stopping state, a stop event sent to trackers with a timeout. At most 5 seconds later, the torrent switches into Stopped state.

func (*Torrent) Torrent added in v0.16.0

func (t *Torrent) Torrent() ([]byte, error)

Torrent returns the metainfo bytes (contents of .torrent file). Returns error if torrent has no metadata yet.

func (*Torrent) Trackers

func (t *Torrent) Trackers() []Tracker

Trackers returns the list of trackers of this torrent.

func (*Torrent) Verify added in v0.10.0

func (t *Torrent) Verify() error

Verify pieces of torrent by reading all of the torrents files from disk. After Verify called, the torrent is stopped, then verification starts and the torrent switches into Verifying state. The torrent stays stopped after verification finishes.

func (*Torrent) Webseeds added in v0.7.0

func (t *Torrent) Webseeds() []Webseed

Webseeds returns the list of WebSeed sources in the torrent.

type Tracker

type Tracker struct {
	URL          string
	Status       TrackerStatus
	Leechers     int
	Seeders      int
	Error        *AnnounceError
	Warning      string
	LastAnnounce time.Time
	NextAnnounce time.Time
}

Tracker is a server that tracks the peers of torrents.

type TrackerStatus

type TrackerStatus int

TrackerStatus is status of the Tracker.

const (
	// NotContactedYet indicates that no announce request has been made to the tracker.
	NotContactedYet TrackerStatus = iota
	// Contacting the tracker. Sending request or waiting response from the tracker.
	Contacting
	// Working indicates that the tracker has responded as expected.
	Working
	// NotWorking indicates that the tracker didn't respond or returned an error.
	NotWorking
)

type Webseed added in v0.7.0

type Webseed struct {
	URL           string
	Error         error
	DownloadSpeed int
}

Webseed is a HTTP source defined in Torrent. Client can download from these sources along with peers from the swarm.

Jump to

Keyboard shortcuts

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