metainfo

package
Version: v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2021 License: Apache-2.0 Imports: 18 Imported by: 2

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 HashSize = 20

HashSize is the size of the InfoHash.

Variables

View Source
var ErrInvalidAddr = fmt.Errorf("invalid compact information of ip and port")

ErrInvalidAddr is returned when the compact address is invalid.

Functions

This section is empty.

Types

type Address

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

Address represents a client/server listening on a UDP port implementing the DHT protocol.

func NewAddress

func NewAddress(ip net.IP, port uint16) Address

NewAddress returns a new Address.

func NewAddressFromString

func NewAddressFromString(s string) (addr Address, err error)

NewAddressFromString returns a new Address by the address string.

func NewAddressesFromString

func NewAddressesFromString(s string) (addrs []Address, err error)

NewAddressesFromString returns a list of Addresses by the address string.

func (Address) Equal

func (a Address) Equal(o Address) bool

Equal reports whether n is equal to o, which is equal to

n.HasIPAndPort(o.IP, o.Port)

func (*Address) FromString

func (a *Address) FromString(addr string) (err error)

FromString parses and sets the ip from the string addr.

func (*Address) FromUDPAddr

func (a *Address) FromUDPAddr(ua *net.UDPAddr)

FromUDPAddr sets the ip from net.UDPAddr.

func (Address) HasIPAndPort

func (a Address) HasIPAndPort(ip net.IP, port uint16) bool

HasIPAndPort reports whether the current node has the ip and the port.

func (Address) MarshalBencode

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

MarshalBencode implements the interface bencode.Marshaler.

func (Address) MarshalBinary

func (a Address) MarshalBinary() (data []byte, err error)

MarshalBinary implements the interface binary.BinaryMarshaler.

func (Address) String

func (a Address) String() string

func (Address) UDPAddr

func (a Address) UDPAddr() *net.UDPAddr

UDPAddr creates a new net.UDPAddr.

func (*Address) UnmarshalBencode

func (a *Address) UnmarshalBencode(b []byte) (err error)

UnmarshalBencode implements the interface bencode.Unmarshaler.

func (*Address) UnmarshalBinary

func (a *Address) UnmarshalBinary(b []byte) (err error)

UnmarshalBinary implements the interface binary.BinaryUnmarshaler.

func (Address) WriteBinary

func (a Address) WriteBinary(w io.Writer) (m int, err error)

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

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 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 added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

func (fp FilePiece) Blocks() PieceBlocks

Blocks returns the lists of the blocks of the piece.

func (FilePiece) TotalOffset added in v0.2.0

func (fp FilePiece) TotalOffset(pieceLength int64) int64

TotalOffset return the total offset from the beginning of all the files.

type FilePieces added in v0.2.0

type FilePieces []FilePiece

FilePieces is a set of FilePieces.

func (FilePieces) Len added in v0.2.0

func (fps FilePieces) Len() int

func (FilePieces) Less added in v0.2.0

func (fps FilePieces) Less(i, j int) bool

func (FilePieces) Merge added in v0.2.0

func (fps FilePieces) Merge() FilePieces

Merge merges the contiguous pieces to the one piece.

func (FilePieces) Swap added in v0.2.0

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 added in v0.4.0

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 added in v0.4.0

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

UnmarshalBinary implements the interface binary.BinaryUnmarshaler.

func (Hash) WriteBinary added in v0.4.0

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 HostAddress

type HostAddress struct {
	Host string
	Port uint16
}

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

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 added in v0.2.0

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 added in v0.2.0

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 []HostAddress, 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
	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 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 added in v0.2.0

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 added in v0.2.0

type PieceBlocks []PieceBlock

PieceBlocks is a set of PieceBlocks.

func (PieceBlocks) Len added in v0.2.0

func (pbs PieceBlocks) Len() int

func (PieceBlocks) Less added in v0.2.0

func (pbs PieceBlocks) Less(i, j int) bool

func (PieceBlocks) Swap added in v0.2.0

func (pbs PieceBlocks) Swap(i, j int)

type Reader added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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
t or T : Toggle theme light dark auto
y or Y : Canonical URL