metainfo

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 16 Imported by: 0

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

View Source
const HashSize = 20

HashSize is the length of a v1 infohash in bytes.

View Source
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) BestPath

func (fi *FileInfo) BestPath() []string

BestPath returns the UTF-8 path when present, otherwise the plain path.

func (*FileInfo) DisplayPath

func (fi *FileInfo) DisplayPath(info *Info) string

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) IsDir

func (ft *FileTree) IsDir() bool

IsDir reports whether the node has child entries.

func (*FileTree) MarshalBencode

func (ft *FileTree) MarshalBencode() ([]byte, error)

MarshalBencode reproduces the wire shape decoded by UnmarshalBencode.

func (*FileTree) NumEntries

func (ft *FileTree) NumEntries() int

NumEntries returns the number of child entries, excluding the file properties key.

func (*FileTree) UnmarshalBencode

func (ft *FileTree) UnmarshalBencode(b []byte) error

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

type Hash [HashSize]byte

Hash is a v1 (SHA-1) infohash.

func HashBytes

func HashBytes(b []byte) Hash

HashBytes returns the SHA-1 digest of b.

func NewHashFromHex

func NewHashFromHex(s string) (Hash, error)

NewHashFromHex decodes a 40-character hex string into a Hash.

func (Hash) Bytes

func (h Hash) Bytes() []byte

Bytes returns the hash as a byte slice.

func (Hash) HexString

func (h Hash) HexString() string

HexString returns the lowercase hex encoding of the hash.

func (Hash) IsZero

func (h Hash) IsZero() bool

IsZero reports whether the hash is all zero bytes.

func (Hash) String

func (h Hash) String() string

String returns the lowercase hex encoding of the 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

func HashV2Bytes(b []byte) HashV2

HashV2Bytes returns the SHA-256 digest of b.

func NewHashV2FromHex added in v1.1.0

func NewHashV2FromHex(s string) (HashV2, error)

NewHashV2FromHex decodes a 64-character hex string into a HashV2.

func (HashV2) Bytes added in v1.1.0

func (h HashV2) Bytes() []byte

Bytes returns the hash as a byte slice.

func (HashV2) HexString added in v1.1.0

func (h HashV2) HexString() string

HexString returns the lowercase hex encoding of the hash.

func (HashV2) IsZero added in v1.1.0

func (h HashV2) IsZero() bool

IsZero reports whether the hash is all zero bytes.

func (HashV2) String added in v1.1.0

func (h HashV2) String() string

String returns the lowercase hex encoding of the hash.

func (HashV2) ToShort added in v1.1.0

func (h HashV2) ToShort() Hash

ToShort truncates the hash to 20 bytes for use in auxiliary interfaces, like DHT and trackers (BEP 52).

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) BestName

func (info *Info) BestName() string

BestName returns the UTF-8 name when present, otherwise the plain name.

func (*Info) HasV1

func (info *Info) HasV1() bool

HasV1 reports whether the info can be used as a v1 info dict.

func (*Info) HasV2

func (info *Info) HasV2() bool

HasV2 reports whether the info is a v2 (BEP 52) info dict.

func (*Info) IsDir

func (info *Info) IsDir() bool

IsDir reports whether the torrent describes a directory of files.

func (*Info) NumPieces

func (info *Info) NumPieces() int

NumPieces returns the number of pieces in the torrent.

func (*Info) TotalLength

func (info *Info) TotalLength() int64

TotalLength returns the sum of all file lengths, using the v2 file tree when present.

func (*Info) UpvertedFiles

func (info *Info) UpvertedFiles() []FileInfo

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

func ParseMagnetUri(uri string) (m Magnet, err error)

ParseMagnetUri parses a magnet-formatted URI into a Magnet. At most one v1 and one v2 infohash may be present; neither is required.

func (Magnet) String added in v1.1.0

func (m Magnet) String() string

String renders the magnet link as a URI.

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

func Load(r io.Reader) (*MetaInfo, error)

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

func LoadFromFile(filename string) (*MetaInfo, error)

LoadFromFile decodes a MetaInfo from the named file.

func (*MetaInfo) HashInfoBytes

func (mi *MetaInfo) HashInfoBytes() Hash

HashInfoBytes returns the v1 infohash of the raw info dict.

func (*MetaInfo) Magnet added in v1.1.0

func (mi *MetaInfo) Magnet() (m Magnet, err error)

Magnet creates a Magnet from mi. It supports v1, hybrid, and v2 torrents.

func (*MetaInfo) UnmarshalInfo

func (mi *MetaInfo) UnmarshalInfo() (Info, error)

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.

func (*MetaInfo) Write

func (mi *MetaInfo) Write(w io.Writer) error

Write encodes mi as bencode to w.

type Node

type Node string

Node is a DHT bootstrap node from BEP 5, as "host:port".

func (*Node) UnmarshalBencode

func (n *Node) UnmarshalBencode(b []byte) error

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

func (u *UrlList) UnmarshalBencode(b []byte) error

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.

Jump to

Keyboard shortcuts

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