2016-08-10 02:08:15 +02:00
|
|
|
// Package middleware implements the TrackerLogic interface by executing
|
|
|
|
// a series of middleware hooks.
|
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2016-08-17 04:32:15 +02:00
|
|
|
"context"
|
2016-08-10 02:08:15 +02:00
|
|
|
"time"
|
|
|
|
|
2016-08-17 03:42:08 +02:00
|
|
|
"github.com/chihaya/chihaya/bittorrent"
|
|
|
|
"github.com/chihaya/chihaya/frontend"
|
2017-06-20 14:58:44 +02:00
|
|
|
"github.com/chihaya/chihaya/pkg/log"
|
2017-02-03 03:09:25 +01:00
|
|
|
"github.com/chihaya/chihaya/pkg/stop"
|
2016-08-17 03:42:08 +02:00
|
|
|
"github.com/chihaya/chihaya/storage"
|
2016-08-10 02:08:15 +02:00
|
|
|
)
|
|
|
|
|
2016-09-05 20:06:42 +02:00
|
|
|
// Config holds the configuration common across all middleware.
|
2016-08-10 02:08:15 +02:00
|
|
|
type Config struct {
|
2017-10-08 23:41:00 +02:00
|
|
|
AnnounceInterval time.Duration `yaml:"announce_interval"`
|
2016-08-10 02:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ frontend.TrackerLogic = &Logic{}
|
|
|
|
|
2016-09-05 20:06:42 +02:00
|
|
|
// NewLogic creates a new instance of a TrackerLogic that executes the provided
|
|
|
|
// middleware hooks.
|
2016-09-01 03:09:46 +02:00
|
|
|
func NewLogic(cfg Config, peerStore storage.PeerStore, preHooks, postHooks []Hook) *Logic {
|
2017-10-08 23:41:00 +02:00
|
|
|
return &Logic{
|
2016-09-01 03:09:46 +02:00
|
|
|
announceInterval: cfg.AnnounceInterval,
|
|
|
|
peerStore: peerStore,
|
2017-10-08 23:41:00 +02:00
|
|
|
preHooks: append(preHooks, &responseHook{store: peerStore}),
|
2016-09-08 02:25:10 +02:00
|
|
|
postHooks: append(postHooks, &swarmInteractionHook{store: peerStore}),
|
2016-08-10 02:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logic is an implementation of the TrackerLogic that functions by
|
|
|
|
// executing a series of middleware hooks.
|
|
|
|
type Logic struct {
|
2016-09-01 03:09:46 +02:00
|
|
|
announceInterval time.Duration
|
|
|
|
peerStore storage.PeerStore
|
|
|
|
preHooks []Hook
|
|
|
|
postHooks []Hook
|
2016-08-10 02:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleAnnounce generates a response for an Announce.
|
2017-06-06 04:53:17 +02:00
|
|
|
func (l *Logic) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest) (_ context.Context, resp *bittorrent.AnnounceResponse, err error) {
|
2016-09-06 00:23:10 +02:00
|
|
|
resp = &bittorrent.AnnounceResponse{
|
2016-09-08 16:46:16 +02:00
|
|
|
Interval: l.announceInterval,
|
|
|
|
MinInterval: l.announceInterval,
|
|
|
|
Compact: req.Compact,
|
2016-08-10 02:08:15 +02:00
|
|
|
}
|
2016-09-01 03:09:46 +02:00
|
|
|
for _, h := range l.preHooks {
|
2016-09-06 00:23:10 +02:00
|
|
|
if ctx, err = h.HandleAnnounce(ctx, req, resp); err != nil {
|
2017-06-06 04:53:17 +02:00
|
|
|
return nil, nil, err
|
2016-08-10 02:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 14:58:44 +02:00
|
|
|
log.Debug("generated announce response", resp)
|
2017-06-06 04:53:17 +02:00
|
|
|
return ctx, resp, nil
|
2016-08-10 02:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AfterAnnounce does something with the results of an Announce after it has
|
|
|
|
// been completed.
|
|
|
|
func (l *Logic) AfterAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) {
|
2016-09-06 00:23:10 +02:00
|
|
|
var err error
|
2016-09-01 03:09:46 +02:00
|
|
|
for _, h := range l.postHooks {
|
2016-09-06 00:23:10 +02:00
|
|
|
if ctx, err = h.HandleAnnounce(ctx, req, resp); err != nil {
|
2017-06-20 14:58:44 +02:00
|
|
|
log.Error("post-announce hooks failed", log.Err(err))
|
2016-08-10 02:08:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleScrape generates a response for a Scrape.
|
2017-06-06 04:53:17 +02:00
|
|
|
func (l *Logic) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest) (_ context.Context, resp *bittorrent.ScrapeResponse, err error) {
|
2016-09-06 00:23:10 +02:00
|
|
|
resp = &bittorrent.ScrapeResponse{
|
2017-01-29 18:47:07 +01:00
|
|
|
Files: make([]bittorrent.Scrape, 0, len(req.InfoHashes)),
|
2016-08-10 02:08:15 +02:00
|
|
|
}
|
2016-09-01 03:09:46 +02:00
|
|
|
for _, h := range l.preHooks {
|
2016-09-06 00:23:10 +02:00
|
|
|
if ctx, err = h.HandleScrape(ctx, req, resp); err != nil {
|
2017-06-06 04:53:17 +02:00
|
|
|
return nil, nil, err
|
2016-08-10 02:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 14:58:44 +02:00
|
|
|
log.Debug("generated scrape response", resp)
|
2017-06-06 04:53:17 +02:00
|
|
|
return ctx, resp, nil
|
2016-08-10 02:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AfterScrape does something with the results of a Scrape after it has been
|
|
|
|
// completed.
|
|
|
|
func (l *Logic) AfterScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) {
|
2016-09-06 00:23:10 +02:00
|
|
|
var err error
|
2016-09-01 03:09:46 +02:00
|
|
|
for _, h := range l.postHooks {
|
2016-09-06 00:23:10 +02:00
|
|
|
if ctx, err = h.HandleScrape(ctx, req, resp); err != nil {
|
2017-06-20 14:58:44 +02:00
|
|
|
log.Error("post-scrape hooks failed", log.Err(err))
|
2016-08-10 02:08:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-24 19:38:05 +02:00
|
|
|
|
|
|
|
// Stop stops the Logic.
|
|
|
|
//
|
2017-02-03 03:09:25 +01:00
|
|
|
// This stops any hooks that implement stop.stop.
|
2016-09-24 19:38:05 +02:00
|
|
|
func (l *Logic) Stop() []error {
|
2017-02-03 03:09:25 +01:00
|
|
|
stopGroup := stop.NewGroup()
|
2016-09-24 19:38:05 +02:00
|
|
|
for _, hook := range l.preHooks {
|
2017-02-03 03:09:25 +01:00
|
|
|
stoppable, ok := hook.(stop.Stopper)
|
2016-09-24 19:38:05 +02:00
|
|
|
if ok {
|
|
|
|
stopGroup.Add(stoppable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, hook := range l.postHooks {
|
2017-02-03 03:09:25 +01:00
|
|
|
stoppable, ok := hook.(stop.Stopper)
|
2016-09-24 19:38:05 +02:00
|
|
|
if ok {
|
|
|
|
stopGroup.Add(stoppable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stopGroup.Stop()
|
|
|
|
}
|