mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-23 16:28:11 -07:00
s/backend/middleware
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/jzelinskie/trakr/bittorrent"
|
||||
)
|
||||
|
||||
// Hook abstracts the concept of anything that needs to interact with a
|
||||
// BitTorrent client's request and response to a BitTorrent tracker.
|
||||
type Hook interface {
|
||||
HandleAnnounce(context.Context, *bittorrent.AnnounceRequest, *bittorrent.AnnounceResponse) error
|
||||
HandleScrape(context.Context, *bittorrent.ScrapeRequest, *bittorrent.ScrapeResponse) error
|
||||
}
|
||||
|
||||
type nopHook struct{}
|
||||
|
||||
func (nopHook) HandleAnnounce(context.Context, *bittorrent.AnnounceRequest, *bittorrent.AnnounceResponse) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (nopHook) HandleScrape(context.Context, *bittorrent.ScrapeRequest, *bittorrent.ScrapeResponse) error {
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
// Package middleware implements the TrackerLogic interface by executing
|
||||
// a series of middleware hooks.
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/jzelinskie/trakr/bittorrent"
|
||||
"github.com/jzelinskie/trakr/frontend"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
AnnounceInterval time.Duration `yaml:"announce_interval"`
|
||||
}
|
||||
|
||||
var _ frontend.TrackerLogic = &Logic{}
|
||||
|
||||
func NewLogic(config Config, peerStore PeerStore, announcePreHooks, announcePostHooks, scrapePreHooks, scrapePostHooks []Hook) *Logic {
|
||||
l := &Logic{
|
||||
announceInterval: config.AnnounceInterval,
|
||||
peerStore: peerStore,
|
||||
announcePreHooks: announcePreHooks,
|
||||
announcePostHooks: announcePostHooks,
|
||||
scrapePreHooks: scrapePreHooks,
|
||||
scrapePostHooks: scrapePostHooks,
|
||||
}
|
||||
|
||||
if len(l.announcePreHooks) == 0 {
|
||||
l.announcePreHooks = []Hook{nopHook{}}
|
||||
}
|
||||
|
||||
if len(l.announcePostHooks) == 0 {
|
||||
l.announcePostHooks = []Hook{nopHook{}}
|
||||
}
|
||||
|
||||
if len(l.scrapePreHooks) == 0 {
|
||||
l.scrapePreHooks = []Hook{nopHook{}}
|
||||
}
|
||||
|
||||
if len(l.scrapePostHooks) == 0 {
|
||||
l.scrapePostHooks = []Hook{nopHook{}}
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
// Logic is an implementation of the TrackerLogic that functions by
|
||||
// executing a series of middleware hooks.
|
||||
type Logic struct {
|
||||
announceInterval time.Duration
|
||||
peerStore PeerStore
|
||||
announcePreHooks []Hook
|
||||
announcePostHooks []Hook
|
||||
scrapePreHooks []Hook
|
||||
scrapePostHooks []Hook
|
||||
}
|
||||
|
||||
// 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,
|
||||
}
|
||||
for _, h := range l.announcePreHooks {
|
||||
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) {
|
||||
for _, h := range l.announcePostHooks {
|
||||
if err := h.HandleAnnounce(ctx, req, resp); err != nil {
|
||||
log.Println("trakr: post-announce hooks failed:", err.Error())
|
||||
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),
|
||||
}
|
||||
for _, h := range l.scrapePreHooks {
|
||||
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) {
|
||||
for _, h := range l.scrapePostHooks {
|
||||
if err := h.HandleScrape(ctx, req, resp); err != nil {
|
||||
log.Println("trakr: post-scrape hooks failed:", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/jzelinskie/trakr/bittorrent"
|
||||
"github.com/jzelinskie/trakr/stopper"
|
||||
)
|
||||
|
||||
// ErrResourceDoesNotExist is the error returned by all delete methods in the
|
||||
// store if the requested resource does not exist.
|
||||
var ErrResourceDoesNotExist = bittorrent.ClientError("resource does not exist")
|
||||
|
||||
// PeerStore is an interface that abstracts the interactions of storing and
|
||||
// manipulating Peers such that it can be implemented for various data stores.
|
||||
type PeerStore interface {
|
||||
// PutSeeder adds a Seeder to the Swarm identified by the provided infoHash.
|
||||
PutSeeder(infoHash bittorrent.InfoHash, p bittorrent.Peer) error
|
||||
|
||||
// DeleteSeeder removes a Seeder from the Swarm identified by the provided
|
||||
// infoHash.
|
||||
//
|
||||
// If the Swarm or Peer does not exist, this function should return
|
||||
// ErrResourceDoesNotExist.
|
||||
DeleteSeeder(infoHash bittorrent.InfoHash, p bittorrent.Peer) error
|
||||
|
||||
// PutLeecher adds a Leecher to the Swarm identified by the provided
|
||||
// infoHash.
|
||||
PutLeecher(infoHash bittorrent.InfoHash, p bittorrent.Peer) error
|
||||
|
||||
// DeleteLeecher removes a Leecher from the Swarm identified by the provided
|
||||
// infoHash.
|
||||
//
|
||||
// If the Swarm or Peer does not exist, this function should return
|
||||
// ErrResourceDoesNotExist.
|
||||
DeleteLeecher(infoHash bittorrent.InfoHash, p bittorrent.Peer) error
|
||||
|
||||
// GraduateLeecher promotes a Leecher to a Seeder in the Swarm identified by
|
||||
// the provided infoHash.
|
||||
//
|
||||
// If the given Peer is not present as a Leecher, add the Peer as a Seeder
|
||||
// and return no error.
|
||||
GraduateLeecher(infoHash bittorrent.InfoHash, p bittorrent.Peer) error
|
||||
|
||||
// AnnouncePeers is a best effort attempt to return Peers from the Swarm
|
||||
// identified by the provided infoHash. The returned Peers are required to be
|
||||
// either all IPv4 or all IPv6.
|
||||
//
|
||||
// The returned Peers should strive be:
|
||||
// - as close to length equal to numWant as possible without going over
|
||||
// - all IPv4 or all IPv6 depending on the provided ipv6 boolean
|
||||
// - if seeder is true, should ideally return more leechers than seeders
|
||||
// - if seeder is false, should ideally return more seeders than leechers
|
||||
AnnouncePeers(infoHash bittorrent.InfoHash, seeder bool, numWant int, ipv6 bool) (peers []bittorrent.Peer, err error)
|
||||
|
||||
// CollectGarbage deletes all Peers from the PeerStore which are older than
|
||||
// the cutoff time. This function must be able to execute while other methods
|
||||
// on this interface are being executed in parallel.
|
||||
CollectGarbage(cutoff time.Time) error
|
||||
|
||||
// Stopper is an interface that expects a Stop method to stops the PeerStore.
|
||||
// For more details see the documentation in the stopper package.
|
||||
stopper.Stopper
|
||||
}
|
||||
Reference in New Issue
Block a user