2014-07-06 23:56:54 +02:00
|
|
|
// Copyright 2014 The Chihaya Authors. All rights reserved.
|
2014-07-02 03:40:29 +02:00
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
2014-07-16 10:40:17 +02:00
|
|
|
// Package http implements an http-serving BitTorrent tracker.
|
2014-07-02 03:40:29 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2014-07-22 07:38:07 +02:00
|
|
|
"net"
|
2014-07-02 03:40:29 +02:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
"github.com/stretchr/graceful"
|
|
|
|
|
|
|
|
"github.com/chihaya/chihaya/config"
|
2014-07-22 07:38:07 +02:00
|
|
|
"github.com/chihaya/chihaya/stats"
|
2014-07-17 06:09:56 +02:00
|
|
|
"github.com/chihaya/chihaya/tracker"
|
2014-07-23 06:33:00 +02:00
|
|
|
"github.com/chihaya/chihaya/tracker/models"
|
2014-07-02 03:40:29 +02:00
|
|
|
)
|
|
|
|
|
2014-07-17 06:09:56 +02:00
|
|
|
type ResponseHandler func(http.ResponseWriter, *http.Request, httprouter.Params) (int, error)
|
2014-07-02 03:40:29 +02:00
|
|
|
|
2014-07-17 06:09:56 +02:00
|
|
|
type Server struct {
|
|
|
|
config *config.Config
|
|
|
|
tracker *tracker.Tracker
|
2014-07-02 03:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func makeHandler(handler ResponseHandler) httprouter.Handle {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
start := time.Now()
|
2014-07-16 17:52:59 +02:00
|
|
|
|
|
|
|
httpCode, err := handler(w, r, p)
|
2014-07-22 07:40:52 +02:00
|
|
|
stats.RecordEvent(stats.HandledRequest)
|
|
|
|
|
2014-07-23 06:33:00 +02:00
|
|
|
if err != nil && err != models.ErrBadRequest {
|
2014-07-22 07:40:52 +02:00
|
|
|
stats.RecordEvent(stats.ErroredRequest)
|
2014-07-16 17:52:59 +02:00
|
|
|
http.Error(w, err.Error(), httpCode)
|
2014-07-09 06:53:57 +02:00
|
|
|
}
|
2014-07-16 12:01:09 +02:00
|
|
|
|
2014-07-22 20:57:36 +02:00
|
|
|
duration := time.Since(start)
|
|
|
|
stats.RecordTiming(stats.ResponseTime, duration)
|
|
|
|
|
2014-07-16 17:52:59 +02:00
|
|
|
if glog.V(2) {
|
|
|
|
glog.Infof(
|
|
|
|
"Completed %v %s %s in %v",
|
|
|
|
httpCode,
|
|
|
|
http.StatusText(httpCode),
|
|
|
|
r.URL.Path,
|
2014-07-22 20:57:36 +02:00
|
|
|
duration,
|
2014-07-16 17:52:59 +02:00
|
|
|
)
|
|
|
|
}
|
2014-07-02 03:40:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-17 07:00:26 +02:00
|
|
|
func newRouter(s *Server) *httprouter.Router {
|
2014-07-02 03:40:29 +02:00
|
|
|
r := httprouter.New()
|
2014-07-09 06:53:57 +02:00
|
|
|
|
2014-07-17 06:09:56 +02:00
|
|
|
if s.config.Private {
|
|
|
|
r.GET("/users/:passkey/announce", makeHandler(s.serveAnnounce))
|
|
|
|
r.GET("/users/:passkey/scrape", makeHandler(s.serveScrape))
|
2014-07-09 06:53:57 +02:00
|
|
|
|
2014-07-17 06:09:56 +02:00
|
|
|
r.PUT("/users/:passkey", makeHandler(s.putUser))
|
|
|
|
r.DELETE("/users/:passkey", makeHandler(s.delUser))
|
2014-07-02 03:40:29 +02:00
|
|
|
} else {
|
2014-07-17 06:09:56 +02:00
|
|
|
r.GET("/announce", makeHandler(s.serveAnnounce))
|
|
|
|
r.GET("/scrape", makeHandler(s.serveScrape))
|
2014-07-02 03:40:29 +02:00
|
|
|
}
|
|
|
|
|
2014-07-17 06:09:56 +02:00
|
|
|
if s.config.Whitelist {
|
|
|
|
r.PUT("/clients/:clientID", makeHandler(s.putClient))
|
|
|
|
r.DELETE("/clients/:clientID", makeHandler(s.delClient))
|
2014-07-09 06:53:57 +02:00
|
|
|
}
|
|
|
|
|
2014-07-17 06:09:56 +02:00
|
|
|
r.GET("/torrents/:infohash", makeHandler(s.getTorrent))
|
|
|
|
r.PUT("/torrents/:infohash", makeHandler(s.putTorrent))
|
|
|
|
r.DELETE("/torrents/:infohash", makeHandler(s.delTorrent))
|
|
|
|
r.GET("/check", makeHandler(s.check))
|
2014-07-22 07:19:09 +02:00
|
|
|
r.GET("/stats", makeHandler(s.stats))
|
2014-07-09 07:34:34 +02:00
|
|
|
|
2014-07-02 03:40:29 +02:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2014-07-22 07:38:07 +02:00
|
|
|
func (s *Server) connState(conn net.Conn, state http.ConnState) {
|
|
|
|
switch state {
|
|
|
|
case http.StateNew:
|
|
|
|
stats.RecordEvent(stats.AcceptedConnection)
|
|
|
|
|
|
|
|
case http.StateClosed:
|
|
|
|
stats.RecordEvent(stats.ClosedConnection)
|
|
|
|
|
|
|
|
case http.StateHijacked:
|
|
|
|
panic("connection impossibly hijacked")
|
|
|
|
|
|
|
|
case http.StateActive: // Ignore.
|
|
|
|
case http.StateIdle: // Ignore.
|
|
|
|
|
|
|
|
default:
|
|
|
|
glog.Errorf("Connection transitioned to unknown state %s (%d)", state, state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-17 06:09:56 +02:00
|
|
|
func Serve(cfg *config.Config, tkr *tracker.Tracker) {
|
|
|
|
srv := &Server{
|
|
|
|
config: cfg,
|
|
|
|
tracker: tkr,
|
2014-07-02 03:40:29 +02:00
|
|
|
}
|
|
|
|
|
2014-07-16 17:52:59 +02:00
|
|
|
glog.V(0).Info("Starting on ", cfg.Addr)
|
2014-07-22 07:38:07 +02:00
|
|
|
|
|
|
|
grace := graceful.Server{
|
|
|
|
Timeout: cfg.RequestTimeout.Duration,
|
|
|
|
ConnState: srv.connState,
|
|
|
|
Server: &http.Server{
|
|
|
|
Addr: cfg.Addr,
|
|
|
|
Handler: newRouter(srv),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
grace.ListenAndServe()
|
|
|
|
|
2014-07-21 09:54:22 +02:00
|
|
|
err := srv.tracker.Close()
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Failed to shutdown tracker cleanly: %s", err.Error())
|
|
|
|
}
|
2014-07-02 03:40:29 +02:00
|
|
|
}
|