2015-02-20 07:12:47 +01:00
|
|
|
// Copyright 2015 The Chihaya Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package udp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2015-07-01 00:08:36 +02:00
|
|
|
"errors"
|
2015-02-20 07:12:47 +01:00
|
|
|
"net"
|
2015-02-20 08:18:44 +01:00
|
|
|
|
|
|
|
"github.com/chihaya/chihaya/stats"
|
|
|
|
"github.com/chihaya/chihaya/tracker/models"
|
2015-02-20 07:12:47 +01:00
|
|
|
)
|
|
|
|
|
2015-02-26 03:47:01 +01:00
|
|
|
const (
|
|
|
|
connectActionID uint32 = iota
|
|
|
|
announceActionID
|
|
|
|
scrapeActionID
|
|
|
|
errorActionID
|
2015-04-04 21:41:58 +02:00
|
|
|
announceDualStackActionID
|
2015-02-26 03:47:01 +01:00
|
|
|
)
|
2015-02-20 08:18:44 +01:00
|
|
|
|
2015-10-26 21:22:54 +01:00
|
|
|
// Option-Types described in BEP41 and BEP45.
|
|
|
|
const (
|
|
|
|
optionEndOfOptions byte = 0x0
|
|
|
|
optionNOP = 0x1
|
|
|
|
optionURLData = 0x2
|
|
|
|
optionIPv6 = 0x3
|
|
|
|
)
|
|
|
|
|
2015-02-20 08:18:44 +01:00
|
|
|
var (
|
2015-02-26 03:47:01 +01:00
|
|
|
// initialConnectionID is the magic initial connection ID specified by BEP 15.
|
|
|
|
initialConnectionID = []byte{0, 0, 0x04, 0x17, 0x27, 0x10, 0x19, 0x80}
|
|
|
|
|
2015-03-29 09:03:19 +02:00
|
|
|
// emptyIPs are the value of an IP field that has been left blank.
|
|
|
|
emptyIPv4 = []byte{0, 0, 0, 0}
|
|
|
|
emptyIPv6 = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
|
|
|
|
|
|
|
// eventIDs map IDs to event names.
|
2015-02-26 03:47:01 +01:00
|
|
|
eventIDs = []string{
|
|
|
|
"",
|
|
|
|
"completed",
|
|
|
|
"started",
|
|
|
|
"stopped",
|
|
|
|
}
|
|
|
|
|
2015-02-20 20:08:46 +01:00
|
|
|
errMalformedPacket = models.ProtocolError("malformed packet")
|
|
|
|
errMalformedIP = models.ProtocolError("malformed IP address")
|
|
|
|
errMalformedEvent = models.ProtocolError("malformed event ID")
|
2015-09-13 19:10:41 +02:00
|
|
|
errUnknownAction = models.ProtocolError("unknown action ID")
|
2015-07-01 00:08:36 +02:00
|
|
|
errBadConnectionID = errors.New("bad connection ID")
|
2015-02-20 08:18:44 +01:00
|
|
|
)
|
|
|
|
|
2015-02-20 20:08:46 +01:00
|
|
|
// handleTorrentError writes err to w if err is a models.ClientError.
|
2015-02-20 08:18:44 +01:00
|
|
|
func handleTorrentError(err error, w *Writer) {
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-21 20:35:21 +01:00
|
|
|
if models.IsPublicError(err) {
|
2015-02-20 08:18:44 +01:00
|
|
|
w.WriteError(err)
|
|
|
|
stats.RecordEvent(stats.ClientError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 20:08:46 +01:00
|
|
|
// handlePacket decodes and processes one UDP request, returning the response.
|
2015-06-27 03:07:29 +02:00
|
|
|
func (s *Server) handlePacket(packet []byte, addr *net.UDPAddr) (response []byte, actionName string, err error) {
|
2015-02-20 07:12:47 +01:00
|
|
|
if len(packet) < 16 {
|
2015-06-27 03:07:29 +02:00
|
|
|
// Malformed, no client packets are less than 16 bytes.
|
|
|
|
// We explicitly return nothing in case this is a DoS attempt.
|
|
|
|
err = errMalformedPacket
|
|
|
|
return
|
2015-02-20 07:12:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
connID := packet[0:8]
|
|
|
|
action := binary.BigEndian.Uint32(packet[8:12])
|
|
|
|
transactionID := packet[12:16]
|
|
|
|
|
2015-02-20 18:35:31 +01:00
|
|
|
writer := &Writer{
|
|
|
|
buf: new(bytes.Buffer),
|
|
|
|
|
|
|
|
connectionID: connID,
|
|
|
|
transactionID: transactionID,
|
|
|
|
}
|
2015-02-24 03:30:45 +01:00
|
|
|
defer func() { response = writer.buf.Bytes() }()
|
2015-02-20 18:52:49 +01:00
|
|
|
|
2015-02-20 21:38:27 +01:00
|
|
|
if action != 0 && !s.connIDGen.Matches(connID, addr.IP) {
|
2015-06-27 03:07:29 +02:00
|
|
|
err = errBadConnectionID
|
|
|
|
writer.WriteError(err)
|
2015-02-20 18:52:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-20 07:12:47 +01:00
|
|
|
switch action {
|
2015-02-24 03:30:45 +01:00
|
|
|
case connectActionID:
|
2015-02-20 20:08:46 +01:00
|
|
|
actionName = "connect"
|
2015-06-27 03:07:29 +02:00
|
|
|
|
2015-02-20 07:12:47 +01:00
|
|
|
if !bytes.Equal(connID, initialConnectionID) {
|
2015-06-27 03:07:29 +02:00
|
|
|
err = errMalformedPacket
|
|
|
|
return
|
2015-02-20 07:12:47 +01:00
|
|
|
}
|
|
|
|
|
2015-02-20 20:08:46 +01:00
|
|
|
writer.writeHeader(0)
|
2015-02-20 21:38:27 +01:00
|
|
|
writer.buf.Write(s.connIDGen.Generate(addr.IP))
|
2015-02-20 07:12:47 +01:00
|
|
|
|
2015-02-24 03:30:45 +01:00
|
|
|
case announceActionID:
|
2015-02-20 20:08:46 +01:00
|
|
|
actionName = "announce"
|
2015-02-20 08:18:44 +01:00
|
|
|
|
2015-06-27 03:07:29 +02:00
|
|
|
var ann *models.Announce
|
|
|
|
ann, err = s.newAnnounce(packet, addr.IP)
|
2015-02-20 18:52:49 +01:00
|
|
|
if err == nil {
|
|
|
|
err = s.tracker.HandleAnnounce(ann, writer)
|
2015-02-20 18:35:31 +01:00
|
|
|
}
|
2015-02-20 08:18:44 +01:00
|
|
|
|
2015-02-20 18:52:49 +01:00
|
|
|
handleTorrentError(err, writer)
|
|
|
|
|
2015-02-24 03:30:45 +01:00
|
|
|
case scrapeActionID:
|
2015-02-20 20:08:46 +01:00
|
|
|
actionName = "scrape"
|
2015-02-20 18:35:31 +01:00
|
|
|
|
2015-06-27 03:07:29 +02:00
|
|
|
var scrape *models.Scrape
|
|
|
|
scrape, err = s.newScrape(packet)
|
2015-02-20 18:52:49 +01:00
|
|
|
if err == nil {
|
|
|
|
err = s.tracker.HandleScrape(scrape, writer)
|
2015-02-20 18:35:31 +01:00
|
|
|
}
|
2015-02-20 08:18:44 +01:00
|
|
|
|
2015-02-20 18:52:49 +01:00
|
|
|
handleTorrentError(err, writer)
|
2015-09-13 19:10:41 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
err = errUnknownAction
|
2015-02-20 07:12:47 +01:00
|
|
|
}
|
2015-02-20 18:52:49 +01:00
|
|
|
|
2015-02-20 07:12:47 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-20 20:08:46 +01:00
|
|
|
// newAnnounce decodes one announce packet, returning a models.Announce.
|
2015-02-20 08:18:44 +01:00
|
|
|
func (s *Server) newAnnounce(packet []byte, ip net.IP) (*models.Announce, error) {
|
|
|
|
if len(packet) < 98 {
|
|
|
|
return nil, errMalformedPacket
|
|
|
|
}
|
|
|
|
|
|
|
|
infohash := packet[16:36]
|
|
|
|
peerID := packet[36:56]
|
|
|
|
|
|
|
|
downloaded := binary.BigEndian.Uint64(packet[56:64])
|
|
|
|
left := binary.BigEndian.Uint64(packet[64:72])
|
|
|
|
uploaded := binary.BigEndian.Uint64(packet[72:80])
|
|
|
|
|
|
|
|
eventID := packet[83]
|
|
|
|
if eventID > 3 {
|
|
|
|
return nil, errMalformedEvent
|
|
|
|
}
|
|
|
|
|
2015-03-29 09:03:19 +02:00
|
|
|
ipv4bytes := packet[84:88]
|
|
|
|
if s.config.AllowIPSpoofing && !bytes.Equal(ipv4bytes, emptyIPv4) {
|
|
|
|
ip = net.ParseIP(string(ipv4bytes))
|
2015-02-20 08:18:44 +01:00
|
|
|
}
|
2015-02-20 21:38:27 +01:00
|
|
|
|
2015-02-20 08:18:44 +01:00
|
|
|
if ip == nil {
|
|
|
|
return nil, errMalformedIP
|
2015-02-20 21:38:27 +01:00
|
|
|
} else if ipv4 := ip.To4(); ipv4 != nil {
|
2015-02-20 17:44:05 +01:00
|
|
|
ip = ipv4
|
|
|
|
}
|
2015-02-20 08:18:44 +01:00
|
|
|
|
|
|
|
numWant := binary.BigEndian.Uint32(packet[92:96])
|
|
|
|
port := binary.BigEndian.Uint16(packet[96:98])
|
|
|
|
|
2015-04-07 01:06:06 +02:00
|
|
|
announce := &models.Announce{
|
|
|
|
Config: s.config,
|
|
|
|
Downloaded: downloaded,
|
|
|
|
Event: eventIDs[eventID],
|
|
|
|
IPv4: models.Endpoint{
|
|
|
|
IP: ip,
|
|
|
|
Port: port,
|
|
|
|
},
|
|
|
|
Infohash: string(infohash),
|
|
|
|
Left: left,
|
|
|
|
NumWant: int(numWant),
|
|
|
|
PeerID: string(peerID),
|
|
|
|
Uploaded: uploaded,
|
|
|
|
}
|
|
|
|
|
2015-04-07 01:15:07 +02:00
|
|
|
if err := s.handleOptionalParameters(packet, announce); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return announce, nil
|
2015-04-07 01:06:06 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 01:15:07 +02:00
|
|
|
// handleOptionalParameters parses the optional parameters as described in BEP41
|
2015-04-07 01:06:06 +02:00
|
|
|
// and updates an announce with the values parsed.
|
2015-04-07 01:15:07 +02:00
|
|
|
func (s *Server) handleOptionalParameters(packet []byte, announce *models.Announce) error {
|
2015-03-29 09:03:19 +02:00
|
|
|
if len(packet) > 98 {
|
|
|
|
optionStartIndex := 98
|
|
|
|
for optionStartIndex < len(packet)-1 {
|
|
|
|
option := packet[optionStartIndex]
|
|
|
|
switch option {
|
|
|
|
case optionEndOfOptions:
|
2015-04-07 01:15:07 +02:00
|
|
|
return nil
|
2015-03-29 09:03:19 +02:00
|
|
|
|
|
|
|
case optionNOP:
|
|
|
|
optionStartIndex++
|
|
|
|
|
|
|
|
case optionURLData:
|
|
|
|
if optionStartIndex+1 > len(packet)-1 {
|
2015-04-07 01:15:07 +02:00
|
|
|
return errMalformedPacket
|
2015-03-29 09:03:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
length := int(packet[optionStartIndex+1])
|
|
|
|
if optionStartIndex+1+length > len(packet)-1 {
|
2015-04-07 01:15:07 +02:00
|
|
|
return errMalformedPacket
|
2015-03-29 09:03:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Actually parse the URL Data as described in BEP41.
|
|
|
|
|
|
|
|
optionStartIndex += 1 + length
|
|
|
|
|
|
|
|
case optionIPv6:
|
|
|
|
if optionStartIndex+19 > len(packet)-1 {
|
2015-04-07 01:15:07 +02:00
|
|
|
return errMalformedPacket
|
2015-03-29 09:03:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ipv6bytes := packet[optionStartIndex+1 : optionStartIndex+17]
|
|
|
|
if s.config.AllowIPSpoofing && !bytes.Equal(ipv6bytes, emptyIPv6) {
|
2015-04-07 01:06:06 +02:00
|
|
|
announce.IPv6.IP = net.ParseIP(string(ipv6bytes)).To16()
|
|
|
|
announce.IPv6.Port = binary.BigEndian.Uint16(packet[optionStartIndex+17 : optionStartIndex+19])
|
|
|
|
if announce.IPv6.IP == nil {
|
2015-04-07 01:15:07 +02:00
|
|
|
return errMalformedIP
|
2015-03-29 09:03:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
optionStartIndex += 19
|
|
|
|
|
|
|
|
default:
|
2015-04-07 01:15:07 +02:00
|
|
|
return nil
|
2015-03-29 09:03:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 01:06:06 +02:00
|
|
|
// There was no optional parameters to parse.
|
2015-04-07 01:15:07 +02:00
|
|
|
return nil
|
2015-02-20 07:12:47 +01:00
|
|
|
}
|
2015-02-20 18:35:31 +01:00
|
|
|
|
2015-02-20 20:08:46 +01:00
|
|
|
// newScrape decodes one announce packet, returning a models.Scrape.
|
2015-02-20 18:35:31 +01:00
|
|
|
func (s *Server) newScrape(packet []byte) (*models.Scrape, error) {
|
2015-02-20 20:08:46 +01:00
|
|
|
if len(packet) < 36 {
|
2015-02-20 18:35:31 +01:00
|
|
|
return nil, errMalformedPacket
|
|
|
|
}
|
|
|
|
|
|
|
|
var infohashes []string
|
|
|
|
packet = packet[16:]
|
|
|
|
|
|
|
|
if len(packet)%20 != 0 {
|
|
|
|
return nil, errMalformedPacket
|
|
|
|
}
|
|
|
|
|
|
|
|
for len(packet) >= 20 {
|
|
|
|
infohash := packet[:20]
|
|
|
|
infohashes = append(infohashes, string(infohash))
|
2015-02-20 18:52:49 +01:00
|
|
|
packet = packet[20:]
|
2015-02-20 18:35:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &models.Scrape{
|
|
|
|
Config: s.config,
|
|
|
|
Infohashes: infohashes,
|
|
|
|
}, nil
|
|
|
|
}
|