2016-08-03 09:11:52 +02:00
|
|
|
package udp
|
|
|
|
|
|
|
|
import (
|
2016-08-31 04:21:05 +02:00
|
|
|
"bytes"
|
2016-08-03 09:11:52 +02:00
|
|
|
"encoding/binary"
|
2016-08-31 04:21:05 +02:00
|
|
|
"fmt"
|
2016-08-03 09:11:52 +02:00
|
|
|
"net"
|
2016-08-31 04:21:05 +02:00
|
|
|
"sync"
|
2016-08-03 09:11:52 +02:00
|
|
|
|
2016-08-17 03:42:08 +02:00
|
|
|
"github.com/chihaya/chihaya/bittorrent"
|
2016-08-03 09:11:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
connectActionID uint32 = iota
|
|
|
|
announceActionID
|
|
|
|
scrapeActionID
|
|
|
|
errorActionID
|
2016-09-02 22:42:01 +02:00
|
|
|
announceV6ActionID
|
2016-08-03 09:11:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Option-Types as described in BEP 41 and BEP 45.
|
|
|
|
const (
|
|
|
|
optionEndOfOptions byte = 0x0
|
|
|
|
optionNOP = 0x1
|
|
|
|
optionURLData = 0x2
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// initialConnectionID is the magic initial connection ID specified by BEP 15.
|
|
|
|
initialConnectionID = []byte{0, 0, 0x04, 0x17, 0x27, 0x10, 0x19, 0x80}
|
|
|
|
|
|
|
|
// eventIDs map values described in BEP 15 to Events.
|
|
|
|
eventIDs = []bittorrent.Event{
|
|
|
|
bittorrent.None,
|
|
|
|
bittorrent.Completed,
|
|
|
|
bittorrent.Started,
|
|
|
|
bittorrent.Stopped,
|
|
|
|
}
|
|
|
|
|
2016-08-31 04:21:05 +02:00
|
|
|
errMalformedPacket = bittorrent.ClientError("malformed packet")
|
|
|
|
errMalformedIP = bittorrent.ClientError("malformed IP address")
|
|
|
|
errMalformedEvent = bittorrent.ClientError("malformed event ID")
|
|
|
|
errUnknownAction = bittorrent.ClientError("unknown action ID")
|
|
|
|
errBadConnectionID = bittorrent.ClientError("bad connection ID")
|
|
|
|
errUnknownOptionType = bittorrent.ClientError("unknown option type")
|
2016-08-03 09:11:52 +02:00
|
|
|
)
|
|
|
|
|
2017-10-08 23:35:50 +02:00
|
|
|
// ParseOptions is the configuration used to parse an Announce Request.
|
2016-08-03 09:11:52 +02:00
|
|
|
//
|
2017-10-08 23:35:50 +02:00
|
|
|
// If AllowIPSpoofing is true, IPs provided via params will be used.
|
|
|
|
type ParseOptions struct {
|
|
|
|
AllowIPSpoofing bool `yaml:"allowIPSpoofing"`
|
|
|
|
bittorrent.RequestSanitizer `yaml:",inline"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseAnnounce parses an AnnounceRequest from a UDP request.
|
2016-09-02 22:42:01 +02:00
|
|
|
//
|
2017-10-08 23:35:50 +02:00
|
|
|
// If v6 is true, the announce is parsed the "opentracker way":
|
2016-09-02 22:42:01 +02:00
|
|
|
// http://opentracker.blog.h3q.com/2007/12/28/the-ipv6-situation/
|
2017-10-08 23:35:50 +02:00
|
|
|
func ParseAnnounce(r Request, v6 bool, opts ParseOptions) (*bittorrent.AnnounceRequest, error) {
|
2016-09-02 22:42:01 +02:00
|
|
|
ipEnd := 84 + net.IPv4len
|
|
|
|
if v6 {
|
|
|
|
ipEnd = 84 + net.IPv6len
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.Packet) < ipEnd+10 {
|
2016-08-03 09:11:52 +02:00
|
|
|
return nil, errMalformedPacket
|
|
|
|
}
|
|
|
|
|
2016-08-05 07:47:04 +02:00
|
|
|
infohash := r.Packet[16:36]
|
|
|
|
peerID := r.Packet[36:56]
|
|
|
|
downloaded := binary.BigEndian.Uint64(r.Packet[56:64])
|
|
|
|
left := binary.BigEndian.Uint64(r.Packet[64:72])
|
|
|
|
uploaded := binary.BigEndian.Uint64(r.Packet[72:80])
|
2016-08-03 09:11:52 +02:00
|
|
|
|
2016-08-05 07:47:04 +02:00
|
|
|
eventID := int(r.Packet[83])
|
2016-08-03 09:11:52 +02:00
|
|
|
if eventID >= len(eventIDs) {
|
|
|
|
return nil, errMalformedEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := r.IP
|
2017-10-08 23:35:50 +02:00
|
|
|
ipProvided := false
|
2016-09-02 22:42:01 +02:00
|
|
|
ipbytes := r.Packet[84:ipEnd]
|
2017-10-08 23:35:50 +02:00
|
|
|
if opts.AllowIPSpoofing {
|
2016-10-04 17:22:35 +02:00
|
|
|
// Make sure the bytes are copied to a new slice.
|
|
|
|
copy(ip, net.IP(ipbytes))
|
2017-10-08 23:35:50 +02:00
|
|
|
ipProvided = true
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
2017-10-08 23:35:50 +02:00
|
|
|
if !opts.AllowIPSpoofing && r.IP == nil {
|
2016-08-03 09:11:52 +02:00
|
|
|
// We have no IP address to fallback on.
|
|
|
|
return nil, errMalformedIP
|
|
|
|
}
|
|
|
|
|
2016-09-02 22:42:01 +02:00
|
|
|
numWant := binary.BigEndian.Uint32(r.Packet[ipEnd+4 : ipEnd+8])
|
|
|
|
port := binary.BigEndian.Uint16(r.Packet[ipEnd+8 : ipEnd+10])
|
2016-08-03 09:11:52 +02:00
|
|
|
|
2016-09-02 22:42:01 +02:00
|
|
|
params, err := handleOptionalParameters(r.Packet[ipEnd+10:])
|
2016-08-03 09:11:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-10-08 23:35:50 +02:00
|
|
|
request := &bittorrent.AnnounceRequest{
|
|
|
|
Event: eventIDs[eventID],
|
|
|
|
InfoHash: bittorrent.InfoHashFromBytes(infohash),
|
|
|
|
NumWant: uint32(numWant),
|
|
|
|
Left: left,
|
|
|
|
Downloaded: downloaded,
|
|
|
|
Uploaded: uploaded,
|
|
|
|
IPProvided: ipProvided,
|
|
|
|
NumWantProvided: true,
|
|
|
|
EventProvided: true,
|
2016-08-03 09:11:52 +02:00
|
|
|
Peer: bittorrent.Peer{
|
|
|
|
ID: bittorrent.PeerIDFromBytes(peerID),
|
2016-11-28 20:55:04 +01:00
|
|
|
IP: bittorrent.IP{IP: ip},
|
2016-08-03 09:11:52 +02:00
|
|
|
Port: port,
|
|
|
|
},
|
|
|
|
Params: params,
|
2017-10-08 23:35:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := opts.SanitizeAnnounce(request); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return request, nil
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 04:21:05 +02:00
|
|
|
type buffer struct {
|
|
|
|
bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
var bufferFree = sync.Pool{
|
|
|
|
New: func() interface{} { return new(buffer) },
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBuffer() *buffer {
|
|
|
|
return bufferFree.Get().(*buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buffer) free() {
|
|
|
|
b.Reset()
|
|
|
|
bufferFree.Put(b)
|
|
|
|
}
|
|
|
|
|
2016-08-03 09:11:52 +02:00
|
|
|
// handleOptionalParameters parses the optional parameters as described in BEP
|
|
|
|
// 41 and updates an announce with the values parsed.
|
2016-08-31 04:21:05 +02:00
|
|
|
func handleOptionalParameters(packet []byte) (bittorrent.Params, error) {
|
|
|
|
if len(packet) == 0 {
|
|
|
|
return bittorrent.ParseURLData("")
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 04:21:05 +02:00
|
|
|
var buf = newBuffer()
|
|
|
|
defer buf.free()
|
|
|
|
|
|
|
|
for i := 0; i < len(packet); {
|
|
|
|
option := packet[i]
|
2016-08-03 09:11:52 +02:00
|
|
|
switch option {
|
|
|
|
case optionEndOfOptions:
|
2016-08-31 04:21:05 +02:00
|
|
|
return bittorrent.ParseURLData(buf.String())
|
2016-08-03 09:11:52 +02:00
|
|
|
case optionNOP:
|
2016-08-31 04:21:05 +02:00
|
|
|
i++
|
2016-08-03 09:11:52 +02:00
|
|
|
case optionURLData:
|
2016-08-31 04:21:05 +02:00
|
|
|
if i+1 >= len(packet) {
|
|
|
|
return nil, errMalformedPacket
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 04:21:05 +02:00
|
|
|
length := int(packet[i+1])
|
|
|
|
if i+2+length > len(packet) {
|
|
|
|
return nil, errMalformedPacket
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 04:21:05 +02:00
|
|
|
n, err := buf.Write(packet[i+2 : i+2+length])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if n != length {
|
|
|
|
return nil, fmt.Errorf("expected to write %d bytes, wrote %d", length, n)
|
|
|
|
}
|
2016-08-03 09:11:52 +02:00
|
|
|
|
2016-08-31 04:21:05 +02:00
|
|
|
i += 2 + length
|
2016-08-03 09:11:52 +02:00
|
|
|
default:
|
2016-08-31 04:21:05 +02:00
|
|
|
return nil, errUnknownOptionType
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-31 04:21:05 +02:00
|
|
|
return bittorrent.ParseURLData(buf.String())
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseScrape parses a ScrapeRequest from a UDP request.
|
2017-10-08 23:35:50 +02:00
|
|
|
func ParseScrape(r Request, opts ParseOptions) (*bittorrent.ScrapeRequest, error) {
|
2016-08-03 09:11:52 +02:00
|
|
|
// If a scrape isn't at least 36 bytes long, it's malformed.
|
2016-08-05 07:47:04 +02:00
|
|
|
if len(r.Packet) < 36 {
|
2016-08-03 09:11:52 +02:00
|
|
|
return nil, errMalformedPacket
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip past the initial headers and check that the bytes left equal the
|
|
|
|
// length of a valid list of infohashes.
|
2016-08-05 07:47:04 +02:00
|
|
|
r.Packet = r.Packet[16:]
|
|
|
|
if len(r.Packet)%20 != 0 {
|
2016-08-03 09:11:52 +02:00
|
|
|
return nil, errMalformedPacket
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate a list of infohashes and append it to the list until we're out.
|
|
|
|
var infohashes []bittorrent.InfoHash
|
2016-08-05 07:47:04 +02:00
|
|
|
for len(r.Packet) >= 20 {
|
|
|
|
infohashes = append(infohashes, bittorrent.InfoHashFromBytes(r.Packet[:20]))
|
|
|
|
r.Packet = r.Packet[20:]
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2017-10-08 23:35:50 +02:00
|
|
|
// Sanitize the request.
|
|
|
|
request := &bittorrent.ScrapeRequest{InfoHashes: infohashes}
|
|
|
|
if err := opts.SanitizeScrape(request); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return request, nil
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|