tor

package
v0.0.0-...-c8d95c1 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2024 License: MIT Imports: 37 Imported by: 0

Documentation

Overview

Package tor implements behaviour of torrents in storrent.

Index

Constants

View Source
const IdlePriority = int8(math.MinInt8)

IdlePriority is returned for pieces that have been requested for reasons other than a client requiring them.

Variables

View Source
var ErrConnectionSelf = errors.New("connection to self")
View Source
var ErrDuplicateConnection = errors.New("duplicate connection")
View Source
var ErrMartianAddress = errors.New("martian address")
View Source
var ErrMetadataIncomplete = errors.New("metadata incomplete")
View Source
var ErrShortWrite = errors.New("short write")
View Source
var ErrTorrentDead = errors.New("torrent is dead")

Functions

func Announce

func Announce(h hash.Hash, ipv6 bool) error

Announce performs a DHT announce.

func Client

func Client(conn net.Conn, t *Torrent, ip net.IP, port int, proxy string, cryptoHandshake bool, cryptoOptions *crypto.Options) error

Client establishes a connection with a client.

func DialClient

func DialClient(ctx context.Context, t *Torrent, ip net.IP, port int, cryptoOptions *crypto.Options) error

DialClient connects to a peer.

func Expire

func Expire() int

Expire discards pieces in order to meet our memory usage target.

func NewWriter

func NewWriter(t *Torrent, index, offset, length uint32) *writer

NewWriter creates a writer. The chunks covered by length have already been marked as in-flight by the caller.

func Range

func Range(f func(hash.Hash, *Torrent) bool)

func Server

func Server(conn net.Conn, cryptoOptions *crypto.Options) error

Server accepts a peer connection.

func WriteTorrent

func WriteTorrent(w io.Writer, t *Torrent) error

WriteTorrent writes a torrent file to an io.Writer.

Types

type Available

type Available []uint16

Available is an array indicating the number of locally available copies of each piece in a torrent.

func (Available) Available

func (a Available) Available(index int) int

func (Available) AvailableRange

func (a Available) AvailableRange(t *Torrent, offset int64, length int64) int

AvailableRange returns the number of copies of a subrange of a torrent.

type BFile

type BFile struct {
	Path   path.Path `bencode:"path"`
	Path8  path.Path `bencode:"path.utf-8,omitempty"`
	Length int64     `bencode:"length"`
	Attr   string    `bencode:"attr,omitempty"`
}

BFile is a file entry in a torrent file.

type BInfo

type BInfo struct {
	Name        string  `bencode:"name"`
	Name8       string  `bencode:"name.utf-8,omitempty"`
	PieceLength uint32  `bencode:"piece length"`
	Pieces      []byte  `bencode:"pieces"`
	Length      int64   `bencode:"length"`
	Files       []BFile `bencode:"files,omitempty"`
}

BInfo is the info dictionary of a torrent file.

type BTorrent

type BTorrent struct {
	Info         bencode.RawMessage `bencode:"info"`
	CreationDate int64              `bencode:"creation date,omitempty"`
	Announce     string             `bencode:"announce,omitempty"`
	AnnounceList [][]string         `bencode:"announce-list,omitempty"`
	URLList      listOrString       `bencode:"url-list,omitempty"`
	HTTPSeeds    listOrString       `bencode:"httpseeds,omitempty"`
}

BTorrent stores the contents of a torrent file.

type ParseURLError

type ParseURLError struct {
	Err error
}

func (ParseURLError) Error

func (e ParseURLError) Error() string

func (ParseURLError) Unwrap

func (e ParseURLError) Unwrap() error

type Reader

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

A Reader reads data from a torrent. It can span piece boundaries.

func (*Reader) Close

func (r *Reader) Close() error

Close closes a reader. Any in-flight requests are cancelled.

func (*Reader) Read

func (r *Reader) Read(a []byte) (n int, err error)

Read reads data from a torrent, performing prefetch as necessary.

func (*Reader) Seek

func (r *Reader) Seek(o int64, whence int) (n int64, err error)

Seek sets the position of a Reader. It doesn't trigger prefetch.

func (*Reader) SetContext

func (r *Reader) SetContext(ctx context.Context)

SetContext changes the context that will abort any requests sent by the reader.

type Requested

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

Requested is a set of pieces that are requested for a torrent.

func (*Requested) Add

func (rs *Requested) Add(index uint32, prio int8, want bool) (<-chan struct{}, bool)

Add adds a piece to a set of requested pieces. It returns a channel that will be closed when the piece is complete, as well as a boolean that indicates if the priority of the piece has been increased.

func (*Requested) Count

func (rs *Requested) Count(f func(uint32) bool) int

Count counts the number of request pieces that satisfy a given predicate.

func (*Requested) Del

func (rs *Requested) Del(index uint32, prio int8) bool

Del deletes a request for a piece. It returns true if the piece has been cancelled (no other clients are requesting this piece).

func (*Requested) DelIdle

func (rs *Requested) DelIdle()

DelIdle cancels all pieces that are not currently requested by any client.

func (*Requested) DelIdlePiece

func (rs *Requested) DelIdlePiece(index uint32)

DelIdlePiece deletes a given piece only if it is not currently requested by a client.

func (*Requested) Done

func (rs *Requested) Done(index uint32)

Done is called to indicate that a piece has finished downloading.

type RequestedPiece

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

type Torfile

type Torfile struct {
	Path    path.Path
	Offset  int64 // offset within the torrent
	Length  int64 // length of the file
	Padding bool  // true if a padding file
}

Torfile represents a file within a torrent.

type Torrent

type Torrent struct {
	Hash         hash.Hash
	MyId         hash.Hash
	Info         []byte // raw info dictionary
	CreationDate int64

	PieceHashes []hash.Hash
	Name        string
	Files       []Torfile // nil if single-file torrent
	Pieces      piece.Pieces

	Event   chan peer.TorEvent
	Done    chan struct{}
	Deleted chan struct{}

	Log *log.Logger
	// contains filtered or unexported fields
}

Torrent represents an active torrent.

func AddTorrent

func AddTorrent(ctx context.Context, t *Torrent) (*Torrent, error)

AddTorrent starts a new torrent.

func Get

func Get(hash hash.Hash) *Torrent

Get finds a torrent by hash.

func GetByName

func GetByName(name string) *Torrent

GetByName gets a torrent by name. If behaves deterministically if multiple torrents have the same name.

func GetTorrent

func GetTorrent(ctx context.Context, proxy string, url string) (*Torrent, error)

GetTorrent fetches a torrent file from a web server.

func New

func New(proxy string, hsh hash.Hash, dn string,
	info []byte, cdate int64, announce [][]tracker.Tracker,
	webseeds []webseed.Webseed) (*Torrent, error)

func ReadMagnet

func ReadMagnet(proxy string, m string) (*Torrent, error)

ReadMagnet parses a magnet link.

func ReadTorrent

func ReadTorrent(proxy string, r io.Reader) (*Torrent, error)

ReadTorrent reads a torrent from an io.Reader. The given proxy will be used when accessing trackers for this torrent.

func (*Torrent) AddKnown

func (t *Torrent) AddKnown(ip net.IP, port int, id hash.Hash, version string,
	kind known.Kind) error

func (*Torrent) BadPeer

func (t *Torrent) BadPeer(p uint32, bad bool) error

BadPeer is called when a peer participated in a failed piece.

func (*Torrent) BadPeers

func (t *Torrent) BadPeers(peers []uint32, bad bool)

func (*Torrent) DropPeer

func (t *Torrent) DropPeer() (bool, error)

DropPeer requests that a peer should be dropped.

func (*Torrent) GetAvailable

func (t *Torrent) GetAvailable() (Available, error)

GetAvailable returns information about piece availability.

func (*Torrent) GetConf

func (t *Torrent) GetConf() (peer.TorConf, error)

GetConf returns the configuration of a torrent.

func (*Torrent) GetKnown

func (t *Torrent) GetKnown(id hash.Hash, ip net.IP, port int) (*known.Peer, error)

func (*Torrent) GetKnowns

func (t *Torrent) GetKnowns() ([]known.Peer, error)

func (*Torrent) GetPeer

func (t *Torrent) GetPeer(id hash.Hash) (*peer.Peer, error)

GetPeer returns a given peer.

func (*Torrent) GetPeers

func (t *Torrent) GetPeers() ([]*peer.Peer, error)

GetPeers returns all peers of a torrent.

func (*Torrent) GetStats

func (t *Torrent) GetStats() (*peer.TorStats, error)

func (*Torrent) Have

func (t *Torrent) Have(index uint32, have bool) error

Have indicates that a piece is available.

func (*Torrent) InfoComplete

func (t *Torrent) InfoComplete() bool

InfoComplete returns true if the metadata of a torrent is complete.

func (*Torrent) Kill

func (t *Torrent) Kill(ctx context.Context) error

func (*Torrent) MetadataComplete

func (torrent *Torrent) MetadataComplete() error

MetadataComplete must be called when a torrent's metadata is complete.

func (*Torrent) NewPeer

func (t *Torrent) NewPeer(proxy string, conn net.Conn, ip net.IP, port int, incoming bool, result protocol.HandshakeResult, init []byte) error

func (*Torrent) NewReader

func (t *Torrent) NewReader(ctx context.Context, offset int64, length int64) *Reader

NewReader creates a new Reader.

func (*Torrent) Request

func (t *Torrent) Request(index uint32, prio int8, request bool, want bool) (bool, <-chan struct{}, error)

Request requests a piece from a torrent with given priority and updates the piece's access time. It returns a boolean indicating whether the piece was requested and a channel that will be closed when the piece is complete (nil if already complete).

func (*Torrent) SetConf

func (t *Torrent) SetConf(conf peer.TorConf) error

SetConf reconfigures a torrent.

func (*Torrent) Trackers

func (t *Torrent) Trackers() [][]tracker.Tracker

Trackers returns the set of trackers of a torrent.

func (*Torrent) Webseeds

func (t *Torrent) Webseeds() []webseed.Webseed

Webseeds returns the set of webseeds of a torrent.

type UnknownSchemeError

type UnknownSchemeError struct {
	Scheme string
}

func (UnknownSchemeError) Error

func (e UnknownSchemeError) Error() string

Directories

Path Synopsis
Package piece implements a data structure used for storing the actual contents of a torrent.
Package piece implements a data structure used for storing the actual contents of a torrent.

Jump to

Keyboard shortcuts

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