models

package
v0.0.0-...-c57407d Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2021 License: BSD-2-Clause Imports: 6 Imported by: 0

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")

	// ErrUserDNE is returned when a user does not exist.
	ErrUserDNE = NotFoundError("user does not exist")

	// 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")

	// ErrInvalidPasskey is returned when a passkey is not properly formatted.
	ErrInvalidPasskey = ClientError("passkey is invalid")
)

Functions

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"`
	Infohash   string `json:"infohash"`
	Left       uint64 `json:"left"`
	NumWant    int    `json:"numwant"`
	Passkey    string `json:"passkey"`
	PeerID     string `json:"peer_id"`
	Uploaded   uint64 `json:"uploaded"`

	IP   string `json:"ip"`
	Port uint16 `json:"port"`

	Torrent *Torrent `json:"-"`
	User    *User    `json:"-"`
	Peer    *Peer    `json:"-"`
}

Announce is an Announce by a Peer.

func (*Announce) BuildPeer

func (a *Announce) BuildPeer(u *User, t *Torrent) (err error)

BuildPeer creates the Peer representation of an Announce. When provided nil for the user or torrent parameter, it creates a Peer{UserID: 0} or Peer{TorrentID: 0}, respectively.

func (*Announce) ClientID

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

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

type AnnounceDelta

type AnnounceDelta struct {
	Peer    *Peer
	Torrent *Torrent
	User    *User

	// Created is true if this announce created a new peer or changed an existing
	// peer's address
	Created bool
	// Snatched is true if this announce completed the download
	Snatched bool

	// Uploaded contains the upload delta for this announce, in bytes
	Uploaded    uint64
	RawUploaded uint64

	// Downloaded contains the download delta for this announce, in bytes
	Downloaded    uint64
	RawDownloaded uint64
}

AnnounceDelta contains the changes to a Peer's state. These changes are recorded by the backend driver.

type AnnounceResponse

type AnnounceResponse struct {
	Announce              *Announce
	Complete, Incomplete  int
	Interval, MinInterval int64
	Peers                 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 NotFoundError

type NotFoundError ClientError

func (NotFoundError) Error

func (e NotFoundError) Error() string

type Peer

type Peer struct {
	IP           string `json:"ip"`
	Port         uint16 `json:"port"`
	ID           string `json:"id"`
	UserID       uint64 `json:"userId"`
	TorrentID    uint64 `json:"torrentId"`
	Uploaded     uint64 `json:"uploaded"`
	Downloaded   uint64 `json:"downloaded"`
	Left         uint64 `json:"left"`
	LastAnnounce int64  `json:"lastAnnounce"`
}

Peer represents a participant in a BitTorrent swarm.

func (*Peer) Key

func (p *Peer) Key() PeerKey

Key returns a PeerKey for the given peer.

func (*Peer) MarshalBencode

func (p *Peer) MarshalBencode() ([]byte, error)

MarshalBencode implements bencode writing format

type PeerKey

type PeerKey string

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

func NewPeerKey

func NewPeerKey(peerID, pub string) PeerKey

NewPeerKey creates a properly formatted PeerKey given public addresses

func (PeerKey) Addr

func (pk PeerKey) Addr() string

Addr returns the address of a peer key

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[PeerKey]Peer
	Seeders bool `json:"seeders"`
	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(peers PeerList, a *Announce, wanted int) (ls PeerList)

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"`

	Passkey    string
	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 {
	ID       uint64 `json:"id"`
	Infohash string `json:"infohash"`

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

	Snatches       uint64  `json:"snatches"`
	UpMultiplier   float64 `json:"upMultiplier"`
	DownMultiplier float64 `json:"downMultiplier"`
	LastAction     int64   `json:"lastAction"`

	Info *TorrentInfo `json:"info"`
}

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.

type TorrentCategory

type TorrentCategory struct {
	ID          int    `json:"id"`
	Name        string `json:"name"`
	Description string `json:"desc"`
}

TorrentCategory contains all info describing a category of torrents on the index

type TorrentInfo

type TorrentInfo struct {
	UserID      uint64   `json:"owner_user_id"`
	UploadDate  int64    `json:"uploaded"`
	Category    string   `json:"category"`
	TorrentName string   `json:"name"`
	Description string   `json:"desc"`
	Files       []string `json:"files"`
	Tags        []string `json:"tags"`
}

TorrentInfo holds all index metadata for a torrent on private trackers

type User

type User struct {
	ID             uint64  `json:"id"`
	Passkey        string  `json:"passkey"`
	Username       string  `json:"username"`
	Cred           string  `json:"credential"`
	UpMultiplier   float64 `json:"upMultiplier"`
	DownMultiplier float64 `json:"downMultiplier"`
}

User is a registered user for private trackers.

Jump to

Keyboard shortcuts

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