return all the public addresses in NatInfo

This commit is contained in:
Marten Seemann 2023-05-10 11:52:55 +03:00 committed by jennijuju
parent 80d39f6616
commit 48365f4f08
6 changed files with 31 additions and 5 deletions

View File

@ -73,4 +73,5 @@ type CommonNet interface {
type NatInfo struct {
Reachability network.Reachability
PublicAddrs []string
}

View File

@ -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
},
}

View File

@ -1701,7 +1701,10 @@ Inputs: `null`
Response:
```json
{
"Reachability": 1
"Reachability": 1,
"PublicAddrs": [
"string value"
]
}
```

View File

@ -3709,7 +3709,10 @@ Inputs: `null`
Response:
```json
{
"Reachability": 1
"Reachability": 1,
"PublicAddrs": [
"string value"
]
}
```

View File

@ -5021,7 +5021,10 @@ Inputs: `null`
Response:
```json
{
"Reachability": 1
"Reachability": 1,
"PublicAddrs": [
"string value"
]
}
```

View File

@ -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) {