forked from capossele/drand
/
client_rest.go
132 lines (119 loc) · 3.13 KB
/
client_rest.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
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
121
122
123
124
125
126
127
128
129
130
131
132
package net
import (
"bytes"
"crypto/tls"
"fmt"
"io/ioutil"
"net"
"net/http"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/dedis/drand/protobuf/drand"
)
type restClient struct {
marshaller runtime.Marshaler
manager *CertManager
}
// NewRestClient returns a client capable of calling external public method on
// drand nodes using the RESP API
func NewRestClient() ExternalClient {
return &restClient{
marshaller: defaultJSONMarshaller,
manager: NewCertManager(),
}
}
// NewRestClientFromCertManager returns a Rest Client with the given cert
// manager
func NewRestClientFromCertManager(c *CertManager) ExternalClient {
client := NewRestClient().(*restClient)
client.manager = c
return client
}
func (r *restClient) Public(p Peer, in *drand.PublicRandRequest) (*drand.PublicRandResponse, error) {
base := restAddr(p)
var req *http.Request
var err error
basePath := base + "/api/public"
if in.GetRound() == 0 {
// then simple GET method
req, err = http.NewRequest("GET", basePath, nil)
} else {
buff, err := r.marshaller.Marshal(in)
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s/%d", basePath, in.GetRound())
req, err = http.NewRequest("GET", url, bytes.NewBuffer(buff))
}
if err != nil {
return nil, err
}
respBody, err := r.doRequest(p, req)
if err != nil {
return nil, err
}
drandResponse := new(drand.PublicRandResponse)
return drandResponse, r.marshaller.Unmarshal(respBody, drandResponse)
}
func (r *restClient) Private(p Peer, in *drand.PrivateRandRequest) (*drand.PrivateRandResponse, error) {
base := restAddr(p)
buff, err := r.marshaller.Marshal(in)
if err != nil {
return nil, err
}
url := base + "/api/private"
req, err := http.NewRequest("POST", url, bytes.NewBuffer(buff))
if err != nil {
return nil, err
}
respBody, err := r.doRequest(p, req)
if err != nil {
return nil, err
}
drandResponse := new(drand.PrivateRandResponse)
return drandResponse, r.marshaller.Unmarshal(respBody, drandResponse)
}
func (r *restClient) DistKey(p Peer, in *drand.DistKeyRequest) (*drand.DistKeyResponse, error) {
base := restAddr(p)
buff, err := r.marshaller.Marshal(in)
if err != nil {
return nil, err
}
url := base + "/api/info/distkey"
req, err := http.NewRequest("GET", url, bytes.NewBuffer(buff))
if err != nil {
return nil, err
}
respBody, err := r.doRequest(p, req)
if err != nil {
return nil, err
}
drandResponse := new(drand.DistKeyResponse)
return drandResponse, r.marshaller.Unmarshal(respBody, drandResponse)
}
func (r *restClient) doRequest(remote Peer, req *http.Request) ([]byte, error) {
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
pool := r.manager.Pool()
if remote.IsTLS() {
h, _, err := net.SplitHostPort(remote.Address())
if err != nil {
return nil, err
}
conf := &tls.Config{
RootCAs: pool,
ServerName: h,
}
client.Transport = &http.Transport{TLSClientConfig: conf}
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
return ioutil.ReadAll(resp.Body)
}
func restAddr(p Peer) string {
if p.IsTLS() {
return "https://" + p.Address()
}
return "http://" + p.Address()
}