2016-08-04 20:08:26 +02:00
|
|
|
// Package bittorrent implements all of the abstractions used to decouple the
|
|
|
|
// protocol of a BitTorrent tracker from the logic of handling Announces and
|
|
|
|
// Scrapes.
|
2016-08-03 09:11:52 +02:00
|
|
|
package bittorrent
|
|
|
|
|
|
|
|
import (
|
2018-02-13 10:44:39 +01:00
|
|
|
"fmt"
|
2016-08-03 09:11:52 +02:00
|
|
|
"net"
|
|
|
|
"time"
|
2017-05-07 00:48:44 +02:00
|
|
|
|
2017-06-20 14:58:44 +02:00
|
|
|
"github.com/chihaya/chihaya/pkg/log"
|
2016-08-03 09:11:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// PeerID represents a peer ID.
|
|
|
|
type PeerID [20]byte
|
|
|
|
|
|
|
|
// PeerIDFromBytes creates a PeerID from a byte slice.
|
|
|
|
//
|
|
|
|
// It panics if b is not 20 bytes long.
|
|
|
|
func PeerIDFromBytes(b []byte) PeerID {
|
|
|
|
if len(b) != 20 {
|
|
|
|
panic("peer ID must be 20 bytes")
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf [20]byte
|
|
|
|
copy(buf[:], b)
|
|
|
|
return PeerID(buf)
|
|
|
|
}
|
|
|
|
|
2018-02-13 10:44:39 +01:00
|
|
|
// String implements fmt.Stringer, returning the base16 encoded PeerID.
|
2017-05-07 00:48:44 +02:00
|
|
|
func (p PeerID) String() string {
|
2018-02-13 10:44:39 +01:00
|
|
|
return fmt.Sprintf("%x", p[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// RawString returns a 20-byte string of the raw bytes of the ID.
|
|
|
|
func (p PeerID) RawString() string {
|
2017-05-07 00:48:44 +02:00
|
|
|
return string(p[:])
|
|
|
|
}
|
|
|
|
|
2016-08-03 09:11:52 +02:00
|
|
|
// PeerIDFromString creates a PeerID from a string.
|
|
|
|
//
|
|
|
|
// It panics if s is not 20 bytes long.
|
|
|
|
func PeerIDFromString(s string) PeerID {
|
|
|
|
if len(s) != 20 {
|
|
|
|
panic("peer ID must be 20 bytes")
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf [20]byte
|
|
|
|
copy(buf[:], s)
|
|
|
|
return PeerID(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InfoHash represents an infohash.
|
|
|
|
type InfoHash [20]byte
|
|
|
|
|
|
|
|
// InfoHashFromBytes creates an InfoHash from a byte slice.
|
|
|
|
//
|
|
|
|
// It panics if b is not 20 bytes long.
|
|
|
|
func InfoHashFromBytes(b []byte) InfoHash {
|
|
|
|
if len(b) != 20 {
|
|
|
|
panic("infohash must be 20 bytes")
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf [20]byte
|
|
|
|
copy(buf[:], b)
|
|
|
|
return InfoHash(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InfoHashFromString creates an InfoHash from a string.
|
|
|
|
//
|
|
|
|
// It panics if s is not 20 bytes long.
|
|
|
|
func InfoHashFromString(s string) InfoHash {
|
|
|
|
if len(s) != 20 {
|
|
|
|
panic("infohash must be 20 bytes")
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf [20]byte
|
|
|
|
copy(buf[:], s)
|
|
|
|
return InfoHash(buf)
|
|
|
|
}
|
|
|
|
|
2018-02-13 10:44:39 +01:00
|
|
|
// String implements fmt.Stringer, returning the base16 encoded InfoHash.
|
2017-05-07 00:48:44 +02:00
|
|
|
func (i InfoHash) String() string {
|
2018-02-13 10:44:39 +01:00
|
|
|
return fmt.Sprintf("%x", i[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// RawString returns a 20-byte string of the raw bytes of the InfoHash.
|
|
|
|
func (i InfoHash) RawString() string {
|
2017-05-07 00:48:44 +02:00
|
|
|
return string(i[:])
|
|
|
|
}
|
|
|
|
|
2016-08-03 09:11:52 +02:00
|
|
|
// AnnounceRequest represents the parsed parameters from an announce request.
|
|
|
|
type AnnounceRequest struct {
|
2017-10-08 19:19:55 +02:00
|
|
|
Event Event
|
|
|
|
InfoHash InfoHash
|
|
|
|
Compact bool
|
|
|
|
EventProvided bool
|
|
|
|
NumWantProvided bool
|
|
|
|
IPProvided bool
|
|
|
|
NumWant uint32
|
|
|
|
Left uint64
|
|
|
|
Downloaded uint64
|
|
|
|
Uploaded uint64
|
2016-08-03 09:11:52 +02:00
|
|
|
|
|
|
|
Peer
|
|
|
|
Params
|
|
|
|
}
|
|
|
|
|
2017-10-18 04:02:06 +02:00
|
|
|
// LogFields renders the current response as a set of log fields.
|
|
|
|
func (r AnnounceRequest) LogFields() log.Fields {
|
|
|
|
return log.Fields{
|
|
|
|
"event": r.Event,
|
|
|
|
"infoHash": r.InfoHash,
|
|
|
|
"compact": r.Compact,
|
|
|
|
"eventProvided": r.EventProvided,
|
|
|
|
"numWantProvided": r.NumWantProvided,
|
|
|
|
"ipProvided": r.IPProvided,
|
|
|
|
"numWant": r.NumWant,
|
|
|
|
"left": r.Left,
|
|
|
|
"downloaded": r.Downloaded,
|
|
|
|
"uploaded": r.Uploaded,
|
|
|
|
"peer": r.Peer,
|
|
|
|
"params": r.Params,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-03 09:11:52 +02:00
|
|
|
// AnnounceResponse represents the parameters used to create an announce
|
|
|
|
// response.
|
|
|
|
type AnnounceResponse struct {
|
|
|
|
Compact bool
|
2016-09-08 16:46:16 +02:00
|
|
|
Complete uint32
|
|
|
|
Incomplete uint32
|
2016-08-03 09:11:52 +02:00
|
|
|
Interval time.Duration
|
|
|
|
MinInterval time.Duration
|
|
|
|
IPv4Peers []Peer
|
|
|
|
IPv6Peers []Peer
|
|
|
|
}
|
|
|
|
|
2017-10-18 04:02:06 +02:00
|
|
|
// LogFields renders the current response as a set of log fields.
|
|
|
|
func (r AnnounceResponse) LogFields() log.Fields {
|
2017-05-07 00:48:44 +02:00
|
|
|
return log.Fields{
|
2017-10-18 04:02:06 +02:00
|
|
|
"compact": r.Compact,
|
|
|
|
"complete": r.Complete,
|
|
|
|
"interval": r.Interval,
|
|
|
|
"minInterval": r.MinInterval,
|
|
|
|
"ipv4Peers": r.IPv4Peers,
|
|
|
|
"ipv6Peers": r.IPv6Peers,
|
2017-05-07 00:48:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-03 09:11:52 +02:00
|
|
|
// ScrapeRequest represents the parsed parameters from a scrape request.
|
|
|
|
type ScrapeRequest struct {
|
2017-01-20 20:34:39 +01:00
|
|
|
AddressFamily AddressFamily
|
|
|
|
InfoHashes []InfoHash
|
|
|
|
Params Params
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2017-10-18 04:02:06 +02:00
|
|
|
// LogFields renders the current response as a set of log fields.
|
|
|
|
func (r ScrapeRequest) LogFields() log.Fields {
|
|
|
|
return log.Fields{
|
|
|
|
"addressFamily": r.AddressFamily,
|
|
|
|
"infoHashes": r.InfoHashes,
|
|
|
|
"params": r.Params,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-03 09:11:52 +02:00
|
|
|
// ScrapeResponse represents the parameters used to create a scrape response.
|
2017-01-29 18:47:07 +01:00
|
|
|
//
|
|
|
|
// The Scrapes must be in the same order as the InfoHashes in the corresponding
|
|
|
|
// ScrapeRequest.
|
2016-08-03 09:11:52 +02:00
|
|
|
type ScrapeResponse struct {
|
2017-01-29 18:47:07 +01:00
|
|
|
Files []Scrape
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2017-05-07 00:48:44 +02:00
|
|
|
// LogFields renders the current response as a set of Logrus fields.
|
|
|
|
func (sr ScrapeResponse) LogFields() log.Fields {
|
|
|
|
return log.Fields{
|
|
|
|
"files": sr.Files,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-03 09:11:52 +02:00
|
|
|
// Scrape represents the state of a swarm that is returned in a scrape response.
|
|
|
|
type Scrape struct {
|
2017-01-29 18:47:07 +01:00
|
|
|
InfoHash InfoHash
|
2016-08-03 09:11:52 +02:00
|
|
|
Snatches uint32
|
|
|
|
Complete uint32
|
|
|
|
Incomplete uint32
|
|
|
|
}
|
|
|
|
|
2016-11-28 20:55:04 +01:00
|
|
|
// AddressFamily is the address family of an IP address.
|
|
|
|
type AddressFamily uint8
|
|
|
|
|
2017-10-18 04:02:06 +02:00
|
|
|
func (af AddressFamily) String() string {
|
|
|
|
switch af {
|
|
|
|
case IPv4:
|
|
|
|
return "IPv4"
|
|
|
|
case IPv6:
|
|
|
|
return "IPv6"
|
|
|
|
default:
|
|
|
|
panic("tried to print unknown AddressFamily")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 20:55:04 +01:00
|
|
|
// AddressFamily constants.
|
|
|
|
const (
|
|
|
|
IPv4 AddressFamily = iota
|
|
|
|
IPv6
|
|
|
|
)
|
|
|
|
|
|
|
|
// IP is a net.IP with an AddressFamily.
|
|
|
|
type IP struct {
|
|
|
|
net.IP
|
|
|
|
AddressFamily
|
|
|
|
}
|
|
|
|
|
2017-10-18 04:02:06 +02:00
|
|
|
func (ip IP) String() string {
|
|
|
|
return ip.IP.String()
|
|
|
|
}
|
|
|
|
|
2016-08-03 09:11:52 +02:00
|
|
|
// Peer represents the connection details of a peer that is returned in an
|
|
|
|
// announce response.
|
|
|
|
type Peer struct {
|
|
|
|
ID PeerID
|
2016-11-28 20:55:04 +01:00
|
|
|
IP IP
|
2016-08-03 09:11:52 +02:00
|
|
|
Port uint16
|
|
|
|
}
|
|
|
|
|
2018-02-13 10:44:39 +01:00
|
|
|
// String implements fmt.Stringer to return a human-readable representation.
|
|
|
|
// The string will have the format <PeerID>@[<IP>]:<port>, for example
|
|
|
|
// "0102030405060708090a0b0c0d0e0f1011121314@[10.11.12.13]:1234"
|
|
|
|
func (p Peer) String() string {
|
|
|
|
return fmt.Sprintf("%s@[%s]:%d", p.ID.String(), p.IP.String(), p.Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogFields renders the current peer as a set of Logrus fields.
|
|
|
|
func (p Peer) LogFields() log.Fields {
|
|
|
|
return log.Fields{
|
|
|
|
"ID": p.ID,
|
|
|
|
"IP": p.IP,
|
|
|
|
"port": p.Port,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-03 09:11:52 +02:00
|
|
|
// Equal reports whether p and x are the same.
|
|
|
|
func (p Peer) Equal(x Peer) bool { return p.EqualEndpoint(x) && p.ID == x.ID }
|
|
|
|
|
|
|
|
// EqualEndpoint reports whether p and x have the same endpoint.
|
2016-11-28 20:55:04 +01:00
|
|
|
func (p Peer) EqualEndpoint(x Peer) bool { return p.Port == x.Port && p.IP.Equal(x.IP.IP) }
|
2016-08-03 09:11:52 +02:00
|
|
|
|
|
|
|
// ClientError represents an error that should be exposed to the client over
|
|
|
|
// the BitTorrent protocol implementation.
|
|
|
|
type ClientError string
|
|
|
|
|
|
|
|
// Error implements the error interface for ClientError.
|
|
|
|
func (c ClientError) Error() string { return string(c) }
|