torrent

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PIECE_QUEUE            = 256
	PEX_CHANNEL            = 512
	DISCOVERY_LIMIT        = 64
	CONFIRMED_PEER_QUEUE   = 812
	HANDSHAKE_WAIT_TIMEOUT = 10
	READ_MSG_TIMEOUT       = 130
	REQUEST_BLOCK_SIZE     = 16384
	MAX_CHOKED_TIME        = 60 * time.Second
	MAX_BACKLOG            = 32
	MAX_MSG_LEN            = 262144
	PEX_INTERVAL           = 60 * time.Second
)
View Source
const (
	ExtendedHandshakeID = 0
	UtPexID             = 1
)
View Source
const MAGIC_CONSTANT = 0x41727101980

Variables

This section is empty.

Functions

func Open

func Open(r io.Reader) (*bencodeObject, error)

func Unmarshal

func Unmarshal(r io.Reader, ben *bencodeObject) error

func Verify

func Verify(tf *TorrentFile) error

Types

type BencodeType

type BencodeType int
const (
	DICT BencodeType = iota
	LIST
	INT
	STRING
	END
)

type Bitfield

type Bitfield []byte

func (Bitfield) ClearPiece added in v0.1.1

func (bf Bitfield) ClearPiece(index int)

func (Bitfield) HasPiece

func (bf Bitfield) HasPiece(index int) bool

func (Bitfield) SetPiece

func (bf Bitfield) SetPiece(index int)

type Downloader

type Downloader struct {
	Stats Stats
	// contains filtered or unexported fields
}

func NewDownloader

func NewDownloader(tf *TorrentFile) (*Downloader, error)

func (*Downloader) AddPeer

func (d *Downloader) AddPeer(p *PeerCon)

func (*Downloader) Done added in v0.2.2

func (d *Downloader) Done() <-chan struct{}

func (*Downloader) GetActivePeers added in v0.2.2

func (d *Downloader) GetActivePeers() []PeerInfo

func (*Downloader) GetBitfield added in v0.2.2

func (d *Downloader) GetBitfield() Bitfield

func (*Downloader) PickPiece added in v0.1.1

func (d *Downloader) PickPiece(peerBitfield Bitfield) (int, bool)

func (*Downloader) PrintLogs added in v0.2.0

func (d *Downloader) PrintLogs()

func (*Downloader) Wait

func (d *Downloader) Wait()

type FileInfo

type FileInfo struct {
	Path   string
	Length int
}

type HTTPConnector

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

func NewHTTPConnector

func NewHTTPConnector(baseURL string) *HTTPConnector

type HTTPTracker added in v0.2.0

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

func NewHTTPTracker added in v0.2.0

func NewHTTPTracker(rawURL string) *HTTPTracker

func (*HTTPTracker) SendRequest added in v0.2.0

func (ht *HTTPTracker) SendRequest(hr *http.Request) (*http.Response, error)

type Message

type Message struct {
	ID      MessageID
	Payload []byte
}

func (*Message) Serialize

func (m *Message) Serialize() []byte

type MessageID

type MessageID uint8
const (
	CHOKE MessageID = iota
	UNCHOKE
	INTERESTED
	NOT_INTERESTED
	HAVE
	BITFIELD
	REQUEST
	PIECE
	CANCEL
	EXTENDED = 20
)

type Peer

type Peer struct {
	IP net.IP
	// contains filtered or unexported fields
}

func UnmarshalPeers

func UnmarshalPeers(data []byte) []Peer

type PeerCon

type PeerCon struct {
	RemotePeerID string
	// contains filtered or unexported fields
}

func NewPeerCon

func NewPeerCon(tf *TorrentFile, p *Peer, bits Bitfield, pexCh chan string) *PeerCon

func (*PeerCon) DownloadLoop

func (p *PeerCon) DownloadLoop(d *Downloader, results chan Piece)

func (*PeerCon) ReadMessage

func (p *PeerCon) ReadMessage() (*Message, error)

func (*PeerCon) SendBitfield added in v0.2.0

func (p *PeerCon) SendBitfield() error

func (*PeerCon) SendExtendedHandshake added in v0.1.1

func (p *PeerCon) SendExtendedHandshake() error

func (*PeerCon) SendInterested

func (p *PeerCon) SendInterested() error

func (*PeerCon) SendMessage

func (p *PeerCon) SendMessage(msg *Message) error

func (*PeerCon) SendPiece added in v0.2.0

func (p *PeerCon) SendPiece(index, begin uint32, data []byte) error

func (*PeerCon) SendRequest

func (p *PeerCon) SendRequest(index, begin, length int) error

func (*PeerCon) SendUnchoke

func (p *PeerCon) SendUnchoke() error

func (*PeerCon) ShakeHands

func (p *PeerCon) ShakeHands() error

type PeerInfo added in v0.2.2

type PeerInfo struct {
	Addr   string
	ID     string
	Choked bool
}

type Piece

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

type Stats added in v0.2.0

type Stats struct {
	TotalSize            int64
	PexProcessed         atomic.Int32
	PexAdded             atomic.Int32
	PexSent              atomic.Int32
	PeersProcessed       atomic.Int32
	PeersConfirmed       atomic.Int32
	PeersDenied          atomic.Int32
	StartTime            time.Time
	GlobalBitfield       Bitfield
	TotalWritten         int64
	CurrentlyDownloading atomic.Int32
	Failed               atomic.Int32
	NumPeers             atomic.Int32
	Searching            atomic.Int32
	NotFound             atomic.Int32
	UnchokedPeers        atomic.Int32
	Seeders              atomic.Int32
	BitfieldRecv         atomic.Int32
	BitfieldMiss         atomic.Int32
	ValidTrackers        atomic.Int32
	PeersProvided        atomic.Int32
}

type TCPConnector

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

func NewTCPConnector

func NewTCPConnector(p *Peer) *TCPConnector

func (*TCPConnector) Close

func (c *TCPConnector) Close() error

func (*TCPConnector) Recv

func (c *TCPConnector) Recv(size int32, timeout float32) ([]byte, *net.TCPAddr, error)

func (*TCPConnector) RecvAll

func (c *TCPConnector) RecvAll(size int32, timeout float32) ([]byte, *net.TCPAddr, error)

func (*TCPConnector) Send

func (c *TCPConnector) Send(buf []byte) error

func (*TCPConnector) SetDestination

func (c *TCPConnector) SetDestination(addr *net.TCPAddr)

func (*TCPConnector) SetDestinationTo

func (c *TCPConnector) SetDestinationTo(remoteAddr string) error

type TorrentFile

type TorrentFile struct {
	Announce     string
	AnnounceList [][]string
	InfoHash     [20]byte
	PieceHashes  [][20]byte
	PieceLength  int
	Length       int
	Files        []FileInfo
	Name         string
}

func NewTorrentFile

func NewTorrentFile(path string) (*TorrentFile, error)

func (*TorrentFile) DownloadLength added in v0.2.1

func (bto *TorrentFile) DownloadLength() int64

type TorrentWriter

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

func NewTorrentWriter

func NewTorrentWriter(tf *TorrentFile, d *Downloader) (*TorrentWriter, error)

func (*TorrentWriter) Read

func (w *TorrentWriter) Read(index int, begin int, length int) ([]byte, error)

func (*TorrentWriter) Write

func (w *TorrentWriter) Write(index int, begin int, data []byte) error

type UDPConnector

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

func NewUDPConnector

func NewUDPConnector(localAddr string) (*UDPConnector, error)

func (*UDPConnector) Close

func (c *UDPConnector) Close() error

func (*UDPConnector) Recv

func (c *UDPConnector) Recv(size int32, timeout float32) ([]byte, *net.UDPAddr, error)

func (*UDPConnector) Send

func (c *UDPConnector) Send(buf []byte) error

func (*UDPConnector) SetDestination

func (c *UDPConnector) SetDestination(addr *net.UDPAddr)

func (*UDPConnector) SetDestinationTo

func (c *UDPConnector) SetDestinationTo(localAddr string) error

type UDPTracker

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

func NewUDPTracker

func NewUDPTracker(rawURL string) (*UDPTracker, error)

Jump to

Keyboard shortcuts

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