Merge pull request #212 from mrd0ll4r/refactor-cotexts

middleware: make hooks return a context
This commit is contained in:
Jimmy Zelinskie 2016-09-05 18:58:54 -04:00 committed by GitHub
commit 2c2336901c
4 changed files with 25 additions and 41 deletions

View file

@ -55,25 +55,25 @@ func NewHook(cfg Config) (middleware.Hook, error) {
return h, nil return h, nil
} }
func (h *hook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) error { func (h *hook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) (context.Context, error) {
clientID := bittorrent.NewClientID(req.Peer.ID) clientID := bittorrent.NewClientID(req.Peer.ID)
if len(h.approved) > 0 { if len(h.approved) > 0 {
if _, found := h.approved[clientID]; !found { if _, found := h.approved[clientID]; !found {
return ErrClientUnapproved return ctx, ErrClientUnapproved
} }
} }
if len(h.unapproved) > 0 { if len(h.unapproved) > 0 {
if _, found := h.unapproved[clientID]; found { if _, found := h.unapproved[clientID]; found {
return ErrClientUnapproved return ctx, ErrClientUnapproved
} }
} }
return nil return ctx, nil
} }
func (h *hook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) error { func (h *hook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) (context.Context, error) {
// Scrapes don't require any protection. // Scrapes don't require any protection.
return nil return ctx, nil
} }

View file

@ -9,16 +9,6 @@ import (
// Hook abstracts the concept of anything that needs to interact with a // Hook abstracts the concept of anything that needs to interact with a
// BitTorrent client's request and response to a BitTorrent tracker. // BitTorrent client's request and response to a BitTorrent tracker.
type Hook interface { type Hook interface {
HandleAnnounce(context.Context, *bittorrent.AnnounceRequest, *bittorrent.AnnounceResponse) error HandleAnnounce(context.Context, *bittorrent.AnnounceRequest, *bittorrent.AnnounceResponse) (context.Context, error)
HandleScrape(context.Context, *bittorrent.ScrapeRequest, *bittorrent.ScrapeResponse) error HandleScrape(context.Context, *bittorrent.ScrapeRequest, *bittorrent.ScrapeResponse) (context.Context, error)
}
type nopHook struct{}
func (nopHook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) error {
return nil
}
func (nopHook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) error {
return nil
} }

View file

@ -98,26 +98,26 @@ func (h *hook) Stop() {
close(h.closing) close(h.closing)
} }
func (h *hook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) error { func (h *hook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) (context.Context, error) {
if req.Params == nil { if req.Params == nil {
return ErrMissingJWT return ctx, ErrMissingJWT
} }
jwtParam, ok := req.Params.String("jwt") jwtParam, ok := req.Params.String("jwt")
if !ok { if !ok {
return ErrMissingJWT return ctx, ErrMissingJWT
} }
if err := validateJWT(req.InfoHash, []byte(jwtParam), h.cfg.Issuer, h.cfg.Audience, h.publicKeys); err != nil { if err := validateJWT(req.InfoHash, []byte(jwtParam), h.cfg.Issuer, h.cfg.Audience, h.publicKeys); err != nil {
return ErrInvalidJWT return ctx, ErrInvalidJWT
} }
return nil return ctx, nil
} }
func (h *hook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) error { func (h *hook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) (context.Context, error) {
// Scrapes don't require any protection. // Scrapes don't require any protection.
return nil return ctx, nil
} }
func validateJWT(ih bittorrent.InfoHash, jwtBytes []byte, cfgIss, cfgAud string, publicKeys map[string]crypto.PublicKey) error { func validateJWT(ih bittorrent.InfoHash, jwtBytes []byte, cfgIss, cfgAud string, publicKeys map[string]crypto.PublicKey) error {

View file

@ -29,14 +29,6 @@ func NewLogic(cfg Config, peerStore storage.PeerStore, preHooks, postHooks []Hoo
postHooks: postHooks, postHooks: postHooks,
} }
if len(l.preHooks) == 0 {
l.preHooks = []Hook{nopHook{}}
}
if len(l.postHooks) == 0 {
l.postHooks = []Hook{nopHook{}}
}
return l return l
} }
@ -50,12 +42,12 @@ type Logic struct {
} }
// HandleAnnounce generates a response for an Announce. // HandleAnnounce generates a response for an Announce.
func (l *Logic) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest) (*bittorrent.AnnounceResponse, error) { func (l *Logic) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest) (resp *bittorrent.AnnounceResponse, err error) {
resp := &bittorrent.AnnounceResponse{ resp = &bittorrent.AnnounceResponse{
Interval: l.announceInterval, Interval: l.announceInterval,
} }
for _, h := range l.preHooks { for _, h := range l.preHooks {
if err := h.HandleAnnounce(ctx, req, resp); err != nil { if ctx, err = h.HandleAnnounce(ctx, req, resp); err != nil {
return nil, err return nil, err
} }
} }
@ -66,8 +58,9 @@ func (l *Logic) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequ
// AfterAnnounce does something with the results of an Announce after it has // AfterAnnounce does something with the results of an Announce after it has
// been completed. // been completed.
func (l *Logic) AfterAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) { func (l *Logic) AfterAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) {
var err error
for _, h := range l.postHooks { for _, h := range l.postHooks {
if err := h.HandleAnnounce(ctx, req, resp); err != nil { if ctx, err = h.HandleAnnounce(ctx, req, resp); err != nil {
log.Errorln("chihaya: post-announce hooks failed:", err.Error()) log.Errorln("chihaya: post-announce hooks failed:", err.Error())
return return
} }
@ -75,12 +68,12 @@ func (l *Logic) AfterAnnounce(ctx context.Context, req *bittorrent.AnnounceReque
} }
// HandleScrape generates a response for a Scrape. // HandleScrape generates a response for a Scrape.
func (l *Logic) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest) (*bittorrent.ScrapeResponse, error) { func (l *Logic) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest) (resp *bittorrent.ScrapeResponse, err error) {
resp := &bittorrent.ScrapeResponse{ resp = &bittorrent.ScrapeResponse{
Files: make(map[bittorrent.InfoHash]bittorrent.Scrape), Files: make(map[bittorrent.InfoHash]bittorrent.Scrape),
} }
for _, h := range l.preHooks { for _, h := range l.preHooks {
if err := h.HandleScrape(ctx, req, resp); err != nil { if ctx, err = h.HandleScrape(ctx, req, resp); err != nil {
return nil, err return nil, err
} }
} }
@ -91,8 +84,9 @@ func (l *Logic) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest)
// AfterScrape does something with the results of a Scrape after it has been // AfterScrape does something with the results of a Scrape after it has been
// completed. // completed.
func (l *Logic) AfterScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) { func (l *Logic) AfterScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) {
var err error
for _, h := range l.postHooks { for _, h := range l.postHooks {
if err := h.HandleScrape(ctx, req, resp); err != nil { if ctx, err = h.HandleScrape(ctx, req, resp); err != nil {
log.Errorln("chihaya: post-scrape hooks failed:", err.Error()) log.Errorln("chihaya: post-scrape hooks failed:", err.Error())
return return
} }