tracker/server/announce.go

261 lines
5.5 KiB
Go
Raw Normal View History

// Copyright 2013 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.
2013-06-22 01:31:32 +02:00
package server
import (
"errors"
"fmt"
2013-06-23 09:56:28 +02:00
"log"
"net/http"
"path"
2013-07-04 00:24:03 +02:00
"strconv"
2013-07-12 06:36:24 +02:00
"time"
"github.com/pushrax/chihaya/storage"
2013-06-22 01:31:32 +02:00
)
2013-06-23 09:56:28 +02:00
func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
passkey, _ := path.Split(r.URL.Path)
2013-07-12 06:36:24 +02:00
user, err := s.FindUser(passkey)
2013-06-22 01:31:32 +02:00
if err != nil {
fail(err, w, r)
2013-06-22 01:31:32 +02:00
return
}
2013-07-12 06:36:24 +02:00
aq, err := newAnnounceQuery(r)
2013-06-22 01:31:32 +02:00
if err != nil {
fail(errors.New("Malformed request"), w, r)
2013-06-22 01:31:32 +02:00
return
}
2013-07-12 06:36:24 +02:00
peerID := aq.PeerID()
ok, err := s.dataStore.ClientWhitelisted(peerID)
if err != nil {
log.Panicf("server: %s", err)
}
if !ok {
fail(errors.New("Your client is not approved"), w, r)
2013-06-22 01:31:32 +02:00
return
}
2013-07-12 06:36:24 +02:00
torrent, exists, err := s.dataStore.FindTorrent(aq.Infohash())
2013-06-22 01:31:32 +02:00
if err != nil {
2013-06-23 09:56:28 +02:00
log.Panicf("server: %s", err)
2013-06-22 01:31:32 +02:00
}
if !exists {
fail(errors.New("This torrent does not exist"), w, r)
2013-06-22 01:31:32 +02:00
return
}
tx, err := s.dataStore.Begin()
2013-07-04 00:24:03 +02:00
if err != nil {
log.Panicf("server: %s", err)
}
2013-07-12 06:36:24 +02:00
left := aq.Left()
if torrent.Pruned && left == 0 {
err := tx.Unprune(torrent.ID)
2013-06-22 01:31:32 +02:00
if err != nil {
2013-06-23 09:56:28 +02:00
log.Panicf("server: %s", err)
2013-06-22 01:31:32 +02:00
}
2013-07-12 06:36:24 +02:00
} else if torrent.Pruned {
e := fmt.Errorf("This torrent does not exist (pruned: %t, left: %d)", torrent.Pruned, left)
fail(e, w, r)
2013-06-22 01:31:32 +02:00
return
}
2013-07-12 06:36:24 +02:00
_ = aq.NumWant(s.conf.DefaultNumWant)
if s.conf.Slots && user.Slots != -1 && aq.Left() != 0 {
if user.UsedSlots >= user.Slots {
fail(errors.New("You've run out of download slots."), w, r)
return
}
}
_, isLeecher := torrent.Leechers[peerID]
_, isSeeder := torrent.Seeders[peerID]
event := aq.Event()
completed := "completed" == event
if event == "stopped" || event == "paused" {
if left == 0 {
err := tx.RmSeeder(torrent.ID, peerID)
if err != nil {
log.Panicf("server: %s", err)
}
} else {
err := tx.RmLeecher(torrent.ID, peerID)
if err != nil {
log.Panicf("server: %s", err)
}
err = tx.DecrementSlots(user.ID)
if err != nil {
log.Panicf("server: %s", err)
}
}
} else if completed {
err := tx.Snatch(user.ID, torrent.ID)
if err != nil {
log.Panicf("server: %s", err)
}
}
}
// An AnnounceQuery is a parsedQuery that guarantees the existance
// of parameters required for torrent client announces.
type announceQuery struct {
pq *parsedQuery
ip string
created int64
}
func newAnnounceQuery(r *http.Request) (*announceQuery, error) {
pq, err := parseQuery(r.URL.RawQuery)
if err != nil {
return nil, err
}
infohash, _ := pq.Params["info_hash"]
if infohash == "" {
return nil, errors.New("infohash does not exist")
}
peerId, _ := pq.Params["peer_id"]
if peerId == "" {
return nil, errors.New("peerId does not exist")
}
_, err = pq.getUint64("port")
if err != nil {
return nil, errors.New("port does not exist")
}
_, err = pq.getUint64("uploaded")
if err != nil {
return nil, errors.New("uploaded does not exist")
}
_, err = pq.getUint64("downloaded")
if err != nil {
return nil, errors.New("downloaded does not exist")
}
_, err = pq.getUint64("left")
if err != nil {
return nil, errors.New("left does not exist")
}
aq := &announceQuery{
pq: pq,
created: time.Now().Unix(),
}
aq.ip, err = aq.determineIP(r)
if err != nil {
return nil, err
}
return aq, nil
}
func (aq *announceQuery) Infohash() string {
infohash, _ := aq.pq.Params["info_hash"]
if infohash == "" {
panic("announceQuery missing infohash")
}
return infohash
}
func (aq *announceQuery) PeerID() string {
peerID, _ := aq.pq.Params["peer_id"]
if peerID == "" {
panic("announceQuery missing peer_id")
}
return peerID
}
func (aq *announceQuery) Port() uint64 {
port, err := aq.pq.getUint64("port")
if err != nil {
panic("announceQuery missing port")
}
return port
}
func (aq *announceQuery) IP() string {
return aq.ip
}
func (aq *announceQuery) Uploaded() uint64 {
ul, err := aq.pq.getUint64("uploaded")
if err != nil {
panic("announceQuery missing uploaded")
}
return ul
}
func (aq *announceQuery) Downloaded() uint64 {
dl, err := aq.pq.getUint64("downloaded")
if err != nil {
panic("announceQuery missing downloaded")
}
return dl
}
func (aq *announceQuery) Left() uint64 {
left, err := aq.pq.getUint64("left")
if err != nil {
panic("announceQuery missing left")
}
return left
}
func (aq *announceQuery) Event() string {
return aq.pq.Params["event"]
}
func (aq *announceQuery) determineIP(r *http.Request) (string, error) {
if ip, ok := aq.pq.Params["ip"]; ok {
return ip, nil
} else if ip, ok := aq.pq.Params["ipv4"]; ok {
return ip, nil
} else if ips, ok := aq.pq.Params["X-Real-Ip"]; ok && len(ips) > 0 {
return string(ips[0]), nil
} else {
portIndex := len(r.RemoteAddr) - 1
for ; portIndex >= 0; portIndex-- {
if r.RemoteAddr[portIndex] == ':' {
break
}
}
if portIndex != -1 {
return r.RemoteAddr[0:portIndex], nil
} else {
return "", errors.New("Failed to parse IP address")
}
}
}
func (aq *announceQuery) NumWant(fallback int) int {
if numWantStr, exists := aq.pq.Params["numWant"]; exists {
2013-07-04 00:24:03 +02:00
numWant, err := strconv.Atoi(numWantStr)
if err != nil {
2013-07-12 06:36:24 +02:00
return fallback
2013-07-04 00:24:03 +02:00
}
2013-07-12 06:36:24 +02:00
return numWant
2013-07-04 00:24:03 +02:00
} else {
2013-07-12 06:36:24 +02:00
return fallback
2013-07-04 00:24:03 +02:00
}
2013-07-12 06:36:24 +02:00
}
func (aq *announceQuery) Peer(uid, tid uint64) *storage.Peer {
return &storage.Peer{
ID: aq.PeerID(),
UserID: uid,
TorrentID: tid,
2013-07-04 00:24:03 +02:00
2013-07-12 06:36:24 +02:00
IP: aq.IP(),
Port: aq.Port(),
LastAnnounce: aq.created,
Uploaded: aq.Uploaded(),
Downloaded: aq.Downloaded(),
}
2013-06-22 01:31:32 +02:00
}