Example #1
0
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
}
Example #2
0
func (app *App) DialPeer(pub *peer.PublicKey) (PeerClient, error) {
	var addr string
	find := func(tx *db.Tx) error {
		p, err := tx.Peers().Get(pub)
		if err != nil {
			return err
		}
		a, err := p.Locations().Get()
		if err != nil {
			return err
		}
		addr = a
		return nil
	}
	if err := app.DB.View(find); err != nil {
		return nil, err
	}

	auth := &grpcedtls.Authenticator{
		Config:  app.GetTLSConfig,
		PeerPub: (*[ed25519.PublicKeySize]byte)(pub),
	}

	// TODO never delay here.
	// https://github.com/grpc/grpc-go/blob/8ce50750fe22e967aa8b1d308b21511844674b57/clientconn.go#L85
	conn, err := grpc.Dial(addr,
		grpc.WithTransportCredentials(auth),
		grpc.WithTimeout(30*time.Second),
	)
	if err != nil {
		return nil, err
	}
	client := wirepeer.NewPeerClient(conn)
	p := &peerClient{
		PeerClient: client,
		conn:       conn,
	}
	return p, nil
}