Documentation ¶
Overview ¶
Package torrent implements a torrent client
- simple api.
- performant.
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 (CustomNetworkProtocols) ¶
package main import ( "context" "io/ioutil" "log" "net" "github.com/anacrolix/utp" "github.com/james-lawrence/torrent" "github.com/james-lawrence/torrent/sockets" ) func main() { var ( err error metadata torrent.Metadata ) l, err := utp.NewSocket("udp", "0.0.0.0:0") if err != nil { log.Fatalln(err) return } defer l.Close() s := sockets.New(l, &net.Dialer{LocalAddr: l.Addr()}) c, _ := torrent.NewSocketsBind(s).Bind(torrent.NewClient(torrent.NewDefaultClientConfig())) defer c.Close() if metadata, err = torrent.NewFromMagnet("magnet:?xt=urn:btih:ZOCMZQIPFFW7OLLMIC5HUB6BPCSDEOQU"); err != nil { log.Fatalln(err) return } if err = c.DownloadInto(context.Background(), metadata, ioutil.Discard); err != nil { log.Fatalln(err) return } log.Print("torrent downloaded") }
Output:
Example (Download) ¶
package main import ( "context" "io/ioutil" "log" "github.com/james-lawrence/torrent" "github.com/james-lawrence/torrent/autobind" ) func main() { var ( err error metadata torrent.Metadata ) c, _ := autobind.NewDefaultClient() defer c.Close() if metadata, err = torrent.NewFromMagnet("magnet:?xt=urn:btih:ZOCMZQIPFFW7OLLMIC5HUB6BPCSDEOQU"); err != nil { log.Fatalln(err) return } if err = c.DownloadInto(context.Background(), metadata, ioutil.Discard); err != nil { log.Fatalln(err) return } log.Print("torrent downloaded") }
Output:
Example (FileReader) ¶
package main import ( "github.com/james-lawrence/torrent" ) func main() { var f torrent.File // Accesses the parts of the torrent pertaining to f. Data will be // downloaded as required, per the configuration of the torrent.Reader. r := f.NewReader() defer r.Close() }
Output:
Index ¶
- Constants
- func NewMagnet(md Metadata) metainfo.Magnet
- type Binder
- type Client
- func (cl *Client) AddDHTNodes(nodes []string)
- func (cl *Client) Bind(s socket) (err error)
- func (cl *Client) Close()
- func (cl *Client) Closed() <-chan struct{}
- func (cl *Client) Config() *ClientConfig
- func (cl *Client) DhtServers() []*dht.Server
- func (cl *Client) Download(ctx context.Context, m Metadata, options ...Tuner) (r Reader, err error)
- func (cl *Client) DownloadInto(ctx context.Context, m Metadata, dst io.Writer, options ...Tuner) (err error)
- func (cl *Client) ListenAddrs() (ret []net.Addr)
- func (cl *Client) LocalPort() (port int)
- func (cl *Client) MaybeStart(t Metadata, failed error) (dl Torrent, added bool, err error)
- func (cl *Client) PeerID() PeerID
- func (cl *Client) Start(t Metadata) (dl Torrent, added bool, err error)
- func (cl *Client) Stop(t Metadata) (err error)
- func (cl *Client) String() string
- 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 ClientConfig
- type ClientConfigOption
- type ConnStats
- type File
- func (f *File) BytesCompleted() int64
- func (f *File) DisplayPath() string
- func (f *File) Download()
- func (f File) FileInfo() metainfo.FileInfo
- func (f *File) Length() int64
- func (f *File) NewReader() Reader
- func (f *File) Offset() int64
- func (f File) Path() string
- func (f *File) Priority() piecePriority
- func (f *File) SetPriority(prio piecePriority)
- func (f *File) State() (ret []FilePieceState)
- func (f *File) Torrent() Torrent
- type FilePieceState
- type Handle
- type HeaderObfuscationPolicy
- type IpPort
- type Metadata
- func New(info metainfo.Hash, options ...Option) (t Metadata, err error)
- func NewFromFile(path string, options ...Option) (t Metadata, err error)
- func NewFromInfo(i metainfo.Info, options ...Option) (t Metadata, err error)
- func NewFromMagnet(uri string) (t Metadata, err error)
- func NewFromMetaInfo(mi *metainfo.MetaInfo, options ...Option) (t Metadata, err error)
- func NewFromMetaInfoFile(path string, options ...Option) (t Metadata, err error)
- type Option
- type Peer
- type PeerExtensionBits
- type PeerID
- type Peers
- type Piece
- type PieceState
- type PieceStateChange
- type PieceStateRun
- type Reader
- type Torrent
- type TorrentStats
- type Tuner
Examples ¶
Constants ¶
const ( PiecePriorityNone piecePriority = iota // Not wanted. Must be the zero value. PiecePriorityNormal // Wanted. PiecePriorityHigh // Wanted a lot. 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. Highest urgency. )
const DefaultHTTPUserAgent = "Go-Torrent/1.0"
DefaultHTTPUserAgent ...
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Binder ¶
type Binder interface { // Bind to the given client if err is nil. Bind(cl *Client, err error) (*Client, error) }
Binder binds network sockets to the client.
func NewSocketsBind ¶
func NewSocketsBind(s ...socket) Binder
NewSocketsBind binds a set of sockets to the client. it bypasses any disable checks (tcp,udp, ip4/6) from the configuration.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client contain zero or more Torrents. A Client manages a blocklist, the TCP/UDP protocol ports, and DHT as desired.
func NewClient ¶
func NewClient(cfg *ClientConfig) (_ *Client, err error)
NewClient create a new client from the provided config. nil is acceptable.
func (*Client) AddDHTNodes ¶
AddDHTNodes adds nodes to the DHT servers.
func (*Client) Close ¶
func (cl *Client) Close()
Close stops the client. All connections to peers are closed and all activity will come to a halt.
func (*Client) Closed ¶
func (cl *Client) Closed() <-chan struct{}
Closed returns a channel to detect when the client is closed.
func (*Client) Config ¶
func (cl *Client) Config() *ClientConfig
Config underlying configuration for the client.
func (*Client) DhtServers ¶
func (cl *Client) DhtServers() []*dht.Server
DhtServers returns the set of DHT servers.
func (*Client) Download ¶
Download the specified torrent. Download differs from Start in that it blocks until the metadata is downloaded and returns a valid reader for use.
func (*Client) DownloadInto ¶
func (cl *Client) DownloadInto(ctx context.Context, m Metadata, dst io.Writer, options ...Tuner) (err error)
DownloadInto downloads the torrent into the provided destination. see Download for more information. TODO: context timeout only applies to fetching metadata, not the entire download.
func (*Client) ListenAddrs ¶
ListenAddrs addresses currently being listened to.
func (*Client) LocalPort ¶
LocalPort returns the local port being listened on. WARNING: this method can panic. this is method is odd given a client can be attached to multiple ports on different listeners.
func (*Client) MaybeStart ¶
MaybeStart is a convience method that consumes the return types of the torrent creation methods: New, NewFromFile, etc. it is all respects identical to the Start method.
func (*Client) Start ¶
Start the specified torrent. Start adds starts up the torrent within the client downloading the missing pieces as needed. if you want to wait until the torrent is completed use Download.
func (*Client) Stop ¶
Stop the specified torrent, this halts all network activity around the torrent for this client.
func (*Client) Torrent ¶
Torrent returns a handle to the given torrent, if it's present in the client.
func (*Client) WaitAll ¶
WaitAll returns true when all torrents are completely downloaded and false if the client is stopped before that.
func (*Client) WriteStatus ¶
WriteStatus writes out a human readable status of the client, such as for writing to a HTTP status page.
type ClientConfig ¶
type ClientConfig struct { // Store torrent file data in this directory unless .DefaultStorage is // specified. DataDir string `long:"data-dir" description:"directory to store downloaded torrent data"` NoDefaultPortForwarding bool UpnpID string // 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"` DhtStartingNodes dht.StartingNodesGetter // 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 only upload to encourage the peer to reciprocate. Seed bool `long:"seed"` // Only applies to chunks uploaded to peers, to maintain responsiveness // communicating local Client state to peers. Each limiter token // represents one byte. The Limiter's burst must be large enough to fit a // whole chunk, which is usually 16 KiB (see TorrentSpec.ChunkSize). UploadRateLimiter *rate.Limiter // Rate limits all reads from connections to peers. Each limiter token // represents one byte. The Limiter's burst must be bigger than the // largest Read performed on a the underlying rate-limiting io.Reader // minus one. This is likely to be the larger of the main read loop buffer // (~4096), and the requested chunk size (~16KiB, see // TorrentSpec.ChunkSize). DownloadRateLimiter *rate.Limiter // User-provided Client peer ID. If not present, one is generated automatically. PeerID string // 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 HeaderObfuscationPolicy HeaderObfuscationPolicy // The crypto methods to offer when initiating connections with header obfuscation. CryptoProvides mse.CryptoMethod // Chooses the crypto method to use when receiving connections with header obfuscation. CryptoSelector mse.CryptoSelector // Sets usage of Socks5 Proxy. Authentication should be included in the url if needed. // Examples: socks5://demo:demo@192.168.99.100:1080 // http://proxy.domain.com:3128 ProxyURL string DisableIPv4Peers bool Logger logger // standard logging for errors, defaults to stderr Warn logger // warn logging Debug logger // debug logging, defaults to discard // HTTPProxy defines proxy for HTTP requests. // Format: func(*Request) (*url.URL, error), // or result of http.ProxyURL(HTTPProxy). // By default, it is composed from ClientConfig.ProxyURL, // if not set explicitly in ClientConfig struct HTTPProxy func(*http.Request) (*url.URL, error) // HTTPUserAgent changes default UserAgent for HTTP requests HTTPUserAgent string // 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 // 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 // Peer dial timeout to use when there are limited peers. NominalDialTimeout time.Duration // Minimum peer dial timeout to use (even if we have lots of peers). MinDialTimeout time.Duration EstablishedConnsPerTorrent int HalfOpenConnsPerTorrent int // Maximum number of peer addresses in reserve. TorrentPeersHighWater int // Minumum number of peers before effort is made to obtain more peers. TorrentPeersLowWater int // 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 // The IP addresses as our peers should see them. May differ from the // local interfaces due to NAT or other network configurations. PublicIP4 net.IP PublicIP6 net.IP DisableAcceptRateLimiting bool ConnTracker *conntrack.Instance connections.Handshaker // OnQuery hook func DHTOnQuery func(query *krpc.Msg, source net.Addr) (propagate bool) DHTAnnouncePeer func(ih metainfo.Hash, ip net.IP, port int, portOk bool) // contains filtered or unexported fields }
ClientConfig not safe to modify this after it's given to a Client.
func NewDefaultClientConfig ¶
func NewDefaultClientConfig(options ...ClientConfigOption) *ClientConfig
NewDefaultClientConfig default client configuration.
type ClientConfigOption ¶
type ClientConfigOption func(*ClientConfig)
ClientConfigOption options for the client configuration
func ClientConfigInfoLogger ¶
func ClientConfigInfoLogger(l *log.Logger) ClientConfigOption
ClientConfigInfoLogger set the info logger
func ClientConfigSeed ¶
func ClientConfigSeed(b bool) ClientConfigOption
ClientConfigSeed enable/disable seeding
type ConnStats ¶
type ConnStats struct { // Total bytes on the wire. Includes handshakes and encryption. BytesWritten count BytesWrittenData count BytesRead count BytesReadData count BytesReadUsefulData count ChunksWritten count ChunksRead count ChunksReadUseful count ChunksReadWasted count MetadataChunksRead count // Number of pieces data was written to, that subsequently passed verification. PiecesDirtiedGood count // Number of pieces data was written to, that subsequently failed // verification. Note that a connection may not have been the sole dirtier // of a piece. PiecesDirtiedBad count }
ConnStats various connection-level metrics. At the Torrent level these are aggregates. Chunks are messages with data payloads. Data is actual torrent content without any overhead. Useful is something we needed locally. Unwanted is something we didn't ask for (but may still be useful). Written is things sent to the peer, and Read is stuff received from them.
type File ¶
type File struct {
// contains filtered or unexported fields
}
File provides access to regions of torrent data that correspond to its files.
func (*File) BytesCompleted ¶
BytesCompleted number of bytes of the entire file we have completed. This is the sum of completed pieces, and dirtied chunks of incomplete pieces.
func (*File) DisplayPath ¶
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()
Download requests that all pieces containing data in the file be downloaded.
func (*File) SetPriority ¶
func (f *File) SetPriority(prio piecePriority)
SetPriority the minimum priority for pieces in the File.
type FilePieceState ¶
type FilePieceState struct { Bytes int64 // Bytes within the piece that are part of this File. PieceState }
FilePieceState the download status of a piece that comprises part of a File.
type HeaderObfuscationPolicy ¶
type HeaderObfuscationPolicy struct { RequirePreferred bool // Whether the value of Preferred is a strict requirement. Preferred bool // Whether header obfuscation is preferred. }
HeaderObfuscationPolicy ...
type Metadata ¶
type Metadata 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 Webseeds []string DHTNodes []string // The chunk size to use for outbound requests. Defaults to 16KiB if not // set. ChunkSize int Storage storage.ClientImpl }
Metadata specifies the metadata of a torrent for adding to a client. There are helpers for magnet URIs and torrent metainfo files.
func NewFromFile ¶
NewFromFile convience method to create a torrent directly from a file.
func NewFromInfo ¶
NewFromInfo creates a torrent from metainfo.Info
func NewFromMagnet ¶
NewFromMagnet creates a torrent from a magnet uri.
func NewFromMetaInfo ¶
NewFromMetaInfo create a torrent from metainfo
func NewFromMetaInfoFile ¶
NewFromMetaInfoFile loads torrent metadata stored in a file.
type Option ¶
type Option func(*Metadata)
Option for the torrent.
func OptionChunk ¶
OptionChunk sets the size of the chunks to use for outbound requests
func OptionDisplayName ¶
OptionDisplayName set the display name for the torrent.
func OptionNodes ¶
OptionNodes supplimental nodes to add to the dht of the client.
func OptionStorage ¶
func OptionStorage(s storage.ClientImpl) Option
OptionStorage set the storage implementation for the torrent.
func OptionTrackers ¶
OptionTrackers set the trackers for the torrent.
func OptionWebseeds ¶
OptionWebseeds set the webseed hosts for the torrent.
type Peer ¶
type Peer struct { ID [20]byte IP net.IP Port int Source peerSource // Peer is known to support encryption. SupportsEncryption bool btprotocol.PexPeerFlags // Whether we can ignore poor or bad behaviour from the peer. Trusted bool }
Peer connection info, handed about publicly.
func (*Peer) FromPex ¶
func (me *Peer) FromPex(na krpc.NodeAddr, fs btprotocol.PexPeerFlags)
FromPex generate Peer from peer exchange
type PeerExtensionBits ¶
type PeerExtensionBits = pp.ExtensionBits
PeerExtensionBits define what extensions are available.
type Peers ¶
type Peers []Peer
func (*Peers) AppendFromPex ¶
func (me *Peers) AppendFromPex(nas []krpc.NodeAddr, fs []btprotocol.PexPeerFlags)
type Piece ¶
type Piece struct {
// contains filtered or unexported fields
}
func (*Piece) SetPriority ¶
func (p *Piece) SetPriority(prio piecePriority)
func (*Piece) VerifyData ¶
func (p *Piece) VerifyData()
VerifyData forces the piece data to be rehashed.
type PieceState ¶
type PieceState struct { Priority piecePriority storage.Completion // 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 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 interface { io.Reader io.Seeker io.Closer missinggo.ReadContexter SetReadahead(int64) SetResponsive() }
Reader for a torrent
type Torrent ¶
type Torrent interface { Metadata() Metadata Tune(...Tuner) error Name() string // TODO: remove, should be pulled from Metadata() Metainfo() metainfo.MetaInfo // TODO: remove, should be pulled from Metadata() BytesCompleted() int64 // TODO: maybe should be pulled from torrent, it has a reference to the storage implementation. or maybe part of the Stats call? VerifyData() // TODO: maybe should be pulled from torrent, it has a reference to the storage implementation. NewReader() Reader // TODO: maybe should be pulled from torrent, it has a reference to the storage implementation. Stats() TorrentStats // TODO: rename TorrentStats, it stutters. Info() *metainfo.Info // TODO: remove, this should be pulled from Metadata() GotInfo() <-chan struct{} // TODO: remove, torrents should never be returned in they don't have the meta info. DownloadAll() // TODO: rethink this. does it even need to exist or can it be rolled up into Start/Download. Files() []*File // TODO: maybe should be pulled from Metadata(), it has a reference to the storage implementation. SubscribePieceStateChanges() *pubsub.Subscription PieceStateRuns() []PieceStateRun }
Torrent represents the state of a torrent within a client. interface is currently being used to ease the transition of to a cleaner API. Many methods should not be called before the info is available, see .Info and .GotInfo.
type TorrentStats ¶
type TorrentStats struct { // Aggregates stats over all connections past and present. Some values may // not have much meaning in the aggregate context. ConnStats // metrics marking the progress of the torrent // these are in chunks. Missing int Outstanding int Unverified int Completed int // Ordered by expected descending quantities (if all is well). MaximumAllowedPeers int TotalPeers int PendingPeers int ActivePeers int ConnectedSeeders int HalfOpenPeers int Seeding bool }
TorrentStats high level stats about the torrent.
type Tuner ¶
type Tuner func(*torrent)
Tuner runtime tuning of an actively running torrent.
func TuneClientPeer ¶
TuneClientPeer adds a trusted, pending peer for each of the Client's addresses. used for tests.
func TuneMaxConnections ¶
TuneMaxConnections adjust the maximum connections allowed for a torrent.
Source Files ¶
- Peer.go
- Peers.go
- autobind.go
- bep40.go
- bitqueue.go
- chunks.go
- client.go
- closewrapper.go
- config.go
- conn_stats.go
- connection.go
- deadlineio.go
- digests.go
- doc.go
- errors.go
- file.go
- global.go
- logging.go
- metadata.go
- misc.go
- multiless.go
- peer_pool.go
- peerid.go
- pex.go
- piece.go
- piecestate.go
- portfwd.go
- protocol.go
- ratelimitreader.go
- reader.go
- socket.go
- torrent.go
- torrent_stats.go
- tracker_scraper.go
- worst_conns.go
Directories ¶
Path | Synopsis |
---|---|
Package autobind for automically binding on the local server this package is only for convience and it's suggested to use torrent.NewSocketsBind instead.
|
Package autobind for automically binding on the local server this package is only for convience and it's suggested to use torrent.NewSocketsBind instead. |
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. |
dht
|
|
v2
Package dht implements a Distributed Hash Table (DHT) part of the BitTorrent protocol, as specified by BEP 5: http://www.bittorrent.org/beps/bep_0005.html
|
Package dht implements a Distributed Hash Table (DHT) part of the BitTorrent protocol, as specified by BEP 5: http://www.bittorrent.org/beps/bep_0005.html |
internal
|
|
testutil
Package testutil contains stuff for testing torrent-related behaviour.
|
Package testutil contains stuff for testing torrent-related behaviour. |
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 sockets implements examples of how to write sockets for the torrent client.
|
Package sockets implements examples of how to write sockets for the torrent client. |
Package storage implements storage backends for package torrent.
|
Package storage implements storage backends for package torrent. |
util
|
|
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. |
x
|
|
langx
Package langx provides small utility functions to extend the standard golang language.
|
Package langx provides small utility functions to extend the standard golang language. |