2021-06-29 12:07:00 +00:00
|
|
|
package net
|
2021-06-24 14:02:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2021-06-29 12:07:00 +00:00
|
|
|
"go.uber.org/fx"
|
|
|
|
|
2021-06-24 14:02:51 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
2021-06-29 12:07:00 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/metrics"
|
2021-06-24 14:02:51 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2021-06-29 12:07:00 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/protocol"
|
2021-06-24 14:02:51 +00:00
|
|
|
swarm "github.com/libp2p/go-libp2p-swarm"
|
|
|
|
basichost "github.com/libp2p/go-libp2p/p2p/host/basic"
|
|
|
|
"github.com/libp2p/go-libp2p/p2p/net/conngater"
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
|
|
|
"github.com/filecoin-project/lotus/node/modules/lp2p"
|
|
|
|
)
|
|
|
|
|
2021-06-29 12:07:00 +00:00
|
|
|
type NetAPI struct {
|
2021-06-24 14:02:51 +00:00
|
|
|
fx.In
|
|
|
|
|
|
|
|
RawHost lp2p.RawHost
|
|
|
|
Host host.Host
|
|
|
|
Router lp2p.BaseIpfsRouting
|
|
|
|
ConnGater *conngater.BasicConnectionGater
|
|
|
|
Reporter metrics.Reporter
|
|
|
|
Sk *dtypes.ScoreKeeper
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:07:00 +00:00
|
|
|
func (a *NetAPI) ID(context.Context) (peer.ID, error) {
|
|
|
|
return a.Host.ID(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *NetAPI) NetConnectedness(ctx context.Context, pid peer.ID) (network.Connectedness, error) {
|
2021-06-24 14:02:51 +00:00
|
|
|
return a.Host.Network().Connectedness(pid), nil
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:07:00 +00:00
|
|
|
func (a *NetAPI) NetPubsubScores(context.Context) ([]api.PubsubScore, error) {
|
2021-06-24 14:02:51 +00:00
|
|
|
scores := a.Sk.Get()
|
|
|
|
out := make([]api.PubsubScore, len(scores))
|
|
|
|
i := 0
|
|
|
|
for k, v := range scores {
|
|
|
|
out[i] = api.PubsubScore{ID: k, Score: v}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(out, func(i, j int) bool {
|
|
|
|
return strings.Compare(string(out[i].ID), string(out[j].ID)) > 0
|
|
|
|
})
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:07:00 +00:00
|
|
|
func (a *NetAPI) NetPeers(context.Context) ([]peer.AddrInfo, error) {
|
2021-06-24 14:02:51 +00:00
|
|
|
conns := a.Host.Network().Conns()
|
|
|
|
out := make([]peer.AddrInfo, len(conns))
|
|
|
|
|
|
|
|
for i, conn := range conns {
|
|
|
|
out[i] = peer.AddrInfo{
|
|
|
|
ID: conn.RemotePeer(),
|
|
|
|
Addrs: []ma.Multiaddr{
|
|
|
|
conn.RemoteMultiaddr(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:07:00 +00:00
|
|
|
func (a *NetAPI) NetPeerInfo(_ context.Context, p peer.ID) (*api.ExtendedPeerInfo, error) {
|
2021-06-24 14:02:51 +00:00
|
|
|
info := &api.ExtendedPeerInfo{ID: p}
|
|
|
|
|
|
|
|
agent, err := a.Host.Peerstore().Get(p, "AgentVersion")
|
|
|
|
if err == nil {
|
|
|
|
info.Agent = agent.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, a := range a.Host.Peerstore().Addrs(p) {
|
|
|
|
info.Addrs = append(info.Addrs, a.String())
|
|
|
|
}
|
|
|
|
sort.Strings(info.Addrs)
|
|
|
|
|
|
|
|
protocols, err := a.Host.Peerstore().GetProtocols(p)
|
|
|
|
if err == nil {
|
|
|
|
sort.Strings(protocols)
|
|
|
|
info.Protocols = protocols
|
|
|
|
}
|
|
|
|
|
|
|
|
if cm := a.Host.ConnManager().GetTagInfo(p); cm != nil {
|
|
|
|
info.ConnMgrMeta = &api.ConnMgrInfo{
|
|
|
|
FirstSeen: cm.FirstSeen,
|
|
|
|
Value: cm.Value,
|
|
|
|
Tags: cm.Tags,
|
|
|
|
Conns: cm.Conns,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:07:00 +00:00
|
|
|
func (a *NetAPI) NetConnect(ctx context.Context, p peer.AddrInfo) error {
|
2021-06-24 14:02:51 +00:00
|
|
|
if swrm, ok := a.Host.Network().(*swarm.Swarm); ok {
|
|
|
|
swrm.Backoff().Clear(p.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.Host.Connect(ctx, p)
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:07:00 +00:00
|
|
|
func (a *NetAPI) NetAddrsListen(context.Context) (peer.AddrInfo, error) {
|
2021-06-24 14:02:51 +00:00
|
|
|
return peer.AddrInfo{
|
|
|
|
ID: a.Host.ID(),
|
|
|
|
Addrs: a.Host.Addrs(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:07:00 +00:00
|
|
|
func (a *NetAPI) NetDisconnect(ctx context.Context, p peer.ID) error {
|
2021-06-24 14:02:51 +00:00
|
|
|
return a.Host.Network().ClosePeer(p)
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:07:00 +00:00
|
|
|
func (a *NetAPI) NetFindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) {
|
2021-06-24 14:02:51 +00:00
|
|
|
return a.Router.FindPeer(ctx, p)
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:07:00 +00:00
|
|
|
func (a *NetAPI) NetAutoNatStatus(ctx context.Context) (i api.NatInfo, err error) {
|
2021-06-24 14:02:51 +00:00
|
|
|
autonat := a.RawHost.(*basichost.BasicHost).GetAutoNat()
|
|
|
|
|
|
|
|
if autonat == nil {
|
|
|
|
return api.NatInfo{
|
|
|
|
Reachability: network.ReachabilityUnknown,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var maddr string
|
|
|
|
if autonat.Status() == network.ReachabilityPublic {
|
|
|
|
pa, err := autonat.PublicAddr()
|
|
|
|
if err != nil {
|
|
|
|
return api.NatInfo{}, err
|
|
|
|
}
|
|
|
|
maddr = pa.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.NatInfo{
|
|
|
|
Reachability: autonat.Status(),
|
|
|
|
PublicAddr: maddr,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:07:00 +00:00
|
|
|
func (a *NetAPI) NetAgentVersion(ctx context.Context, p peer.ID) (string, error) {
|
2021-06-24 14:02:51 +00:00
|
|
|
ag, err := a.Host.Peerstore().Get(p, "AgentVersion")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ag == nil {
|
|
|
|
return "unknown", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ag.(string), nil
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:07:00 +00:00
|
|
|
func (a *NetAPI) NetBandwidthStats(ctx context.Context) (metrics.Stats, error) {
|
2021-06-24 14:02:51 +00:00
|
|
|
return a.Reporter.GetBandwidthTotals(), nil
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:07:00 +00:00
|
|
|
func (a *NetAPI) NetBandwidthStatsByPeer(ctx context.Context) (map[string]metrics.Stats, error) {
|
2021-06-24 14:02:51 +00:00
|
|
|
out := make(map[string]metrics.Stats)
|
|
|
|
for p, s := range a.Reporter.GetBandwidthByPeer() {
|
|
|
|
out[p.String()] = s
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:07:00 +00:00
|
|
|
func (a *NetAPI) NetBandwidthStatsByProtocol(ctx context.Context) (map[protocol.ID]metrics.Stats, error) {
|
2021-06-24 14:02:51 +00:00
|
|
|
return a.Reporter.GetBandwidthByProtocol(), nil
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:07:00 +00:00
|
|
|
var _ api.Net = &NetAPI{}
|