tracker/server/announce.go

252 lines
5.7 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"
2013-06-23 09:56:28 +02:00
"log"
"net/http"
"path"
2013-07-04 00:24:03 +02:00
"strconv"
"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) {
// Parse the required parameters off of a query
2013-07-20 00:59:07 +02:00
compact, numWant, infohash, peerID, event, ip, port, uploaded, downloaded, left, err := s.validateAnnounceQuery(r)
2013-06-22 01:31:32 +02:00
if err != nil {
fail(err, w, r)
2013-06-22 01:31:32 +02:00
return
}
// Validate the user's passkey
2013-07-20 00:59:07 +02:00
passkey, _ := path.Split(r.URL.Path)
user, err := s.FindUser(passkey)
2013-06-22 01:31:32 +02:00
if err != nil {
2013-07-20 00:59:07 +02:00
fail(err, w, r)
2013-06-22 01:31:32 +02:00
return
}
// Check if the user's client is whitelisted
2013-07-20 00:59:07 +02:00
whitelisted, err := s.dataStore.ClientWhitelisted(peerID)
if err != nil {
log.Panicf("server: %s", err)
}
2013-07-20 00:59:07 +02:00
if !whitelisted {
fail(errors.New("Your client is not approved"), w, r)
2013-06-22 01:31:32 +02:00
return
}
// Find the specified torrent
2013-07-20 00:59:07 +02:00
torrent, exists, err := s.dataStore.FindTorrent(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
}
// Begin a data store transaction
tx, err := s.dataStore.Begin()
2013-07-04 00:24:03 +02:00
if err != nil {
log.Panicf("server: %s", err)
}
// If the torrent was pruned and the user is seeding, unprune it
2013-07-12 06:36:24 +02:00
if torrent.Pruned && left == 0 {
err := tx.Unprune(torrent)
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
}
// Look for the user in in the pool of seeders and leechers
sp, seeder := torrent.Seeders[peerID]
lp, leecher := torrent.Leechers[peerID]
peer := &storage.Peer{}
switch {
// Guarantee that no user is in both pools
case seeder && leecher:
if left == 0 {
peer = &sp
err := tx.RmLeecher(torrent, peer)
if err != nil {
log.Panicf("server: %s", err)
}
leecher = false
} else {
peer = &lp
err := tx.RmSeeder(torrent, peer)
if err != nil {
log.Panicf("server: %s", err)
}
seeder = false
}
case seeder:
peer = &sp
// TODO update stats
case leecher:
peer = &lp
// TODO update stats
default:
// The user is a new peer
peer = &storage.Peer{
ID: peerID,
UserID: user.ID,
TorrentID: torrent.ID,
IP: ip,
Port: port,
Uploaded: uploaded,
Downloaded: downloaded,
Left: left,
LastAnnounce: time.Now().Unix(),
}
// Check the new user's slots
if s.conf.Slots && user.Slots != -1 && left != 0 {
if user.UsedSlots >= user.Slots {
fail(errors.New("You've run out of download slots."), w, r)
return
}
}
2013-07-12 06:36:24 +02:00
if left == 0 {
err := tx.NewSeeder(torrent, peer)
2013-07-12 06:36:24 +02:00
if err != nil {
log.Panicf("server: %s", err)
}
} else {
err := tx.IncrementSlots(user)
2013-07-12 06:36:24 +02:00
if err != nil {
log.Panicf("server: %s", err)
}
err = tx.NewLeecher(torrent, peer)
2013-07-12 06:36:24 +02:00
if err != nil {
log.Panicf("server: %s", err)
}
}
}
// Handle any events given to us by the user
switch {
case event == "stopped" || event == "paused":
if seeder {
err := tx.RmSeeder(torrent, peer)
if err != nil {
log.Panicf("server: %s", err)
}
}
if leecher {
err := tx.RmLeecher(torrent, peer)
if err != nil {
log.Panicf("server: %s", err)
}
err = tx.DecrementSlots(user)
if err != nil {
log.Panicf("server: %s", err)
}
}
case event == "completed":
err := tx.Snatch(user, torrent)
if err != nil {
log.Panicf("server: %s", err)
}
if leecher {
err := tx.RmLeecher(torrent, peer)
if err != nil {
log.Panicf("server: %s", err)
}
err = tx.NewSeeder(torrent, peer)
if err != nil {
log.Panicf("server: %s", err)
}
}
case leecher && left == 0:
// Completed event from the peer was never received
err := tx.RmLeecher(torrent, peer)
if err != nil {
log.Panicf("server: %s", err)
}
err = tx.NewSeeder(torrent, peer)
2013-07-12 06:36:24 +02:00
if err != nil {
log.Panicf("server: %s", err)
}
}
// TODO compact, response, etc...
2013-07-12 06:36:24 +02:00
}
2013-07-20 00:59:07 +02:00
func (s *Server) validateAnnounceQuery(r *http.Request) (compact bool, numWant int, infohash, peerID, event, ip string, port, uploaded, downloaded, left uint64, err error) {
2013-07-12 06:36:24 +02:00
pq, err := parseQuery(r.URL.RawQuery)
if err != nil {
2013-07-20 00:59:07 +02:00
return false, 0, "", "", "", "", 0, 0, 0, 0, err
}
compact = pq.Params["compact"] == "1"
numWant = determineNumWant(s.conf.DefaultNumWant, pq)
infohash, _ = pq.Params["info_hash"]
peerID, _ = pq.Params["peer_id"]
event, _ = pq.Params["event"]
ip, _ = determineIP(r, pq)
port, portErr := pq.getUint64("port")
uploaded, uploadedErr := pq.getUint64("uploaded")
downloaded, downloadedErr := pq.getUint64("downloaded")
left, leftErr := pq.getUint64("left")
if infohash == "" ||
peerID == "" ||
ip == "" ||
portErr != nil ||
uploadedErr != nil ||
downloadedErr != nil ||
leftErr != nil {
2013-07-20 00:59:07 +02:00
return false, 0, "", "", "", "", 0, 0, 0, 0, errors.New("Malformed request")
}
return
2013-07-12 06:36:24 +02:00
}
2013-07-20 00:59:07 +02:00
func determineNumWant(fallback int, pq *parsedQuery) int {
if numWantStr, exists := pq.Params["numWant"]; exists {
numWant, err := strconv.Atoi(numWantStr)
if err != nil {
return fallback
}
return numWant
} else {
return fallback
2013-07-12 06:36:24 +02:00
}
}
2013-07-20 00:59:07 +02:00
func determineIP(r *http.Request, pq *parsedQuery) (string, error) {
if ip, ok := pq.Params["ip"]; ok {
2013-07-12 06:36:24 +02:00
return ip, nil
2013-07-20 00:59:07 +02:00
} else if ip, ok := pq.Params["ipv4"]; ok {
2013-07-12 06:36:24 +02:00
return ip, nil
2013-07-20 00:59:07 +02:00
} else if ips, ok := pq.Params["X-Real-Ip"]; ok && len(ips) > 0 {
2013-07-12 06:36:24 +02:00
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")
}
}
}