/
security.go
53 lines (42 loc) · 1.09 KB
/
security.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package security
import (
log "github.com/sirupsen/logrus"
cfg "github.com/bytom/vapor/config"
)
const logModule = "p2pSecurity"
type Security struct {
filter *PeerFilter
blacklist *Blacklist
peersBanScore *PeersBanScore
}
func NewSecurity(config *cfg.Config) *Security {
return &Security{
filter: NewPeerFilter(),
blacklist: NewBlacklist(config),
peersBanScore: NewPeersScore(),
}
}
func (s *Security) DoFilter(ip string, pubKey string) error {
return s.filter.doFilter(ip, pubKey)
}
func (s *Security) IsBanned(ip string, level byte, reason string) bool {
if ok := s.peersBanScore.Increase(ip, level, reason); !ok {
return false
}
if err := s.blacklist.AddPeer(ip); err != nil {
log.WithFields(log.Fields{"module": logModule, "err": err}).Error("fail on add ban peer")
}
//clear peer score
s.peersBanScore.DelPeer(ip)
return true
}
func (s *Security) RegisterFilter(filter Filter) {
s.filter.register(filter)
}
func (s *Security) Start() error {
if err := s.blacklist.LoadPeers(); err != nil {
return err
}
s.filter.register(s.blacklist)
return nil
}