2016-02-28 09:39:55 +01:00
|
|
|
// Copyright 2016 The Chihaya Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
2016-03-30 21:29:04 +02:00
|
|
|
package client
|
2016-02-28 09:39:55 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/chihaya/chihaya"
|
|
|
|
"github.com/chihaya/chihaya/server/store"
|
|
|
|
"github.com/chihaya/chihaya/tracker"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
tracker.RegisterAnnounceMiddleware("client_blacklist", blacklistAnnounceClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrBlockedClient is returned by an announce middleware if the announcing
|
|
|
|
// Client is disallowed.
|
|
|
|
var ErrBlockedClient = tracker.ClientError("disallowed client")
|
|
|
|
|
|
|
|
// blacklistAnnounceClient provides a middleware that only allows Clients to
|
|
|
|
// announce that are not stored in a ClientStore.
|
|
|
|
func blacklistAnnounceClient(next tracker.AnnounceHandler) tracker.AnnounceHandler {
|
2016-03-03 02:18:55 +01:00
|
|
|
return func(cfg *chihaya.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) error {
|
2016-02-28 09:39:55 +01:00
|
|
|
blacklisted, err := store.MustGetStore().FindClient(req.PeerID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if blacklisted {
|
|
|
|
return ErrBlockedClient
|
|
|
|
}
|
|
|
|
return next(cfg, req, resp)
|
|
|
|
}
|
|
|
|
}
|