forked from irinazheltisheva/powergate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
120 lines (104 loc) · 3.66 KB
/
Copy pathclient.go
File metadata and controls
120 lines (104 loc) · 3.66 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package client
import (
"context"
"crypto/tls"
"strings"
buildinfoRpc "github.com/textileio/powergate/buildinfo/rpc"
ffsRpc "github.com/textileio/powergate/ffs/rpc"
healthRpc "github.com/textileio/powergate/health/rpc"
askRpc "github.com/textileio/powergate/index/ask/rpc"
faultsRpc "github.com/textileio/powergate/index/faults/rpc"
minerRpc "github.com/textileio/powergate/index/miner/rpc"
netRpc "github.com/textileio/powergate/net/rpc"
reputationRpc "github.com/textileio/powergate/reputation/rpc"
walletRpc "github.com/textileio/powergate/wallet/rpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
// Client provides the client api.
type Client struct {
Asks *Asks
Miners *Miners
Faults *Faults
Wallet *Wallet
Reputation *Reputation
FFS *FFS
Health *Health
Net *Net
conn *grpc.ClientConn
buildInfoClient buildinfoRpc.RPCServiceClient
}
type ctxKey string
// AuthKey is the key that should be used to set the auth token in a Context.
const AuthKey = ctxKey("ffstoken")
// TokenAuth provides token based auth.
type TokenAuth struct {
Secure bool
}
// GetRequestMetadata returns request metadata that includes the auth token.
func (t TokenAuth) GetRequestMetadata(ctx context.Context, _ ...string) (map[string]string, error) {
md := map[string]string{}
token, ok := ctx.Value(AuthKey).(string)
if ok && token != "" {
md["X-ffs-Token"] = token
}
return md, nil
}
// RequireTransportSecurity specifies if the connection should be secure.
func (t TokenAuth) RequireTransportSecurity() bool {
return t.Secure
}
// CreateClientConn creates a gRPC connection with sensible defaults and the provided overrides.
func CreateClientConn(target string, optsOverrides ...grpc.DialOption) (*grpc.ClientConn, error) {
var creds credentials.TransportCredentials
if strings.Contains(target, "443") {
creds = credentials.NewTLS(&tls.Config{})
}
auth := TokenAuth{}
var opts []grpc.DialOption
if creds != nil {
opts = append(opts, grpc.WithTransportCredentials(creds))
auth.Secure = true
} else {
opts = append(opts, grpc.WithInsecure())
}
opts = append(opts, grpc.WithPerRPCCredentials(auth))
opts = append(opts, optsOverrides...)
conn, err := grpc.Dial(target, opts...)
if err != nil {
return nil, err
}
return conn, nil
}
// NewClient creates a client.
func NewClient(host string, optsOverrides ...grpc.DialOption) (*Client, error) {
conn, err := CreateClientConn(host, optsOverrides...)
if err != nil {
return nil, err
}
client := &Client{
Asks: &Asks{client: askRpc.NewRPCServiceClient(conn)},
Miners: &Miners{client: minerRpc.NewRPCServiceClient(conn)},
Faults: &Faults{client: faultsRpc.NewRPCServiceClient(conn)},
Wallet: &Wallet{client: walletRpc.NewRPCServiceClient(conn)},
Reputation: &Reputation{client: reputationRpc.NewRPCServiceClient(conn)},
FFS: &FFS{client: ffsRpc.NewRPCServiceClient(conn)},
Health: &Health{client: healthRpc.NewRPCServiceClient(conn)},
Net: &Net{client: netRpc.NewRPCServiceClient(conn)},
conn: conn,
buildInfoClient: buildinfoRpc.NewRPCServiceClient(conn),
}
return client, nil
}
// Host returns the client host address.
func (c *Client) Host() string {
return c.conn.Target()
}
// BuildInfo returns build info about the server.
func (c *Client) BuildInfo(ctx context.Context) (*buildinfoRpc.BuildInfoResponse, error) {
return c.buildInfoClient.BuildInfo(ctx, &buildinfoRpc.BuildInfoRequest{})
}
// Close closes the client's grpc connection and cancels any active requests.
func (c *Client) Close() error {
return c.conn.Close()
}