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"
|
2016-04-04 15:07:35 +02:00
|
|
|
"github.com/chihaya/chihaya/pkg/clientid"
|
2016-02-28 09:39:55 +01:00
|
|
|
"github.com/chihaya/chihaya/server/store"
|
|
|
|
"github.com/chihaya/chihaya/tracker"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
tracker.RegisterAnnounceMiddleware("client_whitelist", whitelistAnnounceClient)
|
|
|
|
}
|
|
|
|
|
2016-04-04 15:07:35 +02:00
|
|
|
// 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")
|
|
|
|
|
2016-02-28 09:39:55 +01:00
|
|
|
// whitelistAnnounceClient provides a middleware that only allows Clients to
|
2016-04-04 15:07:35 +02:00
|
|
|
// announce that are stored in the StringStore.
|
2016-02-28 09:39:55 +01:00
|
|
|
func whitelistAnnounceClient(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-05-17 05:48:23 +02:00
|
|
|
whitelisted, err := store.MustGetStore().HasString(PrefixClient + clientid.New(string(req.PeerID[:])))
|
2016-02-28 09:39:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !whitelisted {
|
2016-04-05 02:10:07 +02:00
|
|
|
return ErrNotWhitelistedClient
|
2016-02-28 09:39:55 +01:00
|
|
|
}
|
|
|
|
return next(cfg, req, resp)
|
|
|
|
}
|
|
|
|
}
|