1
+package peerlog
2
+
3
+import (
4
+ "fmt"
5
+
6
+ core "github.com/ipfs/go-ipfs/core"
7
+ plugin "github.com/ipfs/go-ipfs/plugin"
8
+ logging "github.com/ipfs/go-log"
9
+ network "github.com/libp2p/go-libp2p-core/network"
10
+)
11
+
12
+var log = logging.Logger("plugin/peerlog")
13
+
14
+// Log all the PeerIDs we see
15
+//
16
+// Usage:
17
+// GOLOG_FILE=~/peer.log IPFS_LOGGING_FMT=json ipfs daemon
18
+// Output:
19
+// {"level":"info","ts":"2020-02-10T13:54:26.639Z","logger":"plugin/peerlog","caller":"peerlog/peerlog.go:51","msg":"connected","peer":"QmS2H72gdrekXJggGdE9SunXPntBqdkJdkXQJjuxcH8Cbt"}
20
+// {"level":"info","ts":"2020-02-10T13:54:59.095Z","logger":"plugin/peerlog","caller":"peerlog/peerlog.go:56","msg":"disconnected","peer":"QmS2H72gdrekXJggGdE9SunXPntBqdkJdkXQJjuxcH8Cbt"}
21
+//
22
+type peerLogPlugin struct{}
23
+
24
+var _ plugin.PluginDaemonInternal = (*peerLogPlugin)(nil)
25
+
26
+// Plugins is exported list of plugins that will be loaded
27
+var Plugins = []plugin.Plugin{
28
+ &peerLogPlugin{},
29
+}
30
+
31
+// Name returns the plugin's name, satisfying the plugin.Plugin interface.
32
+func (*peerLogPlugin) Name() string {
33
+ return "peerlog"
34
+}
35
+
36
+// Version returns the plugin's version, satisfying the plugin.Plugin interface.
37
+func (*peerLogPlugin) Version() string {
38
+ return "0.1.0"
39
+}
40
+
41
+// Init initializes plugin
42
+func (*peerLogPlugin) Init(*plugin.Environment) error {
43
+ fmt.Println("peerLogPlugin enabled - PeerIDs will be logged")
44
+ return nil
45
+}
46
+
47
+func (*peerLogPlugin) Start(node *core.IpfsNode) error {
48
+ // Ensure logs from this plugin get printed regardless of global IPFS_LOGGING value
49
+ logging.SetLogLevel("plugin/peerlog", "info")
50
+ var notifee network.NotifyBundle
51
+ notifee.ConnectedF = func(net network.Network, conn network.Conn) {
52
+ log.Infow("connected",
53
+ "peer", conn.RemotePeer().Pretty(),
54
+ )
55
+ }
56
+ notifee.DisconnectedF = func(net network.Network, conn network.Conn) {
57
+ log.Infow("disconnected",
58
+ "peer", conn.RemotePeer().Pretty(),
59
+ )
60
+ }
61
+ node.PeerHost.Network().Notify(¬ifee)
62
+ return nil
63
+}
64
+
65
+func (*peerLogPlugin) Close() error {
66
+ return nil
67
+}