github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/p2p/security/filter.go (about)

     1  package security
     2  
     3  import "sync"
     4  
     5  type Filter interface {
     6  	DoFilter(string, string) error
     7  }
     8  
     9  type PeerFilter struct {
    10  	filterChain []Filter
    11  	mtx         sync.RWMutex
    12  }
    13  
    14  func NewPeerFilter() *PeerFilter {
    15  	return &PeerFilter{
    16  		filterChain: make([]Filter, 0),
    17  	}
    18  }
    19  
    20  func (pf *PeerFilter) register(filter Filter) {
    21  	pf.mtx.Lock()
    22  	defer pf.mtx.Unlock()
    23  
    24  	pf.filterChain = append(pf.filterChain, filter)
    25  }
    26  
    27  func (pf *PeerFilter) doFilter(ip string, pubKey string) error {
    28  	pf.mtx.RLock()
    29  	defer pf.mtx.RUnlock()
    30  
    31  	for _, filter := range pf.filterChain {
    32  		if err := filter.DoFilter(ip, pubKey); err != nil {
    33  			return err
    34  		}
    35  	}
    36  
    37  	return nil
    38  }