lotus/cli/net.go

126 lines
2.1 KiB
Go
Raw Permalink Normal View History

2019-07-08 19:07:16 +00:00
package cli
import (
"fmt"
"sort"
"strings"
2019-07-08 19:07:16 +00:00
"gopkg.in/urfave/cli.v2"
"github.com/filecoin-project/lotus/lib/addrutil"
2019-07-08 19:07:16 +00:00
)
var netCmd = &cli.Command{
Name: "net",
Usage: "Manage P2P Network",
Subcommands: []*cli.Command{
netPeers,
netConnect,
2019-07-08 21:01:15 +00:00
netListen,
2019-07-22 17:20:07 +00:00
netId,
2019-07-08 19:07:16 +00:00
},
}
var netPeers = &cli.Command{
Name: "peers",
Usage: "Print peers",
Action: func(cctx *cli.Context) error {
2019-10-03 18:12:30 +00:00
api, closer, err := GetAPI(cctx)
2019-07-10 17:28:49 +00:00
if err != nil {
return err
}
2019-10-03 18:12:30 +00:00
defer closer()
2019-07-18 23:16:23 +00:00
ctx := ReqContext(cctx)
2019-07-09 13:43:21 +00:00
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
})
2019-07-09 13:43:21 +00:00
for _, peer := range peers {
fmt.Println(peer)
}
2019-07-08 19:07:16 +00:00
return nil
},
}
2019-07-08 21:01:15 +00:00
var netListen = &cli.Command{
Name: "listen",
Usage: "List listen addresses",
Action: func(cctx *cli.Context) error {
2019-10-03 18:12:30 +00:00
api, closer, err := GetAPI(cctx)
2019-07-10 17:28:49 +00:00
if err != nil {
return err
}
2019-10-03 18:12:30 +00:00
defer closer()
2019-07-18 23:16:23 +00:00
ctx := ReqContext(cctx)
2019-07-08 21:01:15 +00:00
2019-07-09 13:43:21 +00:00
addrs, err := api.NetAddrsListen(ctx)
if err != nil {
return err
}
2019-07-09 17:03:36 +00:00
for _, peer := range addrs.Addrs {
fmt.Printf("%s/p2p/%s\n", peer, addrs.ID)
2019-07-09 13:43:21 +00:00
}
2019-07-08 21:01:15 +00:00
return nil
},
}
2019-07-08 19:07:16 +00:00
var netConnect = &cli.Command{
Name: "connect",
Usage: "Connect to a peer",
ArgsUsage: "<peer multiaddr>",
Action: func(cctx *cli.Context) error {
2019-10-03 18:12:30 +00:00
api, closer, err := GetAPI(cctx)
2019-07-10 17:28:49 +00:00
if err != nil {
return err
}
2019-10-03 18:12:30 +00:00
defer closer()
2019-07-18 23:16:23 +00:00
ctx := ReqContext(cctx)
pis, err := addrutil.ParseAddresses(ctx, cctx.Args().Slice())
2019-07-08 19:07:16 +00:00
if err != nil {
return err
}
for _, pi := range pis {
fmt.Printf("connect %s: ", pi.ID.Pretty())
err := api.NetConnect(ctx, pi)
2019-07-08 19:07:16 +00:00
if err != nil {
fmt.Println("failure")
return err
}
fmt.Println("success")
}
return nil
},
}
2019-07-22 17:20:07 +00:00
var netId = &cli.Command{
Name: "id",
Usage: "Get node identity",
Action: func(cctx *cli.Context) error {
2019-10-03 18:12:30 +00:00
api, closer, err := GetAPI(cctx)
2019-07-22 17:20:07 +00:00
if err != nil {
return err
}
2019-10-03 18:12:30 +00:00
defer closer()
2019-07-22 17:20:07 +00:00
2019-07-18 23:16:23 +00:00
ctx := ReqContext(cctx)
2019-07-22 17:20:07 +00:00
pid, err := api.ID(ctx)
if err != nil {
return err
}
fmt.Println(pid)
return nil
},
}