middleware/infohash: move prefix to within package

Let middlewares own their prefixes, makes it simpler
to manage for us.
This commit is contained in:
Josh de Kock 2016-04-06 02:36:00 +01:00
parent d139335111
commit d4099a5abf
5 changed files with 14 additions and 14 deletions

View file

@ -7,11 +7,11 @@ It also provides the configurable scrape middleware `infohash_blacklist` and `in
#### For Announces #### For Announces
The `infohash_blacklist` middleware uses all infohashes stored in the `StringStore` with the `store.PrefixInfohash` prefix to blacklist, i.e. block announces. The `infohash_blacklist` middleware uses all infohashes stored in the `StringStore` with the `PrefixInfohash` prefix to blacklist, i.e. block announces.
#### For Scrapes #### For Scrapes
The configurable `infohash_blacklist` middleware uses all infohashes stored in the `StringStore` with the `store.PrefixInfohash` prefix to blacklist scrape requests. The configurable `infohash_blacklist` middleware uses all infohashes stored in the `StringStore` with the `PrefixInfohash` prefix to blacklist scrape requests.
The scrape middleware has two modes of operation: _Block_ and _Filter_. The scrape middleware has two modes of operation: _Block_ and _Filter_.
@ -25,11 +25,11 @@ See the configuration section for information about how to configure the scrape
#### For Announces #### For Announces
The `infohash_blacklist` middleware uses all infohashes stored in the `StringStore` with the `store.PrefixInfohash` prefix to whitelist, i.e. allow announces. The `infohash_blacklist` middleware uses all infohashes stored in the `StringStore` with the `PrefixInfohash` prefix to whitelist, i.e. allow announces.
#### For Scrapes #### For Scrapes
The configurable `infohash_blacklist` middleware uses all infohashes stored in the `StringStore` with the `store.PrefixInfohash` prefix to whitelist scrape requests. The configurable `infohash_blacklist` middleware uses all infohashes stored in the `StringStore` with the `PrefixInfohash` prefix to whitelist scrape requests.
The scrape middleware has two modes of operation: _Block_ and _Filter_. The scrape middleware has two modes of operation: _Block_ and _Filter_.

View file

@ -23,7 +23,7 @@ var ErrBlockedInfohash = tracker.ClientError("disallowed infohash")
// for infohashes that are not stored in a StringStore. // for infohashes that are not stored in a StringStore.
func blacklistAnnounceInfohash(next tracker.AnnounceHandler) tracker.AnnounceHandler { func blacklistAnnounceInfohash(next tracker.AnnounceHandler) tracker.AnnounceHandler {
return func(cfg *chihaya.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) (err error) { return func(cfg *chihaya.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) (err error) {
blacklisted, err := store.MustGetStore().HasString(store.PrefixInfohash + string(req.InfoHash)) blacklisted, err := store.MustGetStore().HasString(PrefixInfohash + string(req.InfoHash))
if err != nil { if err != nil {
return err return err
} else if blacklisted { } else if blacklisted {
@ -67,7 +67,7 @@ func blacklistFilterScrape(next tracker.ScrapeHandler) tracker.ScrapeHandler {
infohashes := req.InfoHashes infohashes := req.InfoHashes
for i, ih := range infohashes { for i, ih := range infohashes {
blacklisted, err = storage.HasString(store.PrefixInfohash + string(ih)) blacklisted, err = storage.HasString(PrefixInfohash + string(ih))
if err != nil { if err != nil {
return err return err
@ -87,7 +87,7 @@ func blacklistBlockScrape(next tracker.ScrapeHandler) tracker.ScrapeHandler {
storage := store.MustGetStore() storage := store.MustGetStore()
for _, ih := range req.InfoHashes { for _, ih := range req.InfoHashes {
blacklisted, err = storage.HasString(store.PrefixInfohash + string(ih)) blacklisted, err = storage.HasString(PrefixInfohash + string(ih))
if err != nil { if err != nil {
return err return err

View file

@ -41,7 +41,7 @@ func TestASetUp(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
srv.Start() srv.Start()
store.MustGetStore().PutString(store.PrefixInfohash + "abc") store.MustGetStore().PutString(PrefixInfohash + "abc")
} }
func TestBlacklistAnnounceMiddleware(t *testing.T) { func TestBlacklistAnnounceMiddleware(t *testing.T) {

View file

@ -15,11 +15,14 @@ func init() {
tracker.RegisterScrapeMiddlewareConstructor("infohash_whitelist", whitelistScrapeInfohash) tracker.RegisterScrapeMiddlewareConstructor("infohash_whitelist", whitelistScrapeInfohash)
} }
// PrefixInfohash is the prefix to be used for infohashes.
const PrefixInfohash = "ih-"
// whitelistAnnounceInfohash provides a middleware that only allows announces // whitelistAnnounceInfohash provides a middleware that only allows announces
// for infohashes that are not stored in a StringStore // for infohashes that are not stored in a StringStore
func whitelistAnnounceInfohash(next tracker.AnnounceHandler) tracker.AnnounceHandler { func whitelistAnnounceInfohash(next tracker.AnnounceHandler) tracker.AnnounceHandler {
return func(cfg *chihaya.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) (err error) { return func(cfg *chihaya.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) (err error) {
whitelisted, err := store.MustGetStore().HasString(store.PrefixInfohash + string(req.InfoHash)) whitelisted, err := store.MustGetStore().HasString(PrefixInfohash + string(req.InfoHash))
if err != nil { if err != nil {
return err return err
@ -63,7 +66,7 @@ func whitelistFilterScrape(next tracker.ScrapeHandler) tracker.ScrapeHandler {
infohashes := req.InfoHashes infohashes := req.InfoHashes
for i, ih := range infohashes { for i, ih := range infohashes {
whitelisted, err = storage.HasString(store.PrefixInfohash + string(ih)) whitelisted, err = storage.HasString(PrefixInfohash + string(ih))
if err != nil { if err != nil {
return err return err
@ -83,7 +86,7 @@ func whitelistBlockScrape(next tracker.ScrapeHandler) tracker.ScrapeHandler {
storage := store.MustGetStore() storage := store.MustGetStore()
for _, ih := range req.InfoHashes { for _, ih := range req.InfoHashes {
whitelisted, err = storage.HasString(store.PrefixInfohash + string(ih)) whitelisted, err = storage.HasString(PrefixInfohash + string(ih))
if err != nil { if err != nil {
return err return err

View file

@ -6,9 +6,6 @@ package store
import "fmt" import "fmt"
// PrefixInfohash is the prefix to be used for infohashes.
const PrefixInfohash = "ih-"
var stringStoreDrivers = make(map[string]StringStoreDriver) var stringStoreDrivers = make(map[string]StringStoreDriver)
// StringStore represents an interface for manipulating strings. // StringStore represents an interface for manipulating strings.