|
| 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 | + if err := logging.SetLogLevel("plugin/peerlog", "info"); err != nil { |
| 50 | + return fmt.Errorf("failed to set log level: %w", err) |
| 51 | + } |
| 52 | + var notifee network.NotifyBundle |
| 53 | + notifee.ConnectedF = func(net network.Network, conn network.Conn) { |
| 54 | + log.Infow("connected", |
| 55 | + "peer", conn.RemotePeer().Pretty(), |
| 56 | + ) |
| 57 | + } |
| 58 | + notifee.DisconnectedF = func(net network.Network, conn network.Conn) { |
| 59 | + log.Infow("disconnected", |
| 60 | + "peer", conn.RemotePeer().Pretty(), |
| 61 | + ) |
| 62 | + } |
| 63 | + node.PeerHost.Network().Notify(¬ifee) |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +func (*peerLogPlugin) Close() error { |
| 68 | + return nil |
| 69 | +} |
0 commit comments