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-09-05 18:10:42 +02:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2016-08-17 03:42:08 +02:00
|
|
|
"github.com/chihaya/chihaya/bittorrent"
|
|
|
|
"github.com/chihaya/chihaya/frontend"
|
|
|
|
"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 {
|
|
|
|
AnnounceInterval time.Duration `yaml:"announce_interval"`
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2016-08-10 02:08:15 +02:00
|
|
|
l := &Logic{
|
2016-09-01 03:09:46 +02:00
|
|
|
announceInterval: cfg.AnnounceInterval,
|
|
|
|
peerStore: peerStore,
|
|
|
|
preHooks: preHooks,
|
|
|
|
postHooks: postHooks,
|
2016-08-10 02:08:15 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 03:09:46 +02:00
|
|
|
if len(l.preHooks) == 0 {
|
|
|
|
l.preHooks = []Hook{nopHook{}}
|
2016-08-10 02:08:15 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 03:09:46 +02:00
|
|
|
if len(l.postHooks) == 0 {
|
|
|
|
l.postHooks = []Hook{nopHook{}}
|
2016-08-10 02:08:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (l *Logic) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest) (*bittorrent.AnnounceResponse, error) {
|
|
|
|
resp := &bittorrent.AnnounceResponse{
|
|
|
|
Interval: l.announceInterval,
|
|
|
|
}
|
2016-09-01 03:09:46 +02:00
|
|
|
for _, h := range l.preHooks {
|
2016-08-10 02:08:15 +02:00
|
|
|
if err := h.HandleAnnounce(ctx, req, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-01 03:09:46 +02:00
|
|
|
for _, h := range l.postHooks {
|
2016-08-10 02:08:15 +02:00
|
|
|
if err := h.HandleAnnounce(ctx, req, resp); err != nil {
|
2016-09-05 18:10:42 +02:00
|
|
|
log.Errorln("chihaya: post-announce hooks failed:", err.Error())
|
2016-08-10 02:08:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleScrape generates a response for a Scrape.
|
|
|
|
func (l *Logic) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest) (*bittorrent.ScrapeResponse, error) {
|
|
|
|
resp := &bittorrent.ScrapeResponse{
|
|
|
|
Files: make(map[bittorrent.InfoHash]bittorrent.Scrape),
|
|
|
|
}
|
2016-09-01 03:09:46 +02:00
|
|
|
for _, h := range l.preHooks {
|
2016-08-10 02:08:15 +02:00
|
|
|
if err := h.HandleScrape(ctx, req, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-01 03:09:46 +02:00
|
|
|
for _, h := range l.postHooks {
|
2016-08-10 02:08:15 +02:00
|
|
|
if err := h.HandleScrape(ctx, req, resp); err != nil {
|
2016-09-05 18:10:42 +02:00
|
|
|
log.Errorln("chihaya: post-scrape hooks failed:", err.Error())
|
2016-08-10 02:08:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|