diff --git a/middleware/clientapproval/clientapproval.go b/middleware/clientapproval/clientapproval.go index 22fec00..e09cd62 100644 --- a/middleware/clientapproval/clientapproval.go +++ b/middleware/clientapproval/clientapproval.go @@ -55,25 +55,25 @@ func NewHook(cfg Config) (middleware.Hook, error) { 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) if len(h.approved) > 0 { if _, found := h.approved[clientID]; !found { - return ErrClientUnapproved + return ctx, ErrClientUnapproved } } if len(h.unapproved) > 0 { 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. - return nil + return ctx, nil } diff --git a/middleware/hooks.go b/middleware/hooks.go index 7a6c5ae..0cfbeac 100644 --- a/middleware/hooks.go +++ b/middleware/hooks.go @@ -9,16 +9,6 @@ import ( // 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(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 + HandleAnnounce(context.Context, *bittorrent.AnnounceRequest, *bittorrent.AnnounceResponse) (context.Context, error) + HandleScrape(context.Context, *bittorrent.ScrapeRequest, *bittorrent.ScrapeResponse) (context.Context, error) } diff --git a/middleware/jwt/jwt.go b/middleware/jwt/jwt.go index 8b9c937..7fcfe3d 100644 --- a/middleware/jwt/jwt.go +++ b/middleware/jwt/jwt.go @@ -98,26 +98,26 @@ func (h *hook) Stop() { 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 { - return ErrMissingJWT + return ctx, ErrMissingJWT } jwtParam, ok := req.Params.String("jwt") if !ok { - return ErrMissingJWT + return ctx, ErrMissingJWT } 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. - return nil + return ctx, nil } func validateJWT(ih bittorrent.InfoHash, jwtBytes []byte, cfgIss, cfgAud string, publicKeys map[string]crypto.PublicKey) error { diff --git a/middleware/middleware.go b/middleware/middleware.go index b6be862..56cff22 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -29,14 +29,6 @@ func NewLogic(cfg Config, peerStore storage.PeerStore, preHooks, postHooks []Hoo postHooks: postHooks, } - if len(l.preHooks) == 0 { - l.preHooks = []Hook{nopHook{}} - } - - if len(l.postHooks) == 0 { - l.postHooks = []Hook{nopHook{}} - } - return l } @@ -50,12 +42,12 @@ type Logic struct { } // HandleAnnounce generates a response for an Announce. -func (l *Logic) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest) (*bittorrent.AnnounceResponse, error) { - resp := &bittorrent.AnnounceResponse{ +func (l *Logic) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest) (resp *bittorrent.AnnounceResponse, err error) { + resp = &bittorrent.AnnounceResponse{ Interval: l.announceInterval, } 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 } } @@ -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 // been completed. func (l *Logic) AfterAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) { + var err error 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()) return } @@ -75,12 +68,12 @@ func (l *Logic) AfterAnnounce(ctx context.Context, req *bittorrent.AnnounceReque } // HandleScrape generates a response for a Scrape. -func (l *Logic) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest) (*bittorrent.ScrapeResponse, error) { - resp := &bittorrent.ScrapeResponse{ +func (l *Logic) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest) (resp *bittorrent.ScrapeResponse, err error) { + resp = &bittorrent.ScrapeResponse{ Files: make(map[bittorrent.InfoHash]bittorrent.Scrape), } 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 } } @@ -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 // completed. func (l *Logic) AfterScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) { + var err error 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()) return }