Commit 6fa2ab0d authored by Łukasz Magiera's avatar Łukasz Magiera

coreapi swarm: rewire address listing cmds

License: MIT
Signed-off-by: default avatarŁukasz Magiera <magik6k@gmail.com>
parent df9f1018
......@@ -21,7 +21,7 @@ import (
"gx/ipfs/QmVBUpxsHh53rNcufqxMpLAmz37eGyLJUaexDy1W9YkiNk/go-ipfs-config"
"gx/ipfs/QmXTmUCBtDUrzDYVzASogLiNph7EBuYqEgPL7QoHNMzUnz/go-ipfs-cmds"
ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr"
"gx/ipfs/QmbNepETomvmXfz1X5pHNFD2QuPqnqi47dTd94QJWSorQ3/go-libp2p-peer"
peer "gx/ipfs/QmbNepETomvmXfz1X5pHNFD2QuPqnqi47dTd94QJWSorQ3/go-libp2p-peer"
pstore "gx/ipfs/QmfAQMFpgDU2U4BXG64qVr8HSiictfWvkSBz7Y2oDj65st/go-libp2p-peerstore"
inet "gx/ipfs/QmfDPh144WGBqRxZb1TGDHerbMnZATrHZggAPw7putNnBq/go-libp2p-net"
)
......@@ -66,45 +66,39 @@ var swarmPeersCmd = &cmds.Command{
cmdkit.BoolOption("direction", "Also list information about the direction of connection"),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
n, err := cmdenv.GetNode(env)
api, err := cmdenv.GetApi(env)
if err != nil {
return err
}
if n.PeerHost == nil {
return err
}
verbose, _ := req.Options["verbose"].(bool)
latency, _ := req.Options["latency"].(bool)
streams, _ := req.Options["streams"].(bool)
direction, _ := req.Options["direction"].(bool)
conns := n.PeerHost.Network().Conns()
conns, err := api.Swarm().Peers(req.Context)
if err != nil {
return err
}
var out connInfos
for _, c := range conns {
pid := c.RemotePeer()
addr := c.RemoteMultiaddr()
ci := connInfo{
Addr: addr.String(),
Peer: pid.Pretty(),
Addr: c.Address().String(),
Peer: c.ID().Pretty(),
}
/*
// FIXME(steb):
swcon, ok := c.(*swarm.Conn)
if ok {
ci.Muxer = fmt.Sprintf("%T", swcon.StreamConn().Conn())
}
*/
if verbose || direction {
// set direction
ci.Direction = c.Stat().Direction
ci.Direction = c.Direction()
}
if verbose || latency {
lat := n.Peerstore.LatencyEWMA(pid)
lat, err := c.Latency(req.Context)
if err != nil {
return err
}
if lat == 0 {
ci.Latency = "n/a"
} else {
......@@ -112,10 +106,13 @@ var swarmPeersCmd = &cmds.Command{
}
}
if verbose || streams {
strs := c.GetStreams()
strs, err := c.Streams(req.Context)
if err != nil {
return err
}
for _, s := range strs {
ci.Streams = append(ci.Streams, streamInfo{Protocol: string(s.Protocol())})
ci.Streams = append(ci.Streams, streamInfo{Protocol: string(s)})
}
}
sort.Sort(&ci)
......@@ -229,26 +226,25 @@ var swarmAddrsCmd = &cmds.Command{
"listen": swarmAddrsListenCmd,
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
n, err := cmdenv.GetNode(env)
api, err := cmdenv.GetApi(env)
if err != nil {
return err
}
if n.PeerHost == nil {
addrs, err := api.Swarm().KnownAddrs(req.Context)
if err != nil {
return err
}
addrs := make(map[string][]string)
ps := n.PeerHost.Network().Peerstore()
for _, p := range ps.Peers() {
out := make(map[string][]string)
for p, paddrs := range addrs {
s := p.Pretty()
for _, a := range ps.Addrs(p) {
addrs[s] = append(addrs[s], a.String())
for _, a := range paddrs {
out[s] = append(out[s], a.String())
}
sort.Sort(sort.StringSlice(addrs[s]))
}
return cmds.EmitOnce(res, &addrMap{Addrs: addrs})
return cmds.EmitOnce(res, &addrMap{Addrs: out})
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
......@@ -288,23 +284,27 @@ var swarmAddrsLocalCmd = &cmds.Command{
cmdkit.BoolOption("id", "Show peer ID in addresses."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
n, err := cmdenv.GetNode(env)
api, err := cmdenv.GetApi(env)
if err != nil {
return err
}
if n.PeerHost == nil {
showid, _ := req.Options["id"].(bool)
self, err := api.Key().Self(req.Context)
if err != nil {
return err
}
showid, _ := req.Options["id"].(bool)
id := n.Identity.Pretty()
maddrs, err := api.Swarm().LocalAddrs(req.Context)
if err != nil {
return err
}
var addrs []string
for _, addr := range n.PeerHost.Addrs() {
for _, addr := range maddrs {
saddr := addr.String()
if showid {
saddr = path.Join(saddr, "ipfs", id)
saddr = path.Join(saddr, "ipfs", self.ID().Pretty())
}
addrs = append(addrs, saddr)
}
......@@ -325,17 +325,13 @@ var swarmAddrsListenCmd = &cmds.Command{
`,
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
n, err := cmdenv.GetNode(env)
api, err := cmdenv.GetApi(env)
if err != nil {
return err
}
if n.PeerHost == nil {
return err
}
var addrs []string
maddrs, err := n.PeerHost.Network().InterfaceListenAddresses()
maddrs, err := api.Swarm().ListenAddrs(req.Context)
if err != nil {
return err
}
......
......@@ -33,6 +33,9 @@ type KeyAPI interface {
// List lists keys stored in keystore
List(ctx context.Context) ([]Key, error)
// Self returns the 'main' node key
Self(ctx context.Context) (Key, error)
// Remove removes keys from keystore. Returns ipns path of the removed key
Remove(ctx context.Context, name string) (Key, error)
}
......@@ -9,12 +9,13 @@ import (
"gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
"gx/ipfs/QmbNepETomvmXfz1X5pHNFD2QuPqnqi47dTd94QJWSorQ3/go-libp2p-peer"
pstore "gx/ipfs/QmfAQMFpgDU2U4BXG64qVr8HSiictfWvkSBz7Y2oDj65st/go-libp2p-peerstore"
net "gx/ipfs/QmfDPh144WGBqRxZb1TGDHerbMnZATrHZggAPw7putNnBq/go-libp2p-net"
)
var (
ErrNotConnected = errors.New("not connected")
ErrConnNotFound = errors.New("conn not found")
)
)
// ConnectionInfo contains information about a peer
type ConnectionInfo interface {
......@@ -24,6 +25,9 @@ type ConnectionInfo interface {
// Address returns the multiaddress via which we are connected with the peer
Address() ma.Multiaddr
// Direction returns which way the connection was established
Direction() net.Direction
// Latency returns last known round trip time to the peer
Latency(context.Context) (time.Duration, error)
......@@ -41,4 +45,8 @@ type SwarmAPI interface {
// Peers returns the list of peers we are connected to
Peers(context.Context) ([]ConnectionInfo, error)
KnownAddrs(context.Context) (map[peer.ID][]ma.Multiaddr, error)
LocalAddrs(context.Context) ([]ma.Multiaddr, error)
ListenAddrs(context.Context) ([]ma.Multiaddr, error)
}
......@@ -216,3 +216,7 @@ func (api *KeyAPI) Remove(ctx context.Context, name string) (coreiface.Key, erro
return &key{"", pid}, nil
}
func (api *KeyAPI) Self(ctx context.Context) (coreiface.Key, error) {
return &key{"self", api.node.Identity}, nil
}
......@@ -3,6 +3,7 @@ package coreapi
import (
"context"
"fmt"
"sort"
"time"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
......@@ -24,6 +25,7 @@ type SwarmAPI struct {
type connInfo struct {
api *CoreAPI
conn net.Conn
dir net.Direction
addr ma.Multiaddr
peer peer.ID
......@@ -80,6 +82,41 @@ func (api *SwarmAPI) Disconnect(ctx context.Context, addr ma.Multiaddr) error {
return nil
}
func (api *SwarmAPI) KnownAddrs(context.Context) (map[peer.ID][]ma.Multiaddr, error) {
if api.node.PeerHost == nil {
return nil, coreiface.ErrOffline
}
addrs := make(map[peer.ID][]ma.Multiaddr)
ps := api.node.PeerHost.Network().Peerstore()
for _, p := range ps.Peers() {
for _, a := range ps.Addrs(p) {
addrs[p] = append(addrs[p], a)
}
sort.Slice(addrs[p], func(i, j int) bool {
return addrs[p][i].String() < addrs[p][j].String()
})
}
return addrs, nil
}
func (api *SwarmAPI) LocalAddrs(context.Context) ([]ma.Multiaddr, error) {
if api.node.PeerHost == nil {
return nil, coreiface.ErrOffline
}
return api.node.PeerHost.Addrs(), nil
}
func (api *SwarmAPI) ListenAddrs(context.Context) ([]ma.Multiaddr, error) {
if api.node.PeerHost == nil {
return nil, coreiface.ErrOffline
}
return api.node.PeerHost.Network().InterfaceListenAddresses()
}
func (api *SwarmAPI) Peers(context.Context) ([]coreiface.ConnectionInfo, error) {
if api.node.PeerHost == nil {
return nil, coreiface.ErrOffline
......@@ -95,6 +132,7 @@ func (api *SwarmAPI) Peers(context.Context) ([]coreiface.ConnectionInfo, error)
ci := &connInfo{
api: api.CoreAPI,
conn: c,
dir: c.Stat().Direction,
addr: addr,
peer: pid,
......@@ -122,6 +160,10 @@ func (ci *connInfo) Address() ma.Multiaddr {
return ci.addr
}
func (ci *connInfo) Direction() net.Direction {
return ci.dir
}
func (ci *connInfo) Latency(context.Context) (time.Duration, error) {
return ci.api.node.Peerstore.LatencyEWMA(peer.ID(ci.ID())), nil
}
......
......@@ -199,8 +199,11 @@ test_set_address_vars() {
'
if ipfs swarm addrs local >/dev/null 2>&1; then
test_expect_success "get swarm addresses" '
ipfs swarm addrs local > addrs_out
'
test_expect_success "set swarm address vars" '
ipfs swarm addrs local > addrs_out &&
SWARM_MADDR=$(grep "127.0.0.1" addrs_out) &&
SWARM_PORT=$(port_from_maddr $SWARM_MADDR)
'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment