hyprspace/p2p/multigater.go
Roland Urbano f0c30f0769
Add filterPrivateAddresses option to block RFC1918/link-local dials (#101)
* Add filterPrivateAddresses option to block RFC1918/link-local dials (#22)

* Apply suggestion from @max-privatevoid

Co-authored-by: Max <max@privatevoid.net>

* Simplified config parsing

Signed-off-by: Roland Urbano <urbano.roland@gmail.com>

* NewRecursionGater: return connmgr.ConnectionGater

* implement MultiGater

* implement FilterGater

* use MultiGater for private address filtering

---------

Signed-off-by: Roland Urbano <urbano.roland@gmail.com>
Co-authored-by: Max <max@privatevoid.net>
2026-05-18 22:20:44 +02:00

66 lines
1.5 KiB
Go

package p2p
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"
multiaddr "github.com/multiformats/go-multiaddr"
)
// MultiGater combines a list of ConnectionGaters. All gaters must return true to allow the connection.
type MultiGater struct {
gaters []connmgr.ConnectionGater
}
func NewMultiGater(gaters ...connmgr.ConnectionGater) connmgr.ConnectionGater {
return MultiGater{
gaters: gaters,
}
}
func (m MultiGater) InterceptAccept(addrs network.ConnMultiaddrs) (allow bool) {
for _, g := range m.gaters {
if !g.InterceptAccept(addrs) {
return false
}
}
return true
}
func (m MultiGater) InterceptAddrDial(p peer.ID, addr multiaddr.Multiaddr) (allow bool) {
for _, g := range m.gaters {
if !g.InterceptAddrDial(p, addr) {
return false
}
}
return true
}
func (m MultiGater) InterceptPeerDial(p peer.ID) (allow bool) {
for _, g := range m.gaters {
if !g.InterceptPeerDial(p) {
return false
}
}
return true
}
func (m MultiGater) InterceptSecured(d network.Direction, p peer.ID, addrs network.ConnMultiaddrs) (allow bool) {
for _, g := range m.gaters {
if !g.InterceptSecured(d, p, addrs) {
return false
}
}
return true
}
func (m MultiGater) InterceptUpgraded(conn network.Conn) (allow bool, reason control.DisconnectReason) {
for _, g := range m.gaters {
if ok, reason := g.InterceptUpgraded(conn); !ok {
return false, reason
}
}
return true, 0
}