mirror of
https://github.com/sot-tech/mochi.git
synced 2026-05-29 20:29:27 -07:00
middleware/client: make client middleware use StringStore and remove ClientStore
Fixes #158
This commit is contained in:
@@ -4,22 +4,22 @@ This package provides the announce middlewares `client_whitelist` and `client_bl
|
||||
|
||||
### `client_blacklist`
|
||||
|
||||
The `client_blacklist` middleware uses all clientIDs stored in the `ClientStore` to blacklist, i.e. block announces.
|
||||
The `client_blacklist` middleware uses all clientIDs stored in the `StringStore` to blacklist, i.e. block announces.
|
||||
|
||||
The clientID part of the peerID of an announce is matched against the `ClientStore`, if it's contained within the `ClientStore`, the announce is aborted.
|
||||
The clientID part of the peerID of an announce is matched against the `StringStore`, if it's contained within the `StringStore`, the announce is aborted.
|
||||
|
||||
### `client_whitelist`
|
||||
|
||||
The `client_whitelist` middleware uses all clientIDs stored in the `ClientStore` to whitelist, i.e. allow announces.
|
||||
The `client_whitelist` middleware uses all clientIDs stored in the `StringStore` to whitelist, i.e. allow announces.
|
||||
|
||||
The clientID part of the peerID of an announce is matched against the `ClientStore`, if it's _not_ contained within the `ClientStore`, the announce is aborted.
|
||||
The clientID part of the peerID of an announce is matched against the `StringStore`, if it's _not_ contained within the `StringStore`, the announce is aborted.
|
||||
|
||||
### Important things to notice
|
||||
|
||||
Both middlewares operate on announce requests only.
|
||||
|
||||
Both middlewares use the same `ClientStore`.
|
||||
Both middlewares use the same `StringStore`.
|
||||
It is therefore not advised to have both the `client_blacklist` and the `client_whitelist` middleware running.
|
||||
(If you add clientID to the `ClientStore`, it will be used for blacklisting and whitelisting.
|
||||
(If you add clientID to the `StringStore`, it will be used for blacklisting and whitelisting.
|
||||
If your store contains no clientIDs, no announces will be blocked by the blacklist, but all announces will be blocked by the whitelist.
|
||||
If your store contains all clientIDs, no announces will be blocked by the whitelist, but all announces will be blocked by the blacklist.)
|
||||
@@ -6,6 +6,7 @@ package client
|
||||
|
||||
import (
|
||||
"github.com/chihaya/chihaya"
|
||||
"github.com/chihaya/chihaya/pkg/clientid"
|
||||
"github.com/chihaya/chihaya/server/store"
|
||||
"github.com/chihaya/chihaya/tracker"
|
||||
)
|
||||
@@ -19,11 +20,10 @@ func init() {
|
||||
var ErrBlockedClient = tracker.ClientError("disallowed client")
|
||||
|
||||
// blacklistAnnounceClient provides a middleware that only allows Clients to
|
||||
// announce that are not stored in a ClientStore.
|
||||
// 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().FindClient(req.PeerID)
|
||||
|
||||
blacklisted, err := store.MustGetStore().HasString(PrefixClient + clientid.New(string(req.PeerID)))
|
||||
if err != nil {
|
||||
return err
|
||||
} else if blacklisted {
|
||||
|
||||
@@ -6,6 +6,7 @@ package client
|
||||
|
||||
import (
|
||||
"github.com/chihaya/chihaya"
|
||||
"github.com/chihaya/chihaya/pkg/clientid"
|
||||
"github.com/chihaya/chihaya/server/store"
|
||||
"github.com/chihaya/chihaya/tracker"
|
||||
)
|
||||
@@ -14,12 +15,18 @@ func init() {
|
||||
tracker.RegisterAnnounceMiddleware("client_whitelist", whitelistAnnounceClient)
|
||||
}
|
||||
|
||||
// PrefixClient is the prefix to be used for client peer IDs.
|
||||
const PrefixClient = "c-"
|
||||
|
||||
// ErrNotWhitelistedClient is returned by an announce middleware if the
|
||||
// announcing Client is not whitelisted.
|
||||
var ErrNotWhitelistedClient = tracker.ClientError("client not whitelisted")
|
||||
|
||||
// whitelistAnnounceClient provides a middleware that only allows Clients to
|
||||
// announce that are stored in a ClientStore.
|
||||
// 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().FindClient(req.PeerID)
|
||||
|
||||
whitelisted, err := store.MustGetStore().HasString(PrefixClient + clientid.New(string(req.PeerID)))
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !whitelisted {
|
||||
|
||||
Reference in New Issue
Block a user