Documentation
¶
Overview ¶
Package metainfo parses .torrent files (BEP 3, BEP 12, BEP 19, BEP 52), tolerating the malformed optional fields that real-world trackers serve.
Index ¶
Constants ¶
const HashSize = 20
HashSize is the length of a v1 infohash in bytes.
const HashV2Size = 32
HashV2Size is the length of a v2 infohash in bytes.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AnnounceList ¶
type AnnounceList [][]string
AnnounceList is the tiers of trackers from BEP 12.
func (AnnounceList) Clone ¶
func (al AnnounceList) Clone() AnnounceList
Clone returns a copy of the tier list; the tier slices themselves are shared.
func (AnnounceList) DistinctValues ¶
func (al AnnounceList) DistinctValues() []string
DistinctValues returns the tracker URLs across all tiers, deduplicated in order of first appearance.
func (AnnounceList) OverridesAnnounce ¶
func (al AnnounceList) OverridesAnnounce(announce string) bool
OverridesAnnounce reports whether al should be preferred over the single announce URL.
func (*AnnounceList) UnmarshalBencode ¶
func (al *AnnounceList) UnmarshalBencode(b []byte) error
UnmarshalBencode is deliberately lenient because trackers serve malformed announce-lists in the wild (FunFile encodes an empty one as the bencode string "0:"). A non-empty string decodes as a single tier, a string element in the list becomes its own tier, non-string junk inside a tier is skipped, tiers left empty are dropped, and any other value type decodes as nil. Only malformed bencode is an error.
type ExtendedFileAttrs ¶
type ExtendedFileAttrs struct {
Attr string `bencode:"attr,omitempty"`
SymlinkPath []string `bencode:"symlink path,omitempty"`
Sha1 string `bencode:"sha1,omitempty"`
}
ExtendedFileAttrs are the optional file attributes from BEP 47.
type FileInfo ¶
type FileInfo struct {
Length int64 `bencode:"length"`
Path []string `bencode:"path"`
PathUtf8 []string `bencode:"path.utf-8,omitempty"`
ExtendedFileAttrs
TorrentOffset int64 `bencode:"-"`
}
FileInfo is one file in a multi-file torrent (BEP 3).
func (*FileInfo) DisplayPath ¶
DisplayPath returns the file's path within a directory torrent, or the torrent name for a single-file torrent.
type FileTree ¶
type FileTree struct {
File FileTreeFile
Dir map[string]FileTree
}
FileTree is the BEP 52 v2 file tree.
func (*FileTree) MarshalBencode ¶
MarshalBencode reproduces the wire shape decoded by UnmarshalBencode.
func (*FileTree) NumEntries ¶
NumEntries returns the number of child entries, excluding the file properties key.
func (*FileTree) UnmarshalBencode ¶
UnmarshalBencode decodes the wire shape: a dict whose empty key holds the file properties for a leaf and whose other keys are child names. The whole tree is decoded in a single pass; recursing with a fresh Unmarshal per level would retain a raw copy of every subtree once per ancestor, letting a small crafted torrent allocate hundreds of times its size.
type FileTreeFile ¶
type FileTreeFile struct {
Length int64 `bencode:"length"`
PiecesRoot string `bencode:"pieces root"`
}
FileTreeFile holds the properties of a leaf file in a FileTree.
type Hash ¶
Hash is a v1 (SHA-1) infohash.
func NewHashFromHex ¶
NewHashFromHex decodes a 40-character hex string into a Hash.
type HashV2 ¶ added in v1.1.0
type HashV2 [HashV2Size]byte
HashV2 is a v2 (SHA-256) infohash (BEP 52).
func HashV2Bytes ¶ added in v1.1.0
HashV2Bytes returns the SHA-256 digest of b.
func NewHashV2FromHex ¶ added in v1.1.0
NewHashV2FromHex decodes a 64-character hex string into a HashV2.
type Info ¶
type Info struct {
PieceLength int64 `bencode:"piece length"`
Pieces []byte `bencode:"pieces,omitempty"`
Name string `bencode:"name"`
NameUtf8 string `bencode:"name.utf-8,omitempty"`
Length int64 `bencode:"length,omitempty"`
ExtendedFileAttrs
Private *bool `bencode:"private,omitempty,ignore_unmarshal_type_error"`
Source string `bencode:"source,omitempty"`
Files []FileInfo `bencode:"files,omitempty"`
MetaVersion int64 `bencode:"meta version,omitempty"`
FileTree FileTree `bencode:"file tree,omitempty"`
}
Info is the torrent info dictionary (BEP 3, BEP 52).
func (*Info) TotalLength ¶
TotalLength returns the sum of all file lengths, using the v2 file tree when present.
func (*Info) UpvertedFiles ¶
UpvertedFiles returns the torrent's files in a common form: the v2 file tree when present, otherwise the v1 files list, with a single-file torrent converted to one entry with a nil Path.
type Magnet ¶ added in v1.1.0
type Magnet struct {
InfoHash Hash // v1 infohash; zero when absent
InfoHashV2 HashV2 // v2 infohash; zero when absent
Trackers []string // "tr" values
DisplayName string // "dn" value, if not empty
Params url.Values // All other values, such as "x.pe", "as", "xs" etc.
}
Magnet is the components of a magnet link (BEP 9). It supports v1, hybrid, and v2 (BEP 52) links.
func ParseMagnetUri ¶ added in v1.1.0
ParseMagnetUri parses a magnet-formatted URI into a Magnet. At most one v1 and one v2 infohash may be present; neither is required.
type MetaInfo ¶
type MetaInfo struct {
InfoBytes bencode.Bytes `bencode:"info,omitempty"`
Announce string `bencode:"announce,omitempty"`
AnnounceList AnnounceList `bencode:"announce-list,omitempty"`
Nodes []Node `bencode:"nodes,omitempty,ignore_unmarshal_type_error"`
CreationDate int64 `bencode:"creation date,omitempty,ignore_unmarshal_type_error"`
Comment string `bencode:"comment,omitempty,ignore_unmarshal_type_error"`
CreatedBy string `bencode:"created by,omitempty,ignore_unmarshal_type_error"`
Encoding string `bencode:"encoding,omitempty,ignore_unmarshal_type_error"`
UrlList UrlList `bencode:"url-list,omitempty"`
PieceLayers map[string]string `bencode:"piece layers,omitempty,ignore_unmarshal_type_error"`
}
MetaInfo is a decoded .torrent file (BEP 3).
func Load ¶
Load decodes a single bencode value from r as a MetaInfo. Trailing ASCII whitespace and NUL bytes are tolerated, a deviation from anacrolix/torrent: some tracker webservers append a newline after the torrent payload.
func LoadFromFile ¶
LoadFromFile decodes a MetaInfo from the named file.
func (*MetaInfo) HashInfoBytes ¶
HashInfoBytes returns the v1 infohash of the raw info dict.
func (*MetaInfo) Magnet ¶ added in v1.1.0
Magnet creates a Magnet from mi. It supports v1, hybrid, and v2 torrents.
func (*MetaInfo) UnmarshalInfo ¶
UnmarshalInfo decodes the raw info dict.
func (*MetaInfo) UpvertedAnnounceList ¶
func (mi *MetaInfo) UpvertedAnnounceList() AnnounceList
UpvertedAnnounceList returns the announce-list, converted from the single announce field when the list does not override it.
type Node ¶
type Node string
Node is a DHT bootstrap node from BEP 5, as "host:port".
func (*Node) UnmarshalBencode ¶
UnmarshalBencode accepts either a "host:port" string or a [host, port] pair. Any other shape returns a bencode.UnmarshalTypeError so that fields tagged ignore_unmarshal_type_error can drop it.
type UrlList ¶
type UrlList []string
UrlList is the list of web seed URLs from BEP 19.
func (*UrlList) UnmarshalBencode ¶
UnmarshalBencode accepts either a single URL string or a list, with the same leniency as AnnounceList: non-string junk inside a list is skipped and any other value type decodes as nil.