Skip to content

Commit

Permalink
Added unban peer
Browse files Browse the repository at this point in the history
  • Loading branch information
Chinwendu20 committed Apr 17, 2023
1 parent 9badd7f commit 10f9b8c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
31 changes: 31 additions & 0 deletions banman/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ type Store interface {

// Status returns the ban status for a given IP network.
Status(*net.IPNet) (Status, error)

// UnBanIPNet unbans previously banned peer
UnBanIPNet(ipNet *net.IPNet) error
}

// NewStore returns a Store backed by a database.
Expand Down Expand Up @@ -136,6 +139,34 @@ func (s *banStore) BanIPNet(ipNet *net.IPNet, reason Reason, duration time.Durat
})
}

// UnBanIPNet removes a ban record for the IP network within the store.
func (s *banStore) UnBanIPNet(ipNet *net.IPNet) error {
err := walletdb.Update(s.db, func(tx walletdb.ReadWriteTx) error {
banStore := tx.ReadWriteBucket(banStoreBucket)
if banStore == nil {
return ErrCorruptedStore
}
banIndex := banStore.NestedReadWriteBucket(banBucket)
if banIndex == nil {
return ErrCorruptedStore
}
reasonIndex := banStore.NestedReadWriteBucket(reasonBucket)
if reasonIndex == nil {
return ErrCorruptedStore
}

var ipNetBuf bytes.Buffer
if err := encodeIPNet(&ipNetBuf, ipNet); err != nil {
return fmt.Errorf("unable to encode %v: %v", ipNet, err)
}
k := ipNetBuf.Bytes()

return removeBannedIPNet(banIndex, reasonIndex, k)
})

return err
}

// addBannedIPNet adds an entry to the ban store for the given IP network.
func addBannedIPNet(banIndex, reasonIndex walletdb.ReadWriteBucket,
ipNetKey []byte, reason Reason, duration time.Duration) error {
Expand Down
27 changes: 27 additions & 0 deletions neutrino.go
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,33 @@ func (s *ChainService) BanPeer(addr string, reason banman.Reason) error {
return s.banStore.BanIPNet(ipNet, reason, BanDuration)
}

// UnBanPeer connects and unban's a previously banned peer
func (s *ChainService) UnBanPeer(addr string) error {
log.Warnf("UnBanning peer %v", addr)

defer func() {

if sp := s.PeerByAddr(addr); sp != nil {
tcpAddr, err := s.addrStringToNetAddr(addr)
if err != nil {
log.Warnf("unable to lookup IP for "+
"%v: %v", addr, err)
}
s.connManager.Connect(&connmgr.ConnReq{
Addr: tcpAddr,
Permanent: true,
})
}
}()

ipNet, err := banman.ParseIPNet(addr, nil)
if err != nil {
return fmt.Errorf("unable to parse IP network for peer %v: %v",
addr, err)
}
return s.banStore.UnBanIPNet(ipNet)
}

// IsBanned returns true if the peer is banned, and false otherwise.
func (s *ChainService) IsBanned(addr string) bool {
ipNet, err := banman.ParseIPNet(addr, nil)
Expand Down

0 comments on commit 10f9b8c

Please sign in to comment.