Documentation ¶
Overview ¶
Package metainfo is used to encode or decode the metainfo of the torrent file and the MagNet link.
Index ¶
- Constants
- Variables
- func LookupNetAddr(shost string) ([]net.Addr, error)
- type Address
- func (a Address) Addr() net.Addr
- func (a Address) Equal(o Address) bool
- func (a *Address) FromString(addr string) (err error)
- func (a *Address) FromUDPAddr(ua net.Addr)
- func (a Address) HasIPAndPort(ip net.Addr, port uint16) bool
- func (a Address) IsIPv6() bool
- func (a Address) MarshalBencode() (b []byte, err error)
- func (a Address) MarshalBinary() (data []byte, err error)
- func (a Address) String() string
- func (a Address) To16() *net.IPAddr
- func (a Address) To4() *net.IPAddr
- func (a *Address) UnmarshalBencode(b []byte) (err error)
- func (a *Address) UnmarshalBinary(b []byte) (err error)
- func (a Address) WriteBinary(w io.Writer) (m int, err error)
- type AnnounceList
- type Bytes
- 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 HostAddress
- func (a HostAddress) Addresses() (addrs []Address, err error)
- func (a HostAddress) Equal(o HostAddress) bool
- func (a *HostAddress) FromString(addr string) (err error)
- func (a HostAddress) MarshalBencode() (b []byte, err error)
- func (a HostAddress) String() string
- func (a *HostAddress) UnmarshalBencode(b []byte) (err error)
- 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 HashSize = 20
HashSize is the size of the InfoHash.
Variables ¶
var ErrInvalidAddr = fmt.Errorf("invalid compact information of ip and port")
ErrInvalidAddr is returned when the compact address is invalid.
Functions ¶
Types ¶
type Address ¶
Address represents a client/server listening on a UDP port implementing the DHT protocol.
func NewAddress ¶
NewAddress returns a new Address.
func NewAddressFromString ¶
NewAddressFromString returns a new Address by the address string.
func NewAddressesFromString ¶
NewAddressesFromString returns a list of Addresses by the address string.
func (Address) Equal ¶
Equal reports whether n is equal to o, which is equal to
n.HasIPAndPort(o.IP, o.Port)
func (*Address) FromString ¶
FromString parses and sets the ip from the string addr.
func (*Address) FromUDPAddr ¶
FromUDPAddr sets the ip from net.UDPAddr.
func (Address) HasIPAndPort ¶
HasIPAndPort reports whether the current node has the ip and the port.
func (Address) MarshalBencode ¶
MarshalBencode implements the interface bencode.Marshaler.
func (Address) MarshalBinary ¶
MarshalBinary implements the interface binary.BinaryMarshaler.
func (*Address) UnmarshalBencode ¶
UnmarshalBencode implements the interface bencode.Unmarshaler.
func (*Address) UnmarshalBinary ¶
UnmarshalBinary implements the interface binary.BinaryUnmarshaler.
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 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 HostAddress ¶
HostAddress is the same as the Address, but the host part may be either a domain or a ip.
func NewHostAddress ¶
func NewHostAddress(host string, port uint16) HostAddress
NewHostAddress returns a new host addrress.
func NewHostAddressFromString ¶
func NewHostAddressFromString(s string) (addr HostAddress, err error)
NewHostAddressFromString returns a new host address by the string.
func (HostAddress) Addresses ¶
func (a HostAddress) Addresses() (addrs []Address, err error)
Addresses parses the host address to a list of Addresses.
func (HostAddress) Equal ¶
func (a HostAddress) Equal(o HostAddress) bool
Equal reports whether a is equal to o.
func (*HostAddress) FromString ¶
func (a *HostAddress) FromString(addr string) (err error)
FromString parses and sets the host from the string addr.
func (HostAddress) MarshalBencode ¶
func (a HostAddress) MarshalBencode() (b []byte, err error)
MarshalBencode implements the interface bencode.Marshaler.
func (HostAddress) String ¶
func (a HostAddress) String() string
func (*HostAddress) UnmarshalBencode ¶
func (a *HostAddress) UnmarshalBencode(b []byte) (err error)
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.
func (Magnet) Peers ¶
func (m Magnet) Peers() (peers []HostAddress, err error)
Peers returns the list of the addresses of the peers.
See BEP 9
type MetaInfo ¶
type MetaInfo struct { InfoBytes Bytes `bencode:"info"` // BEP 3 Announce string `bencode:"announce,omitempty"` // BEP 3 AnnounceList AnnounceList `bencode:"announce-list,omitempty"` // BEP 12 Nodes []HostAddress `bencode:"nodes,omitempty"` // BEP 5 URLList URLList `bencode:"url-list,omitempty"` // BEP 19 // 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.