From 48365f4f08b3f4126ec40cdfa8a83073cc16d0bc Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Wed, 10 May 2023 11:52:55 +0300 Subject: [PATCH] return all the public addresses in NatInfo --- api/api_net.go | 1 + cli/net.go | 3 +++ documentation/en/api-v0-methods-miner.md | 5 ++++- documentation/en/api-v0-methods.md | 5 ++++- documentation/en/api-v1-unstable-methods.md | 5 ++++- node/impl/net/net.go | 17 +++++++++++++++-- 6 files changed, 31 insertions(+), 5 deletions(-) diff --git a/api/api_net.go b/api/api_net.go index c1a8f8514..cfcd8d87e 100644 --- a/api/api_net.go +++ b/api/api_net.go @@ -73,4 +73,5 @@ type CommonNet interface { type NatInfo struct { Reachability network.Reachability + PublicAddrs []string } diff --git a/cli/net.go b/cli/net.go index 1266bb923..516b44b7f 100644 --- a/cli/net.go +++ b/cli/net.go @@ -445,6 +445,9 @@ var NetReachability = &cli.Command{ } fmt.Println("AutoNAT status: ", i.Reachability.String()) + if len(i.PublicAddrs) > 0 { + fmt.Println("Public address:", i.PublicAddrs) + } return nil }, } diff --git a/documentation/en/api-v0-methods-miner.md b/documentation/en/api-v0-methods-miner.md index 2be9e5891..997778069 100644 --- a/documentation/en/api-v0-methods-miner.md +++ b/documentation/en/api-v0-methods-miner.md @@ -1701,7 +1701,10 @@ Inputs: `null` Response: ```json { - "Reachability": 1 + "Reachability": 1, + "PublicAddrs": [ + "string value" + ] } ``` diff --git a/documentation/en/api-v0-methods.md b/documentation/en/api-v0-methods.md index 8c4db7f8f..522e1a6de 100644 --- a/documentation/en/api-v0-methods.md +++ b/documentation/en/api-v0-methods.md @@ -3709,7 +3709,10 @@ Inputs: `null` Response: ```json { - "Reachability": 1 + "Reachability": 1, + "PublicAddrs": [ + "string value" + ] } ``` diff --git a/documentation/en/api-v1-unstable-methods.md b/documentation/en/api-v1-unstable-methods.md index bc158ea11..305592c6e 100644 --- a/documentation/en/api-v1-unstable-methods.md +++ b/documentation/en/api-v1-unstable-methods.md @@ -5021,7 +5021,10 @@ Inputs: `null` Response: ```json { - "Reachability": 1 + "Reachability": 1, + "PublicAddrs": [ + "string value" + ] } ``` diff --git a/node/impl/net/net.go b/node/impl/net/net.go index 5a7382ecc..5341092ce 100644 --- a/node/impl/net/net.go +++ b/node/impl/net/net.go @@ -16,6 +16,7 @@ import ( "github.com/libp2p/go-libp2p/p2p/net/swarm" "github.com/libp2p/go-libp2p/p2p/protocol/ping" ma "github.com/multiformats/go-multiaddr" + manet "github.com/multiformats/go-multiaddr/net" "go.uber.org/fx" "golang.org/x/xerrors" @@ -134,7 +135,7 @@ func (a *NetAPI) NetFindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, err return a.Router.FindPeer(ctx, p) } -func (a *NetAPI) NetAutoNatStatus(ctx context.Context) (i api.NatInfo, err error) { +func (a *NetAPI) NetAutoNatStatus(context.Context) (i api.NatInfo, err error) { autonat := a.RawHost.(*basichost.BasicHost).GetAutoNat() if autonat == nil { @@ -143,7 +144,19 @@ func (a *NetAPI) NetAutoNatStatus(ctx context.Context) (i api.NatInfo, err error }, nil } - return api.NatInfo{Reachability: autonat.Status()}, nil + var addrs []string + if autonat.Status() == network.ReachabilityPublic { + for _, addr := range a.Host.Addrs() { + if manet.IsPublicAddr(addr) { + addrs = append(addrs, addr.String()) + } + } + } + + return api.NatInfo{ + Reachability: autonat.Status(), + PublicAddrs: addrs, + }, nil } func (a *NetAPI) NetAgentVersion(ctx context.Context, p peer.ID) (string, error) {