2013-06-23 12:21:59 +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-23 09:56:28 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
2013-06-24 04:34:13 +02:00
|
|
|
"log"
|
2013-06-23 09:56:28 +02:00
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/pushrax/chihaya/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request) {
|
2013-07-25 16:03:04 +02:00
|
|
|
// Parse the query
|
|
|
|
pq, err := parseQuery(r.URL.RawQuery)
|
2013-06-23 09:56:28 +02:00
|
|
|
if err != nil {
|
2013-07-25 16:03:04 +02:00
|
|
|
fail(errors.New("Error parsing query"), w, r)
|
2013-06-23 09:56:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-07-25 16:03:04 +02:00
|
|
|
// Start a transaction
|
|
|
|
tx, err := s.dbConnPool.Get()
|
2013-06-23 09:56:28 +02:00
|
|
|
if err != nil {
|
2013-07-25 16:03:04 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find and validate the user
|
|
|
|
passkey, _ := path.Split(r.URL.Path)
|
|
|
|
_, err = validateUser(tx, passkey)
|
|
|
|
if err != nil {
|
|
|
|
fail(err, w, r)
|
2013-06-23 09:56:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
io.WriteString(w, "d")
|
2013-07-12 06:36:24 +02:00
|
|
|
writeBencoded(w, "files")
|
|
|
|
if pq.Infohashes != nil {
|
|
|
|
for _, infohash := range pq.Infohashes {
|
2013-07-25 16:03:04 +02:00
|
|
|
torrent, exists, err := tx.FindTorrent(infohash)
|
2013-06-23 09:56:28 +02:00
|
|
|
if err != nil {
|
2013-06-24 04:34:13 +02:00
|
|
|
log.Panicf("server: %s", err)
|
2013-06-23 09:56:28 +02:00
|
|
|
}
|
|
|
|
if exists {
|
2013-07-12 06:36:24 +02:00
|
|
|
writeBencoded(w, infohash)
|
2013-06-23 09:56:28 +02:00
|
|
|
writeScrapeInfo(w, torrent)
|
|
|
|
}
|
|
|
|
}
|
2013-07-12 06:36:24 +02:00
|
|
|
} else if infohash, exists := pq.Params["info_hash"]; exists {
|
2013-07-25 16:03:04 +02:00
|
|
|
torrent, exists, err := tx.FindTorrent(infohash)
|
2013-06-23 09:56:28 +02:00
|
|
|
if err != nil {
|
2013-06-24 04:34:13 +02:00
|
|
|
log.Panicf("server: %s", err)
|
2013-06-23 09:56:28 +02:00
|
|
|
}
|
|
|
|
if exists {
|
2013-07-12 06:36:24 +02:00
|
|
|
writeBencoded(w, infohash)
|
2013-06-23 09:56:28 +02:00
|
|
|
writeScrapeInfo(w, torrent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
io.WriteString(w, "e")
|
2013-06-24 04:34:13 +02:00
|
|
|
|
2013-07-25 16:03:04 +02:00
|
|
|
// Finish up and write headers
|
2013-06-24 04:34:13 +02:00
|
|
|
r.Close = true
|
|
|
|
w.Header().Add("Content-Type", "text/plain")
|
|
|
|
w.Header().Add("Connection", "close")
|
|
|
|
w.(http.Flusher).Flush()
|
2013-06-23 09:56:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeScrapeInfo(w io.Writer, torrent *storage.Torrent) {
|
|
|
|
io.WriteString(w, "d")
|
2013-07-12 06:36:24 +02:00
|
|
|
writeBencoded(w, "complete")
|
|
|
|
writeBencoded(w, len(torrent.Seeders))
|
|
|
|
writeBencoded(w, "downloaded")
|
|
|
|
writeBencoded(w, torrent.Snatches)
|
|
|
|
writeBencoded(w, "incomplete")
|
|
|
|
writeBencoded(w, len(torrent.Leechers))
|
2013-06-23 09:56:28 +02:00
|
|
|
io.WriteString(w, "e")
|
|
|
|
}
|