Documentation ¶
Overview ¶
Package torrent implements a torrent client. Goals include:
- Configurable data storage, such as file, mmap, and piece-based.
- Downloading on demand: torrent.Reader will request only the data required to satisfy Reads, which is ideal for streaming and torrentfs.
BitTorrent features implemented include:
- Protocol obfuscation
- DHT
- uTP
- PEX
- Magnet links
- IP Blocklists
- Some IPv6
- HTTP and UDP tracker clients
- BEPs:
- 3: Basic BitTorrent protocol
- 5: DHT
- 6: Fast Extension (have all/none only)
- 7: IPv6 Tracker Extension
- 9: ut_metadata
- 10: Extension protocol
- 11: PEX
- 12: Multitracker metadata extension
- 15: UDP Tracker Protocol
- 20: Peer ID convention ("-GTnnnn-")
- 23: Tracker Returns Compact Peer Lists
- 27: Private torrents
- 29: uTorrent transport protocol
- 41: UDP Tracker Protocol Extensions
- 42: DHT Security extension
- 43: Read-only DHT Nodes
Example ¶
package main import ( "log" "github.com/anacrolix/torrent" ) func main() { c, _ := torrent.NewClient(nil) defer c.Close() t, _ := c.AddMagnet("magnet:?xt=urn:btih:ZOCMZQIPFFW7OLLMIC5HUB6BPCSDEOQU") <-t.GotInfo() t.DownloadAll() c.WaitAll() log.Print("ermahgerd, torrent downloaded") }
Output:
Example (FileReader) ¶
package main import ( "github.com/anacrolix/missinggo" "github.com/anacrolix/torrent" ) func main() { var ( t *torrent.Torrent f torrent.File ) r := t.NewReader() defer r.Close() // Access the parts of the torrent pertaining to f. Data will be // downloaded as required, per the configuration of the torrent.Reader. _ = missinggo.NewSectionReadSeeker(r, f.Offset(), f.Length()) }
Output:
Index ¶
- Constants
- Variables
- func NewUtpSocket(network, addr string) (utpSocket, error)
- type Client
- func (cl *Client) AddDHTNodes(nodes []string)
- func (cl *Client) AddMagnet(uri string) (T *Torrent, err error)
- func (cl *Client) AddTorrent(mi *metainfo.MetaInfo) (T *Torrent, err error)
- func (cl *Client) AddTorrentFromFile(filename string) (T *Torrent, err error)
- func (cl *Client) AddTorrentInfoHash(infoHash metainfo.Hash) (t *Torrent, new bool)
- func (cl *Client) AddTorrentInfoHashWithStorage(infoHash metainfo.Hash, specStorage storage.ClientImpl) (t *Torrent, new bool)
- func (cl *Client) AddTorrentSpec(spec *TorrentSpec) (t *Torrent, new bool, err error)
- func (cl *Client) BadPeerIPs() []string
- func (cl *Client) Close()
- func (cl *Client) DHT() *dht.Server
- func (cl *Client) IPBlockList() iplist.Ranger
- func (cl *Client) ListenAddr() net.Addr
- func (cl *Client) PeerID() [20]byte
- func (cl *Client) SetIPBlockList(list iplist.Ranger)
- func (cl *Client) Torrent(ih metainfo.Hash) (t *Torrent, ok bool)
- func (cl *Client) Torrents() []*Torrent
- func (cl *Client) WaitAll() bool
- func (cl *Client) WriteStatus(_w io.Writer)
- type Config
- type ConnStats
- type EncryptionPolicy
- type ExtensionBit
- type File
- func (f *File) Cancel()
- func (f *File) DisplayPath() string
- func (f *File) Download()
- func (f File) FileInfo() metainfo.FileInfo
- func (f *File) Length() int64
- func (f *File) Offset() int64
- func (f File) Path() string
- func (f *File) PrioritizeRegion(off, len int64)
- func (f *File) State() (ret []FilePieceState)
- func (f *File) Torrent() *Torrent
- type FilePieceState
- type Handle
- type Peer
- type Piece
- type PieceState
- type PieceStateChange
- type PieceStateRun
- type Reader
- func (r *Reader) Close() error
- func (r *Reader) CurrentPos() int64
- func (r *Reader) Read(b []byte) (n int, err error)
- func (r *Reader) ReadContext(ctx context.Context, b []byte) (n int, err error)
- func (r *Reader) Seek(off int64, whence int) (ret int64, err error)
- func (r *Reader) SetNonResponsive()
- func (r *Reader) SetReadahead(readahead int64)
- func (r *Reader) SetResponsive()
- func (r *Reader) Torrent() *Torrent
- type Torrent
- func (t *Torrent) AddPeers(pp []Peer)
- func (t *Torrent) AddTrackers(announceList [][]string)
- func (t *Torrent) BytesCompleted() int64
- func (t *Torrent) BytesMissing() int64
- func (t *Torrent) CancelPieces(begin, end int)
- func (t *Torrent) Closed() <-chan struct{}
- func (t *Torrent) DownloadAll()
- func (t *Torrent) DownloadPieces(begin, end int)
- func (t *Torrent) Drop()
- func (t *Torrent) Files() (ret []File)
- func (t *Torrent) GotInfo() <-chan struct{}
- func (t *Torrent) Info() *metainfo.Info
- func (t *Torrent) InfoHash() metainfo.Hash
- func (t *Torrent) KnownSwarm() (ks []Peer)
- func (t *Torrent) Length() int64
- func (t *Torrent) Metainfo() metainfo.MetaInfo
- func (t *Torrent) Name() string
- func (t *Torrent) NewReader() (ret *Reader)
- func (t *Torrent) NumPieces() int
- func (t *Torrent) Piece(i int) *Piece
- func (t *Torrent) PieceBytesMissing(piece int) int64
- func (t *Torrent) PieceState(piece int) PieceState
- func (t *Torrent) PieceStateRuns() []PieceStateRun
- func (t *Torrent) Seeding() bool
- func (t *Torrent) SetDisplayName(dn string)
- func (t *Torrent) SetInfoBytes(b []byte) (err error)
- func (t *Torrent) SetMaxEstablishedConns(max int) (oldMax int)
- func (t *Torrent) Stats() TorrentStats
- func (t *Torrent) String() string
- func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription
- func (t *Torrent) VerifyData()
- type TorrentSpec
- type TorrentStats
Examples ¶
Constants ¶
const ( ExtensionBitDHT = 0 // http://www.bittorrent.org/beps/bep_0005.html ExtensionBitExtended = 20 // http://www.bittorrent.org/beps/bep_0010.html ExtensionBitFast = 2 // http://www.bittorrent.org/beps/bep_0006.html )
const ( PiecePriorityNone piecePriority = iota // Not wanted. PiecePriorityNormal // Wanted. PiecePriorityReadahead // May be required soon. // Succeeds a piece where a read occurred. Currently the same as Now, apparently due to issues with caching. PiecePriorityNext PiecePriorityNow // A Reader is reading in this piece. )
Variables ¶
Functions ¶
func NewUtpSocket ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Clients contain zero or more Torrents. A Client manages a blocklist, the TCP/UDP protocol ports, and DHT as desired.
func (*Client) AddDHTNodes ¶
func (*Client) AddTorrent ¶
func (*Client) AddTorrentFromFile ¶
func (*Client) AddTorrentInfoHash ¶
func (*Client) AddTorrentInfoHashWithStorage ¶
func (cl *Client) AddTorrentInfoHashWithStorage(infoHash metainfo.Hash, specStorage storage.ClientImpl) (t *Torrent, new bool)
Adds a torrent by InfoHash with a custom Storage implementation. If the torrent already exists then this Storage is ignored and the existing torrent returned with `new` set to `false`
func (*Client) AddTorrentSpec ¶
func (cl *Client) AddTorrentSpec(spec *TorrentSpec) (t *Torrent, new bool, err error)
Add or merge a torrent spec. If the torrent is already present, the trackers will be merged with the existing ones. If the Info isn't yet known, it will be set. The display name is replaced if the new spec provides one. Returns new if the torrent wasn't already in the client. Note that any `Storage` defined on the spec will be ignored if the torrent is already present (i.e. `new` return value is `true`)
func (*Client) BadPeerIPs ¶
func (*Client) Close ¶
func (cl *Client) Close()
Stops the client. All connections to peers are closed and all activity will come to a halt.
func (*Client) IPBlockList ¶
func (*Client) ListenAddr ¶
func (*Client) SetIPBlockList ¶
func (*Client) WaitAll ¶
Returns true when all torrents are completely downloaded and false if the client is stopped before that.
func (*Client) WriteStatus ¶
Writes out a human readable status of the client, such as for writing to a HTTP status page.
type Config ¶
type Config struct { // Store torrent file data in this directory unless TorrentDataOpener is // specified. DataDir string `long:"data-dir" description:"directory to store downloaded torrent data"` // The address to listen for new uTP and TCP bittorrent protocol // connections. DHT shares a UDP socket with uTP unless configured // otherwise. ListenAddr string `long:"listen-addr" value-name:"HOST:PORT"` // Don't announce to trackers. This only leaves DHT to discover peers. DisableTrackers bool `long:"disable-trackers"` DisablePEX bool `long:"disable-pex"` // Don't create a DHT. NoDHT bool `long:"disable-dht"` // Overrides the default DHT configuration. DHTConfig dht.ServerConfig // Never send chunks to peers. NoUpload bool `long:"no-upload"` // Disable uploading even when it isn't fair. DisableAggressiveUpload bool `long:"disable-aggressive-upload"` // Upload even after there's nothing in it for us. By default uploading is // not altruistic, we'll upload slightly more than we download from each // peer. Seed bool `long:"seed"` // Events are data bytes sent in pieces. The burst must be large enough to // fit a whole chunk, which is usually 16 KiB. UploadRateLimiter *rate.Limiter // The events are bytes read from connections. The burst must be bigger // than the largest Read performed on a Conn minus one. This is likely to // be the larger of the main read loop buffer (~4096), and the requested // chunk size (~16KiB). DownloadRateLimiter *rate.Limiter // User-provided Client peer ID. If not present, one is generated automatically. PeerID string // For the bittorrent protocol. DisableUTP bool // For the bittorrent protocol. DisableTCP bool `long:"disable-tcp"` // Called to instantiate storage for each added torrent. Builtin backends // are in the storage package. If not set, the "file" implementation is // used. DefaultStorage storage.ClientImpl EncryptionPolicy IPBlocklist iplist.Ranger DisableIPv6 bool `long:"disable-ipv6"` // Perform logging and any other behaviour that will help debug. Debug bool `help:"enable debugging"` // HTTP client used to query the tracker endpoint. Default is DefaultHTTPClient HTTP *http.Client // Updated occasionally to when there's been some changes to client // behaviour in case other clients are assuming anything of us. See also // `bep20`. ExtendedHandshakeClientVersion string // default "go.torrent dev 20150624" // Peer ID client identifier prefix. We'll update this occasionally to // reflect changes to client behaviour that other clients may depend on. // Also see `extendedHandshakeClientVersion`. Bep20 string // default "-GT0001-" NominalDialTimeout time.Duration // default time.Second * 30 MinDialTimeout time.Duration // default 5 * time.Second EstablishedConnsPerTorrent int // default 80 HalfOpenConnsPerTorrent int // default 80 TorrentPeersHighWater int // default 200 TorrentPeersLowWater int // default 50 // Limit how long handshake can take. This is to reduce the lingering // impact of a few bad apples. 4s loses 1% of successful handshakes that // are obtained with 60s timeout, and 5% of unsuccessful handshakes. HandshakesTimeout time.Duration // default 20 * time.Second }
Override Client defaults.
type ConnStats ¶
type ConnStats struct { // Torrent "piece" messages, or data chunks. ChunksWritten int64 // Num piece messages sent. ChunksRead int64 // Total bytes on the wire. Includes handshakes and encryption. BytesWritten int64 BytesRead int64 // Data bytes, actual torrent data. DataBytesWritten int64 DataBytesRead int64 }
type EncryptionPolicy ¶
type ExtensionBit ¶
type ExtensionBit uint
type File ¶
type File struct {
// contains filtered or unexported fields
}
Provides access to regions of torrent data that correspond to its files.
func (*File) DisplayPath ¶
The relative file path for a multi-file torrent, and the torrent name for a single-file torrent.
func (*File) Download ¶
func (f *File) Download()
Requests that all pieces containing data in the file be downloaded.
func (*File) PrioritizeRegion ¶
Requests that torrent pieces containing bytes in the given region of the file be downloaded.
func (*File) State ¶
func (f *File) State() (ret []FilePieceState)
Returns the state of pieces in this file.
type FilePieceState ¶
type FilePieceState struct { Bytes int64 // Bytes within the piece that are part of this File. PieceState }
type Piece ¶
type Piece struct {
// contains filtered or unexported fields
}
func (*Piece) VerifyData ¶
func (p *Piece) VerifyData()
type PieceState ¶
type PieceState struct { Priority piecePriority // The piece is available in its entirety. Complete bool // The piece is being hashed, or is queued for hash. Checking bool // Some of the piece has been obtained. Partial bool }
The current state of a piece.
type PieceStateChange ¶
type PieceStateChange struct { Index int PieceState }
type PieceStateRun ¶
type PieceStateRun struct { PieceState Length int // How many consecutive pieces have this state. }
Represents a series of consecutive pieces with the same state.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Accesses Torrent data via a Client. Reads block until the data is available. Seeks and readahead also drive Client behaviour.
func (*Reader) ReadContext ¶
func (*Reader) SetReadahead ¶
Configure the number of bytes ahead of a read that should also be prioritized in preparation for further reads.
func (*Reader) SetResponsive ¶
func (r *Reader) SetResponsive()
Don't wait for pieces to complete and be verified. Read calls return as soon as they can when the underlying chunks become available.
type Torrent ¶
type Torrent struct {
// contains filtered or unexported fields
}
Maintains state of torrent within a Client.
func (*Torrent) AddTrackers ¶
func (*Torrent) BytesCompleted ¶
Number of bytes of the entire torrent we have completed.
func (*Torrent) BytesMissing ¶
func (*Torrent) CancelPieces ¶
func (*Torrent) Closed ¶
func (t *Torrent) Closed() <-chan struct{}
Returns a channel that is closed when the Torrent is closed.
func (*Torrent) DownloadAll ¶
func (t *Torrent) DownloadAll()
Marks the entire torrent for download. Requires the info first, see GotInfo.
func (*Torrent) DownloadPieces ¶
func (*Torrent) Drop ¶
func (t *Torrent) Drop()
Drop the torrent from the client, and close it. It's always safe to do this. No data corruption can, or should occur to either the torrent's data, or connected peers.
func (*Torrent) Files ¶
Returns handles to the files in the torrent. This requires the metainfo is available first.
func (*Torrent) GotInfo ¶
func (t *Torrent) GotInfo() <-chan struct{}
Returns a channel that is closed when the info (.Info()) for the torrent has become available.
func (*Torrent) InfoHash ¶
The torrent's infohash. This is fixed and cannot change. It uniquely identifies a torrent.
func (*Torrent) KnownSwarm ¶
KnownSwarm returns the known subset of the peers in the Torrent's swarm, including active, pending, and half-open peers.
func (*Torrent) Length ¶
The completed length of all the torrent data, in all its files. This is derived from the torrent info, when it is available.
func (*Torrent) Metainfo ¶
Returns a run-time generated metainfo for the torrent that includes the info bytes and announce-list as currently known to the client.
func (*Torrent) Name ¶
The current working name for the torrent. Either the name in the info dict, or a display name given such as by the dn value in a magnet link, or "".
func (*Torrent) NewReader ¶
Returns a Reader bound to the torrent's data. All read calls block until the data requested is actually available.
func (*Torrent) NumPieces ¶
The number of pieces in the torrent. This requires that the info has been obtained first.
func (*Torrent) PieceBytesMissing ¶
Get missing bytes count for specific piece.
func (*Torrent) PieceState ¶
func (t *Torrent) PieceState(piece int) PieceState
func (*Torrent) PieceStateRuns ¶
func (t *Torrent) PieceStateRuns() []PieceStateRun
Returns the state of pieces of the torrent. They are grouped into runs of same state. The sum of the state run lengths is the number of pieces in the torrent.
func (*Torrent) Seeding ¶
Returns true if the torrent is currently being seeded. This occurs when the client is willing to upload without wanting anything in return.
func (*Torrent) SetDisplayName ¶
Clobbers the torrent display name. The display name is used as the torrent name if the metainfo is not available.
func (*Torrent) SetInfoBytes ¶
func (*Torrent) SetMaxEstablishedConns ¶
func (*Torrent) Stats ¶
func (t *Torrent) Stats() TorrentStats
func (*Torrent) SubscribePieceStateChanges ¶
func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription
The subscription emits as (int) the index of pieces as their state changes. A state change is when the PieceState for a piece alters in value.
func (*Torrent) VerifyData ¶
func (t *Torrent) VerifyData()
type TorrentSpec ¶
type TorrentSpec struct { // The tiered tracker URIs. Trackers [][]string InfoHash metainfo.Hash InfoBytes []byte // The name to use if the Name field from the Info isn't available. DisplayName string // The chunk size to use for outbound requests. Defaults to 16KiB if not // set. ChunkSize int Storage storage.ClientImpl }
Specifies a new torrent for adding to a client. There are helpers for magnet URIs and torrent metainfo files.
func TorrentSpecFromMagnetURI ¶
func TorrentSpecFromMagnetURI(uri string) (spec *TorrentSpec, err error)
func TorrentSpecFromMetaInfo ¶
func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) (spec *TorrentSpec)
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
magnet-metainfo
Converts magnet URIs and info hashes into torrent metainfo files.
|
Converts magnet URIs and info hashes into torrent metainfo files. |
torrent
Downloads torrents from the command-line.
|
Downloads torrents from the command-line. |
torrent-pick
Downloads torrents from the command-line.
|
Downloads torrents from the command-line. |
torrentfs
Mounts a FUSE filesystem backed by torrents and magnet links.
|
Mounts a FUSE filesystem backed by torrents and magnet links. |
internal
|
|
Package iplist handles the P2P Plaintext Format described by https://en.wikipedia.org/wiki/PeerGuardian#P2P_plaintext_format.
|
Package iplist handles the P2P Plaintext Format described by https://en.wikipedia.org/wiki/PeerGuardian#P2P_plaintext_format. |
cmd/pack-blocklist
Takes P2P blocklist text format in stdin, and outputs the packed format from the iplist package.
|
Takes P2P blocklist text format in stdin, and outputs the packed format from the iplist package. |
Package logonce implements an io.Writer facade that only performs distinct writes.
|
Package logonce implements an io.Writer facade that only performs distinct writes. |
Package storage implements storage backends for package torrent.
|
Package storage implements storage backends for package torrent. |
dirwatch
Package dirwatch provides filesystem-notification based tracking of torrent info files and magnet URIs in a directory.
|
Package dirwatch provides filesystem-notification based tracking of torrent info files and magnet URIs in a directory. |