httptracker

package
v0.0.0-...-e294334 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package httptracker implements the tracker protocol based on HTTP/HTTPS.

You can use the package to implement a HTTP tracker server to track the information that other peers upload or download the file, or to create a HTTP tracker client to communicate with the HTTP tracker server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Base32

func Base32(b64addr string) string

func NewRequestWithContext

func NewRequestWithContext(c context.Context, method, url string, body io.Reader) (*http.Request, error)

NewRequestWithContext returns a new Request given a method, URL, and optional body.

Types

type AnnounceRequest

type AnnounceRequest struct {
	// InfoHash is the sha1 hash of the bencoded form of the info value from the metainfo file.
	InfoHash metainfo.Hash `bencode:"info_hash"` // BEP 3

	// PeerID is the id of the downloader.
	//
	// Each downloader generates its own id at random at the start of a new download.
	PeerID metainfo.Hash `bencode:"peer_id"` // BEP 3

	// Uploaded is the total amount uploaded so far, encoded in base ten ascii.
	Uploaded int64 `bencode:"uploaded"` // BEP 3

	// Downloaded is the total amount downloaded so far, encoded in base ten ascii.
	Downloaded int64 `bencode:"downloaded"` // BEP 3

	// Left is the number of bytes this peer still has to download,
	// encoded in base ten ascii.
	//
	// Note that this can't be computed from downloaded and the file length
	// since it might be a resume, and there's a chance that some of the
	// downloaded data failed an integrity check and had to be re-downloaded.
	//
	// If less than 0, math.MaxInt64 will be used for HTTP trackers instead.
	Left int64 `bencode:"left"` // BEP 3

	// Port is the port that this peer is listening on.
	//
	// Common behavior is for a downloader to try to listen on port 6881,
	// and if that port is taken try 6882, then 6883, etc. and give up after 6889.
	Port uint16 `bencode:"port"` // BEP 3

	// IP is the ip or DNS name which this peer is at, which generally used
	// for the origin if it's on the same machine as the tracker.
	//
	// Optional.
	IP string `bencode:"ip,omitempty"` // BEP 3

	// If not present, this is one of the announcements done at regular intervals.
	// An announcement using started is sent when a download first begins,
	// and one using completed is sent when the download is complete.
	// No completed is sent if the file was complete when started.
	// Downloaders send an announcement using stopped when they cease downloading.
	//
	// Optional
	Event uint32 `bencode:"event,omitempty"` // BEP 3

	// Compact indicates whether it hopes the tracker to return the compact
	// peer lists.
	//
	// Optional
	Compact bool `bencode:"compact,omitempty"` // BEP 23

	// NumWant is the number of peers that the client would like to receive
	// from the tracker. This value is permitted to be zero. If omitted,
	// typically defaults to 50 peers.
	//
	// See https://wiki.theory.org/index.php/BitTorrentSpecification
	//
	// Optional.
	NumWant int32 `bencode:"numwant,omitempty"`

	Key int32 `bencode:"key,omitempty"`
}

AnnounceRequest is the tracker announce requests.

BEP 3

func (*AnnounceRequest) FromQuery

func (r *AnnounceRequest) FromQuery(vs url.Values) (err error)

FromQuery converts URL Query to itself.

func (AnnounceRequest) ToQuery

func (r AnnounceRequest) ToQuery() (vs url.Values)

ToQuery converts the Request to URL Query.

type AnnounceResponse

type AnnounceResponse struct {
	FailureReason string `bencode:"failure reason,omitempty"`

	// Interval is the seconds the downloader should wait before next rerequest.
	Interval uint32 `bencode:"interval,omitempty"` // BEP 3

	// Peers is the list of the peers.
	Peers Peers `bencode:"peers,omitempty"` // BEP 3, BEP 23

	// Peers6 is only used for ipv6 in the compact case.
	Peers6 Peers6 `bencode:"peers6,omitempty"` // BEP 7

	// Complete is the number of peers with the entire file.
	Complete uint32 `bencode:"complete,omitempty"`
	// Incomplete is the number of non-seeder peers.
	Incomplete uint32 `bencode:"incomplete,omitempty"`
	// TrackerID is that the client should send back on its next announcements.
	// If absent and a previous announce sent a tracker id,
	// do not discard the old value; keep using it.
	TrackerID string `bencode:"tracker id,omitempty"`
}

AnnounceResponse is a announce response.

type Client

type Client struct {
	Client      *http.Client
	ID          metainfo.Hash
	AnnounceURL string
	ScrapeURL   string
}

Client represents a tracker client based on HTTP/HTTPS.

func NewClient

func NewClient(announceURL, scrapeURL string) *Client

NewClient returns a new HTTPClient.

scrapeURL may be empty, which will replace the "announce" in announceURL with "scrape" to generate the scrapeURL.

func (*Client) Announce

func (t *Client) Announce(c context.Context, req *AnnounceRequest) (resp AnnounceResponse, err error)

Announce sends a Announce request to the tracker.

func (*Client) Close

func (t *Client) Close() error

Close closes the client, which does nothing at present.

func (*Client) Scrape

func (t *Client) Scrape(c context.Context, infohashes []metainfo.Hash) (resp ScrapeResponse, err error)

Scrape sends a Scrape request to the tracker.

func (*Client) String

func (t *Client) String() string

type Peer

type Peer struct {
	// ID is the peer's self-selected ID.
	ID string `bencode:"peer id"` // BEP 3

	// IP is the IP address or dns name.
	IP   string `bencode:"ip"`   // BEP 3
	Port uint16 `bencode:"port"` // BEP 3
}

Peer is a tracker peer.

func (Peer) Addresses

func (p Peer) Addresses() (addrs []metainfo.Address, err error)

Addresses returns the list of the addresses that the peer listens on.

type Peers

type Peers []Peer

Peers is a set of the peers.

func (Peers) MarshalBencode

func (ps Peers) MarshalBencode() (b []byte, err error)

MarshalBencode implements the interface bencode.Marshaler.

func (*Peers) UnmarshalBencode

func (ps *Peers) UnmarshalBencode(b []byte) (err error)

UnmarshalBencode implements the interface bencode.Unmarshaler.

type Peers6

type Peers6 []Peer

Peers6 is a set of the peers for IPv6 in the compact case.

BEP 7

func (Peers6) MarshalBencode

func (ps Peers6) MarshalBencode() (b []byte, err error)

MarshalBencode implements the interface bencode.Marshaler.

func (*Peers6) UnmarshalBencode

func (ps *Peers6) UnmarshalBencode(b []byte) (err error)

UnmarshalBencode implements the interface bencode.Unmarshaler.

type ScrapeResponse

type ScrapeResponse struct {
	FailureReason string `bencode:"failure_reason,omitempty"`

	Files map[metainfo.Hash]ScrapeResponseResult `bencode:"files,omitempty"`
}

ScrapeResponse represents a Scrape response.

BEP 48

func (*ScrapeResponse) DecodeFrom

func (sr *ScrapeResponse) DecodeFrom(r io.Reader) (err error)

DecodeFrom reads the []byte data from r and decodes them to sr by bencode.

r may be the body of the request from the http client.

func (ScrapeResponse) EncodeTo

func (sr ScrapeResponse) EncodeTo(w io.Writer) (err error)

EncodeTo encodes the response to []byte by bencode and write the result into w.

w may be http.ResponseWriter.

type ScrapeResponseResult

type ScrapeResponseResult struct {
	// Complete is the number of active peers that have completed downloading.
	Complete uint32 `bencode:"complete"` // BEP 48

	// Incomplete is the number of active peers that have not completed downloading.
	Incomplete uint32 `bencode:"incomplete"` // BEP 48

	// The number of peers that have ever completed downloading.
	Downloaded uint32 `bencode:"downloaded"` // BEP 48
}

ScrapeResponseResult is the result of the scraped file.

Jump to

Keyboard shortcuts

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