-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
hello.go
65 lines (58 loc) · 1.39 KB
/
hello.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/libp2p/go-libp2p"
inet "github.com/libp2p/go-libp2p/core/network"
"github.com/urfave/cli/v2"
cborutil "github.com/filecoin-project/go-cbor-util"
lcli "github.com/filecoin-project/lotus/cli"
"github.com/filecoin-project/lotus/node/hello"
)
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
}