models

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2016 License: BSD-2-Clause Imports: 7 Imported by: 65

Documentation

Overview

Package models implements the common data types used throughout a BitTorrent tracker.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMalformedRequest is returned when a request does not contain the
	// required parameters needed to create a model.
	ErrMalformedRequest = ClientError("malformed request")

	// ErrBadRequest is returned when a request is invalid in the peer's
	// current state. For example, announcing a "completed" event while
	// not a leecher or a "stopped" event while not active.
	ErrBadRequest = ClientError("bad request")

	// ErrTorrentDNE is returned when a torrent does not exist.
	ErrTorrentDNE = NotFoundError("torrent does not exist")

	// ErrClientUnapproved is returned when a clientID is not in the whitelist.
	ErrClientUnapproved = ClientError("client is not approved")
)

Functions

func AppendPeer

func AppendPeer(ipv4s, ipv6s *PeerList, ann *Announce, peer *Peer) int

AppendPeer adds a peer to its corresponding peerlist.

func IsPublicError

func IsPublicError(err error) bool

IsPublicError determines whether an error should be propogated to the client.

Types

type Announce

type Announce struct {
	Config *config.Config `json:"config"`

	Compact    bool     `json:"compact"`
	Downloaded uint64   `json:"downloaded"`
	Event      string   `json:"event"`
	IPv4       Endpoint `json:"ipv4"`
	IPv6       Endpoint `json:"ipv6"`
	Infohash   string   `json:"infohash"`
	Left       uint64   `json:"left"`
	NumWant    int      `json:"numwant"`
	PeerID     string   `json:"peer_id"`
	Uploaded   uint64   `json:"uploaded"`
	JWT        string   `json:"jwt"`

	Torrent *Torrent `json:"-"`
	Peer    *Peer    `json:"-"`
	PeerV4  *Peer    `json:"-"` // Only valid if HasIPv4() is true.
	PeerV6  *Peer    `json:"-"` // Only valid if HasIPv6() is true.
}

Announce is an Announce by a Peer.

func (*Announce) BuildPeer

func (a *Announce) BuildPeer(t *Torrent)

BuildPeer creates the Peer representation of an Announce. BuildPeer creates one peer for each IP in the announce, and panics if there are none.

func (*Announce) ClientID

func (a *Announce) ClientID() (clientID string)

ClientID returns the part of a PeerID that identifies a Peer's client software.

func (*Announce) HasIPv4

func (a *Announce) HasIPv4() bool

HasIPv4 determines whether or not an announce has an IPv4 endpoint.

func (*Announce) HasIPv6

func (a *Announce) HasIPv6() bool

HasIPv6 determines whether or not an announce has an IPv6 endpoint.

type AnnounceResponse

type AnnounceResponse struct {
	Announce              *Announce
	Complete, Incomplete  int
	Interval, MinInterval time.Duration
	IPv4Peers, IPv6Peers  PeerList

	Compact bool
}

AnnounceResponse contains the information needed to fulfill an announce.

type ClientError

type ClientError string

func (ClientError) Error

func (e ClientError) Error() string

type Endpoint

type Endpoint struct {
	IP   net.IP `json:"ip"`
	Port uint16 `json:"port"`
}

Endpoint is an IP and port pair.

IP always has length net.IPv4len if IPv4, and net.IPv6len if IPv6.

type NotFoundError

type NotFoundError ClientError

func (NotFoundError) Error

func (e NotFoundError) Error() string

type Peer

type Peer struct {
	ID           string `json:"id"`
	Uploaded     uint64 `json:"uploaded"`
	Downloaded   uint64 `json:"downloaded"`
	Left         uint64 `json:"left"`
	LastAnnounce int64  `json:"lastAnnounce"`
	Endpoint
}

Peer represents a participant in a BitTorrent swarm.

func (*Peer) HasIPv4

func (p *Peer) HasIPv4() bool

HasIPv4 determines if a peer's IP address can be represented as an IPv4 address.

func (*Peer) HasIPv6

func (p *Peer) HasIPv6() bool

HasIPv6 determines if a peer's IP address can be represented as an IPv6 address.

func (*Peer) Key

func (p *Peer) Key() PeerKey

Key returns a PeerKey for the given peer.

type PeerKey

type PeerKey string

PeerKey is the key used to uniquely identify a peer in a swarm.

func NewPeerKey

func NewPeerKey(peerID string, ip net.IP) PeerKey

NewPeerKey creates a properly formatted PeerKey.

func (PeerKey) IP

func (pk PeerKey) IP() net.IP

IP parses and returns the IP address for a given PeerKey.

func (PeerKey) PeerID

func (pk PeerKey) PeerID() string

PeerID returns the PeerID section of a PeerKey.

type PeerList

type PeerList []Peer

PeerList represents a list of peers: either seeders or leechers.

type PeerMap

type PeerMap struct {
	Peers   map[string]map[PeerKey]Peer `json:"peers"`
	Seeders bool                        `json:"seeders"`
	Config  config.SubnetConfig         `json:"config"`
	Size    int32                       `json:"size"`
	sync.RWMutex
}

PeerMap is a thread-safe map from PeerKeys to Peers. When PreferredSubnet is enabled, it is a thread-safe map of maps from MaskedIPs to Peerkeys to Peers.

func NewPeerMap

func NewPeerMap(seeders bool, cfg *config.Config) *PeerMap

NewPeerMap initializes the map for a new PeerMap.

func (*PeerMap) AppendPeers

func (pm *PeerMap) AppendPeers(ipv4s, ipv6s PeerList, ann *Announce, wanted int) (PeerList, PeerList)

AppendPeers adds peers to given IPv4 or IPv6 lists.

func (*PeerMap) Contains

func (pm *PeerMap) Contains(pk PeerKey) bool

Contains is true if a peer is contained with a PeerMap.

func (*PeerMap) Delete

func (pm *PeerMap) Delete(pk PeerKey)

Delete is a thread-safe delete from a PeerMap.

func (*PeerMap) Len

func (pm *PeerMap) Len() int

Len returns the number of peers within a PeerMap.

func (*PeerMap) LookUp

func (pm *PeerMap) LookUp(pk PeerKey) (peer Peer, exists bool)

LookUp is a thread-safe read from a PeerMap.

func (*PeerMap) Purge

func (pm *PeerMap) Purge(unixtime int64)

Purge iterates over all of the peers within a PeerMap and deletes them if they are older than the provided time.

func (*PeerMap) Put

func (pm *PeerMap) Put(p Peer)

Put is a thread-safe write to a PeerMap.

type ProtocolError

type ProtocolError ClientError

func (ProtocolError) Error

func (e ProtocolError) Error() string

type Scrape

type Scrape struct {
	Config     *config.Config `json:"config"`
	Infohashes []string
}

Scrape is a Scrape by a Peer.

type ScrapeResponse

type ScrapeResponse struct {
	Files []*Torrent
}

ScrapeResponse contains the information needed to fulfill a scrape.

type Torrent

type Torrent struct {
	Infohash   string `json:"infohash"`
	Snatches   uint64 `json:"snatches"`
	LastAction int64  `json:"lastAction"`

	Seeders  *PeerMap `json:"seeders"`
	Leechers *PeerMap `json:"leechers"`
}

Torrent represents a BitTorrent swarm and its metadata.

func (*Torrent) PeerCount

func (t *Torrent) PeerCount() int

PeerCount returns the total number of peers connected on this Torrent.

Jump to

Keyboard shortcuts

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