File: rpc.go

package info (click to toggle)
mirrorbits 0.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 984 kB
  • sloc: sh: 675; makefile: 93
file content (63 lines) | stat: -rw-r--r-- 1,433 bytes parent folder | download | duplicates (2)
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
// Copyright (c) 2014-2019 Ludovic Fauvet
// Licensed under the MIT license

package cli

import (
	"context"
	"fmt"
	"os"
	"strconv"

	"github.com/etix/mirrorbits/core"
	"github.com/etix/mirrorbits/rpc"
	"github.com/golang/protobuf/ptypes/empty"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

func (c *cli) GetRPC() rpc.CLIClient {
	c.Lock()
	defer c.Unlock()

	if c.rpcconn == nil {
		conn, err := grpc.Dial(core.RPCHost+":"+strconv.FormatUint(uint64(core.RPCPort), 10),
			grpc.WithInsecure(),
			grpc.WithBlock(),
			grpc.FailOnNonTempDialError(true),
			grpc.WithPerRPCCredentials(c.creds))
		if err != nil {
			fmt.Fprintf(os.Stderr, "rpc: %s\n", err)
			os.Exit(1)
		}
		c.rpcconn = conn
		client := rpc.NewCLIClient(c.rpcconn)
		_, err = client.Ping(context.Background(), &empty.Empty{})
		s := status.Convert(err)
		if s.Code() == codes.Unauthenticated {
			if len(c.creds.Password) == 0 {
				fmt.Fprintf(os.Stderr, "Please set the server password with the -P option.\n")
			} else {
				fmt.Fprintf(os.Stderr, "Password refused\n")
			}
			os.Exit(1)
		}
	}

	return rpc.NewCLIClient(c.rpcconn)
}

type loginCreds struct {
	Password string
}

func (c *loginCreds) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
	return map[string]string{
		"password": c.Password,
	}, nil
}

func (c *loginCreds) RequireTransportSecurity() bool {
	return false
}