Skip to content

Commit 1f64f32

Browse files
Merge branch 'master' into fix/drop-gogo
2 parents 1e26fb8 + dbb8d9e commit 1f64f32

File tree

5 files changed

+26
-0
lines changed

5 files changed

+26
-0
lines changed

dht.go

+3
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ type IpfsDHT struct {
163163
// addrFilter is used to filter the addresses we put into the peer store.
164164
// Mostly used to filter out localhost and local addresses.
165165
addrFilter func([]ma.Multiaddr) []ma.Multiaddr
166+
167+
onRequestHook func(ctx context.Context, s network.Stream, req pb.Message)
166168
}
167169

168170
// Assert that IPFS assumptions about interfaces aren't broken. These aren't a
@@ -306,6 +308,7 @@ func makeDHT(h host.Host, cfg dhtcfg.Config) (*IpfsDHT, error) {
306308
routingTablePeerFilter: cfg.RoutingTable.PeerFilter,
307309
rtPeerDiversityFilter: cfg.RoutingTable.DiversityFilter,
308310
addrFilter: cfg.AddressFilter,
311+
onRequestHook: cfg.OnRequestHook,
309312

310313
fixLowPeersChan: make(chan struct{}, 1),
311314

dht_net.go

+4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ func (dht *IpfsDHT) handleNewMessage(s network.Stream) bool {
101101
metrics.ReceivedBytes.M(int64(msgLen)),
102102
)
103103

104+
if dht.onRequestHook != nil {
105+
dht.onRequestHook(ctx, s, req)
106+
}
107+
104108
handler := dht.handlerForMsgType(req.GetType())
105109
if handler == nil {
106110
stats.Record(ctx, metrics.ReceivedMessageErrors.M(1))

dht_options.go

+13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dht
22

33
import (
4+
"context"
45
"fmt"
56
"testing"
67
"time"
@@ -12,6 +13,7 @@ import (
1213
"github.com/libp2p/go-libp2p-kbucket/peerdiversity"
1314
record "github.com/libp2p/go-libp2p-record"
1415
"github.com/libp2p/go-libp2p/core/host"
16+
"github.com/libp2p/go-libp2p/core/network"
1517
"github.com/libp2p/go-libp2p/core/peer"
1618
"github.com/libp2p/go-libp2p/core/protocol"
1719

@@ -368,3 +370,14 @@ func WithCustomMessageSender(messageSenderBuilder func(h host.Host, protos []pro
368370
return nil
369371
}
370372
}
373+
374+
// OnRequestHook registers a callback function that will be invoked for every
375+
// incoming DHT protocol message.
376+
// Note: Ensure that the callback executes efficiently, as it will block the
377+
// entire message handler.
378+
func OnRequestHook(f func(ctx context.Context, s network.Stream, req pb.Message)) Option {
379+
return func(c *dhtcfg.Config) error {
380+
c.OnRequestHook = f
381+
return nil
382+
}
383+
}

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/ipfs/go-datastore v0.6.0
1717
github.com/ipfs/go-detect-race v0.0.1
1818
github.com/ipfs/go-log/v2 v2.5.1
19+
github.com/ipfs/go-test v0.0.4
1920
github.com/libp2p/go-libp2p v0.38.2
2021
github.com/libp2p/go-libp2p-kbucket v0.6.4
2122
github.com/libp2p/go-libp2p-record v0.3.1
@@ -63,6 +64,8 @@ require (
6364
github.com/gorilla/websocket v1.5.3 // indirect
6465
github.com/hashicorp/errwrap v1.1.0 // indirect
6566
github.com/huin/goupnp v1.3.0 // indirect
67+
github.com/ipfs/go-block-format v0.2.0 // indirect
68+
github.com/ipfs/go-ipfs-util v0.0.3 // indirect
6669
github.com/ipfs/go-log v1.0.5 // indirect
6770
github.com/ipld/go-ipld-prime v0.21.0 // indirect
6871
github.com/jackpal/go-nat-pmp v1.0.2 // indirect

internal/config/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"context"
45
"fmt"
56
"time"
67

@@ -14,6 +15,7 @@ import (
1415
"github.com/libp2p/go-libp2p-kbucket/peerdiversity"
1516
record "github.com/libp2p/go-libp2p-record"
1617
"github.com/libp2p/go-libp2p/core/host"
18+
"github.com/libp2p/go-libp2p/core/network"
1719
"github.com/libp2p/go-libp2p/core/peer"
1820
"github.com/libp2p/go-libp2p/core/protocol"
1921
ma "github.com/multiformats/go-multiaddr"
@@ -63,6 +65,7 @@ type Config struct {
6365

6466
BootstrapPeers func() []peer.AddrInfo
6567
AddressFilter func([]ma.Multiaddr) []ma.Multiaddr
68+
OnRequestHook func(ctx context.Context, s network.Stream, req pb.Message)
6669

6770
// test specific Config options
6871
DisableFixLowPeers bool

0 commit comments

Comments
 (0)