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.
|
2016-08-03 09:11:52 +02:00
|
|
|
package http
|
|
|
|
|
2016-08-05 07:47:04 +02:00
|
|
|
import (
|
2016-08-17 04:32:15 +02:00
|
|
|
"context"
|
2017-02-02 08:01:05 +01:00
|
|
|
"crypto/tls"
|
2018-09-09 17:22:19 +02:00
|
|
|
"errors"
|
2016-08-05 07:47:04 +02:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
|
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"
|
2018-09-09 17:14:24 +02:00
|
|
|
"github.com/chihaya/chihaya/pkg/stop"
|
2016-08-05 07:47:04 +02:00
|
|
|
)
|
|
|
|
|
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.
|
2016-08-03 09:11:52 +02:00
|
|
|
type Config struct {
|
2017-05-12 13:12:35 +02:00
|
|
|
Addr string `yaml:"addr"`
|
2018-09-09 17:22:19 +02:00
|
|
|
HTTPSAddr string `yaml:"https_addr"`
|
2017-05-12 13:12:35 +02:00
|
|
|
ReadTimeout time.Duration `yaml:"read_timeout"`
|
|
|
|
WriteTimeout time.Duration `yaml:"write_timeout"`
|
2018-09-09 15:01:53 +02:00
|
|
|
IdleTimeout time.Duration `yaml:"idle_timeout"`
|
|
|
|
EnableKeepAlive bool `yaml:"enable_keepalive"`
|
2017-05-12 13:12:35 +02:00
|
|
|
TLSCertPath string `yaml:"tls_cert_path"`
|
|
|
|
TLSKeyPath string `yaml:"tls_key_path"`
|
2017-12-05 10:38:50 +01:00
|
|
|
EnableLegacyPHPURLs bool `yaml:"enable_legacy_php_urls"`
|
2017-05-12 13:12:35 +02:00
|
|
|
EnableRequestTiming bool `yaml:"enable_request_timing"`
|
2017-10-08 23:16:02 +02:00
|
|
|
ParseOptions `yaml:",inline"`
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
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,
|
2018-09-09 17:22:19 +02:00
|
|
|
"httpsAddr": cfg.HTTPSAddr,
|
2017-05-12 13:12:35 +02:00
|
|
|
"readTimeout": cfg.ReadTimeout,
|
|
|
|
"writeTimeout": cfg.WriteTimeout,
|
2018-09-09 15:01:53 +02:00
|
|
|
"idleTimeout": cfg.IdleTimeout,
|
|
|
|
"enableKeepAlive": cfg.EnableKeepAlive,
|
2017-05-12 13:12:35 +02:00
|
|
|
"tlsCertPath": cfg.TLSCertPath,
|
|
|
|
"tlsKeyPath": cfg.TLSKeyPath,
|
2017-12-05 10:38:50 +01:00
|
|
|
"enableLegacyPHPURLs": cfg.EnableLegacyPHPURLs,
|
2017-05-12 13:12:35 +02:00
|
|
|
"enableRequestTiming": cfg.EnableRequestTiming,
|
2017-10-08 23:16:02 +02:00
|
|
|
"allowIPSpoofing": cfg.AllowIPSpoofing,
|
|
|
|
"realIPHeader": cfg.RealIPHeader,
|
|
|
|
"maxNumWant": cfg.MaxNumWant,
|
|
|
|
"defaultNumWant": cfg.DefaultNumWant,
|
2017-10-18 04:02:45 +02:00
|
|
|
"maxScrapeInfoHashes": cfg.MaxScrapeInfoHashes,
|
2017-05-07 00:48:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-30 14:24:48 +02:00
|
|
|
// Default config constants.
|
|
|
|
const (
|
|
|
|
defaultReadTimeout = 2 * time.Second
|
|
|
|
defaultWriteTimeout = 2 * time.Second
|
2018-09-09 15:01:53 +02:00
|
|
|
defaultIdleTimeout = 30 * time.Second
|
2017-09-30 14:24:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Validate sanity checks values set in a config and returns a new config with
|
|
|
|
// default values replacing anything that is invalid.
|
|
|
|
//
|
|
|
|
// This function warns to the logger when a value is changed.
|
|
|
|
func (cfg Config) Validate() Config {
|
|
|
|
validcfg := cfg
|
|
|
|
|
|
|
|
if cfg.ReadTimeout <= 0 {
|
|
|
|
validcfg.ReadTimeout = defaultReadTimeout
|
|
|
|
log.Warn("falling back to default configuration", log.Fields{
|
|
|
|
"name": "http.ReadTimeout",
|
|
|
|
"provided": cfg.ReadTimeout,
|
|
|
|
"default": validcfg.ReadTimeout,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.WriteTimeout <= 0 {
|
|
|
|
validcfg.WriteTimeout = defaultWriteTimeout
|
|
|
|
log.Warn("falling back to default configuration", log.Fields{
|
|
|
|
"name": "http.WriteTimeout",
|
|
|
|
"provided": cfg.WriteTimeout,
|
|
|
|
"default": validcfg.WriteTimeout,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-09 15:01:53 +02:00
|
|
|
if cfg.IdleTimeout <= 0 {
|
|
|
|
validcfg.IdleTimeout = defaultIdleTimeout
|
|
|
|
|
|
|
|
if cfg.EnableKeepAlive {
|
|
|
|
// If keepalive is disabled, this configuration isn't used anyway.
|
|
|
|
log.Warn("falling back to default configuration", log.Fields{
|
|
|
|
"name": "http.IdleTimeout",
|
|
|
|
"provided": cfg.IdleTimeout,
|
|
|
|
"default": validcfg.IdleTimeout,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-30 14:24:48 +02:00
|
|
|
return validcfg
|
|
|
|
}
|
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
// Frontend represents the state of an HTTP BitTorrent Frontend.
|
2016-08-07 04:41:33 +02:00
|
|
|
type Frontend struct {
|
2017-04-30 04:29:27 +02:00
|
|
|
srv *http.Server
|
2018-09-09 17:22:19 +02:00
|
|
|
tlsSrv *http.Server
|
2017-04-30 04:29:27 +02:00
|
|
|
tlsCfg *tls.Config
|
2016-08-03 09:11:52 +02:00
|
|
|
|
2016-08-10 02:08:15 +02:00
|
|
|
logic frontend.TrackerLogic
|
2016-08-03 09:11:52 +02:00
|
|
|
Config
|
|
|
|
}
|
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
// NewFrontend creates a new instance of an HTTP Frontend that asynchronously
|
|
|
|
// serves requests.
|
2017-09-30 14:24:48 +02:00
|
|
|
func NewFrontend(logic frontend.TrackerLogic, provided Config) (*Frontend, error) {
|
|
|
|
cfg := provided.Validate()
|
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
f := &Frontend{
|
2016-08-10 02:08:15 +02:00
|
|
|
logic: logic,
|
|
|
|
Config: cfg,
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
2017-04-30 04:29:27 +02:00
|
|
|
|
2018-09-09 17:22:19 +02:00
|
|
|
if cfg.Addr == "" && cfg.HTTPSAddr == "" {
|
|
|
|
return nil, errors.New("must specify addr or https_addr or both")
|
|
|
|
}
|
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 17:22:19 +02:00
|
|
|
if cfg.HTTPSAddr != "" && f.tlsCfg == nil {
|
|
|
|
return nil, errors.New("must specify tls_cert_path and tls_key_path when using https_addr")
|
|
|
|
}
|
|
|
|
if cfg.HTTPSAddr == "" && f.tlsCfg != nil {
|
|
|
|
return nil, errors.New("must specify https_addr when using tls_cert_path and tls_key_path")
|
|
|
|
}
|
|
|
|
|
2018-12-30 11:36:32 +01:00
|
|
|
var listenerHTTP, listenerHTTPS net.Listener
|
|
|
|
var err error
|
|
|
|
if cfg.Addr != "" {
|
|
|
|
listenerHTTP, err = net.Listen("tcp", f.Addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cfg.HTTPSAddr != "" {
|
|
|
|
listenerHTTPS, err = net.Listen("tcp", f.HTTPSAddr)
|
|
|
|
if err != nil {
|
|
|
|
if listenerHTTP != nil {
|
|
|
|
listenerHTTP.Close()
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 17:22:19 +02:00
|
|
|
if cfg.Addr != "" {
|
|
|
|
go func() {
|
2018-12-30 11:36:32 +01:00
|
|
|
if err := f.serveHTTP(listenerHTTP); err != nil {
|
2018-09-09 17:22:19 +02:00
|
|
|
log.Fatal("failed while serving http", log.Err(err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.HTTPSAddr != "" {
|
|
|
|
go func() {
|
2018-12-30 11:36:32 +01:00
|
|
|
if err := f.serveHTTPS(listenerHTTPS); err != nil {
|
2018-09-09 17:22:19 +02:00
|
|
|
log.Fatal("failed while serving https", log.Err(err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2017-04-30 04:29:27 +02:00
|
|
|
|
|
|
|
return f, nil
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2016-11-27 10:56:51 +01:00
|
|
|
// Stop provides a thread-safe way to shutdown a currently running Frontend.
|
2018-09-09 17:14:24 +02:00
|
|
|
func (f *Frontend) Stop() stop.Result {
|
2018-09-09 17:22:19 +02:00
|
|
|
stopGroup := stop.NewGroup()
|
|
|
|
|
|
|
|
if f.srv != nil {
|
|
|
|
stopGroup.AddFunc(f.makeStopFunc(f.srv))
|
|
|
|
}
|
|
|
|
if f.tlsSrv != nil {
|
|
|
|
stopGroup.AddFunc(f.makeStopFunc(f.tlsSrv))
|
|
|
|
}
|
2017-04-30 04:29:27 +02:00
|
|
|
|
2018-09-09 17:22:19 +02:00
|
|
|
return stopGroup.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Frontend) makeStopFunc(stopSrv *http.Server) stop.Func {
|
|
|
|
return func() stop.Result {
|
|
|
|
c := make(stop.Channel)
|
|
|
|
go func() {
|
|
|
|
c.Done(stopSrv.Shutdown(context.Background()))
|
|
|
|
}()
|
|
|
|
return c.Result()
|
|
|
|
}
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
func (f *Frontend) handler() http.Handler {
|
2016-08-03 09:11:52 +02:00
|
|
|
router := httprouter.New()
|
2017-04-30 04:29:27 +02:00
|
|
|
router.GET("/announce", f.announceRoute)
|
|
|
|
router.GET("/scrape", f.scrapeRoute)
|
2017-12-05 10:38:50 +01:00
|
|
|
|
|
|
|
if f.EnableLegacyPHPURLs {
|
|
|
|
log.Info("http: enabling legacy PHP URLs")
|
|
|
|
router.GET("/announce.php", f.announceRoute)
|
|
|
|
router.GET("/scrape.php", f.scrapeRoute)
|
|
|
|
}
|
|
|
|
|
2016-08-05 07:47:04 +02:00
|
|
|
return router
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2018-12-30 11:36:32 +01:00
|
|
|
// serveHTTP blocks while listening and serving non-TLS HTTP BitTorrent
|
2018-09-09 17:22:19 +02:00
|
|
|
// requests until Stop() is called or an error is returned.
|
2018-12-30 11:36:32 +01:00
|
|
|
func (f *Frontend) serveHTTP(l net.Listener) error {
|
2017-04-30 04:29:27 +02:00
|
|
|
f.srv = &http.Server{
|
|
|
|
Addr: f.Addr,
|
|
|
|
Handler: f.handler(),
|
|
|
|
ReadTimeout: f.ReadTimeout,
|
|
|
|
WriteTimeout: f.WriteTimeout,
|
2018-09-09 15:01:53 +02:00
|
|
|
IdleTimeout: f.IdleTimeout,
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2018-09-09 15:01:53 +02:00
|
|
|
f.srv.SetKeepAlivesEnabled(f.EnableKeepAlive)
|
2017-02-02 08:01:05 +01:00
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
// Start the HTTP server.
|
2018-12-30 11:36:32 +01:00
|
|
|
if err := f.srv.Serve(l); err != http.ErrServerClosed {
|
2018-09-09 17:22:19 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-30 11:36:32 +01:00
|
|
|
// serveHTTPS blocks while listening and serving TLS HTTP BitTorrent
|
2018-09-09 17:22:19 +02:00
|
|
|
// requests until Stop() is called or an error is returned.
|
2018-12-30 11:36:32 +01:00
|
|
|
func (f *Frontend) serveHTTPS(l net.Listener) error {
|
2018-09-09 17:22:19 +02:00
|
|
|
f.tlsSrv = &http.Server{
|
|
|
|
Addr: f.HTTPSAddr,
|
|
|
|
TLSConfig: f.tlsCfg,
|
|
|
|
Handler: f.handler(),
|
|
|
|
ReadTimeout: f.ReadTimeout,
|
|
|
|
WriteTimeout: f.WriteTimeout,
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
2016-08-05 07:47:04 +02:00
|
|
|
|
2018-09-09 17:22:19 +02:00
|
|
|
f.tlsSrv.SetKeepAlivesEnabled(f.EnableKeepAlive)
|
|
|
|
|
|
|
|
// Start the HTTP server.
|
2018-12-30 11:36:32 +01:00
|
|
|
if err := f.tlsSrv.ServeTLS(l, "", ""); err != http.ErrServerClosed {
|
2018-09-09 17:22:19 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-08-05 07:47:04 +02:00
|
|
|
return nil
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
// 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
|
|
|
|
2017-10-08 23:16:02 +02:00
|
|
|
req, err := ParseAnnounce(r, f.ParseOptions)
|
2016-08-03 09:11:52 +02:00
|
|
|
if err != nil {
|
|
|
|
WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
2017-02-05 17:24:53 +01:00
|
|
|
af = new(bittorrent.AddressFamily)
|
2017-02-01 02:58:08 +01:00
|
|
|
*af = req.IP.AddressFamily
|
2016-08-03 09:11:52 +02:00
|
|
|
|
2017-06-06 04:53:17 +02:00
|
|
|
ctx, resp, err := f.logic.HandleAnnounce(context.Background(), req)
|
2016-08-03 09:11:52 +02:00
|
|
|
if err != nil {
|
|
|
|
WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = WriteAnnounceResponse(w, resp)
|
|
|
|
if err != nil {
|
|
|
|
WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-06 04:53:17 +02:00
|
|
|
go f.logic.AfterAnnounce(ctx, req, resp)
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2017-04-30 04:29:27 +02:00
|
|
|
// 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
|
|
|
|
2017-10-08 23:16:02 +02:00
|
|
|
req, err := ParseScrape(r, f.ParseOptions)
|
2016-08-03 09:11:52 +02:00
|
|
|
if err != nil {
|
|
|
|
WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-08 17:40:30 +02:00
|
|
|
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))
|
2016-09-08 17:40:30 +02:00
|
|
|
WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-28 20:55:04 +01:00
|
|
|
reqIP := net.ParseIP(host)
|
|
|
|
if reqIP.To4() != nil {
|
2017-01-20 20:34:39 +01:00
|
|
|
req.AddressFamily = bittorrent.IPv4
|
2016-11-28 20:55:04 +01:00
|
|
|
} else if len(reqIP) == net.IPv6len { // implies reqIP.To4() == nil
|
2017-01-20 20:34:39 +01:00
|
|
|
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})
|
2017-10-18 04:02:45 +02:00
|
|
|
WriteError(w, bittorrent.ErrInvalidIP)
|
2016-11-28 20:55:04 +01:00
|
|
|
return
|
|
|
|
}
|
2017-02-05 17:24:53 +01:00
|
|
|
af = new(bittorrent.AddressFamily)
|
2017-02-01 02:58:08 +01:00
|
|
|
*af = req.AddressFamily
|
2016-11-28 20:55:04 +01:00
|
|
|
|
2017-06-06 04:53:17 +02:00
|
|
|
ctx, resp, err := f.logic.HandleScrape(context.Background(), req)
|
2016-08-03 09:11:52 +02:00
|
|
|
if err != nil {
|
|
|
|
WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = WriteScrapeResponse(w, resp)
|
|
|
|
if err != nil {
|
|
|
|
WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-06 04:53:17 +02:00
|
|
|
go f.logic.AfterScrape(ctx, req, resp)
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|