tracker/server/announce.go

81 lines
1.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"
"net/http"
"path"
2013-06-22 01:31:32 +02:00
"github.com/pushrax/chihaya/config"
2013-06-22 01:31:32 +02:00
)
func (h *handler) serveAnnounce(w http.ResponseWriter, r *http.Request) {
passkey, action := path.Split(r.URL.Path)
user, err := validatePasskey(passkey, h.storage)
2013-06-22 01:31:32 +02:00
if err != nil {
fail(err, w)
2013-06-22 01:31:32 +02:00
return
}
pq, err := parseQuery(r.URL.RawQuery)
if err != nil {
fail(errors.New("Error parsing query"), w)
2013-06-22 01:31:32 +02:00
return
}
ip, err := pq.determineIP(r)
2013-06-22 01:31:32 +02:00
if err != nil {
fail(err, w)
2013-06-22 01:31:32 +02:00
return
}
err = validateParsedQuery(pq)
2013-06-22 01:31:32 +02:00
if err != nil {
fail(errors.New("Malformed request"), w)
2013-06-22 01:31:32 +02:00
return
}
if !whitelisted(pq.params["peerId"], h.conf) {
fail(errors.New("Your client is not approved"), w)
2013-06-22 01:31:32 +02:00
return
}
torrent, exists, err := h.storage.FindTorrent(pq.params["infohash"])
2013-06-22 01:31:32 +02:00
if err != nil {
panic("server: failed to find torrent")
}
if !exists {
fail(errors.New("This torrent does not exist"), w)
2013-06-22 01:31:32 +02:00
return
}
if left, _ := pq.getUint64("left"); torrent.Status == 1 && left == 0 {
2013-06-22 01:31:32 +02:00
err := h.storage.UnpruneTorrent(torrent)
if err != nil {
panic("server: failed to unprune torrent")
}
torrent.Status = 0
} else if torrent.Status != 0 {
fail(
fmt.Errorf(
"This torrent does not exist (status: %d, left: %d)",
torrent.Status,
left,
),
w,
2013-06-22 01:31:32 +02:00
)
return
}
// TODO
2013-06-22 01:31:32 +02:00
}
func whitelisted(peerId string, conf *config.Config) bool {
// TODO
return false
2013-06-22 01:31:32 +02:00
}