tracker/http/announce.go

303 lines
6.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.
2014-07-02 03:40:29 +02:00
package http
2013-06-22 01:31:32 +02:00
import (
2014-06-30 20:59:31 +02:00
"fmt"
2014-06-24 04:47:43 +02:00
"io"
2013-11-24 06:49:20 +01:00
"net/http"
2014-07-02 03:40:29 +02:00
"github.com/julienschmidt/httprouter"
2014-06-24 04:47:43 +02:00
"github.com/chihaya/chihaya/bencode"
"github.com/chihaya/chihaya/drivers/tracker"
"github.com/chihaya/chihaya/models"
2013-06-22 01:31:32 +02:00
)
2014-07-02 03:40:29 +02:00
func (t *Tracker) ServeAnnounce(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
ann, err := models.NewAnnounce(t.cfg, r, p)
if err == models.ErrMalformedRequest {
2014-07-02 03:40:29 +02:00
fail(w, r, err)
return http.StatusOK
2013-11-24 06:49:20 +01:00
}
if err != nil {
return http.StatusInternalServerError
}
2013-11-24 06:49:20 +01:00
2014-07-02 03:40:29 +02:00
conn, err := t.tp.Get()
2013-11-24 06:49:20 +01:00
if err != nil {
2014-07-02 03:40:29 +02:00
return http.StatusInternalServerError
2013-11-24 06:49:20 +01:00
}
2014-07-02 03:40:29 +02:00
if t.cfg.Whitelist {
err = conn.ClientWhitelisted(ann.ClientID())
if err == tracker.ErrClientUnapproved {
2014-07-02 03:40:29 +02:00
fail(w, r, err)
return http.StatusOK
} else if err != nil {
return http.StatusInternalServerError
2014-07-02 03:40:29 +02:00
}
2013-11-24 06:49:20 +01:00
}
2014-06-24 04:47:43 +02:00
var user *models.User
2014-07-02 03:40:29 +02:00
if t.cfg.Private {
user, err = conn.FindUser(ann.Passkey)
if err == tracker.ErrUserDNE {
2014-07-02 03:40:29 +02:00
fail(w, r, err)
return http.StatusOK
} else if err != nil {
return http.StatusInternalServerError
2014-06-24 04:47:43 +02:00
}
2013-11-24 06:49:20 +01:00
}
2014-06-24 04:47:43 +02:00
2014-07-02 03:40:29 +02:00
torrent, err := conn.FindTorrent(ann.Infohash)
if err == tracker.ErrTorrentDNE {
2014-07-02 03:40:29 +02:00
fail(w, r, err)
return http.StatusOK
} else if err != nil {
return http.StatusInternalServerError
2013-11-24 06:49:20 +01:00
}
2014-07-02 03:40:29 +02:00
peer := models.NewPeer(ann, user, torrent)
2014-06-24 04:47:43 +02:00
2014-07-02 03:40:29 +02:00
created, err := updateTorrent(conn, ann, peer, torrent)
2013-11-24 06:49:20 +01:00
if err != nil {
2014-07-02 03:40:29 +02:00
return http.StatusInternalServerError
2013-11-24 06:49:20 +01:00
}
2014-07-02 03:40:29 +02:00
snatched, err := handleEvent(conn, ann, peer, user, torrent)
2014-06-24 04:47:43 +02:00
if err != nil {
2014-07-02 03:40:29 +02:00
return http.StatusInternalServerError
2013-11-24 06:49:20 +01:00
}
2014-07-02 03:40:29 +02:00
if t.cfg.Private {
delta := models.NewAnnounceDelta(ann, peer, user, torrent, created, snatched)
err = t.bc.RecordAnnounce(delta)
if err != nil {
return http.StatusInternalServerError
}
2014-06-26 20:26:58 +02:00
}
2013-11-24 06:49:20 +01:00
2014-07-02 03:40:29 +02:00
writeAnnounceResponse(w, ann, user, torrent)
2014-06-26 23:10:39 +02:00
2014-07-02 03:40:29 +02:00
return http.StatusOK
2014-06-24 04:47:43 +02:00
}
func updateTorrent(c tracker.Conn, a *models.Announce, p *models.Peer, t *models.Torrent) (created bool, err error) {
2014-06-24 04:47:43 +02:00
if !t.Active && a.Left == 0 {
err = c.MarkActive(t)
2014-06-24 04:47:43 +02:00
if err != nil {
return
2013-11-24 06:49:20 +01:00
}
2014-06-24 04:47:43 +02:00
}
2013-11-24 06:49:20 +01:00
2014-06-24 04:47:43 +02:00
switch {
case t.InSeederPool(p):
err = c.SetSeeder(t, p)
2013-11-24 06:49:20 +01:00
if err != nil {
2014-06-24 04:47:43 +02:00
return
2013-11-24 06:49:20 +01:00
}
2014-06-24 04:47:43 +02:00
case t.InLeecherPool(p):
err = c.SetLeecher(t, p)
2013-11-24 06:49:20 +01:00
if err != nil {
2014-06-24 04:47:43 +02:00
return
2013-11-24 06:49:20 +01:00
}
default:
2014-06-24 04:47:43 +02:00
if a.Left == 0 {
err = c.AddSeeder(t, p)
2013-11-24 06:49:20 +01:00
if err != nil {
2014-06-24 04:47:43 +02:00
return
2013-11-24 06:49:20 +01:00
}
} else {
err = c.AddLeecher(t, p)
2013-11-24 06:49:20 +01:00
if err != nil {
2014-06-24 04:47:43 +02:00
return
2013-11-24 06:49:20 +01:00
}
}
2014-06-24 04:47:43 +02:00
created = true
2013-11-24 06:49:20 +01:00
}
2014-06-24 04:47:43 +02:00
return
}
func handleEvent(c tracker.Conn, a *models.Announce, p *models.Peer, u *models.User, t *models.Torrent) (snatched bool, err error) {
2013-11-24 06:49:20 +01:00
switch {
2014-06-24 04:47:43 +02:00
case a.Event == "stopped" || a.Event == "paused":
if t.InSeederPool(p) {
err = c.RemoveSeeder(t, p)
2013-11-24 06:49:20 +01:00
if err != nil {
2014-06-24 04:47:43 +02:00
return
2013-11-24 06:49:20 +01:00
}
}
2014-06-24 04:47:43 +02:00
if t.InLeecherPool(p) {
err = c.RemoveLeecher(t, p)
2013-11-24 06:49:20 +01:00
if err != nil {
2014-06-24 04:47:43 +02:00
return
2013-11-24 06:49:20 +01:00
}
}
2014-06-24 04:47:43 +02:00
case a.Event == "completed":
err = c.IncrementSnatches(t)
2013-11-24 06:49:20 +01:00
if err != nil {
2014-06-24 04:47:43 +02:00
return
2013-11-24 06:49:20 +01:00
}
2014-06-24 04:47:43 +02:00
snatched = true
if t.InLeecherPool(p) {
err = tracker.LeecherFinished(c, t, p)
2013-11-24 06:49:20 +01:00
if err != nil {
2014-06-24 04:47:43 +02:00
return
2013-11-24 06:49:20 +01:00
}
}
2014-06-24 04:47:43 +02:00
case t.InLeecherPool(p) && a.Left == 0:
2013-11-24 06:49:20 +01:00
// A leecher completed but the event was never received
err = tracker.LeecherFinished(c, t, p)
2013-11-24 06:49:20 +01:00
if err != nil {
2014-06-24 04:47:43 +02:00
return
2013-11-24 06:49:20 +01:00
}
}
2014-06-24 04:47:43 +02:00
return
}
2013-11-24 06:49:20 +01:00
2014-06-24 04:47:43 +02:00
func writeAnnounceResponse(w io.Writer, a *models.Announce, u *models.User, t *models.Torrent) {
seedCount := len(t.Seeders)
leechCount := len(t.Leechers)
var peerCount int
if a.Left == 0 {
peerCount = minInt(a.NumWant, leechCount)
} else {
peerCount = minInt(a.NumWant, leechCount+seedCount-1)
}
bencoder := bencode.NewEncoder(w)
2014-06-30 20:59:31 +02:00
fmt.Fprintf(w, "d")
2014-06-24 04:47:43 +02:00
bencoder.Encode("complete")
bencoder.Encode(seedCount)
bencoder.Encode("incomplete")
bencoder.Encode(leechCount)
bencoder.Encode("interval")
bencoder.Encode(a.Config.Announce.Duration)
bencoder.Encode("min interval")
bencoder.Encode(a.Config.MinAnnounce.Duration)
if a.NumWant > 0 && a.Event != "stopped" && a.Event != "paused" {
if a.Compact {
writePeersCompact(w, a, u, t, peerCount)
} else {
writePeersList(w, a, u, t, peerCount)
}
}
2014-06-30 20:59:31 +02:00
fmt.Fprintf(w, "e")
}
func writePeersCompact(w io.Writer, a *models.Announce, u *models.User, t *models.Torrent, peerCount int) {
2014-07-02 03:40:29 +02:00
bencoder := bencode.NewEncoder(w)
ipv4s, ipv6s := getPeers(a, u, t, peerCount)
if len(ipv4s) > 0 {
// 6 is the number of bytes that represents 1 compact IPv4 address.
2014-07-02 03:40:29 +02:00
bencoder.Encode("peers")
fmt.Fprintf(w, "%d:", len(ipv4s)*6)
2014-06-30 20:59:31 +02:00
for _, peer := range ipv4s {
if ip := peer.IP.To4(); ip != nil {
w.Write(ip)
w.Write([]byte{byte(peer.Port >> 8), byte(peer.Port & 0xff)})
}
}
}
2014-06-24 04:47:43 +02:00
if len(ipv6s) > 0 {
// 18 is the number of bytes that represents 1 compact IPv6 address.
2014-07-02 03:40:29 +02:00
bencoder.Encode("peers6")
fmt.Fprintf(w, "%d:", len(ipv6s)*18)
2014-06-30 20:59:31 +02:00
for _, peer := range ipv6s {
if ip := peer.IP.To16(); ip != nil {
w.Write(ip)
w.Write([]byte{byte(peer.Port >> 8), byte(peer.Port & 0xff)})
}
2014-06-26 23:10:39 +02:00
}
}
}
2014-06-26 23:10:39 +02:00
2014-07-02 03:40:29 +02:00
func getPeers(a *models.Announce, u *models.User, t *models.Torrent, peerCount int) (ipv4s, ipv6s []models.Peer) {
if a.Left == 0 {
// If they're seeding, give them only leechers.
2014-06-30 20:59:31 +02:00
splitPeers(&ipv4s, &ipv6s, a, u, t.Leechers, peerCount)
} else {
// If they're leeching, prioritize giving them seeders.
2014-06-30 20:59:31 +02:00
count := splitPeers(&ipv4s, &ipv6s, a, u, t.Seeders, peerCount)
splitPeers(&ipv4s, &ipv6s, a, u, t.Leechers, peerCount-count)
}
return
}
2014-07-02 03:40:29 +02:00
func splitPeers(ipv4s, ipv6s *[]models.Peer, a *models.Announce, u *models.User, peers map[string]models.Peer, peerCount int) (count int) {
for _, peer := range peers {
if count >= peerCount {
break
2013-11-24 06:49:20 +01:00
}
2014-06-30 20:59:31 +02:00
if a.Config.Private && peer.UserID == u.ID {
continue
2013-11-24 06:49:20 +01:00
}
2014-06-30 20:59:31 +02:00
if ip := peer.IP.To4(); len(ip) == 4 {
2014-07-02 03:40:29 +02:00
*ipv4s = append(*ipv4s, peer)
2014-06-30 20:59:31 +02:00
} else if ip := peer.IP.To16(); len(ip) == 16 {
2014-07-02 03:40:29 +02:00
*ipv6s = append(*ipv6s, peer)
2013-11-24 06:49:20 +01:00
}
count++
}
return
}
func writePeersList(w io.Writer, a *models.Announce, u *models.User, t *models.Torrent, peerCount int) {
bencoder := bencode.NewEncoder(w)
2014-06-30 20:59:31 +02:00
ipv4s, ipv6s := getPeers(a, u, t, peerCount)
bencoder.Encode("peers")
2014-06-30 20:59:31 +02:00
fmt.Fprintf(w, "l")
2014-06-30 20:59:31 +02:00
for _, peer := range ipv4s {
2014-07-02 03:40:29 +02:00
writePeerDict(w, &peer)
2014-06-30 20:59:31 +02:00
}
for _, peer := range ipv6s {
2014-07-02 03:40:29 +02:00
writePeerDict(w, &peer)
2013-11-24 06:49:20 +01:00
}
2014-06-30 20:59:31 +02:00
fmt.Fprintf(w, "e")
2013-08-04 21:56:31 +02:00
}
2014-06-30 20:59:31 +02:00
func writePeerDict(w io.Writer, peer *models.Peer) {
2014-06-24 04:47:43 +02:00
bencoder := bencode.NewEncoder(w)
2014-06-30 20:59:31 +02:00
fmt.Fprintf(w, "d")
bencoder.Encode("ip")
bencoder.Encode(peer.IP.String())
bencoder.Encode("peer id")
bencoder.Encode(peer.ID)
bencoder.Encode("port")
bencoder.Encode(peer.Port)
fmt.Fprintf(w, "e")
2013-08-04 21:56:31 +02:00
}
2014-06-24 04:47:43 +02:00
func minInt(a, b int) int {
if a < b {
return a
2013-11-24 06:49:20 +01:00
}
2014-05-08 07:09:02 +02:00
2014-06-24 04:47:43 +02:00
return b
2013-07-12 06:36:24 +02:00
}