2016-08-17 03:01:58 +02:00
|
|
|
// Package clientwhitelist implements a Hook that fails an Announce if the
|
|
|
|
// client's PeerID does not begin with any of the approved prefixes.
|
|
|
|
package clientwhitelist
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/chihaya/chihaya/bittorrent"
|
|
|
|
"github.com/chihaya/chihaya/middleware"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ClientUnapproved is the error returned when a client's PeerID fails to
|
|
|
|
// begin with an approved prefix.
|
|
|
|
var ClientUnapproved = bittorrent.ClientError("unapproved client")
|
|
|
|
|
2016-08-17 04:37:29 +02:00
|
|
|
type hook struct {
|
2016-08-17 03:01:58 +02:00
|
|
|
approved map[bittorrent.ClientID]struct{}
|
|
|
|
}
|
|
|
|
|
2016-08-17 04:37:29 +02:00
|
|
|
func NewHook(approved []string) middleware.Hook {
|
2016-08-17 03:01:58 +02:00
|
|
|
h := &hook{
|
|
|
|
approved: make(map[bittorrent.ClientID]struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, clientID := range approved {
|
|
|
|
h.approved[bittorrent.NewClientID(clientID)] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2016-08-17 04:37:29 +02:00
|
|
|
func (h *hook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) error {
|
2016-08-17 03:01:58 +02:00
|
|
|
if _, found := h.approved[bittorrent.NewClientID(req.Peer.ID)]; !found {
|
|
|
|
return ClientUnapproved
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-17 04:37:29 +02:00
|
|
|
func (h *hook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) error {
|
2016-08-17 03:01:58 +02:00
|
|
|
return nil
|
|
|
|
}
|