-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgrpc_client.cpp
More file actions
450 lines (356 loc) · 15.6 KB
/
grpc_client.cpp
File metadata and controls
450 lines (356 loc) · 15.6 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// This implements the necessary functions to talk to the WACE gRPC server
#include "grpc_client.hpp"
#include <string>
#include "grpcpp/grpcpp.h"
#include "wace.grpc.pb.h"
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using waceproto::SendRequestParams;
using waceproto::SendRequestResult;
using waceproto::SendReqLineAndHeadersParams;
using waceproto::SendReqLineAndHeadersResult;
using waceproto::SendRequestBodyParams;
using waceproto::SendRequestBodyResult;
using waceproto::SendResponseParams;
using waceproto::SendResponseResult;
using waceproto::SendRespLineAndHeadersParams;
using waceproto::SendRespLineAndHeadersResult;
using waceproto::SendResponseBodyParams;
using waceproto::SendResponseBodyResult;
using waceproto::CheckParams;
using waceproto::CheckResult;
using waceproto::InitParams;
using waceproto::InitResult;
using waceproto::CloseParams;
using waceproto::CloseResult;
//--TODO list:
//- Proper error handling
//- Channel needs to be created every time, or reuse the same instance?
//- Change the protobuff to receive the request headers as char ** ??
//- gRPC channel secure?
struct returnStatus{
int grpc_status_code;
std::string grpc_status_message;
int wace_status_code;
};
class WaceClient {
public:
WaceClient(std::shared_ptr<Channel> channel)
: stub_(waceproto::WaceProto::NewStub(channel)) {}
// Assembles client payload, sends it to the server, and returns its response
returnStatus sendRequest(char * transactId, std::string req, char ** modelsId, int modelNumber ) {
// Data to be sent to server
SendRequestParams request;
request.set_transact_id(transactId);
request.set_request(req);
for (int i=0; i<modelNumber; i++){
request.add_model_id(modelsId[i]);
}
// Container for server response
SendRequestResult reply;
// Context can be used to send meta data to server or modify RPC behaviour
ClientContext context;
// Actual Remote Procedure Call
Status status = stub_->SendRequest(&context, request, &reply);
returnStatus res;
res.grpc_status_code = status.error_code();
res.grpc_status_message = status.error_message();
res.wace_status_code = reply.status_code();
return res;
}
returnStatus sendReqLineAndHeaders(char * transactID, char * reqLine, std::string reqHeaders, char ** modelsId, int modelNumber){
SendReqLineAndHeadersParams requestData;
requestData.set_transact_id(transactID);
requestData.set_req_line(reqLine);
requestData.set_req_headers(reqHeaders);
for (int i=0; i<modelNumber; i++){
requestData.add_model_id(modelsId[i]);
}
SendReqLineAndHeadersResult result;
ClientContext context;
Status status= stub_->SendReqLineAndHeaders(&context,requestData,&result);
returnStatus res;
res.grpc_status_code = status.error_code();
res.grpc_status_message = status.error_message();
res.wace_status_code = result.status_code();
return res;
}
returnStatus sendRequestBody(char * transactID, char * body, char ** modelsId, int modelNumber){
SendRequestBodyParams requestData;
requestData.set_transact_id(transactID);
requestData.set_body(body);
for (int i=0; i<modelNumber; i++){
requestData.add_model_id(modelsId[i]);
}
SendRequestBodyResult result;
ClientContext context;
Status status= stub_->SendRequestBody(&context,requestData,&result);
returnStatus res;
res.grpc_status_code = status.error_code();
res.grpc_status_message = status.error_message();
res.wace_status_code = result.status_code();
return res;
}
returnStatus sendResponse(char * transactID, char * response, char ** modelsId, int modelNumber){
SendResponseParams responseData;
responseData.set_transact_id(transactID);
responseData.set_response(response);
for (int i=0; i<modelNumber; i++){
responseData.add_model_id(modelsId[i]);
}
SendResponseResult result;
ClientContext context;
Status status= stub_->SendResponse(&context,responseData,&result);
// Returns results based on RPC status
returnStatus res;
res.grpc_status_code = status.error_code();
res.grpc_status_message = status.error_message();
res.wace_status_code = result.status_code();
return res;
}
returnStatus sendRespLineAndHeaders(char * transactID, char * statusLine, std::string respHeaders, char ** modelsId, int modelNumber){
SendRespLineAndHeadersParams responseData;
responseData.set_transact_id(transactID);
responseData.set_status_line(statusLine);
responseData.set_resp_headers(respHeaders);
for (int i=0; i<modelNumber; i++){
responseData.add_model_id(modelsId[i]);
}
SendRespLineAndHeadersResult result;
ClientContext context;
Status status= stub_->SendRespLineAndHeaders(&context,responseData,&result);
returnStatus res;
res.grpc_status_code = status.error_code();
res.grpc_status_message = status.error_message();
res.wace_status_code = result.status_code();
return res;
}
returnStatus sendResponseBody(char * transactID, char * body, char ** modelsId, int modelNumber){
SendResponseBodyParams responseData;
responseData.set_transact_id(transactID);
responseData.set_body(body);
for (int i=0; i<modelNumber; i++){
responseData.add_model_id(modelsId[i]);
}
SendResponseBodyResult result;
ClientContext context;
Status status= stub_->SendResponseBody(&context,responseData,&result);
returnStatus res;
res.grpc_status_code = status.error_code();
res.grpc_status_message = status.error_message();
res.wace_status_code = result.status_code();
return res;
}
returnStatus check(char * transactID, char * decisionID, std::map<std::string,std::string> wafParams,CheckResult * rpcResults){
CheckParams checkData;
checkData.set_transact_id(transactID);
checkData.set_decision_id(decisionID);
auto wafmap = checkData.mutable_waf_params();
std::map<std::string, std::string>::iterator it;
for (it = wafParams.begin(); it != wafParams.end(); it++){
wafmap->insert(google::protobuf::MapPair<std::string, std::string>(it->first,it->second));
}
CheckResult result;
ClientContext context;
Status status= stub_->Check(&context,checkData,&result);
*rpcResults = result; // return the result of the call
// Returns results based on RPC status
returnStatus res;
res.grpc_status_code = status.error_code();
res.grpc_status_message = status.error_message();
res.wace_status_code = result.status_code();
return res;
}
returnStatus init(char * transactID){
InitParams initP;
initP.set_transact_id(transactID);
InitResult result;
ClientContext context;
Status status= stub_->Init(&context,initP,&result);
returnStatus res;
res.grpc_status_code = status.error_code();
res.grpc_status_message = status.error_message();
res.wace_status_code = result.status_code();
return res;
}
returnStatus close(char * transactID, std::map<std::string,std::string> metrics, int metrics_count){
CloseParams closeP;
closeP.set_transact_id(transactID);
auto metricmap = closeP.mutable_metric();
std::map<std::string, std::string>::iterator it;
for (it = metrics.begin(); it != metrics.end(); it++){
metricmap->insert(google::protobuf::MapPair<std::string, std::string>(it->first,it->second));
}
CloseResult result;
ClientContext context;
Status status= stub_->Close(&context,closeP,&result);
returnStatus res;
res.grpc_status_code = status.error_code();
res.grpc_status_message = status.error_message();
res.wace_status_code = result.status_code();
return res;
}
private:
std::unique_ptr<waceproto::WaceProto::Stub> stub_;
};
int main (){
return 0;
}
extern "C" {
// C Functions
int SendRequest(const char * grpcServerUrl, char * transaction_id, char * req_line, char ** req_headers,int req_header_count, char * body, char ** models_id, int number_of_models, char * *returnMsg){
// Instantiates the client
WaceClient client(grpc::CreateChannel(grpcServerUrl, grpc::InsecureChannelCredentials()));
//concatenate req line, headers and body into one string
std::string req = std::string(req_line) + "\n";
for(int i=0;i<req_header_count;i++){
req = req + "\n" + std::string(req_headers[i]);
}
if (body != NULL){
req = req + "\n" + std::string(body);
}
returnStatus status = client.sendRequest(transaction_id,req,models_id,number_of_models);
//Copy status message to the returnMsg param
*returnMsg = new char[strlen(&status.grpc_status_message[0])+1];
sprintf(*returnMsg, "%s", &status.grpc_status_message[0]);
if (status.grpc_status_code != 0){//there was an error with the rpc call
return -1;
}
return status.wace_status_code;
}
int SendReqLineAndHeaders(const char * grpcServerUrl, char * transaction_id, char * req_line, char ** req_headers,int req_header_count, char ** models_id, int number_of_models, char * *returnMsg){
// Instantiates the client
std::string msg;
std::shared_ptr<grpc::Channel> chan = grpc::CreateChannel(grpcServerUrl, grpc::InsecureChannelCredentials());
WaceClient client(chan);
//conctenate all the char ** req_headers into one string
std::string allHeaders;
for(int i=0;i<req_header_count;i++){
if (i==0){
allHeaders = std::string(req_headers[i]);
}else{
allHeaders = allHeaders + "\n" + std::string(req_headers[i]);
}
}
returnStatus status = client.sendReqLineAndHeaders(transaction_id,req_line,allHeaders, models_id, number_of_models);
//Copy status message to the returnMsg param
*returnMsg = new char[strlen(&status.grpc_status_message[0])+1];
sprintf(*returnMsg,"%s", &status.grpc_status_message[0]);
if (status.grpc_status_code != 0){//there was an error with the rpc call
return -1;
}
return status.wace_status_code;
}
int SendRequestBody(const char * grpcServerUrl, char * transactID, char * body, char ** modelsId, int modelNumber, char * *returnMsg){
// Instantiates the client
WaceClient client(grpc::CreateChannel(grpcServerUrl, grpc::InsecureChannelCredentials()));
returnStatus status = client.sendRequestBody(transactID,body,modelsId,modelNumber);
sprintf(*returnMsg,"%s", &status.grpc_status_message[0]);
if (status.grpc_status_code != 0){//there was an error with the rpc call
return -1;
}
return status.wace_status_code;
}
int SendResponse(const char * grpcServerUrl, char * transactID, char * response, char ** modelsId, int modelNumber, char * *returnMsg){
// Instantiates the client
WaceClient client(grpc::CreateChannel(grpcServerUrl, grpc::InsecureChannelCredentials()));
returnStatus status = client.sendResponse(transactID,response,modelsId,modelNumber);
//Copy status message to the returnMsg param
*returnMsg = new char[strlen(&status.grpc_status_message[0])+1];
sprintf(*returnMsg,"%s", &status.grpc_status_message[0]);
if (status.grpc_status_code != 0){//there was an error with the rpc call
return -1;
}
return status.wace_status_code;
}
int SendRespLineAndHeaders(const char * grpcServerUrl, char * transactID, char * statusLine,char ** respHeaders, int headerNumber, char ** modelsId, int modelNumber, char * *returnMsg){
// Instantiates the client
WaceClient client(grpc::CreateChannel(grpcServerUrl, grpc::InsecureChannelCredentials()));
std::string allHeaders;
for(int i=0;i<headerNumber;i++){
if (i==0){
allHeaders = std::string(respHeaders[i]);
}else{
allHeaders = allHeaders + "\n" + std::string(respHeaders[i]);
}
}
returnStatus status = client.sendRespLineAndHeaders(transactID,statusLine,allHeaders,modelsId,modelNumber);
//Copy status message to the returnMsg param
*returnMsg = new char[strlen(&status.grpc_status_message[0])+1];
sprintf(*returnMsg,"%s", &status.grpc_status_message[0]);
if (status.grpc_status_code != 0){//there was an error with the rpc call
return -1;
}
return status.wace_status_code;
}
int SendResponseBody(const char * grpcServerUrl, char * transactID, char * body, char ** modelsId, int modelNumber, char * *returnMsg){
// Instantiates the client
WaceClient client(grpc::CreateChannel(grpcServerUrl, grpc::InsecureChannelCredentials()));
returnStatus status = client.sendResponseBody(transactID,body,modelsId,modelNumber);
//Copy status message to the returnMsg param
*returnMsg = new char[strlen(&status.grpc_status_message[0])+1];
sprintf(*returnMsg,"%s", &status.grpc_status_message[0]);
if (status.grpc_status_code != 0){//there was an error with the rpc call
return -1;
}
return status.wace_status_code;
}
int Check(const char * grpcServerUrl, char * transactID, char * decisionID, WAFParams * wafParams, int numberWAFParams, int *blockTransaction, char * *waceMsg, char * *returnMsg){
// Instantiates the client
WaceClient client(grpc::CreateChannel(grpcServerUrl, grpc::InsecureChannelCredentials()));
std::map<std::string,std::string> wafParamsMap;
if (wafParams != NULL){
for(int i=0; i<numberWAFParams; i++){
wafParamsMap.insert(std::pair<std::string,std::string>(wafParams[i].key,wafParams[i].value));
}
}
CheckResult res;
returnStatus status = client.check(transactID, decisionID,wafParamsMap,&res);
*blockTransaction = res.block_transaction();
*waceMsg = new char[strlen(&res.msg()[0])+1];
sprintf(*waceMsg,"%s", &res.msg()[0]);
*returnMsg = new char[strlen(&status.grpc_status_message[0])+1];
sprintf(*returnMsg,"%s", &status.grpc_status_message[0]);
if (status.grpc_status_code != 0){//there was an error with the rpc call
return -1;
}
return status.wace_status_code;
}
int Init(const char * grpcServerUrl, char * transaction_id, char * *returnMsg){
// Instantiates the client
std::string msg;
std::shared_ptr<grpc::Channel> chan = grpc::CreateChannel(grpcServerUrl, grpc::InsecureChannelCredentials());
WaceClient client(chan);
//conctenate all the char ** req_headers into one string
returnStatus status = client.init(transaction_id);
//Copy status message to the returnMsg param
*returnMsg = new char[strlen(&status.grpc_status_message[0])+1];
sprintf(*returnMsg,"%s", &status.grpc_status_message[0]);
if (status.grpc_status_code != 0){//there was an error with the rpc call
return -1;
}
return status.wace_status_code;
}
int Close(const char * grpcServerUrl, char * transaction_id, MetricParams * metrics, int metrics_count, char * *returnMsg){
// Instantiates the client
std::string msg;
std::shared_ptr<grpc::Channel> chan = grpc::CreateChannel(grpcServerUrl, grpc::InsecureChannelCredentials());
WaceClient client(chan);
//conctenate all the char ** req_headers into one string
std::map<std::string,std::string> metricParamsMap;
if (metrics != NULL){
for(int i=0; i<metrics_count; i++){
metricParamsMap.insert(std::pair<std::string,std::string>(metrics[i].key,metrics[i].value));
}
}
returnStatus status = client.close(transaction_id, metricParamsMap, metrics_count);
//Copy status message to the returnMsg param
*returnMsg = new char[strlen(&status.grpc_status_message[0])+1];
sprintf(*returnMsg,"%s", &status.grpc_status_message[0]);
if (status.grpc_status_code != 0){//there was an error with the rpc call
return -1;
}
return status.wace_status_code;
}
}