/
ping.go
60 lines (52 loc) · 1.21 KB
/
ping.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
package ping
import (
"fmt"
"log"
clibazil "bazil.org/bazil/cli"
"bazil.org/bazil/cliutil/subcommands"
"bazil.org/bazil/peer"
"bazil.org/bazil/peer/wire"
"bazil.org/bazil/server"
"bazil.org/bazil/util/grpcedtls"
"github.com/agl/ed25519"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
type pingCommand struct {
subcommands.Description
Arguments struct {
Addr string `positional:"metavar=HOST:PORT"`
Pub peer.PublicKey
}
}
func (c *pingCommand) Run() error {
app, err := server.New(clibazil.Bazil.Config.DataDir.String())
if err != nil {
return err
}
defer app.Close()
pub := (*[ed25519.PublicKeySize]byte)(&c.Arguments.Pub)
auth := &grpcedtls.Authenticator{
Config: app.GetTLSConfig,
PeerPub: pub,
}
addr := c.Arguments.Addr
conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(auth))
if err != nil {
return fmt.Errorf("did not connect: %v", err)
}
defer conn.Close()
client := wire.NewPeerClient(conn)
r, err := client.Ping(context.Background(), &wire.PingRequest{})
if err != nil {
return fmt.Errorf("could not greet: %v", err)
}
log.Printf("pong: %#v", r)
return nil
}
var ping = pingCommand{
Description: "ping a peer",
}
func init() {
subcommands.Register(&ping)
}