lotus/cli/net.go
Jakub Sztandera 9fa46dbac3
Stable net peers output
License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
2019-12-11 21:45:19 +01:00

126 lines
2.1 KiB
Go

package cli
import (
"fmt"
"sort"
"strings"
"gopkg.in/urfave/cli.v2"
"github.com/filecoin-project/lotus/lib/addrutil"
)
var netCmd = &cli.Command{
Name: "net",
Usage: "Manage P2P Network",
Subcommands: []*cli.Command{
netPeers,
netConnect,
netListen,
netId,
},
}
var netPeers = &cli.Command{
Name: "peers",
Usage: "Print peers",
Action: func(cctx *cli.Context) error {
api, closer, err := GetAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
peers, err := api.NetPeers(ctx)
if err != nil {
return err
}
sort.Slice(peers, func(i, j int) bool {
return strings.Compare(string(peers[i].ID), string(peers[j].ID)) > 0
})
for _, peer := range peers {
fmt.Println(peer)
}
return nil
},
}
var netListen = &cli.Command{
Name: "listen",
Usage: "List listen addresses",
Action: func(cctx *cli.Context) error {
api, closer, err := GetAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
addrs, err := api.NetAddrsListen(ctx)
if err != nil {
return err
}
for _, peer := range addrs.Addrs {
fmt.Printf("%s/p2p/%s\n", peer, addrs.ID)
}
return nil
},
}
var netConnect = &cli.Command{
Name: "connect",
Usage: "Connect to a peer",
ArgsUsage: "<peer multiaddr>",
Action: func(cctx *cli.Context) error {
api, closer, err := GetAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
pis, err := addrutil.ParseAddresses(ctx, cctx.Args().Slice())
if err != nil {
return err
}
for _, pi := range pis {
fmt.Printf("connect %s: ", pi.ID.Pretty())
err := api.NetConnect(ctx, pi)
if err != nil {
fmt.Println("failure")
return err
}
fmt.Println("success")
}
return nil
},
}
var netId = &cli.Command{
Name: "id",
Usage: "Get node identity",
Action: func(cctx *cli.Context) error {
api, closer, err := GetAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
pid, err := api.ID(ctx)
if err != nil {
return err
}
fmt.Println(pid)
return nil
},
}