2020-05-15 16:31:03 +00:00
|
|
|
package libp2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/libp2p/go-libp2p-core/connmgr"
|
|
|
|
"github.com/libp2p/go-libp2p-core/control"
|
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
|
|
)
|
|
|
|
|
2020-05-20 15:27:31 +00:00
|
|
|
// filtersConnectionGater is an adapter that turns multiaddr.Filter into a
|
|
|
|
// connmgr.ConnectionGater.
|
|
|
|
type filtersConnectionGater ma.Filters
|
2020-05-15 16:31:03 +00:00
|
|
|
|
2020-05-20 15:27:31 +00:00
|
|
|
var _ connmgr.ConnectionGater = (*filtersConnectionGater)(nil)
|
2020-05-15 16:31:03 +00:00
|
|
|
|
2020-05-20 15:27:31 +00:00
|
|
|
func (f *filtersConnectionGater) InterceptAddrDial(_ peer.ID, addr ma.Multiaddr) (allow bool) {
|
2020-05-15 16:31:03 +00:00
|
|
|
return !(*ma.Filters)(f).AddrBlocked(addr)
|
|
|
|
}
|
|
|
|
|
2020-05-20 15:27:31 +00:00
|
|
|
func (f *filtersConnectionGater) InterceptPeerDial(p peer.ID) (allow bool) {
|
2020-05-15 16:31:03 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-09-16 05:07:36 +00:00
|
|
|
func (f *filtersConnectionGater) InterceptAccept(connAddr network.ConnMultiaddrs) (allow bool) {
|
|
|
|
return !(*ma.Filters)(f).AddrBlocked(connAddr.RemoteMultiaddr())
|
2020-05-15 16:31:03 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 05:07:36 +00:00
|
|
|
func (f *filtersConnectionGater) InterceptSecured(_ network.Direction, _ peer.ID, connAddr network.ConnMultiaddrs) (allow bool) {
|
|
|
|
return !(*ma.Filters)(f).AddrBlocked(connAddr.RemoteMultiaddr())
|
2020-05-15 16:31:03 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 15:27:31 +00:00
|
|
|
func (f *filtersConnectionGater) InterceptUpgraded(_ network.Conn) (allow bool, reason control.DisconnectReason) {
|
2020-05-15 16:31:03 +00:00
|
|
|
return true, 0
|
|
|
|
}
|