tracker/frontend/http/frontend.go

265 lines
6.6 KiB
Go
Raw Normal View History

2016-08-07 04:41:33 +02:00
// Package http implements a BitTorrent frontend via the HTTP protocol as
2016-08-04 20:08:26 +02:00
// described in BEP 3 and BEP 23.
package http
2016-08-05 07:47:04 +02:00
import (
2016-08-17 04:32:15 +02:00
"context"
"crypto/tls"
2016-08-05 07:47:04 +02:00
"net"
"net/http"
"time"
"github.com/julienschmidt/httprouter"
"github.com/prometheus/client_golang/prometheus"
2016-11-28 20:55:04 +01:00
"github.com/chihaya/chihaya/bittorrent"
2016-08-17 03:42:08 +02:00
"github.com/chihaya/chihaya/frontend"
2017-06-20 14:58:44 +02:00
"github.com/chihaya/chihaya/pkg/log"
2016-08-05 07:47:04 +02:00
)
2016-08-05 09:35:17 +02:00
func init() {
prometheus.MustRegister(promResponseDurationMilliseconds)
}
2016-11-28 20:55:04 +01:00
// ErrInvalidIP indicates an invalid IP.
var ErrInvalidIP = bittorrent.ClientError("invalid IP")
2016-08-04 20:48:32 +02:00
var promResponseDurationMilliseconds = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
2016-08-17 03:42:08 +02:00
Name: "chihaya_http_response_duration_milliseconds",
2016-08-04 20:48:32 +02:00
Help: "The duration of time it takes to receive and write a response to an API request",
Buckets: prometheus.ExponentialBuckets(9.375, 2, 10),
},
2017-02-01 02:58:08 +01:00
[]string{"action", "address_family", "error"},
2016-08-04 20:48:32 +02:00
)
2016-08-07 04:41:33 +02:00
// recordResponseDuration records the duration of time to respond to a Request
// in milliseconds .
2017-02-01 02:58:08 +01:00
func recordResponseDuration(action string, af *bittorrent.AddressFamily, err error, duration time.Duration) {
2016-08-05 07:47:04 +02:00
var errString string
if err != nil {
if _, ok := err.(bittorrent.ClientError); ok {
errString = err.Error()
} else {
errString = "internal error"
}
2016-08-05 07:47:04 +02:00
}
2017-02-01 02:58:08 +01:00
var afString string
if af == nil {
afString = "Unknown"
} else if *af == bittorrent.IPv4 {
afString = "IPv4"
} else if *af == bittorrent.IPv6 {
afString = "IPv6"
}
2016-08-04 20:48:32 +02:00
promResponseDurationMilliseconds.
2017-02-01 02:58:08 +01:00
WithLabelValues(action, afString, errString).
2016-08-04 20:48:32 +02:00
Observe(float64(duration.Nanoseconds()) / float64(time.Millisecond))
}
2016-08-04 20:08:26 +02:00
// Config represents all of the configurable options for an HTTP BitTorrent
2016-08-07 04:41:33 +02:00
// Frontend.
type Config struct {
2017-05-12 13:12:35 +02:00
Addr string `yaml:"addr"`
ReadTimeout time.Duration `yaml:"read_timeout"`
WriteTimeout time.Duration `yaml:"write_timeout"`
AllowIPSpoofing bool `yaml:"allow_ip_spoofing"`
RealIPHeader string `yaml:"real_ip_header"`
TLSCertPath string `yaml:"tls_cert_path"`
TLSKeyPath string `yaml:"tls_key_path"`
EnableRequestTiming bool `yaml:"enable_request_timing"`
}
2017-05-07 00:48:44 +02:00
// LogFields renders the current config as a set of Logrus fields.
func (cfg Config) LogFields() log.Fields {
return log.Fields{
2017-05-12 13:12:35 +02:00
"addr": cfg.Addr,
"readTimeout": cfg.ReadTimeout,
"writeTimeout": cfg.WriteTimeout,
"allowIPSpoofing": cfg.AllowIPSpoofing,
"realIPHeader": cfg.RealIPHeader,
"tlsCertPath": cfg.TLSCertPath,
"tlsKeyPath": cfg.TLSKeyPath,
"enableRequestTiming": cfg.EnableRequestTiming,
2017-05-07 00:48:44 +02:00
}
}
// Frontend represents the state of an HTTP BitTorrent Frontend.
2016-08-07 04:41:33 +02:00
type Frontend struct {
srv *http.Server
tlsCfg *tls.Config
2016-08-10 02:08:15 +02:00
logic frontend.TrackerLogic
Config
}
// NewFrontend creates a new instance of an HTTP Frontend that asynchronously
// serves requests.
func NewFrontend(logic frontend.TrackerLogic, cfg Config) (*Frontend, error) {
f := &Frontend{
2016-08-10 02:08:15 +02:00
logic: logic,
Config: cfg,
}
// If TLS is enabled, create a key pair.
if cfg.TLSCertPath != "" && cfg.TLSKeyPath != "" {
var err error
f.tlsCfg = &tls.Config{
Certificates: make([]tls.Certificate, 1),
}
f.tlsCfg.Certificates[0], err = tls.LoadX509KeyPair(cfg.TLSCertPath, cfg.TLSKeyPath)
if err != nil {
return nil, err
}
}
go func() {
if err := f.listenAndServe(); err != nil {
2017-06-20 14:58:44 +02:00
log.Fatal("failed while serving http", log.Err(err))
}
}()
return f, nil
}
2016-11-27 10:56:51 +01:00
// Stop provides a thread-safe way to shutdown a currently running Frontend.
func (f *Frontend) Stop() <-chan error {
c := make(chan error)
go func() {
if err := f.srv.Shutdown(context.Background()); err != nil {
c <- err
} else {
close(c)
}
}()
return c
}
func (f *Frontend) handler() http.Handler {
router := httprouter.New()
router.GET("/announce", f.announceRoute)
router.GET("/scrape", f.scrapeRoute)
2016-08-05 07:47:04 +02:00
return router
}
// listenAndServe blocks while listening and serving HTTP BitTorrent requests
// until Stop() is called or an error is returned.
func (f *Frontend) listenAndServe() error {
f.srv = &http.Server{
Addr: f.Addr,
TLSConfig: f.tlsCfg,
Handler: f.handler(),
ReadTimeout: f.ReadTimeout,
WriteTimeout: f.WriteTimeout,
}
// Disable KeepAlives.
f.srv.SetKeepAlivesEnabled(false)
// Start the HTTP server.
if err := f.srv.ListenAndServe(); err != http.ErrServerClosed {
return err
}
2016-08-05 07:47:04 +02:00
return nil
}
// announceRoute parses and responds to an Announce.
func (f *Frontend) announceRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
2016-08-05 07:47:04 +02:00
var err error
2017-05-12 13:12:35 +02:00
var start time.Time
if f.EnableRequestTiming {
start = time.Now()
}
2017-02-01 02:58:08 +01:00
var af *bittorrent.AddressFamily
2017-05-12 13:12:35 +02:00
defer func() {
if f.EnableRequestTiming {
recordResponseDuration("announce", af, err, time.Since(start))
} else {
recordResponseDuration("announce", af, err, time.Duration(0))
}
}()
2016-08-04 20:48:32 +02:00
req, err := ParseAnnounce(r, f.RealIPHeader, f.AllowIPSpoofing)
if err != nil {
WriteError(w, err)
return
}
af = new(bittorrent.AddressFamily)
2017-02-01 02:58:08 +01:00
*af = req.IP.AddressFamily
ctx, resp, err := f.logic.HandleAnnounce(context.Background(), req)
if err != nil {
WriteError(w, err)
return
}
err = WriteAnnounceResponse(w, resp)
if err != nil {
WriteError(w, err)
return
}
go f.logic.AfterAnnounce(ctx, req, resp)
}
// scrapeRoute parses and responds to a Scrape.
func (f *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
2016-08-05 07:47:04 +02:00
var err error
2017-05-12 13:12:35 +02:00
var start time.Time
if f.EnableRequestTiming {
start = time.Now()
}
2017-02-01 02:58:08 +01:00
var af *bittorrent.AddressFamily
2017-05-12 13:12:35 +02:00
defer func() {
if f.EnableRequestTiming {
recordResponseDuration("scrape", af, err, time.Since(start))
} else {
recordResponseDuration("scrape", af, err, time.Duration(0))
}
}()
2016-08-04 20:48:32 +02:00
req, err := ParseScrape(r)
if err != nil {
WriteError(w, err)
return
}
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
2017-06-20 14:58:44 +02:00
log.Error("http: unable to determine remote address for scrape", log.Err(err))
WriteError(w, err)
return
}
2016-11-28 20:55:04 +01:00
reqIP := net.ParseIP(host)
if reqIP.To4() != nil {
req.AddressFamily = bittorrent.IPv4
2016-11-28 20:55:04 +01:00
} else if len(reqIP) == net.IPv6len { // implies reqIP.To4() == nil
req.AddressFamily = bittorrent.IPv6
2016-11-28 20:55:04 +01:00
} else {
2017-06-20 14:58:44 +02:00
log.Error("http: invalid IP: neither v4 nor v6", log.Fields{"RemoteAddr": r.RemoteAddr})
2016-11-28 20:55:04 +01:00
WriteError(w, ErrInvalidIP)
return
}
af = new(bittorrent.AddressFamily)
2017-02-01 02:58:08 +01:00
*af = req.AddressFamily
2016-11-28 20:55:04 +01:00
ctx, resp, err := f.logic.HandleScrape(context.Background(), req)
if err != nil {
WriteError(w, err)
return
}
err = WriteScrapeResponse(w, resp)
if err != nil {
WriteError(w, err)
return
}
go f.logic.AfterScrape(ctx, req, resp)
}