bt

package
v0.0.0-...-38ae272 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ExtID is extension message id.
	ExtID byte = 20
	// ExtReserved is reserved field in handshake when peer wire
	ExtReserved = 0x100000
)
View Source
const (
	// Protocol is "BitTorrent protocol"
	Protocol = "BitTorrent protocol"
)

Variables

This section is empty.

Functions

func CheckExtReserved

func CheckExtReserved(reserved uint64) bool

CheckExtReserved returns true if reserved support extension

func ReadMessage

func ReadMessage(reader io.Reader) ([]byte, error)

ReadMessage read a message from stream

func ReadMessageInto

func ReadMessageInto(reader io.Reader, buf *bytes.Buffer) error

ReadMessageInto will reset the buffer and read message into it

func ReadMessageWithLimit

func ReadMessageWithLimit(reader io.Reader, limit uint32) ([]byte, error)

ReadMessageWithLimit read a message from stream

func SetExtReserved

func SetExtReserved(reserved *uint64)

SetExtReserved set extesntion reserved on handshake

func WriteExtensionMessage

func WriteExtensionMessage(writer io.Writer, id byte, content []byte) error

WriteExtensionMessage write extension message to writer

func WriteMessage

func WriteMessage(writer io.Writer, id byte, payload []byte) error

WriteMessage will write a message into stream

Types

type ExtHSGetter

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

ExtHSGetter can get entry from handshake.

func (*ExtHSGetter) Get

func (g *ExtHSGetter) Get(k string, ptr interface{}) bool

Get entry from handshake

type ExtHSPutter

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

ExtHSPutter only can put entry into handshake

func (*ExtHSPutter) Put

func (p *ExtHSPutter) Put(k string, v interface{})

Put entry into handshake

type ExtMsgSender

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

ExtMsgSender used for send extension message

func (*ExtMsgSender) SendBytes

func (sender *ExtMsgSender) SendBytes(content []byte) error

SendBytes will send content

type ExtProtocol

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

ExtProtocol is extension center

func NewExtProtocol

func NewExtProtocol(exts ...Extension) *ExtProtocol

NewExtProtocol returns a new extension center

func (*ExtProtocol) HandlePayload

func (proto *ExtProtocol) HandlePayload(payload []byte, w io.Writer) error

HandlePayload handle extention message

func (*ExtProtocol) RegistExt

func (proto *ExtProtocol) RegistExt(ext Extension)

RegistExt regists extension into center

func (*ExtProtocol) WriteHandshake

func (proto *ExtProtocol) WriteHandshake(w io.Writer) error

WriteHandshake sends handshake

type Extension

type Extension interface {
	MapKey() string
	BeforeHandshake(hs ExtHSPutter)
	// AfterHandshake will call after handsahke if peer support this extension
	AfterHandshake(hs ExtHSGetter, sender *ExtMsgSender) error
	// Unsupport will call after handshake if peer not support this extension.
	Unsupport()
	HandleMessage(content []byte, sender *ExtMsgSender) error
}

Extension is extension

type FetchMetaExt

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

FetchMetaExt is extension can fetch metadata

func NewFetchMetaExt

func NewFetchMetaExt(infoHash string) *FetchMetaExt

NewFetchMetaExt returns a new FetchMetaExt

func (*FetchMetaExt) AfterHandshake

func (fm *FetchMetaExt) AfterHandshake(hs ExtHSGetter, sender *ExtMsgSender) error

AfterHandshake implements Extension.AfterHandshake

func (*FetchMetaExt) BeforeHandshake

func (fm *FetchMetaExt) BeforeHandshake(hs ExtHSPutter)

BeforeHandshake implements Extension.BeforeHandshake

func (*FetchMetaExt) CheckDone

func (fm *FetchMetaExt) CheckDone() bool

CheckDone if download all pieces returns true else false

func (*FetchMetaExt) FetchRawMeta

func (fm *FetchMetaExt) FetchRawMeta() (RawMeta, error)

FetchRawMeta get the raw metadata

func (*FetchMetaExt) HandleMessage

func (fm *FetchMetaExt) HandleMessage(content []byte, sender *ExtMsgSender) error

HandleMessage implements Extension.HandleMessage

func (*FetchMetaExt) IsSupoort

func (fm *FetchMetaExt) IsSupoort() bool

IsSupoort returns true if peer support this extension other false. Should use it after extesion handshake

func (*FetchMetaExt) MapKey

func (fm *FetchMetaExt) MapKey() string

MapKey implements Extension.MapKey

func (*FetchMetaExt) Unsupport

func (fm *FetchMetaExt) Unsupport()

Unsupport implements Extension.Unsupport

type Meta

type Meta struct {
	Name        string  `bencode:"name"`
	Length      *uint64 `bencode:"length,omitempty"`
	PieceLength uint    `bencode:"piece length"`
	Pieces      []byte  `bencode:"pieces"`

	Files []struct {
		Path   []string `bencode:"path"`
		Length uint64   `bencode:"length"`
	} `bencode:"files,omitempty"`
}

Meta is meta

type MetaOutline

type MetaOutline interface {
	SetName(string)
	AddFile(string, uint64)
}

MetaOutline is outline of metadata

type RawMeta

type RawMeta bencode.RawMessage

RawMeta is raw metadata

func FetchMetadata

func FetchMetadata(ctx context.Context, infoHash string, host string) (RawMeta, error)

FetchMetadata fetch metadata from host.

func (RawMeta) FillOutline

func (rm RawMeta) FillOutline(mo MetaOutline) error

FillOutline can fill MetaOutline

type Stream

type Stream interface {
	io.Writer
	io.Reader
	io.Closer
	Protocol() string
	Reserved() uint64
	InfoHash() string
	PeerID() string
}

Stream is a BitProtocol stream

func DialUseTCP

func DialUseTCP(host string, infohash, peerID string, reserved uint64) (Stream, error)

DialUseTCP create a stream use tcp

type TexExtension

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

TexExtension is tracker exchange extension

func NewTexExtension

func NewTexExtension(trlist []string) *TexExtension

NewTexExtension returns a new TexExtension

func (*TexExtension) AfterHandshake

func (tex *TexExtension) AfterHandshake(hs ExtHSGetter, sender *ExtMsgSender) error

AfterHandshake will call after handsahke if peer support this extension

func (*TexExtension) BeforeHandshake

func (tex *TexExtension) BeforeHandshake(hs ExtHSPutter)

BeforeHandshake implements Extension.BeforeHandshake

func (*TexExtension) HandleMessage

func (tex *TexExtension) HandleMessage(content []byte, sender *ExtMsgSender) error

HandleMessage implements Extension.HandleMessage

func (*TexExtension) IsSupoort

func (tex *TexExtension) IsSupoort() bool

IsSupoort returns true if peer support this extension other false. Should use it after extesion handshake

func (*TexExtension) MapKey

func (tex *TexExtension) MapKey() string

MapKey implements Extension.MapKey

func (*TexExtension) More

func (tex *TexExtension) More() bool

More returns peer have more tracker or not

func (*TexExtension) TrackerList

func (tex *TexExtension) TrackerList() []string

TrackerList returns tracker list

func (*TexExtension) Unsupport

func (tex *TexExtension) Unsupport()

Unsupport will call after handshake if peer not support this extension.

Jump to

Keyboard shortcuts

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