Skip to content

Commit 10175a0

Browse files
committed
feat(peerlog): log protocols/versions
1 parent 6ae79ef commit 10175a0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

plugin/plugins/peerlog/peerlog.go

+35
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
core "github.com/ipfs/go-ipfs/core"
77
plugin "github.com/ipfs/go-ipfs/plugin"
88
logging "github.com/ipfs/go-log"
9+
eventbus "github.com/libp2p/go-eventbus"
10+
event "github.com/libp2p/go-libp2p-core/event"
911
network "github.com/libp2p/go-libp2p-core/network"
1012
)
1113

@@ -50,6 +52,7 @@ func (*peerLogPlugin) Start(node *core.IpfsNode) error {
5052
}
5153
var notifee network.NotifyBundle
5254
notifee.ConnectedF = func(net network.Network, conn network.Conn) {
55+
// TODO: Log transport, country, etc?
5356
log.Infow("connected",
5457
"peer", conn.RemotePeer().Pretty(),
5558
)
@@ -60,6 +63,38 @@ func (*peerLogPlugin) Start(node *core.IpfsNode) error {
6063
)
6164
}
6265
node.PeerHost.Network().Notify(&notifee)
66+
67+
sub, err := node.PeerHost.EventBus().Subscribe(
68+
new(event.EvtPeerIdentificationCompleted),
69+
eventbus.BufSize(1024),
70+
)
71+
if err != nil {
72+
return fmt.Errorf("failed to subscribe to identify notifications")
73+
}
74+
go func() {
75+
defer sub.Close()
76+
for e := range sub.Out() {
77+
switch e := e.(type) {
78+
case event.EvtPeerIdentificationCompleted:
79+
protocols, err := node.Peerstore.GetProtocols(e.Peer)
80+
if err != nil {
81+
log.Errorw("failed to get protocols", "error", err)
82+
continue
83+
}
84+
agent, err := node.Peerstore.Get(e.Peer, "AgentVersion")
85+
if err != nil {
86+
log.Errorw("failed to get protocols", "error", err)
87+
continue
88+
}
89+
log.Infow(
90+
"identified",
91+
"peer", e.Peer.Pretty(),
92+
"agent", agent,
93+
"protocols", protocols,
94+
)
95+
}
96+
}
97+
}()
6398
return nil
6499
}
65100

0 commit comments

Comments
 (0)