mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-26 17:38:14 -07:00
make infohash and peerID byte arrays (#169)
This commit is contained in:
committed by
Jimmy Zelinskie
parent
a081e5195b
commit
7f16c55d81
@@ -23,7 +23,7 @@ var ErrBlacklistedClient = tracker.ClientError("client blacklisted")
|
||||
// announce that are not stored in the StringStore.
|
||||
func blacklistAnnounceClient(next tracker.AnnounceHandler) tracker.AnnounceHandler {
|
||||
return func(cfg *chihaya.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) error {
|
||||
blacklisted, err := store.MustGetStore().HasString(PrefixClient + clientid.New(string(req.PeerID)))
|
||||
blacklisted, err := store.MustGetStore().HasString(PrefixClient + clientid.New(string(req.PeerID[:])))
|
||||
if err != nil {
|
||||
return err
|
||||
} else if blacklisted {
|
||||
|
||||
@@ -26,7 +26,7 @@ var ErrNotWhitelistedClient = tracker.ClientError("client not whitelisted")
|
||||
// announce that are stored in the StringStore.
|
||||
func whitelistAnnounceClient(next tracker.AnnounceHandler) tracker.AnnounceHandler {
|
||||
return func(cfg *chihaya.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) error {
|
||||
whitelisted, err := store.MustGetStore().HasString(PrefixClient + clientid.New(string(req.PeerID)))
|
||||
whitelisted, err := store.MustGetStore().HasString(PrefixClient + clientid.New(string(req.PeerID[:])))
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !whitelisted {
|
||||
|
||||
@@ -28,7 +28,7 @@ var mustGetStore func() store.StringStore
|
||||
// for infohashes that are not stored in a StringStore.
|
||||
func blacklistAnnounceInfohash(next tracker.AnnounceHandler) tracker.AnnounceHandler {
|
||||
return func(cfg *chihaya.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) (err error) {
|
||||
blacklisted, err := mustGetStore().HasString(PrefixInfohash + string(req.InfoHash))
|
||||
blacklisted, err := mustGetStore().HasString(PrefixInfohash + string(req.InfoHash[:]))
|
||||
if err != nil {
|
||||
return err
|
||||
} else if blacklisted {
|
||||
@@ -72,7 +72,7 @@ func blacklistFilterScrape(next tracker.ScrapeHandler) tracker.ScrapeHandler {
|
||||
infohashes := req.InfoHashes
|
||||
|
||||
for i, ih := range infohashes {
|
||||
blacklisted, err = storage.HasString(PrefixInfohash + string(ih))
|
||||
blacklisted, err = storage.HasString(PrefixInfohash + string(ih[:]))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -92,7 +92,7 @@ func blacklistBlockScrape(next tracker.ScrapeHandler) tracker.ScrapeHandler {
|
||||
storage := mustGetStore()
|
||||
|
||||
for _, ih := range req.InfoHashes {
|
||||
blacklisted, err = storage.HasString(PrefixInfohash + string(ih))
|
||||
blacklisted, err = storage.HasString(PrefixInfohash + string(ih[:]))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -40,12 +40,17 @@ var mock store.StringStore = &storeMock{
|
||||
strings: make(map[string]struct{}),
|
||||
}
|
||||
|
||||
var (
|
||||
ih1 = chihaya.InfoHash([20]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
|
||||
ih2 = chihaya.InfoHash([20]byte{2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
|
||||
)
|
||||
|
||||
func TestASetUp(t *testing.T) {
|
||||
mustGetStore = func() store.StringStore {
|
||||
return mock
|
||||
}
|
||||
|
||||
mustGetStore().PutString(PrefixInfohash + "abc")
|
||||
mustGetStore().PutString(PrefixInfohash + string(ih1[:]))
|
||||
}
|
||||
|
||||
func TestBlacklistAnnounceMiddleware(t *testing.T) {
|
||||
@@ -61,11 +66,11 @@ func TestBlacklistAnnounceMiddleware(t *testing.T) {
|
||||
err := handler(nil, &req, &resp)
|
||||
assert.Nil(t, err)
|
||||
|
||||
req.InfoHash = chihaya.InfoHash("abc")
|
||||
req.InfoHash = chihaya.InfoHash(ih1)
|
||||
err = handler(nil, &req, &resp)
|
||||
assert.Equal(t, ErrBlockedInfohash, err)
|
||||
|
||||
req.InfoHash = chihaya.InfoHash("def")
|
||||
req.InfoHash = chihaya.InfoHash(ih2)
|
||||
err = handler(nil, &req, &resp)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
@@ -90,11 +95,11 @@ func TestBlacklistScrapeMiddlewareBlock(t *testing.T) {
|
||||
err = handler(nil, &req, &resp)
|
||||
assert.Nil(t, err)
|
||||
|
||||
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash("abc"), chihaya.InfoHash("def")}
|
||||
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash(ih1), chihaya.InfoHash(ih2)}
|
||||
err = handler(nil, &req, &resp)
|
||||
assert.Equal(t, ErrBlockedInfohash, err)
|
||||
|
||||
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash("def")}
|
||||
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash(ih2)}
|
||||
err = handler(nil, &req, &resp)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
@@ -119,12 +124,12 @@ func TestBlacklistScrapeMiddlewareFilter(t *testing.T) {
|
||||
err = handler(nil, &req, &resp)
|
||||
assert.Nil(t, err)
|
||||
|
||||
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash("abc"), chihaya.InfoHash("def")}
|
||||
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash(ih1), chihaya.InfoHash(ih2)}
|
||||
err = handler(nil, &req, &resp)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []chihaya.InfoHash{chihaya.InfoHash("def")}, req.InfoHashes)
|
||||
assert.Equal(t, []chihaya.InfoHash{chihaya.InfoHash(ih2)}, req.InfoHashes)
|
||||
|
||||
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash("def")}
|
||||
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash(ih2)}
|
||||
err = handler(nil, &req, &resp)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ const PrefixInfohash = "ih-"
|
||||
// for infohashes that are not stored in a StringStore
|
||||
func whitelistAnnounceInfohash(next tracker.AnnounceHandler) tracker.AnnounceHandler {
|
||||
return func(cfg *chihaya.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) (err error) {
|
||||
whitelisted, err := mustGetStore().HasString(PrefixInfohash + string(req.InfoHash))
|
||||
whitelisted, err := mustGetStore().HasString(PrefixInfohash + string(req.InfoHash[:]))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -65,7 +65,7 @@ func whitelistFilterScrape(next tracker.ScrapeHandler) tracker.ScrapeHandler {
|
||||
infohashes := req.InfoHashes
|
||||
|
||||
for i, ih := range infohashes {
|
||||
whitelisted, err = storage.HasString(PrefixInfohash + string(ih))
|
||||
whitelisted, err = storage.HasString(PrefixInfohash + string(ih[:]))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -85,7 +85,7 @@ func whitelistBlockScrape(next tracker.ScrapeHandler) tracker.ScrapeHandler {
|
||||
storage := mustGetStore()
|
||||
|
||||
for _, ih := range req.InfoHashes {
|
||||
whitelisted, err = storage.HasString(PrefixInfohash + string(ih))
|
||||
whitelisted, err = storage.HasString(PrefixInfohash + string(ih[:]))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -26,11 +26,11 @@ func TestWhitelistAnnounceMiddleware(t *testing.T) {
|
||||
err := handler(nil, &req, &resp)
|
||||
assert.Equal(t, ErrBlockedInfohash, err)
|
||||
|
||||
req.InfoHash = chihaya.InfoHash("def")
|
||||
req.InfoHash = chihaya.InfoHash(ih2)
|
||||
err = handler(nil, &req, &resp)
|
||||
assert.Equal(t, ErrBlockedInfohash, err)
|
||||
|
||||
req.InfoHash = chihaya.InfoHash("abc")
|
||||
req.InfoHash = chihaya.InfoHash(ih1)
|
||||
err = handler(nil, &req, &resp)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
@@ -55,11 +55,11 @@ func TestWhitelistScrapeMiddlewareBlock(t *testing.T) {
|
||||
err = handler(nil, &req, &resp)
|
||||
assert.Nil(t, err)
|
||||
|
||||
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash("abc"), chihaya.InfoHash("def")}
|
||||
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash(ih1), chihaya.InfoHash(ih2)}
|
||||
err = handler(nil, &req, &resp)
|
||||
assert.Equal(t, ErrBlockedInfohash, err)
|
||||
|
||||
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash("abc")}
|
||||
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash(ih1)}
|
||||
err = handler(nil, &req, &resp)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
@@ -84,13 +84,13 @@ func TestWhitelistScrapeMiddlewareFilter(t *testing.T) {
|
||||
err = handler(nil, &req, &resp)
|
||||
assert.Nil(t, err)
|
||||
|
||||
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash("abc"), chihaya.InfoHash("def")}
|
||||
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash(ih1), chihaya.InfoHash(ih2)}
|
||||
err = handler(nil, &req, &resp)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []chihaya.InfoHash{chihaya.InfoHash("abc")}, req.InfoHashes)
|
||||
assert.Equal(t, []chihaya.InfoHash{chihaya.InfoHash(ih1)}, req.InfoHashes)
|
||||
|
||||
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash("abc")}
|
||||
req.InfoHashes = []chihaya.InfoHash{chihaya.InfoHash(ih1)}
|
||||
err = handler(nil, &req, &resp)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []chihaya.InfoHash{chihaya.InfoHash("abc")}, req.InfoHashes)
|
||||
assert.Equal(t, []chihaya.InfoHash{chihaya.InfoHash(ih1)}, req.InfoHashes)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user