2019-07-08 19:07:16 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2020-07-31 08:27:22 +00:00
|
|
|
"encoding/json"
|
2019-07-08 19:07:16 +00:00
|
|
|
"fmt"
|
2020-07-31 08:27:22 +00:00
|
|
|
"os"
|
2019-12-11 20:45:19 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2019-07-08 19:07:16 +00:00
|
|
|
|
2020-03-03 04:55:25 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
|
2020-06-02 18:12:53 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2019-10-09 03:16:35 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"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{
|
2020-08-04 18:57:40 +00:00
|
|
|
NetPeers,
|
2019-07-08 19:07:16 +00:00
|
|
|
netConnect,
|
2020-08-04 18:57:40 +00:00
|
|
|
NetListen,
|
|
|
|
NetId,
|
2020-02-18 19:09:00 +00:00
|
|
|
netFindPeer,
|
2020-06-03 01:13:49 +00:00
|
|
|
netScores,
|
2020-08-13 11:44:03 +00:00
|
|
|
NetReachability,
|
2019-07-08 19:07:16 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-04 18:57:40 +00:00
|
|
|
var NetPeers = &cli.Command{
|
2019-07-08 19:07:16 +00:00
|
|
|
Name: "peers",
|
|
|
|
Usage: "Print peers",
|
2019-07-09 12:19:10 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-11 20:45:19 +00:00
|
|
|
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 {
|
2020-06-03 01:13:49 +00:00
|
|
|
fmt.Printf("%s, %s\n", peer.ID, peer.Addrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var netScores = &cli.Command{
|
|
|
|
Name: "scores",
|
|
|
|
Usage: "Print peers' pubsub scores",
|
2020-07-31 08:27:22 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "extended",
|
|
|
|
Usage: "print extended peer scores in json",
|
|
|
|
},
|
|
|
|
},
|
2020-06-03 01:13:49 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := GetAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
scores, err := api.NetPubsubScores(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-31 08:27:22 +00:00
|
|
|
if cctx.Bool("extended") {
|
|
|
|
enc := json.NewEncoder(os.Stdout)
|
|
|
|
for _, peer := range scores {
|
|
|
|
err := enc.Encode(peer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, peer := range scores {
|
|
|
|
fmt.Printf("%s, %f\n", peer.ID, peer.Score.Score)
|
|
|
|
}
|
2019-07-09 13:43:21 +00:00
|
|
|
}
|
2019-07-08 19:07:16 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-04 18:57:40 +00:00
|
|
|
var NetListen = &cli.Command{
|
2019-07-08 21:01:15 +00:00
|
|
|
Name: "listen",
|
|
|
|
Usage: "List listen addresses",
|
2019-07-09 12:19:10 +00:00
|
|
|
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{
|
2019-10-09 20:02:08 +00:00
|
|
|
Name: "connect",
|
|
|
|
Usage: "Connect to a peer",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[peerMultiaddr]",
|
2019-07-09 12:19:10 +00:00
|
|
|
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 12:19:10 +00:00
|
|
|
|
2019-10-09 03:16:35 +00:00
|
|
|
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 {
|
2019-07-09 12:19:10 +00:00
|
|
|
fmt.Printf("connect %s: ", pi.ID.Pretty())
|
2019-07-09 12:25:33 +00:00
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-04 18:57:40 +00:00
|
|
|
var NetId = &cli.Command{
|
2019-07-22 17:20:07 +00:00
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
2020-02-18 19:09:00 +00:00
|
|
|
|
|
|
|
var netFindPeer = &cli.Command{
|
|
|
|
Name: "findpeer",
|
|
|
|
Usage: "Find the addresses of a given peerID",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[peerId]",
|
2020-02-18 19:09:00 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
if cctx.NArg() != 1 {
|
|
|
|
fmt.Println("Usage: findpeer [peer ID]")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pid, err := peer.IDB58Decode(cctx.Args().First())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
api, closer, err := GetAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
addrs, err := api.NetFindPeer(ctx, pid)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(addrs)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2020-08-13 11:18:14 +00:00
|
|
|
|
2020-08-13 11:44:03 +00:00
|
|
|
var NetReachability = &cli.Command{
|
2020-08-13 11:44:43 +00:00
|
|
|
Name: "reachability",
|
|
|
|
Usage: "Print information about reachability from the internet",
|
2020-08-13 11:18:14 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := GetAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
i, err := api.NetAutoNatStatus(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("AutoNAT status: ", i.Reachability.String())
|
2020-08-18 01:12:50 +00:00
|
|
|
if i.PublicAddr != "" {
|
|
|
|
fmt.Println("Public address: ", i.PublicAddr)
|
2020-08-13 11:18:14 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|