2016-09-02 02:30:53 +02:00
|
|
|
// Package clientapproval implements a Hook that fails an Announce based on a
|
|
|
|
// whitelist or blacklist of BitTorrent client IDs.
|
|
|
|
package clientapproval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2017-12-23 20:54:51 +01:00
|
|
|
"fmt"
|
|
|
|
|
2018-12-17 09:59:44 +01:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2016-09-02 02:30:53 +02:00
|
|
|
|
|
|
|
"github.com/chihaya/chihaya/bittorrent"
|
|
|
|
"github.com/chihaya/chihaya/middleware"
|
|
|
|
)
|
|
|
|
|
2017-12-23 20:54:51 +01:00
|
|
|
// Name is the name by which this middleware is registered with Chihaya.
|
|
|
|
const Name = "client approval"
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
middleware.RegisterDriver(Name, driver{})
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ middleware.Driver = driver{}
|
|
|
|
|
|
|
|
type driver struct{}
|
|
|
|
|
|
|
|
func (d driver) NewHook(optionBytes []byte) (middleware.Hook, error) {
|
|
|
|
var cfg Config
|
|
|
|
err := yaml.Unmarshal(optionBytes, &cfg)
|
|
|
|
if err != nil {
|
2022-01-16 05:28:52 +01:00
|
|
|
return nil, fmt.Errorf("invalid options for middleware %s: %w", Name, err)
|
2017-12-23 20:54:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return NewHook(cfg)
|
|
|
|
}
|
|
|
|
|
2016-09-02 02:30:53 +02:00
|
|
|
// ErrClientUnapproved is the error returned when a client's PeerID is invalid.
|
|
|
|
var ErrClientUnapproved = bittorrent.ClientError("unapproved client")
|
|
|
|
|
|
|
|
// Config represents all the values required by this middleware to validate
|
|
|
|
// peers based on their BitTorrent client ID.
|
|
|
|
type Config struct {
|
|
|
|
Whitelist []string `yaml:"whitelist"`
|
|
|
|
Blacklist []string `yaml:"blacklist"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type hook struct {
|
|
|
|
approved map[bittorrent.ClientID]struct{}
|
|
|
|
unapproved map[bittorrent.ClientID]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHook returns an instance of the client approval middleware.
|
|
|
|
func NewHook(cfg Config) (middleware.Hook, error) {
|
|
|
|
h := &hook{
|
|
|
|
approved: make(map[bittorrent.ClientID]struct{}),
|
|
|
|
unapproved: make(map[bittorrent.ClientID]struct{}),
|
|
|
|
}
|
|
|
|
|
2018-09-20 10:12:35 +02:00
|
|
|
if len(cfg.Whitelist) > 0 && len(cfg.Blacklist) > 0 {
|
|
|
|
return nil, fmt.Errorf("using both whitelist and blacklist is invalid")
|
|
|
|
}
|
|
|
|
|
2016-09-02 02:30:53 +02:00
|
|
|
for _, cidString := range cfg.Whitelist {
|
|
|
|
cidBytes := []byte(cidString)
|
|
|
|
if len(cidBytes) != 6 {
|
|
|
|
return nil, errors.New("client ID " + cidString + " must be 6 bytes")
|
|
|
|
}
|
|
|
|
var cid bittorrent.ClientID
|
|
|
|
copy(cid[:], cidBytes)
|
|
|
|
h.approved[cid] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cidString := range cfg.Blacklist {
|
|
|
|
cidBytes := []byte(cidString)
|
|
|
|
if len(cidBytes) != 6 {
|
|
|
|
return nil, errors.New("client ID " + cidString + " must be 6 bytes")
|
|
|
|
}
|
|
|
|
var cid bittorrent.ClientID
|
|
|
|
copy(cid[:], cidBytes)
|
|
|
|
h.unapproved[cid] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
2016-09-06 00:23:10 +02:00
|
|
|
func (h *hook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) (context.Context, error) {
|
2016-09-02 02:30:53 +02:00
|
|
|
clientID := bittorrent.NewClientID(req.Peer.ID)
|
|
|
|
|
|
|
|
if len(h.approved) > 0 {
|
|
|
|
if _, found := h.approved[clientID]; !found {
|
2016-09-06 00:23:10 +02:00
|
|
|
return ctx, ErrClientUnapproved
|
2016-09-02 02:30:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(h.unapproved) > 0 {
|
|
|
|
if _, found := h.unapproved[clientID]; found {
|
2016-09-06 00:23:10 +02:00
|
|
|
return ctx, ErrClientUnapproved
|
2016-09-02 02:30:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-06 00:23:10 +02:00
|
|
|
return ctx, nil
|
2016-09-02 02:30:53 +02:00
|
|
|
}
|
|
|
|
|
2016-09-06 00:23:10 +02:00
|
|
|
func (h *hook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) (context.Context, error) {
|
2016-09-02 02:30:53 +02:00
|
|
|
// Scrapes don't require any protection.
|
2016-09-06 00:23:10 +02:00
|
|
|
return ctx, nil
|
2016-09-02 02:30:53 +02:00
|
|
|
}
|