Skip to content

Commit

Permalink
implement peer blacklist
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Jan 15, 2019
1 parent 2e1fb61 commit be4ca29
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ type PubSub struct {
// eval thunk in event loop
eval chan func()

// peer blacklist
blacklist map[peer.ID]struct{}
blacklistPeer chan peer.ID

peers map[peer.ID]chan *RPC
seenMessages *timecache.TimeCache

Expand Down Expand Up @@ -175,6 +179,8 @@ func NewPubSub(ctx context.Context, h host.Host, rt PubSubRouter, opts ...Option
topics: make(map[string]map[peer.ID]struct{}),
peers: make(map[peer.ID]chan *RPC),
topicVals: make(map[string]*topicVal),
blacklist: make(map[peer.ID]struct{}),
blacklistPeer: make(chan peer.ID),
seenMessages: timecache.NewTimeCache(time.Second * 120),
counter: uint64(time.Now().UnixNano()),
}
Expand Down Expand Up @@ -370,6 +376,10 @@ func (p *PubSub) processLoop(ctx context.Context) {
case thunk := <-p.eval:
thunk()

case pid := <-p.blacklistPeer:
log.Infof("Blacklisting peer %s", pid)
p.blacklist[pid] = struct{}{}

case <-ctx.Done():
log.Info("pubsub processloop shutting down")
return
Expand Down Expand Up @@ -563,6 +573,12 @@ func msgID(pmsg *pb.Message) string {

// pushMsg pushes a message performing validation as necessary
func (p *PubSub) pushMsg(vals []*topicVal, src peer.ID, msg *Message) {
// reject messages from blacklisted peers
if _, ok := p.blacklist[src]; ok {
log.Warningf("dropping message from blacklisted peer %s", src)
return
}

// reject unsigned messages when strict before we even process the id
if p.signStrict && msg.Signature == nil {
log.Debugf("dropping unsigned message from %s", src)
Expand Down Expand Up @@ -817,6 +833,11 @@ func (p *PubSub) ListPeers(topic string) []peer.ID {
return <-out
}

// BlacklistPeer blacklists a peer; all messages from this peer will be unconditionally dropped.
func (p *PubSub) BlacklistPeer(pid peer.ID) {
p.blacklistPeer <- pid
}

// per topic validators
type addValReq struct {
topic string
Expand Down

0 comments on commit be4ca29

Please sign in to comment.