torrent

package
v0.0.0-...-f18cc39 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2013 License: BSD-3-Clause, MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MIN_PEER_THRESHOLD = 30
	DEFAULT_NUMWANT    = 30
)

Performance tuning constants.

View Source
const (
	SEEDING  PeerStatus = iota // Announced a complete copy of the file.
	LEECHING            = iota // Announced an incomplete copy of the file.
	QUASI               = iota // Announced an incomplete copy; but does not appear to be downloading.
)

Variables

This section is empty.

Functions

func CheckHmac

func CheckHmac(secret, hash string) bool

Compares a message (the secret) and an HMAC of that message against a secret-key.

This ensures that the .torrent file originated from this tracker and is linked to an active user account.

The secret and hash are []byte arrays and stored as such.
They must be deserialized

func DecodeInfoDict

func DecodeInfoDict(bencodedInfo []byte) (map[string]interface{}, error)

Types

type BenPeer

type BenPeer struct {
	ID   string `bencode:"peer id"`
	IP   string `bencode:"ip"`
	Port int64  `bencode:"port"`
}

Simpler data structured used for non-compact peer lists.

type Peer

type Peer struct {
	ID     string
	IPAddr net.IP
	Port   uint16

	Status          PeerStatus // what is the peer doing?
	DownloadedBytes int64      // curently completed bytes
	UploadedBytes   int64      // currently uploaded bytes
	LeftBytes       int64      // number of bytes remaining

	LastSeen          time.Time // last announce at:
	LastCompleteBytes int64     // last completed bytes

	Secret string // uniquely identifies a peer
}

A peer being tracked on a torrent

func NewPeer

func NewPeer(peerId, ipAddr, port, secret string) *Peer

Creates a new peer object.

func (*Peer) EncodePeer

func (p *Peer) EncodePeer() *BenPeer

Returns a bencodable version of this peer.

func (*Peer) UpdateLastSeen

func (p *Peer) UpdateLastSeen()

Updates timestamp to current server time.

func (*Peer) UpdateStats

func (p *Peer) UpdateStats(uploaded, downloaded, left string) error

Updates download statistics and promotes the peer if necessary. Strings should be ASCII encoded base 10 numbers. (Per bep-003)

type PeerMap

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

A protected mapping of peerIds to peers.

This map is safe for multiple readers; but read and write calls will block while a writer holds the lock.

func NewPeerMap

func NewPeerMap() *PeerMap

Initalizes a new peer map for a torrent

func (*PeerMap) Map

func (pm *PeerMap) Map() map[string]*Peer

Returns the map.

func (*PeerMap) Sync

func (pm *PeerMap) Sync() *sync.RWMutex

Returns the syncrhonization primitive that can be used to protect concurrent access to this map.

This should be used wherever contention of the map is likely.

type PeerStatus

type PeerStatus int64

type Torrent

type Torrent struct {
	InfoHash string
	Info     *TorrentFile
	// contains filtered or unexported fields
}

Represents a torrent being actively used by the tracker. A torrent includes a [decoded] copy of the metainfo file as well as a list of active peers that is periodically culled.

func NewTorrent

func NewTorrent(file *TorrentFile) *Torrent

Writes a new torrent to be used by the tracker for maintaining peer lists.

func ReadFile

func ReadFile(file multipart.File) *Torrent

Reads a torrent-file from the filesystem. TODO: Model will create torrent-file; obsoleting this.

func (*Torrent) AddPeer

func (t *Torrent) AddPeer(peerId, ipAddr, port, secret string)

Updates the peer-list from an announce requeset.

func (*Torrent) EnumeratePeers

func (t *Torrent) EnumeratePeers() (int, int)

Returns the seeders followed by the leechers for this torrent.

func (*Torrent) GetPeerList

func (t *Torrent) GetPeerList(numWant int) (string, string)

Send numWant -1 for "no peers requested", 0 for default, and n if client wants more peers. Returns a ranked peerlist that attempts to maintain a balanced ratio of seeders:leechers. First return val is compact-form `peers` dict, second is `peers6` dict.

func (*Torrent) ReadPeers

func (t *Torrent) ReadPeers(closure func(map[string]*Peer))

Takes a closure which will obtain a readlock for this torrent's peer-map.

Reading from the torrent's peer map is safe from within the closure.

func (*Torrent) UpdateStatsFor

func (t *Torrent) UpdateStatsFor(peerId string, uploaded, downloaded, left string) error

Updates the in-memory statistics for a peer being tracked for this torrent. Returns an error if the peer is not found or the request cannot be fulfilled. The stats-collector job will ensure they get written to disk.

func (*Torrent) WritePeers

func (t *Torrent) WritePeers(closure func(map[string]*Peer))

Takes a closure which will obtain a writelock for this torrents' peer-map.

Updates to the map are safe within the context of the closure.

type TorrentFile

type TorrentFile struct {
	Announce     string                 `bencode:"announce"`
	Comment      string                 `bencode:"comment"`
	CreatedBy    string                 `bencode:"created by"`
	CreationDate int64                  `bencode:"creation date"`
	Encoding     string                 `bencode:"encoding"`
	Info         map[string]interface{} `bencode:"info"`
}

Represents a `babou` torrent. We don't care about other fields so they will be discarded from uploaded torrents.

func (*TorrentFile) BencodeInfoDict

func (t *TorrentFile) BencodeInfoDict() ([]byte, error)

Returns a bencoded version of the torrent's info dict.

func (*TorrentFile) EncodeInfo

func (t *TorrentFile) EncodeInfo() []byte

Encode's the `info` dictionary into a SHA1 hash; used to uniquely identify a torrent.

func (*TorrentFile) EncodeInfoToString

func (t *TorrentFile) EncodeInfoToString() string

Encode's the `info` dictionary into a SHA1 hash; used to uniquely identify a torrent. Converts hash to a string [two chars per byte]

func (*TorrentFile) WriteFile

func (t *TorrentFile) WriteFile(secret, hash []byte) ([]byte, error)

Converts torrent to SUPRA-PRIVATE torrent

Sets the private flag to 1 and embeds the supplied secret and hash for authentication purposes.

This torrent file SHOULD NOT be shared between users or statistics collection and anti-abuse mechanisms will be skewed for that user.

Jump to

Keyboard shortcuts

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