فهرست منبع

:gear: Add PeerGater to gate message by peer

Ettore Di Giacinto 3 سال پیش
والد
کامیت
717af8628a
3فایلهای تغییر یافته به همراه20 افزوده شده و 1 حذف شده
  1. 7 1
      pkg/node/config.go
  2. 6 0
      pkg/node/connection.go
  3. 7 0
      pkg/node/options.go

+ 7 - 1
pkg/node/config.go

@@ -23,6 +23,7 @@ import (
 	"github.com/libp2p/go-libp2p"
 	"github.com/libp2p/go-libp2p-core/host"
 	"github.com/libp2p/go-libp2p-core/network"
+	"github.com/libp2p/go-libp2p-core/peer"
 
 	"github.com/mudler/edgevpn/pkg/blockchain"
 	discovery "github.com/mudler/edgevpn/pkg/discovery"
@@ -69,7 +70,12 @@ type Config struct {
 
 	Whitelist, Blacklist []string
 
-	Sealer Sealer
+	Sealer    Sealer
+	PeerGater Gater
+}
+
+type Gater interface {
+	Gate(*Node, peer.ID) bool
 }
 
 type Sealer interface {

+ 6 - 0
pkg/node/connection.go

@@ -138,6 +138,12 @@ func (e *Node) handleEvents(ctx context.Context) {
 			if m == nil {
 				continue
 			}
+
+			if e.config.PeerGater != nil && e.config.PeerGater.Gate(e, peer.ID(m.SenderID)) {
+				e.config.Logger.Warnf("gated message from %s", m.SenderID)
+				continue
+			}
+
 			c := m.Copy()
 			str, err := e.config.Sealer.Unseal(c.Message, e.sealkey())
 			if err != nil {

+ 7 - 0
pkg/node/options.go

@@ -175,6 +175,13 @@ func MaxMessageSize(i int) func(cfg *Config) error {
 	}
 }
 
+func WithPeerGater(d Gater) Option {
+	return func(cfg *Config) error {
+		cfg.PeerGater = d
+		return nil
+	}
+}
+
 func WithLedgerAnnounceTime(t time.Duration) func(cfg *Config) error {
 	return func(cfg *Config) error {
 		cfg.LedgerAnnounceTime = t