2015-01-01 18:02:25 +01:00
|
|
|
// Copyright 2015 The Chihaya Authors. All rights reserved.
|
2014-07-08 03:59:41 +02:00
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2014-07-08 04:23:01 +02:00
|
|
|
"encoding/json"
|
2014-07-08 22:55:24 +02:00
|
|
|
"io/ioutil"
|
2014-07-08 03:59:41 +02:00
|
|
|
"net/http"
|
2014-07-16 11:46:33 +02:00
|
|
|
"net/url"
|
2014-09-25 17:34:27 +02:00
|
|
|
"runtime"
|
2014-07-08 03:59:41 +02:00
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2014-07-08 04:23:01 +02:00
|
|
|
|
2014-07-22 02:24:51 +02:00
|
|
|
"github.com/chihaya/chihaya/stats"
|
2014-07-17 06:09:56 +02:00
|
|
|
"github.com/chihaya/chihaya/tracker/models"
|
2014-07-08 03:59:41 +02:00
|
|
|
)
|
|
|
|
|
2014-07-16 17:52:25 +02:00
|
|
|
const jsonContentType = "application/json; charset=UTF-8"
|
|
|
|
|
2014-07-25 09:01:26 +02:00
|
|
|
func handleError(err error) (int, error) {
|
|
|
|
if err == nil {
|
|
|
|
return http.StatusOK, nil
|
2014-07-25 09:39:02 +02:00
|
|
|
} else if _, ok := err.(models.NotFoundError); ok {
|
|
|
|
stats.RecordEvent(stats.ClientError)
|
|
|
|
return http.StatusNotFound, nil
|
2014-07-25 09:01:26 +02:00
|
|
|
} else if _, ok := err.(models.ClientError); ok {
|
|
|
|
stats.RecordEvent(stats.ClientError)
|
|
|
|
return http.StatusBadRequest, nil
|
2014-07-22 07:19:09 +02:00
|
|
|
}
|
2014-07-25 09:01:26 +02:00
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
2014-07-22 07:19:09 +02:00
|
|
|
|
2014-07-25 09:01:26 +02:00
|
|
|
func (s *Server) check(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
2015-02-09 00:05:30 +01:00
|
|
|
// Attempt to ping the backend if private tracker is enabled.
|
|
|
|
if s.config.PrivateEnabled {
|
|
|
|
if err := s.tracker.Backend.Ping(); err != nil {
|
|
|
|
return handleError(err)
|
|
|
|
}
|
2015-02-08 23:15:27 +01:00
|
|
|
}
|
2015-02-09 00:05:30 +01:00
|
|
|
|
2014-07-25 09:01:26 +02:00
|
|
|
_, err := w.Write([]byte("STILL-ALIVE"))
|
|
|
|
return handleError(err)
|
2014-07-22 07:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) stats(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
2014-07-22 07:12:41 +02:00
|
|
|
w.Header().Set("Content-Type", jsonContentType)
|
|
|
|
|
2014-07-25 01:35:15 +02:00
|
|
|
var err error
|
2014-07-25 09:01:26 +02:00
|
|
|
var val interface{}
|
|
|
|
query := r.URL.Query()
|
2014-07-25 01:35:15 +02:00
|
|
|
|
2014-09-25 17:34:27 +02:00
|
|
|
stats.DefaultStats.GoRoutines = runtime.NumGoroutine()
|
|
|
|
|
2014-07-25 09:01:26 +02:00
|
|
|
if _, flatten := query["flatten"]; flatten {
|
|
|
|
val = stats.DefaultStats.Flattened()
|
2014-07-25 01:35:15 +02:00
|
|
|
} else {
|
2014-07-25 09:01:26 +02:00
|
|
|
val = stats.DefaultStats
|
2014-07-25 01:35:15 +02:00
|
|
|
}
|
|
|
|
|
2014-07-25 09:01:26 +02:00
|
|
|
if _, pretty := query["pretty"]; pretty {
|
|
|
|
var buf []byte
|
|
|
|
buf, err = json.MarshalIndent(val, "", " ")
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
_, err = w.Write(buf)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = json.NewEncoder(w).Encode(val)
|
2014-07-16 11:44:26 +02:00
|
|
|
}
|
|
|
|
|
2014-07-25 09:01:26 +02:00
|
|
|
return handleError(err)
|
2014-07-16 11:44:26 +02:00
|
|
|
}
|
|
|
|
|
2014-07-25 09:01:26 +02:00
|
|
|
func handleTorrentError(err error, w *Writer) (int, error) {
|
|
|
|
if err == nil {
|
|
|
|
return http.StatusOK, nil
|
|
|
|
} else if _, ok := err.(models.ClientError); ok {
|
2014-07-24 23:57:44 +02:00
|
|
|
w.WriteError(err)
|
2014-07-25 00:01:26 +02:00
|
|
|
stats.RecordEvent(stats.ClientError)
|
2014-07-17 06:09:56 +02:00
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|
2014-08-13 23:45:34 +02:00
|
|
|
|
2014-07-24 23:57:44 +02:00
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
2014-07-23 07:34:40 +02:00
|
|
|
|
2014-07-24 23:57:44 +02:00
|
|
|
func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
2014-07-25 09:01:26 +02:00
|
|
|
stats.RecordEvent(stats.Announce)
|
|
|
|
|
2014-07-24 23:57:44 +02:00
|
|
|
writer := &Writer{w}
|
|
|
|
ann, err := NewAnnounce(s.config, r, p)
|
|
|
|
if err != nil {
|
2014-07-25 09:01:26 +02:00
|
|
|
return handleTorrentError(err, writer)
|
2014-07-24 23:57:44 +02:00
|
|
|
}
|
|
|
|
|
2014-07-25 09:01:26 +02:00
|
|
|
return handleTorrentError(s.tracker.HandleAnnounce(ann, writer), writer)
|
2014-07-17 06:09:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
2014-07-25 09:01:26 +02:00
|
|
|
stats.RecordEvent(stats.Scrape)
|
|
|
|
|
2014-07-17 06:09:56 +02:00
|
|
|
writer := &Writer{w}
|
2014-07-24 23:57:44 +02:00
|
|
|
scrape, err := NewScrape(s.config, r, p)
|
|
|
|
if err != nil {
|
2014-07-25 09:01:26 +02:00
|
|
|
return handleTorrentError(err, writer)
|
2014-07-17 06:09:56 +02:00
|
|
|
}
|
|
|
|
|
2014-07-25 09:01:26 +02:00
|
|
|
return handleTorrentError(s.tracker.HandleScrape(scrape, writer), writer)
|
2014-07-17 06:09:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
2014-07-16 11:46:33 +02:00
|
|
|
infohash, err := url.QueryUnescape(p.ByName("infohash"))
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusNotFound, err
|
|
|
|
}
|
|
|
|
|
2014-08-13 23:45:34 +02:00
|
|
|
torrent, err := s.tracker.FindTorrent(infohash)
|
2014-07-25 09:05:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return handleError(err)
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-16 17:52:25 +02:00
|
|
|
w.Header().Set("Content-Type", jsonContentType)
|
2014-07-08 04:23:01 +02:00
|
|
|
e := json.NewEncoder(w)
|
2014-07-25 09:01:26 +02:00
|
|
|
return handleError(e.Encode(torrent))
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-17 06:09:56 +02:00
|
|
|
func (s *Server) putTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
2014-07-08 22:55:24 +02:00
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 22:55:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var torrent models.Torrent
|
|
|
|
err = json.Unmarshal(body, &torrent)
|
|
|
|
if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusBadRequest, err
|
2014-07-08 22:55:24 +02:00
|
|
|
}
|
|
|
|
|
2014-08-13 23:45:34 +02:00
|
|
|
s.tracker.PutTorrent(&torrent)
|
|
|
|
return http.StatusOK, nil
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-17 06:09:56 +02:00
|
|
|
func (s *Server) delTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
2014-07-16 11:46:33 +02:00
|
|
|
infohash, err := url.QueryUnescape(p.ByName("infohash"))
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusNotFound, err
|
|
|
|
}
|
|
|
|
|
2014-08-13 23:45:34 +02:00
|
|
|
s.tracker.DeleteTorrent(infohash)
|
|
|
|
return http.StatusOK, nil
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-17 06:09:56 +02:00
|
|
|
func (s *Server) getUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
2014-08-13 23:45:34 +02:00
|
|
|
user, err := s.tracker.FindUser(p.ByName("passkey"))
|
2014-07-23 06:37:30 +02:00
|
|
|
if err == models.ErrUserDNE {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusNotFound, err
|
2014-07-08 04:23:01 +02:00
|
|
|
} else if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-16 17:52:25 +02:00
|
|
|
w.Header().Set("Content-Type", jsonContentType)
|
2014-07-08 04:23:01 +02:00
|
|
|
e := json.NewEncoder(w)
|
2014-07-25 09:01:26 +02:00
|
|
|
return handleError(e.Encode(user))
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-17 06:09:56 +02:00
|
|
|
func (s *Server) putUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
2014-07-08 22:55:24 +02:00
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 22:55:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var user models.User
|
|
|
|
err = json.Unmarshal(body, &user)
|
|
|
|
if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusBadRequest, err
|
2014-07-08 22:55:24 +02:00
|
|
|
}
|
|
|
|
|
2014-08-13 23:45:34 +02:00
|
|
|
s.tracker.PutUser(&user)
|
|
|
|
return http.StatusOK, nil
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-17 06:09:56 +02:00
|
|
|
func (s *Server) delUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
2014-08-13 23:45:34 +02:00
|
|
|
s.tracker.DeleteUser(p.ByName("passkey"))
|
|
|
|
return http.StatusOK, nil
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2015-02-08 08:20:48 +01:00
|
|
|
func (s *Server) getClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
|
|
|
if err := s.tracker.ClientApproved(p.ByName("clientID")); err != nil {
|
|
|
|
return http.StatusNotFound, err
|
|
|
|
}
|
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|
|
|
|
|
2014-07-17 06:09:56 +02:00
|
|
|
func (s *Server) putClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
2014-08-13 23:45:34 +02:00
|
|
|
s.tracker.PutClient(p.ByName("clientID"))
|
|
|
|
return http.StatusOK, nil
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-17 06:09:56 +02:00
|
|
|
func (s *Server) delClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
2014-08-13 23:45:34 +02:00
|
|
|
s.tracker.DeleteClient(p.ByName("clientID"))
|
|
|
|
return http.StatusOK, nil
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|