Documentation ¶
Overview ¶
Package metainfo is used to encode or decode the metainfo of the torrent file and the MagNet link.
Index ¶
- Constants
- type AnnounceList
- type Bytes
- type CompactAddr
- func (a CompactAddr) Equal(o CompactAddr) bool
- func (a CompactAddr) MarshalBencode() (b []byte, err error)
- func (a CompactAddr) MarshalBinary() (data []byte, err error)
- func (a CompactAddr) Network() string
- func (a CompactAddr) String() string
- func (a CompactAddr) UDPAddr() *net.UDPAddr
- func (a *CompactAddr) UnmarshalBencode(b []byte) (err error)
- func (a *CompactAddr) UnmarshalBinary(data []byte) error
- func (a CompactAddr) Valid() bool
- func (a CompactAddr) WriteBinary(w io.Writer) (n int, err error)
- type CompactIPv4Addrs
- type CompactIPv6Addrs
- type File
- type FilePiece
- type FilePieces
- type Hash
- func (h Hash) Bytes() []byte
- func (h Hash) BytesString() string
- func (h Hash) Compare(o Hash) int
- func (h *Hash) FromHexString(s string) (err error)
- func (h *Hash) FromString(s string) (err error)
- func (h Hash) HexString() string
- func (h Hash) IsZero() bool
- func (h Hash) MarshalBencode() (b []byte, err error)
- func (h Hash) MarshalBinary() (data []byte, err error)
- func (h Hash) String() string
- func (h *Hash) UnmarshalBencode(b []byte) (err error)
- func (h *Hash) UnmarshalBinary(b []byte) (err error)
- func (h Hash) WriteBinary(w io.Writer) (m int, err error)
- func (h Hash) Xor(o Hash) (ret Hash)
- type Hashes
- type HostAddr
- type Info
- func (info Info) AllFiles() []File
- func (info Info) CountPieces() int
- func (info Info) GetFileByOffset(offset int64) (file File, fileOffset int64)
- func (info Info) IsDir() bool
- func (info Info) Piece(index int) Piece
- func (info Info) PieceOffset(index, offset uint32) int64
- func (info Info) TotalLength() (ret int64)
- type Magnet
- type MetaInfo
- type Piece
- type PieceBlock
- type PieceBlocks
- type Reader
- type URLList
- type Writer
Constants ¶
const ( PieceSize256KB = 1024 * 256 PieceSize512KB = 2 * PieceSize256KB PieceSize1MB = 2 * PieceSize512KB PieceSize2MB = 2 * PieceSize1MB PieceSize4MB = 2 * PieceSize2MB )
Predefine some sizes of the pieces.
const BlockSize = 16 * 1024 // 2^14 = 16KB
BlockSize is the default size of a piece block.
const HashSize = 20
HashSize is the size of the InfoHash.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AnnounceList ¶
type AnnounceList [][]string
AnnounceList is a list of the announces.
func (AnnounceList) Unique ¶
func (al AnnounceList) Unique() (announces []string)
Unique returns the list of the unique announces.
type CompactAddr ¶
CompactAddr represents an address based on ip and port, which implements "Compact IP-address/port info".
See http://bittorrent.org/beps/bep_0005.html.
func NewCompactAddr ¶
func NewCompactAddr(ip net.IP, port uint16) CompactAddr
NewCompactAddr returns a new compact Addr with ip and port.
func NewCompactAddrFromUDPAddr ¶
func NewCompactAddrFromUDPAddr(ua *net.UDPAddr) CompactAddr
NewCompactAddrFromUDPAddr converts *net.UDPAddr to a new CompactAddr.
func (CompactAddr) Equal ¶
func (a CompactAddr) Equal(o CompactAddr) bool
Equal reports whether a is equal to o.
func (CompactAddr) MarshalBencode ¶
func (a CompactAddr) MarshalBencode() (b []byte, err error)
MarshalBencode implements the interface bencode.Marshaler.
func (CompactAddr) MarshalBinary ¶
func (a CompactAddr) MarshalBinary() (data []byte, err error)
MarshalBinary implements the interface encoding.BinaryMarshaler,
func (CompactAddr) Network ¶
func (a CompactAddr) Network() string
Network implements the interface net.Addr#Network.
func (CompactAddr) String ¶
func (a CompactAddr) String() string
func (CompactAddr) UDPAddr ¶
func (a CompactAddr) UDPAddr() *net.UDPAddr
UDPAddr converts itself to *net.Addr.
func (*CompactAddr) UnmarshalBencode ¶
func (a *CompactAddr) UnmarshalBencode(b []byte) (err error)
UnmarshalBencode implements the interface bencode.Unmarshaler.
func (*CompactAddr) UnmarshalBinary ¶
func (a *CompactAddr) UnmarshalBinary(data []byte) error
UnmarshalBinary implements the interface encoding.BinaryUnmarshaler.
func (CompactAddr) Valid ¶
func (a CompactAddr) Valid() bool
Valid reports whether the addr is valid.
func (CompactAddr) WriteBinary ¶
func (a CompactAddr) WriteBinary(w io.Writer) (n int, err error)
WriteBinary is the same as MarshalBinary, but writes the result into w instead of returning.
type CompactIPv4Addrs ¶
type CompactIPv4Addrs []CompactAddr
CompactIPv4Addrs is a set of IPv4 Addrs.
func (CompactIPv4Addrs) MarshalBencode ¶
func (cas CompactIPv4Addrs) MarshalBencode() (b []byte, err error)
MarshalBencode implements the interface bencode.Marshaler.
func (CompactIPv4Addrs) MarshalBinary ¶
func (cas CompactIPv4Addrs) MarshalBinary() ([]byte, error)
MarshalBinary implements the interface encoding.BinaryMarshaler.
func (*CompactIPv4Addrs) UnmarshalBencode ¶
func (cas *CompactIPv4Addrs) UnmarshalBencode(b []byte) (err error)
UnmarshalBencode implements the interface bencode.Unmarshaler.
func (*CompactIPv4Addrs) UnmarshalBinary ¶
func (cas *CompactIPv4Addrs) UnmarshalBinary(b []byte) (err error)
UnmarshalBinary implements the interface encoding.BinaryUnmarshaler.
type CompactIPv6Addrs ¶
type CompactIPv6Addrs []CompactAddr
CompactIPv6Addrs is a set of IPv6 Addrs.
func (CompactIPv6Addrs) MarshalBencode ¶
func (cas CompactIPv6Addrs) MarshalBencode() (b []byte, err error)
MarshalBencode implements the interface bencode.Marshaler.
func (CompactIPv6Addrs) MarshalBinary ¶
func (cas CompactIPv6Addrs) MarshalBinary() ([]byte, error)
MarshalBinary implements the interface encoding.BinaryMarshaler.
func (*CompactIPv6Addrs) UnmarshalBencode ¶
func (cas *CompactIPv6Addrs) UnmarshalBencode(b []byte) (err error)
UnmarshalBencode implements the interface bencode.Unmarshaler.
func (*CompactIPv6Addrs) UnmarshalBinary ¶
func (cas *CompactIPv6Addrs) UnmarshalBinary(b []byte) (err error)
UnmarshalBinary implements the interface encoding.BinaryUnmarshaler.
type File ¶
type File struct { // Length is the length of the file in bytes. Length int64 `json:"length" bencode:"length"` // BEP 3 // Paths is a list containing one or more string elements that together // represent the path and filename. Each element in the list corresponds // to either a directory name or (in the case of the final element) the // filename. // // For example, a the file "dir1/dir2/file.ext" would consist of three // string elements: "dir1", "dir2", and "file.ext". This is encoded as // a bencoded list of strings such as l4:dir14:dir28:file.exte. Paths []string `json:"path" bencode:"path"` // BEP 3 }
File represents a file in the multi-file case.
func (File) FilePieces ¶
func (f File) FilePieces(info Info) (fps FilePieces)
FilePieces returns the information of the pieces referred by the file.
func (File) PathWithPrefix ¶
PathWithPrefix returns the path of the current with the prefix directory.
type FilePiece ¶
type FilePiece struct { // The index of the current piece. Index uint32 // The offset bytes from the beginning of the current piece, // which is equal to 0 in general. Offset uint32 // The length of the data, which is equal to PieceLength in Info in general. // For most implementations, PieceLength is equal to 2^18. // So, a piece can contain sixteen blocks. Length uint32 }
FilePiece represents the piece range used by a file, which is used to calculate the downloaded piece when downloading the file.
func (FilePiece) Blocks ¶
func (fp FilePiece) Blocks() PieceBlocks
Blocks returns the lists of the blocks of the piece.
func (FilePiece) TotalOffset ¶
TotalOffset return the total offset from the beginning of all the files.
type FilePieces ¶
type FilePieces []FilePiece
FilePieces is a set of FilePieces.
func (FilePieces) Len ¶
func (fps FilePieces) Len() int
func (FilePieces) Less ¶
func (fps FilePieces) Less(i, j int) bool
func (FilePieces) Merge ¶
func (fps FilePieces) Merge() FilePieces
Merge merges the contiguous pieces to the one piece.
func (FilePieces) Swap ¶
func (fps FilePieces) Swap(i, j int)
type Hash ¶
Hash is the 20-byte SHA1 hash used for info and pieces.
func NewHashFromBytes ¶
NewHashFromBytes returns a new Hash from a byte slice.
func NewHashFromHexString ¶
NewHashFromHexString returns a new Hash from a hex string.
func NewHashFromString ¶
NewHashFromString returns a new Hash from a string.
func (Hash) BytesString ¶
BytesString returns the bytes string, that's, string(h[:]).
func (*Hash) FromHexString ¶
FromHexString resets the info hash from the hex string.
func (*Hash) FromString ¶
FromString resets the info hash from the string.
func (Hash) MarshalBencode ¶
MarshalBencode implements the interface bencode.Marshaler.
func (Hash) MarshalBinary ¶
MarshalBinary implements the interface binary.BinaryMarshaler.
func (*Hash) UnmarshalBencode ¶
UnmarshalBencode implements the interface bencode.Unmarshaler.
func (*Hash) UnmarshalBinary ¶
UnmarshalBinary implements the interface binary.BinaryUnmarshaler.
func (Hash) WriteBinary ¶
WriteBinary is the same as MarshalBinary, but writes the result into w instead of returning.
type Hashes ¶
type Hashes []Hash
Hashes is a set of Hashes.
func GeneratePieces ¶
GeneratePieces generates the pieces from the reader.
func GeneratePiecesFromFiles ¶
func GeneratePiecesFromFiles(files []File, pieceLength int64, open func(File) (io.ReadCloser, error)) (Hashes, error)
GeneratePiecesFromFiles generates the pieces from the files.
func (Hashes) MarshalBencode ¶
MarshalBencode implements the interface bencode.Marshaler.
func (*Hashes) UnmarshalBencode ¶
UnmarshalBencode implements the interface bencode.Unmarshaler.
type HostAddr ¶
HostAddr represents an address based on host and port.
func NewHostAddr ¶
NewHostAddr returns a new host Addr.
func ParseHostAddr ¶
ParseHostAddr parses a string s to Addr.
func (HostAddr) MarshalBencode ¶
MarshalBencode implements the interface bencode.Marshaler.
func (*HostAddr) UnmarshalBencode ¶
UnmarshalBencode implements the interface bencode.Unmarshaler.
type Info ¶
type Info struct { // Name is the name of the file in the single file case. // Or, it is the name of the directory in the muliple file case. Name string `json:"name" bencode:"name"` // BEP 3 // PieceLength is the number of bytes in each piece, which is usually // a power of 2. PieceLength int64 `json:"piece length" bencode:"piece length"` // BEP 3 // Pieces is the concatenation of all 20-byte SHA1 hash values, // one per piece (byte string, i.e. not urlencoded). Pieces Hashes `json:"pieces" bencode:"pieces"` // BEP 3 // Length is the length of the file in bytes in the single file case. // // It's mutually exclusive with Files. Length int64 `json:"length,omitempty" bencode:"length,omitempty"` // BEP 3 // Files is the list of all the files in the multi-file case. // // For the purposes of the other keys, the multi-file case is treated // as only having a single file by concatenating the files in the order // they appear in the files list. // // It's mutually exclusive with Length. Files []File `json:"files,omitempty" bencode:"files,omitempty"` // BEP 3 }
Info is the file inforatino.
func NewInfoFromFilePath ¶
NewInfoFromFilePath returns a new Info from a file or directory.
func (Info) AllFiles ¶
AllFiles returns all the files.
Notice: for the single file, the Path is nil.
func (Info) CountPieces ¶
CountPieces returns the number of the pieces.
func (Info) GetFileByOffset ¶
GetFileByOffset returns the file and its offset by the total offset.
If fileOffset is eqaul to file.Length, it means to reach the end.
func (Info) IsDir ¶
IsDir reports whether the name is a directory, that's, the file is not a single file.
func (Info) PieceOffset ¶
PieceOffset returns the total offset of the piece.
offset is the offset relative to the beginning of the piece.
func (Info) TotalLength ¶
TotalLength returns the total length of the torrent file.
type Magnet ¶
type Magnet struct { InfoHash Hash // From "xt" Trackers []string // From "tr" DisplayName string // From "dn" if not empty Params url.Values // All other values, such as "as", "xs", etc }
Magnet link components.
func ParseMagnetURI ¶
ParseMagnetURI parses Magnet-formatted URIs into a Magnet instance.
type MetaInfo ¶
type MetaInfo struct { InfoBytes Bytes `bencode:"info"` // BEP 3 Announce string `bencode:"announce,omitempty"` // BEP 3, Single Tracker AnnounceList AnnounceList `bencode:"announce-list,omitempty"` // BEP 12, Multi-Tracker Nodes []HostAddr `bencode:"nodes,omitempty"` // BEP 5, DHT URLList URLList `bencode:"url-list,omitempty"` // BEP 19, WebSeed // CreationDate is the creation time of the torrent, in standard UNIX epoch // format (seconds since 1-Jan-1970 00:00:00 UTC). CreationDate int64 `bencode:"creation date,omitempty"` // Comment is the free-form textual comments of the author. Comment string `bencode:"comment,omitempty"` // CreatedBy is name and version of the program used to create the .torrent. CreatedBy string `bencode:"created by,omitempty"` // Encoding is the string encoding format used to generate the pieces part // of the info dictionary in the .torrent metafile. Encoding string `bencode:"encoding,omitempty"` }
MetaInfo represents the .torrent file.
func LoadFromFile ¶
LoadFromFile loads a MetaInfo from a file.
func (MetaInfo) Announces ¶
func (mi MetaInfo) Announces() AnnounceList
Announces returns all the announces.
type Piece ¶
type Piece struct {
// contains filtered or unexported fields
}
Piece represents a torrent file piece.
type PieceBlock ¶
type PieceBlock struct { Index uint32 // The index of the piece. Offset uint32 // The offset from the beginning of the piece. Length uint32 // The length of the block, which is equal to 2^14 in general. }
PieceBlock represents a block in a piece.
type PieceBlocks ¶
type PieceBlocks []PieceBlock
PieceBlocks is a set of PieceBlocks.
func (PieceBlocks) Len ¶
func (pbs PieceBlocks) Len() int
func (PieceBlocks) Less ¶
func (pbs PieceBlocks) Less(i, j int) bool
func (PieceBlocks) Swap ¶
func (pbs PieceBlocks) Swap(i, j int)
type Reader ¶
type Reader interface { io.Closer io.ReaderAt Info() Info ReadBlock(pieceIndex, pieceOffset uint32, p []byte) (int, error) }
Reader is used to read the data referred by the torrent file.
type URLList ¶
type URLList []string
URLList represents a list of the url.
BEP 19
func (URLList) FullURL ¶
FullURL returns the index-th full url.
For the single-file case, name is the "name" of "info". For the multi-file case, name is the path "name/path/file" from "info" and "files".
func (URLList) MarshalBencode ¶
MarshalBencode implements the interface bencode.Marshaler.
func (*URLList) UnmarshalBencode ¶
UnmarshalBencode implements the interface bencode.Unmarshaler.