/
resty_consensus_service.go
245 lines (225 loc) · 6.56 KB
/
resty_consensus_service.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package sdk
import (
"crypto/tls"
"errors"
"fmt"
"github.com/blockchain-jd-com/framework-go/gmsm/gmtls"
gmx509 "github.com/blockchain-jd-com/framework-go/gmsm/x509"
"github.com/blockchain-jd-com/framework-go/ledger_model"
"github.com/go-resty/resty/v2"
"github.com/tidwall/gjson"
"io/ioutil"
"net/url"
"strconv"
)
/*
* Author: imuge
* Date: 2020/6/29 下午2:56
*/
var _ ledger_model.ParticipantService = (*RestyConsensusService)(nil)
type RestyConsensusService struct {
host string
port int
secure bool
baseUrl string
security *SSLSecurity
gmSecure bool
gmSecurity *GMSSLSecurity
}
func NewRestyConsensusService(host string, port int) *RestyConsensusService {
baseUrl := fmt.Sprintf("http://%s:%d", host, port)
return &RestyConsensusService{
host: host,
port: port,
secure: false,
baseUrl: baseUrl,
}
}
func NewSecureRestyConsensusService(host string, port int, security *SSLSecurity) *RestyConsensusService {
baseUrl := fmt.Sprintf("https://%s:%d", host, port)
return &RestyConsensusService{
host: host,
port: port,
secure: true,
baseUrl: baseUrl,
security: security,
}
}
func NewGMSecureConsensusService(host string, port int, security *GMSSLSecurity) *RestyConsensusService {
baseUrl := fmt.Sprintf("https://%s:%d", host, port)
return &RestyConsensusService{
host: host,
port: port,
secure: true,
gmSecure: true,
baseUrl: baseUrl,
gmSecurity: security,
}
}
func (r RestyConsensusService) ActivateParticipant(params ledger_model.ActivateParticipantParams) (info bool, err error) {
url := "/management/delegate/activeparticipant"
args := map[string]string{
"ledgerHash": params.LedgerHash,
"consensusHost": params.ConsensusHost,
"consensusPort": strconv.Itoa(params.ConsensusPort),
"consensusSecure": strconv.FormatBool(params.ConsensusSecure),
"remoteManageHost": params.RemoteManageHost,
"remoteManagePort": strconv.Itoa(params.RemoteManagePort),
"remoteManageSecure": strconv.FormatBool(params.RemoteManageSecure),
"shutdown": strconv.FormatBool(params.Shutdown),
}
if !r.gmSecure {
return r.active(url, args)
} else {
return r.gmActive(url, args)
}
}
func (r RestyConsensusService) active(url string, args map[string]string) (info bool, err error) {
client := resty.New()
if r.secure {
if r.security != nil {
client.SetTLSClientConfig(&tls.Config{
RootCAs: r.security.RootCerts,
Certificates: r.security.ClientCerts,
})
} else {
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
}
}
resp, err := client.R().SetFormData(args).SetResult(ActivateParticipantResponse{}).Post(r.baseUrl + url)
if err != nil {
return
}
if !resp.IsSuccess() {
return info, errors.New(resp.String())
}
wrp, ok := resp.Result().(*ActivateParticipantResponse)
if !ok {
return info, errors.New("unparseable response")
} else {
return wrp.Success, nil
}
}
func (r RestyConsensusService) gmActive(activeUrl string, args map[string]string) (info bool, err error) {
var certPool *gmx509.CertPool
if r.gmSecurity != nil {
certPool = r.gmSecurity.RootCerts
}
var certificates []gmtls.Certificate
if nil != r.gmSecurity && nil != r.gmSecurity.EncCert && nil != r.gmSecurity.SigCert {
certificates = []gmtls.Certificate{*r.gmSecurity.SigCert, *r.gmSecurity.EncCert}
} else {
certificates = []gmtls.Certificate{}
}
config := &gmtls.Config{
GMSupport: &gmtls.GMSupport{},
RootCAs: certPool,
ClientAuth: gmtls.NoClientCert,
Certificates: certificates,
InsecureSkipVerify: r.gmSecurity == nil || r.gmSecurity.RootCerts == nil,
}
gmClient := gmtls.NewCustomHTTPSClient(config)
queryForm := url.Values{}
for k, v := range args {
queryForm.Add(k, v)
}
result, err := gmClient.PostForm(r.baseUrl+activeUrl, queryForm)
defer result.Body.Close()
if err != nil {
return
}
raw, err := ioutil.ReadAll(result.Body)
if err != nil {
return
}
tresp := gjson.ParseBytes(raw)
if !tresp.Get("success").Bool() {
return info, errors.New("active error")
} else {
return true, nil
}
}
func (r RestyConsensusService) InactivateParticipant(params ledger_model.InactivateParticipantParams) (info bool, err error) {
url := "/management/delegate/deactiveparticipant"
args := map[string]string{
"ledgerHash": params.LedgerHash,
"participantAddress": params.ParticipantAddress,
}
if !r.gmSecure {
return r.inactive(url, args)
} else {
return r.gmInactive(url, args)
}
}
func (r RestyConsensusService) inactive(url string, args map[string]string) (info bool, err error) {
client := resty.New()
if r.secure {
if r.security != nil {
client.SetTLSClientConfig(&tls.Config{
RootCAs: r.security.RootCerts,
Certificates: r.security.ClientCerts,
})
} else {
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
}
}
resp, err := client.R().SetFormData(args).SetResult(InactivateParticipantResponse{}).Post(r.baseUrl + url)
if err != nil {
return
}
fmt.Println(fmt.Sprintf("%s \n %v \n", url, resp))
if !resp.IsSuccess() {
return info, errors.New(resp.String())
}
wrp, ok := resp.Result().(*InactivateParticipantResponse)
if !ok {
return info, errors.New("unparseable response")
} else {
return wrp.Success, nil
}
}
func (r RestyConsensusService) gmInactive(inactiveUrl string, args map[string]string) (info bool, err error) {
var certPool *gmx509.CertPool
if r.gmSecurity != nil {
certPool = r.gmSecurity.RootCerts
}
var certificates []gmtls.Certificate
if nil != r.gmSecurity && nil != r.gmSecurity.EncCert && nil != r.gmSecurity.SigCert {
certificates = []gmtls.Certificate{*r.gmSecurity.SigCert, *r.gmSecurity.EncCert}
} else {
certificates = []gmtls.Certificate{}
}
config := &gmtls.Config{
GMSupport: &gmtls.GMSupport{},
RootCAs: certPool,
ClientAuth: gmtls.NoClientCert,
Certificates: certificates,
InsecureSkipVerify: r.gmSecurity == nil || r.gmSecurity.RootCerts == nil,
}
gmClient := gmtls.NewCustomHTTPSClient(config)
queryForm := url.Values{}
for k, v := range args {
queryForm.Add(k, v)
}
result, err := gmClient.PostForm(r.baseUrl+inactiveUrl, queryForm)
defer result.Body.Close()
if err != nil {
return
}
raw, err := ioutil.ReadAll(result.Body)
if err != nil {
return
}
tresp := gjson.ParseBytes(raw)
if !tresp.Get("success").Bool() {
return info, errors.New("inactive error")
} else {
return true, nil
}
}
type ActivateParticipantResponse struct {
Success bool `json:"success"`
}
type InactivateParticipantResponse struct {
Success bool `json:"success"`
}