2020-06-14 07:35:05 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2020-06-14 14:47:58 +00:00
|
|
|
"net"
|
2020-07-30 18:42:22 +00:00
|
|
|
"runtime"
|
2020-06-14 14:47:58 +00:00
|
|
|
|
2020-06-14 07:35:05 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/control"
|
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
"github.com/multiformats/go-multiaddr"
|
2020-12-02 19:21:43 +00:00
|
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
2020-06-14 07:35:05 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-12-02 16:45:28 +00:00
|
|
|
const (
|
|
|
|
// Limit for rate limiter when processing new inbound dials.
|
|
|
|
ipLimit = 4
|
2020-07-30 18:42:22 +00:00
|
|
|
|
2020-12-02 16:45:28 +00:00
|
|
|
// Burst limit for inbound dials.
|
|
|
|
ipBurst = 8
|
|
|
|
|
|
|
|
// High watermark buffer signifies the buffer till which
|
|
|
|
// we will handle inbound requests.
|
|
|
|
highWatermarkBuffer = 10
|
|
|
|
)
|
2020-07-30 18:42:22 +00:00
|
|
|
|
2020-06-14 07:35:05 +00:00
|
|
|
// InterceptPeerDial tests whether we're permitted to Dial the specified peer.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (s *Service) InterceptPeerDial(_ peer.ID) (allow bool) {
|
2020-06-14 07:35:05 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// InterceptAddrDial tests whether we're permitted to dial the specified
|
|
|
|
// multiaddr for the given peer.
|
2020-11-13 12:58:13 +00:00
|
|
|
func (s *Service) InterceptAddrDial(pid peer.ID, m multiaddr.Multiaddr) (allow bool) {
|
|
|
|
// Disallow bad peers from dialing in.
|
|
|
|
if s.peers.IsBad(pid) {
|
|
|
|
return false
|
|
|
|
}
|
2020-06-14 14:47:58 +00:00
|
|
|
return filterConnections(s.addrFilter, m)
|
2020-06-14 07:35:05 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 12:58:13 +00:00
|
|
|
// InterceptAccept checks whether the incidental inbound connection is allowed.
|
2020-06-14 07:35:05 +00:00
|
|
|
func (s *Service) InterceptAccept(n network.ConnMultiaddrs) (allow bool) {
|
2020-07-30 18:42:22 +00:00
|
|
|
if !s.validateDial(n.RemoteMultiaddr()) {
|
|
|
|
// Allow other go-routines to run in the event
|
|
|
|
// we receive a large amount of junk connections.
|
|
|
|
runtime.Gosched()
|
|
|
|
log.WithFields(logrus.Fields{"peer": n.RemoteMultiaddr(),
|
|
|
|
"reason": "exceeded dial limit"}).Trace("Not accepting inbound dial from ip address")
|
|
|
|
return false
|
|
|
|
}
|
2020-12-02 16:45:28 +00:00
|
|
|
if s.isPeerAtLimit(true /* inbound */) {
|
2020-06-14 07:35:05 +00:00
|
|
|
log.WithFields(logrus.Fields{"peer": n.RemoteMultiaddr(),
|
|
|
|
"reason": "at peer limit"}).Trace("Not accepting inbound dial")
|
|
|
|
return false
|
|
|
|
}
|
2020-06-14 14:47:58 +00:00
|
|
|
return filterConnections(s.addrFilter, n.RemoteMultiaddr())
|
2020-06-14 07:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InterceptSecured tests whether a given connection, now authenticated,
|
|
|
|
// is allowed.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (s *Service) InterceptSecured(_ network.Direction, _ peer.ID, _ network.ConnMultiaddrs) (allow bool) {
|
2020-06-14 07:35:05 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// InterceptUpgraded tests whether a fully capable connection is allowed.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (s *Service) InterceptUpgraded(_ network.Conn) (allow bool, reason control.DisconnectReason) {
|
2020-06-14 07:35:05 +00:00
|
|
|
return true, 0
|
|
|
|
}
|
2020-06-14 14:47:58 +00:00
|
|
|
|
2020-07-30 18:42:22 +00:00
|
|
|
func (s *Service) validateDial(addr multiaddr.Multiaddr) bool {
|
|
|
|
ip, err := manet.ToIP(addr)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
remaining := s.ipLimiter.Remaining(ip.String())
|
|
|
|
if remaining <= 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
s.ipLimiter.Add(ip.String(), 1)
|
2021-02-17 17:48:44 +00:00
|
|
|
return true
|
2020-07-30 18:42:22 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 14:47:58 +00:00
|
|
|
// configureFilter looks at the provided allow lists and
|
|
|
|
// deny lists to appropriately create a filter.
|
2020-12-14 22:22:55 +00:00
|
|
|
func configureFilter(cfg *Config) (*multiaddr.Filters, error) {
|
|
|
|
addrFilter := multiaddr.NewFilters()
|
2020-06-14 14:47:58 +00:00
|
|
|
// Configure from provided allow list in the config.
|
|
|
|
if cfg.AllowListCIDR != "" {
|
|
|
|
_, ipnet, err := net.ParseCIDR(cfg.AllowListCIDR)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-14 22:22:55 +00:00
|
|
|
addrFilter.AddFilter(*ipnet, multiaddr.ActionAccept)
|
2020-06-14 14:47:58 +00:00
|
|
|
}
|
|
|
|
// Configure from provided deny list in the config.
|
|
|
|
if len(cfg.DenyListCIDR) > 0 {
|
|
|
|
for _, cidr := range cfg.DenyListCIDR {
|
|
|
|
_, ipnet, err := net.ParseCIDR(cidr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-14 22:22:55 +00:00
|
|
|
addrFilter.AddFilter(*ipnet, multiaddr.ActionDeny)
|
2020-06-14 14:47:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return addrFilter, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// filterConnections checks the appropriate ip subnets from our
|
|
|
|
// filter and decides what to do with them. By default libp2p
|
|
|
|
// accepts all incoming dials, so if we have an allow list
|
|
|
|
// we will reject all inbound dials except for those in the
|
|
|
|
// appropriate ip subnets.
|
2020-12-14 22:22:55 +00:00
|
|
|
func filterConnections(f *multiaddr.Filters, a multiaddr.Multiaddr) bool {
|
|
|
|
acceptedNets := f.FiltersForAction(multiaddr.ActionAccept)
|
2020-06-14 14:47:58 +00:00
|
|
|
restrictConns := len(acceptedNets) != 0
|
|
|
|
|
|
|
|
// If we have an allow list added in, we by default reject all
|
|
|
|
// connection attempts except for those coming in from the
|
|
|
|
// appropriate ip subnets.
|
|
|
|
if restrictConns {
|
|
|
|
ip, err := manet.ToIP(a)
|
|
|
|
if err != nil {
|
|
|
|
log.Tracef("Multiaddress has invalid ip: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
for _, ipnet := range acceptedNets {
|
|
|
|
if ipnet.Contains(ip) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
return !f.AddrBlocked(a)
|
|
|
|
}
|