metainfo

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2023 License: Apache-2.0 Imports: 19 Imported by: 1

Documentation

Overview

Package metainfo is used to encode or decode the metainfo of the torrent file and the MagNet link.

Index

Constants

View Source
const (
	PieceSize256KB = 1024 * 256
	PieceSize512KB = 2 * PieceSize256KB
	PieceSize1MB   = 2 * PieceSize512KB
	PieceSize2MB   = 2 * PieceSize1MB
	PieceSize4MB   = 2 * PieceSize2MB
)

Predefine some sizes of the pieces.

View Source
const BlockSize = 16 * 1024 // 2^14 = 16KB

BlockSize is the default size of a piece block.

View Source
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 Bytes

type Bytes = bencode.RawMessage

Bytes is the []byte type.

type CompactAddr

type CompactAddr struct {
	IP   net.IP // For IPv4, its length must be 4.
	Port uint16
}

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

func (f File) Offset(info Info) (ret int64)

Offset returns the offset of the current file from the start.

func (File) Path

func (f File) Path(info Info) string

Path returns the path of the current.

func (File) PathWithPrefix

func (f File) PathWithPrefix(prefix string, info Info) string

PathWithPrefix returns the path of the current with the prefix directory.

func (File) String

func (f File) String() string

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

func (fp FilePiece) TotalOffset(pieceLength int64) int64

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

type Hash [HashSize]byte

Hash is the 20-byte SHA1 hash used for info and pieces.

func NewHash

func NewHash(b []byte) (h Hash)

NewHash converts the 20-bytes to Hash.

func NewHashFromBytes

func NewHashFromBytes(b []byte) (ret Hash)

NewHashFromBytes returns a new Hash from a byte slice.

func NewHashFromHexString

func NewHashFromHexString(s string) (h Hash)

NewHashFromHexString returns a new Hash from a hex string.

func NewHashFromString

func NewHashFromString(s string) (h Hash)

NewHashFromString returns a new Hash from a string.

func NewRandomHash

func NewRandomHash() (h Hash)

NewRandomHash returns a random hash.

func (Hash) Bytes

func (h Hash) Bytes() []byte

Bytes returns the byte slice type.

func (Hash) BytesString

func (h Hash) BytesString() string

BytesString returns the bytes string, that's, string(h[:]).

func (Hash) Compare

func (h Hash) Compare(o Hash) int

Compare returns 0 if h == o, -1 if h < o, or +1 if h > o.

func (*Hash) FromHexString

func (h *Hash) FromHexString(s string) (err error)

FromHexString resets the info hash from the hex string.

func (*Hash) FromString

func (h *Hash) FromString(s string) (err error)

FromString resets the info hash from the string.

func (Hash) HexString

func (h Hash) HexString() string

HexString returns the hex string format.

func (Hash) IsZero

func (h Hash) IsZero() bool

IsZero reports whether the whole hash is zero.

func (Hash) MarshalBencode

func (h Hash) MarshalBencode() (b []byte, err error)

MarshalBencode implements the interface bencode.Marshaler.

func (Hash) MarshalBinary

func (h Hash) MarshalBinary() (data []byte, err error)

MarshalBinary implements the interface binary.BinaryMarshaler.

func (Hash) String

func (h Hash) String() string

String is equal to HexString.

func (*Hash) UnmarshalBencode

func (h *Hash) UnmarshalBencode(b []byte) (err error)

UnmarshalBencode implements the interface bencode.Unmarshaler.

func (*Hash) UnmarshalBinary

func (h *Hash) UnmarshalBinary(b []byte) (err error)

UnmarshalBinary implements the interface binary.BinaryUnmarshaler.

func (Hash) WriteBinary

func (h Hash) WriteBinary(w io.Writer) (m int, err error)

WriteBinary is the same as MarshalBinary, but writes the result into w instead of returning.

func (Hash) Xor

func (h Hash) Xor(o Hash) (ret Hash)

Xor returns the hash of h XOR o.

type Hashes

type Hashes []Hash

Hashes is a set of Hashes.

func GeneratePieces

func GeneratePieces(r io.Reader, pieceLength int64) (hs Hashes, err error)

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

func (hs Hashes) Contains(h Hash) bool

Contains reports whether hs contains h.

func (Hashes) MarshalBencode

func (hs Hashes) MarshalBencode() ([]byte, error)

MarshalBencode implements the interface bencode.Marshaler.

func (*Hashes) UnmarshalBencode

func (hs *Hashes) UnmarshalBencode(b []byte) (err error)

UnmarshalBencode implements the interface bencode.Unmarshaler.

type HostAddr

type HostAddr struct {
	Host string
	Port uint16
}

HostAddr represents an address based on host and port.

func NewHostAddr

func NewHostAddr(host string, port uint16) HostAddr

NewHostAddr returns a new host Addr.

func ParseHostAddr

func ParseHostAddr(s string) (HostAddr, error)

ParseHostAddr parses a string s to Addr.

func (HostAddr) Equal

func (a HostAddr) Equal(o HostAddr) bool

Equal reports whether a is equal to o.

func (HostAddr) MarshalBencode

func (a HostAddr) MarshalBencode() (b []byte, err error)

MarshalBencode implements the interface bencode.Marshaler.

func (HostAddr) String

func (a HostAddr) String() string

func (*HostAddr) UnmarshalBencode

func (a *HostAddr) 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

func NewInfoFromFilePath(root string, pieceLength int64) (info Info, err error)

NewInfoFromFilePath returns a new Info from a file or directory.

func (Info) AllFiles

func (info Info) AllFiles() []File

AllFiles returns all the files.

Notice: for the single file, the Path is nil.

func (Info) CountPieces

func (info Info) CountPieces() int

CountPieces returns the number of the pieces.

func (Info) GetFileByOffset

func (info Info) GetFileByOffset(offset int64) (file File, fileOffset int64)

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

func (info Info) IsDir() bool

IsDir reports whether the name is a directory, that's, the file is not a single file.

func (Info) Piece

func (info Info) Piece(index int) Piece

Piece returns the Piece by the index starting with 0.

func (Info) PieceOffset

func (info Info) PieceOffset(index, offset uint32) int64

PieceOffset returns the total offset of the piece.

offset is the offset relative to the beginning of the piece.

func (Info) TotalLength

func (info Info) TotalLength() (ret int64)

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

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

ParseMagnetURI parses Magnet-formatted URIs into a Magnet instance.

func (Magnet) Peers

func (m Magnet) Peers() (peers []HostAddr, err error)

Peers returns the list of the addresses of the peers.

See BEP 9

func (Magnet) String

func (m Magnet) String() string

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 Load

func Load(r io.Reader) (mi MetaInfo, err error)

Load loads a MetaInfo from an io.Reader.

func LoadFromFile

func LoadFromFile(filename string) (mi MetaInfo, err error)

LoadFromFile loads a MetaInfo from a file.

func (MetaInfo) Announces

func (mi MetaInfo) Announces() AnnounceList

Announces returns all the announces.

func (MetaInfo) Info

func (mi MetaInfo) Info() (info Info, err error)

Info parses the InfoBytes to the Info.

func (MetaInfo) InfoHash

func (mi MetaInfo) InfoHash() Hash

InfoHash returns the hash of the info.

func (MetaInfo) Magnet

func (mi MetaInfo) Magnet(displayName string, infoHash Hash) (m Magnet)

Magnet creates a Magnet from a MetaInfo.

If displayName or infoHash is empty, it will be got from the info part.

func (MetaInfo) Write

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

Write encodes the metainfo to w.

type Piece

type Piece struct {
	// contains filtered or unexported fields
}

Piece represents a torrent file piece.

func (Piece) Hash

func (p Piece) Hash() (h Hash)

Hash returns the hash representation of the piece.

func (Piece) Index

func (p Piece) Index() int

Index returns the index of the current piece.

func (Piece) Length

func (p Piece) Length() int64

Length returns the length of the current piece.

func (Piece) Offset

func (p Piece) Offset() int64

Offset returns the offset that the current piece is in all the files.

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.

func NewReader

func NewReader(rootDir string, info Info) Reader

NewReader returns a new Reader.

type URLList

type URLList []string

URLList represents a list of the url.

BEP 19

func (URLList) FullURL

func (us URLList) FullURL(index int, name string) (url string)

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".

See http://bittorrent.org/beps/bep_0019.html

func (URLList) MarshalBencode

func (us URLList) MarshalBencode() (b []byte, err error)

MarshalBencode implements the interface bencode.Marshaler.

func (*URLList) UnmarshalBencode

func (us *URLList) UnmarshalBencode(b []byte) (err error)

UnmarshalBencode implements the interface bencode.Unmarshaler.

type Writer

type Writer interface {
	io.Closer
	io.WriterAt

	Info() Info
	WriteBlock(pieceIndex, pieceOffset uint32, p []byte) (int, error)
}

Writer is used to write the data referred by the torrent file.

func NewWriter

func NewWriter(rootDir string, info Info, fileMode os.FileMode) Writer

NewWriter returns a new Writer.

If fileMode is equal to 0, it is 0600 by default.

Notice: fileMode is only used when writing the data.

Jump to

Keyboard shortcuts

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