4 log "github.com/sirupsen/logrus"
6 cfg "github.com/bytom/vapor/config"
9 const logModule = "p2pSecurity"
11 type Security struct {
14 peersBanScore *PeersBanScore
17 func NewSecurity(config *cfg.Config) *Security {
19 filter: NewPeerFilter(),
20 blacklist: NewBlacklist(config),
21 peersBanScore: NewPeersScore(),
25 func (s *Security) DoFilter(ip string, pubKey string) error {
26 return s.filter.doFilter(ip, pubKey)
29 func (s *Security) IsBanned(ip string, level byte, reason string) bool {
30 if ok := s.peersBanScore.Increase(ip, level, reason); !ok {
34 if err := s.blacklist.AddPeer(ip); err != nil {
35 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("fail on add ban peer")
38 s.peersBanScore.DelPeer(ip)
42 func (s *Security) RegisterFilter(filter Filter) {
43 s.filter.register(filter)
46 func (s *Security) Start() error {
47 if err := s.blacklist.LoadPeers(); err != nil {
51 s.filter.register(s.blacklist)