2013-06-22 03:43:11 +02:00
|
|
|
// 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-22 03:43:11 +02:00
|
|
|
"fmt"
|
2013-06-23 09:56:28 +02:00
|
|
|
"log"
|
2013-06-22 03:43:11 +02:00
|
|
|
"net/http"
|
|
|
|
"path"
|
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-06-24 04:34:13 +02:00
|
|
|
_, err := validatePasskey(passkey, s.storage)
|
2013-06-22 01:31:32 +02:00
|
|
|
if err != nil {
|
2013-06-24 04:34:13 +02:00
|
|
|
fail(err, w, r)
|
2013-06-22 01:31:32 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pq, err := parseQuery(r.URL.RawQuery)
|
|
|
|
if err != nil {
|
2013-06-24 04:34:13 +02:00
|
|
|
fail(errors.New("Error parsing query"), w, r)
|
2013-06-22 01:31:32 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-24 04:34:13 +02:00
|
|
|
_, err = pq.determineIP(r)
|
2013-06-22 01:31:32 +02:00
|
|
|
if err != nil {
|
2013-06-24 04:34:13 +02:00
|
|
|
fail(err, w, r)
|
2013-06-22 01:31:32 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-23 12:21:59 +02:00
|
|
|
err = pq.validate()
|
2013-06-22 01:31:32 +02:00
|
|
|
if err != nil {
|
2013-06-24 04:34:13 +02:00
|
|
|
fail(errors.New("Malformed request"), w, r)
|
2013-06-22 01:31:32 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-23 12:21:59 +02:00
|
|
|
if !s.conf.Whitelisted(pq.params["peerId"]) {
|
2013-06-24 04:34:13 +02:00
|
|
|
fail(errors.New("Your client is not approved"), w, r)
|
2013-06-22 01:31:32 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-23 09:56:28 +02:00
|
|
|
torrent, exists, err := s.storage.FindTorrent(pq.params["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 {
|
2013-06-24 04:34:13 +02:00
|
|
|
fail(errors.New("This torrent does not exist"), w, r)
|
2013-06-22 01:31:32 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-22 03:43:11 +02:00
|
|
|
if left, _ := pq.getUint64("left"); torrent.Status == 1 && left == 0 {
|
2013-06-23 09:56:28 +02:00
|
|
|
err := s.storage.UnpruneTorrent(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
|
|
|
}
|
|
|
|
torrent.Status = 0
|
|
|
|
} else if torrent.Status != 0 {
|
|
|
|
fail(
|
|
|
|
fmt.Errorf(
|
|
|
|
"This torrent does not exist (status: %d, left: %d)",
|
|
|
|
torrent.Status,
|
|
|
|
left,
|
|
|
|
),
|
2013-06-22 03:43:11 +02:00
|
|
|
w,
|
2013-06-24 04:34:13 +02:00
|
|
|
r,
|
2013-06-22 01:31:32 +02:00
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-23 09:56:28 +02:00
|
|
|
// TODO continue
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|