Merge pull request #3029 from filecoin-project/feat/net-reachability

cli: net reachability command
This commit is contained in:
Łukasz Magiera 2020-08-13 13:45:19 +02:00 committed by GitHub
commit 0cb58311c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 74 additions and 17 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
"github.com/filecoin-project/go-jsonrpc/auth" "github.com/filecoin-project/go-jsonrpc/auth"
@ -28,6 +29,7 @@ type Common interface {
NetDisconnect(context.Context, peer.ID) error NetDisconnect(context.Context, peer.ID) error
NetFindPeer(context.Context, peer.ID) (peer.AddrInfo, error) NetFindPeer(context.Context, peer.ID) (peer.AddrInfo, error)
NetPubsubScores(context.Context) ([]PubsubScore, error) NetPubsubScores(context.Context) ([]PubsubScore, error)
NetAutoNatStatus(context.Context) (NatInfo, error)
// MethodGroup: Common // MethodGroup: Common
@ -65,3 +67,8 @@ type Version struct {
func (v Version) String() string { func (v Version) String() string {
return fmt.Sprintf("%s+api%s", v.Version, v.APIVersion.String()) return fmt.Sprintf("%s+api%s", v.Version, v.APIVersion.String())
} }
type NatInfo struct {
Reachability network.Reachability
PublicAddr ma.Multiaddr
}

View File

@ -49,6 +49,7 @@ type CommonStruct struct {
NetDisconnect func(context.Context, peer.ID) error `perm:"write"` NetDisconnect func(context.Context, peer.ID) error `perm:"write"`
NetFindPeer func(context.Context, peer.ID) (peer.AddrInfo, error) `perm:"read"` NetFindPeer func(context.Context, peer.ID) (peer.AddrInfo, error) `perm:"read"`
NetPubsubScores func(context.Context) ([]api.PubsubScore, error) `perm:"read"` NetPubsubScores func(context.Context) ([]api.PubsubScore, error) `perm:"read"`
NetAutoNatStatus func(context.Context) (api.NatInfo, error) `perm:"read"`
ID func(context.Context) (peer.ID, error) `perm:"read"` ID func(context.Context) (peer.ID, error) `perm:"read"`
Version func(context.Context) (api.Version, error) `perm:"read"` Version func(context.Context) (api.Version, error) `perm:"read"`
@ -331,6 +332,7 @@ func (c *CommonStruct) AuthNew(ctx context.Context, perms []auth.Permission) ([]
func (c *CommonStruct) NetPubsubScores(ctx context.Context) ([]api.PubsubScore, error) { func (c *CommonStruct) NetPubsubScores(ctx context.Context) ([]api.PubsubScore, error) {
return c.Internal.NetPubsubScores(ctx) return c.Internal.NetPubsubScores(ctx)
} }
func (c *CommonStruct) NetConnectedness(ctx context.Context, pid peer.ID) (network.Connectedness, error) { func (c *CommonStruct) NetConnectedness(ctx context.Context, pid peer.ID) (network.Connectedness, error) {
return c.Internal.NetConnectedness(ctx, pid) return c.Internal.NetConnectedness(ctx, pid)
} }
@ -355,6 +357,10 @@ func (c *CommonStruct) NetFindPeer(ctx context.Context, p peer.ID) (peer.AddrInf
return c.Internal.NetFindPeer(ctx, p) return c.Internal.NetFindPeer(ctx, p)
} }
func (c *CommonStruct) NetAutoNatStatus(ctx context.Context) (api.NatInfo, error) {
return c.Internal.NetAutoNatStatus(ctx)
}
// ID implements API.ID // ID implements API.ID
func (c *CommonStruct) ID(ctx context.Context) (peer.ID, error) { func (c *CommonStruct) ID(ctx context.Context) (peer.ID, error) {
return c.Internal.ID(ctx) return c.Internal.ID(ctx)

View File

@ -101,7 +101,6 @@ func DiffAdtMap(preMap, curMap *adt.Map, out AdtMapDiff) error {
} }
} }
return curMap.Delete(k) return curMap.Delete(k)
}); err != nil { }); err != nil {
return err return err

View File

@ -71,7 +71,6 @@ func TestDiffAdtArray(t *testing.T) {
assert.EqualValues(t, []byte{1}, changes.Removed[1].val) assert.EqualValues(t, []byte{1}, changes.Removed[1].val)
} }
func TestDiffAdtMap(t *testing.T) { func TestDiffAdtMap(t *testing.T) {
ctxstoreA := newContextStore() ctxstoreA := newContextStore()
ctxstoreB := newContextStore() ctxstoreB := newContextStore()
@ -135,7 +134,6 @@ type TestDiffMap struct {
var _ AdtMapDiff = &TestDiffMap{} var _ AdtMapDiff = &TestDiffMap{}
func (t *TestDiffMap) AsKey(key string) (adt.Keyer, error) { func (t *TestDiffMap) AsKey(key string) (adt.Keyer, error) {
k, err := adt.ParseUIntKey(key) k, err := adt.ParseUIntKey(key)
if err != nil { if err != nil {

View File

@ -661,8 +661,6 @@ type AddressChange struct {
type DiffInitActorStateFunc func(ctx context.Context, oldState *init_.State, newState *init_.State) (changed bool, user UserData, err error) type DiffInitActorStateFunc func(ctx context.Context, oldState *init_.State, newState *init_.State) (changed bool, user UserData, err error)
func (i *InitActorAddressChanges) AsKey(key string) (adt.Keyer, error) { func (i *InitActorAddressChanges) AsKey(key string) (adt.Keyer, error) {
addr, err := address.NewFromBytes([]byte(key)) addr, err := address.NewFromBytes([]byte(key))
if err != nil { if err != nil {
@ -786,4 +784,3 @@ func (sp *StatePredicates) OnAddressMapChange() DiffInitActorStateFunc {
return true, addressChanges, nil return true, addressChanges, nil
} }
} }

View File

@ -24,6 +24,7 @@ var netCmd = &cli.Command{
NetId, NetId,
netFindPeer, netFindPeer,
netScores, netScores,
NetReachability,
}, },
} }
@ -202,3 +203,28 @@ var netFindPeer = &cli.Command{
return nil return nil
}, },
} }
var NetReachability = &cli.Command{
Name: "reachability",
Usage: "Print information about reachability from the internet",
Action: func(cctx *cli.Context) error {
api, closer, err := GetAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
i, err := api.NetAutoNatStatus(ctx)
if err != nil {
return err
}
fmt.Println("AutoNAT status: ", i.Reachability.String())
if i.PublicAddr != nil {
fmt.Println("Public address: ", i.PublicAddr.String())
}
return nil
},
}

View File

@ -63,6 +63,11 @@ var infoAllCmd = &cli.Command{
return err return err
} }
fmt.Println("\n#: Reachability")
if err := lcli.NetReachability.Action(cctx); err != nil {
return err
}
// Very Verbose info // Very Verbose info
fmt.Println("\n#: Peers") fmt.Println("\n#: Peers")
if err := lcli.NetPeers.Action(cctx); err != nil { if err := lcli.NetPeers.Action(cctx); err != nil {

View File

@ -12,6 +12,7 @@ import (
"github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
swarm "github.com/libp2p/go-libp2p-swarm" swarm "github.com/libp2p/go-libp2p-swarm"
basichost "github.com/libp2p/go-libp2p/p2p/host/basic"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
"go.uber.org/fx" "go.uber.org/fx"
"golang.org/x/xerrors" "golang.org/x/xerrors"
@ -28,6 +29,7 @@ type CommonAPI struct {
fx.In fx.In
APISecret *dtypes.APIAlg APISecret *dtypes.APIAlg
RawHost lp2p.RawHost
Host host.Host Host host.Host
Router lp2p.BaseIpfsRouting Router lp2p.BaseIpfsRouting
Sk *dtypes.ScoreKeeper Sk *dtypes.ScoreKeeper
@ -113,6 +115,23 @@ func (a *CommonAPI) NetFindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo,
return a.Router.FindPeer(ctx, p) return a.Router.FindPeer(ctx, p)
} }
func (a *CommonAPI) NetAutoNatStatus(ctx context.Context) (i api.NatInfo, err error) {
autonat := a.RawHost.(*basichost.BasicHost).AutoNat
var maddr ma.Multiaddr
if autonat.Status() == network.ReachabilityPublic {
maddr, err = autonat.PublicAddr()
if err != nil {
return api.NatInfo{}, err
}
}
return api.NatInfo{
Reachability: autonat.Status(),
PublicAddr: maddr,
}, nil
}
func (a *CommonAPI) ID(context.Context) (peer.ID, error) { func (a *CommonAPI) ID(context.Context) (peer.ID, error) {
return a.Host.ID(), nil return a.Host.ID(), nil
} }