diff --git a/cli/net.go b/cli/net.go index 5248a141015..7768bb7eb07 100644 --- a/cli/net.go +++ b/cli/net.go @@ -150,7 +150,7 @@ var NetPing = &cli.Command{ ctx := ReqContext(cctx) - pis, err := addrInfoFromArg(ctx, cctx) + pis, err := AddrInfoFromArg(ctx, cctx) if err != nil { return err } @@ -271,7 +271,7 @@ var NetConnect = &cli.Command{ defer closer() ctx := ReqContext(cctx) - pis, err := addrInfoFromArg(ctx, cctx) + pis, err := AddrInfoFromArg(ctx, cctx) if err != nil { return err } @@ -290,7 +290,7 @@ var NetConnect = &cli.Command{ }, } -func addrInfoFromArg(ctx context.Context, cctx *cli.Context) ([]peer.AddrInfo, error) { +func AddrInfoFromArg(ctx context.Context, cctx *cli.Context) ([]peer.AddrInfo, error) { pis, err := addrutil.ParseAddresses(ctx, cctx.Args().Slice()) if err != nil { a, perr := address.NewFromString(cctx.Args().First()) diff --git a/cmd/lotus-shed/hello.go b/cmd/lotus-shed/hello.go new file mode 100644 index 00000000000..e3a3756a9ba --- /dev/null +++ b/cmd/lotus-shed/hello.go @@ -0,0 +1,63 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "time" + + cborutil "github.com/filecoin-project/go-cbor-util" + lcli "github.com/filecoin-project/lotus/cli" + "github.com/filecoin-project/lotus/node/hello" + "github.com/libp2p/go-libp2p" + inet "github.com/libp2p/go-libp2p-core/network" + "github.com/urfave/cli/v2" +) + +var resultCh chan bool + +var helloCmd = &cli.Command{ + Name: "hello", + Description: "Get remote peer hello message by multiaddr", + ArgsUsage: "[peerMultiaddr|minerActorAddress]", + Action: func(cctx *cli.Context) error { + ctx := lcli.ReqContext(cctx) + resultCh = make(chan bool, 1) + pis, err := lcli.AddrInfoFromArg(ctx, cctx) + if err != nil { + return err + } + h, err := libp2p.New() + if err != nil { + return err + } + h.SetStreamHandler(hello.ProtocolID, HandleStream) + err = h.Connect(ctx, pis[0]) + if err != nil { + return err + } + ctx, done := context.WithTimeout(ctx, 5*time.Second) + defer done() + select { + case <-resultCh: + case <-ctx.Done(): + fmt.Println("can't get hello message, please try again") + } + return nil + }, +} + +func HandleStream(s inet.Stream) { + var hmsg hello.HelloMessage + if err := cborutil.ReadCborRPC(s, &hmsg); err != nil { + log.Infow("failed to read hello message, disconnecting", "error", err) + _ = s.Conn().Close() + return + } + data, err := json.Marshal(hmsg) + if err != nil { + return + } + fmt.Println(string(data)) + resultCh <- true +} diff --git a/cmd/lotus-shed/main.go b/cmd/lotus-shed/main.go index cfbc73a1487..f99454e0a46 100644 --- a/cmd/lotus-shed/main.go +++ b/cmd/lotus-shed/main.go @@ -39,6 +39,7 @@ func main() { marketCmd, miscCmd, mpoolCmd, + helloCmd, genesisVerifyCmd, mathCmd, minerCmd,