Skip to content

Commit

Permalink
coreapi swarm: rewire address listing cmds
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Sep 19, 2018
1 parent 6fa1289 commit 71d539b
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 51 deletions.
93 changes: 46 additions & 47 deletions core/commands/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
"gx/ipfs/QmYVqYJTVjetcf1guieEgWpK1PZtHPytP624vKzTF1P3r2/go-ipfs-config"
ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr"
inet "gx/ipfs/QmZNJyx9GGCX4GeuHnLB8fxaxMLs4MjTjHokxfQcCd6Nve/go-libp2p-net"
pstore "gx/ipfs/Qmda4cPRvSRyox3SqgJN6DfSZGU5TtHufPTp9uXjFj71X6/go-libp2p-peerstore"
"gx/ipfs/QmeDpqUwwdye8ABKVMPXKuWwPVURFdqTqssbTUB39E2Nwd/go-libp2p-swarm"
iaddr "gx/ipfs/QmePSRaGafvmURQwQkHPDBJsaGwKXC1WpBBHVCQxdr8FPn/go-ipfs-addr"
Expand Down Expand Up @@ -65,58 +66,57 @@ 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) {
n, err := cmdenv.GetNode(env)
api, err := cmdenv.GetApi(env)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

if n.PeerHost == nil {
res.SetError(ErrNotOnline, cmdkit.ErrClient)
return
}

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 {
res.SetError(err, cmdkit.ErrNormal)
return
}

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 {
res.SetError(err, cmdkit.ErrNormal)
return
}

if lat == 0 {
ci.Latency = "n/a"
} else {
ci.Latency = lat.String()
}
}
if verbose || streams {
strs := c.GetStreams()
strs, err := c.Streams(req.Context)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

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)
Expand Down Expand Up @@ -230,28 +230,27 @@ var swarmAddrsCmd = &cmds.Command{
"listen": swarmAddrsListenCmd,
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) {
n, err := cmdenv.GetNode(env)
api, err := cmdenv.GetApi(env)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

if n.PeerHost == nil {
res.SetError(ErrNotOnline, cmdkit.ErrClient)
addrs, err := api.Swarm().KnownAddrs(req.Context)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

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]))
}

cmds.EmitOnce(res, &addrMap{Addrs: addrs})
cmds.EmitOnce(res, &addrMap{Addrs: out})
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
Expand Down Expand Up @@ -291,25 +290,30 @@ var swarmAddrsLocalCmd = &cmds.Command{
cmdkit.BoolOption("id", "Show peer ID in addresses."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) {
n, err := cmdenv.GetNode(env)
api, err := cmdenv.GetApi(env)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

if n.PeerHost == nil {
res.SetError(ErrNotOnline, cmdkit.ErrClient)
showid, _ := req.Options["id"].(bool)
self, err := api.Key().Self(req.Context)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

showid, _ := req.Options["id"].(bool)
id := n.Identity.Pretty()
maddrs, err := api.Swarm().LocalAddrs(req.Context)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

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)
}
Expand All @@ -330,19 +334,14 @@ var swarmAddrsListenCmd = &cmds.Command{
`,
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) {
n, err := cmdenv.GetNode(env)
api, err := cmdenv.GetApi(env)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

if n.PeerHost == nil {
res.SetError(ErrNotOnline, cmdkit.ErrClient)
return
}

var addrs []string
maddrs, err := n.PeerHost.Network().InterfaceListenAddresses()
maddrs, err := api.Swarm().ListenAddrs(req.Context)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand Down
3 changes: 3 additions & 0 deletions core/coreapi/interface/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
10 changes: 9 additions & 1 deletion core/coreapi/interface/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (

"gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr"
net "gx/ipfs/QmZNJyx9GGCX4GeuHnLB8fxaxMLs4MjTjHokxfQcCd6Nve/go-libp2p-net"
"gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
pstore "gx/ipfs/Qmda4cPRvSRyox3SqgJN6DfSZGU5TtHufPTp9uXjFj71X6/go-libp2p-peerstore"
)

var (
ErrNotConnected = errors.New("not connected")
ErrConnNotFound = errors.New("conn not found")
)
)

// ConnectionInfo contains information about a peer
type ConnectionInfo interface {
Expand All @@ -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)

Expand All @@ -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)
}
4 changes: 4 additions & 0 deletions core/coreapi/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
46 changes: 44 additions & 2 deletions core/coreapi/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ package coreapi
import (
"context"
"fmt"
"sort"
"time"

coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"

peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr"
inet "gx/ipfs/QmZNJyx9GGCX4GeuHnLB8fxaxMLs4MjTjHokxfQcCd6Nve/go-libp2p-net"
net "gx/ipfs/QmZNJyx9GGCX4GeuHnLB8fxaxMLs4MjTjHokxfQcCd6Nve/go-libp2p-net"
protocol "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
pstore "gx/ipfs/Qmda4cPRvSRyox3SqgJN6DfSZGU5TtHufPTp9uXjFj71X6/go-libp2p-peerstore"
swarm "gx/ipfs/QmeDpqUwwdye8ABKVMPXKuWwPVURFdqTqssbTUB39E2Nwd/go-libp2p-swarm"
iaddr "gx/ipfs/QmePSRaGafvmURQwQkHPDBJsaGwKXC1WpBBHVCQxdr8FPn/go-ipfs-addr"
inet "gx/ipfs/QmZNJyx9GGCX4GeuHnLB8fxaxMLs4MjTjHokxfQcCd6Nve/go-libp2p-net"
protocol "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
)

type SwarmAPI struct {
Expand All @@ -24,6 +25,7 @@ type SwarmAPI struct {
type connInfo struct {
api *CoreAPI
conn net.Conn
dir net.Direction

addr ma.Multiaddr
peer peer.ID
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 4 additions & 1 deletion test/sharness/lib/test-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,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)
'
Expand Down

0 comments on commit 71d539b

Please sign in to comment.