middleware: remove sanitization mw
This commit is contained in:
parent
47b5e67345
commit
134744a484
3 changed files with 3 additions and 74 deletions
|
@ -2,8 +2,6 @@ package middleware
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
|
||||
"github.com/chihaya/chihaya/bittorrent"
|
||||
"github.com/chihaya/chihaya/storage"
|
||||
|
@ -67,56 +65,6 @@ func (h *swarmInteractionHook) HandleScrape(ctx context.Context, _ *bittorrent.S
|
|||
return ctx, nil
|
||||
}
|
||||
|
||||
// ErrInvalidIP indicates an invalid IP for an Announce.
|
||||
var ErrInvalidIP = errors.New("invalid IP")
|
||||
|
||||
// sanitizationHook enforces semantic assumptions about requests that may have
|
||||
// not been accounted for in a tracker frontend.
|
||||
//
|
||||
// The SanitizationHook performs the following checks:
|
||||
// - maxNumWant: Checks whether the numWant parameter of an announce is below
|
||||
// a limit. Sets it to the limit if the value is higher.
|
||||
// - defaultNumWant: Checks whether the numWant parameter of an announce is
|
||||
// zero. Sets it to the default if it is.
|
||||
// - IP sanitization: Checks whether the announcing Peer's IP address is either
|
||||
// IPv4 or IPv6. Returns ErrInvalidIP if the address is neither IPv4 nor
|
||||
// IPv6. Sets the Peer.AddressFamily field accordingly. Truncates IPv4
|
||||
// addresses to have a length of 4 bytes.
|
||||
type sanitizationHook struct {
|
||||
maxNumWant uint32
|
||||
defaultNumWant uint32
|
||||
maxScrapeInfoHashes uint32
|
||||
}
|
||||
|
||||
func (h *sanitizationHook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) (context.Context, error) {
|
||||
if req.NumWant > h.maxNumWant {
|
||||
req.NumWant = h.maxNumWant
|
||||
}
|
||||
|
||||
if req.NumWant == 0 {
|
||||
req.NumWant = h.defaultNumWant
|
||||
}
|
||||
|
||||
if ip := req.Peer.IP.To4(); ip != nil {
|
||||
req.Peer.IP.IP = ip
|
||||
req.Peer.IP.AddressFamily = bittorrent.IPv4
|
||||
} else if len(req.Peer.IP.IP) == net.IPv6len { // implies req.Peer.IP.To4() == nil
|
||||
req.Peer.IP.AddressFamily = bittorrent.IPv6
|
||||
} else {
|
||||
return ctx, ErrInvalidIP
|
||||
}
|
||||
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func (h *sanitizationHook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) (context.Context, error) {
|
||||
if len(req.InfoHashes) > int(h.maxScrapeInfoHashes) {
|
||||
req.InfoHashes = req.InfoHashes[:h.maxScrapeInfoHashes]
|
||||
}
|
||||
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
type skipResponseHook struct{}
|
||||
|
||||
// SkipResponseHookKey is a key for the context of an Announce or Scrape to
|
||||
|
|
|
@ -15,10 +15,7 @@ import (
|
|||
|
||||
// Config holds the configuration common across all middleware.
|
||||
type Config struct {
|
||||
AnnounceInterval time.Duration `yaml:"announce_interval"`
|
||||
MaxNumWant uint32 `yaml:"max_numwant"`
|
||||
DefaultNumWant uint32 `yaml:"default_numwant"`
|
||||
MaxScrapeInfoHashes uint32 `yaml:"max_scrape_infohashes"`
|
||||
AnnounceInterval time.Duration `yaml:"announce_interval"`
|
||||
}
|
||||
|
||||
var _ frontend.TrackerLogic = &Logic{}
|
||||
|
@ -26,17 +23,12 @@ var _ frontend.TrackerLogic = &Logic{}
|
|||
// NewLogic creates a new instance of a TrackerLogic that executes the provided
|
||||
// middleware hooks.
|
||||
func NewLogic(cfg Config, peerStore storage.PeerStore, preHooks, postHooks []Hook) *Logic {
|
||||
l := &Logic{
|
||||
return &Logic{
|
||||
announceInterval: cfg.AnnounceInterval,
|
||||
peerStore: peerStore,
|
||||
preHooks: []Hook{&sanitizationHook{cfg.MaxNumWant, cfg.DefaultNumWant, cfg.MaxScrapeInfoHashes}},
|
||||
preHooks: append(preHooks, &responseHook{store: peerStore}),
|
||||
postHooks: append(postHooks, &swarmInteractionHook{store: peerStore}),
|
||||
}
|
||||
|
||||
l.preHooks = append(l.preHooks, preHooks...)
|
||||
l.preHooks = append(l.preHooks, &responseHook{store: peerStore})
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
// Logic is an implementation of the TrackerLogic that functions by
|
||||
|
|
|
@ -80,15 +80,4 @@ func BenchmarkHookOverhead(b *testing.B) {
|
|||
benchHookListV6(b, nopHooks)
|
||||
})
|
||||
}
|
||||
|
||||
var sanHooks hookList
|
||||
for i := 1; i < 4; i++ {
|
||||
sanHooks = append(sanHooks, &sanitizationHook{maxNumWant: 50})
|
||||
b.Run(fmt.Sprintf("%dsanitation-v4", i), func(b *testing.B) {
|
||||
benchHookListV4(b, sanHooks)
|
||||
})
|
||||
b.Run(fmt.Sprintf("%dsanitation-v6", i), func(b *testing.B) {
|
||||
benchHookListV6(b, sanHooks)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue