This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkbouncer.hpp
234 lines (202 loc) · 6.98 KB
/
mkbouncer.hpp
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
// Part of Measurement Kit <https://measurement-kit.github.io/>.
// Measurement Kit is free software under the BSD license. See AUTHORS
// and LICENSE for more information on the copying conditions.
#ifndef MEASUREMENT_KIT_MKBOUNCER_HPP
#define MEASUREMENT_KIT_MKBOUNCER_HPP
#include <map>
#include <string>
#include <vector>
/// MKBOUNCER_HELPER_WEB_CONNECTIVITY is the Web connectivity helper
#define MKBOUNCER_HELPER_WEB_CONNECTIVITY "web-connectivity"
/// MKBOUNCER_HELPER_TCP_ECHO is the TCP echo helper
#define MKBOUNCER_HELPER_TCP_ECHO "tcp-echo"
/// MKBOUNCER_HELPER_HTTP_RETURN_JSON_HEADERS is an helper that returns
/// a JSON structure containing the received HTTP headers
#define MKBOUNCER_HELPER_HTTP_RETURN_JSON_HEADERS "http-return-json-headers"
/// MKBOUNCER_INLINE_NAMESPACE controls the inline inner namespace in which
/// public symbols exported by this library are enclosed.
///
/// See <https://github.com/measurement-kit/measurement-kit/issues/1867#issuecomment-514562622>.
#define MKBOUNCER_INLINE_NAMESPACE v0_3_0_or_greater
namespace mk {
namespace bouncer {
inline namespace MKBOUNCER_INLINE_NAMESPACE {
/// Request is a bouncer request
class Request {
public:
/// base_url is the bouncer base URL
std::string base_url = "https://bouncer.ooni.io";
/// ca_bundle_path is the CA bundle path.
std::string ca_bundle_path;
/// helpers contains the list of test helpers to request.
std::vector<std::string> helpers;
/// name is the nettest name.
std::string name;
/// timeout is the timeout in seconds.
int64_t timeout = 30;
/// version is the nettest version.
std::string version;
};
/// Record is a collector or test-helper record.
class Record {
public:
/// type is the record type.
std::string type;
/// address is the record address.
std::string address;
/// front is the front to use in case of domain fronting.
std::string front;
};
/// Response is the bouncer response.
class Response {
public:
/// good indicates whether we good a good response.
bool good = false;
/// reason contains the reason for failure.
std::string reason;
/// collectors lists all available collectors.
std::vector<Record> collectors;
/// helpers lists all available test-helpers.
std::map<std::string, std::vector<Record>> helpers;
/// logs contains the possibly binary logs.
std::vector<std::string> logs;
};
/// perform performs @p request and returns a Response.
Response perform(const Request &request) noexcept;
} // inline namespace MKBOUNCER_INLINE_NAMESPACE
} // namespace bouncer
} // namespace mk
// MKBOUNCER_INLINE_IMPL controls whether to include the implementation inline.
#ifdef MKBOUNCER_INLINE_IMPL
#include <sstream>
#include <curl/curl.h>
#include "json.hpp"
#include "mkcurl.hpp"
#include "mkmock.hpp"
#ifdef MKBOUNCER_MOCK
#define MKBOUNCER_HOOK MKMOCK_HOOK_ENABLED
#else
#define MKBOUNCER_HOOK MKMOCK_HOOK_DISABLED
#endif
namespace mk {
namespace bouncer {
inline namespace MKBOUNCER_INLINE_NAMESPACE {
// curl_reason_for_failure contains the cURL reason for failure.
static std::string curl_reason_for_failure(
const curl::Response &response) noexcept {
if (response.error != 0) {
std::string rv = "bouncer: ";
rv += curl_easy_strerror((CURLcode)response.error);
return rv;
}
if (response.status_code != 200) {
std::string rv = "bouncer: ";
rv += curl_easy_strerror(CURLE_HTTP_RETURNED_ERROR);
return rv;
}
return "bouncer: unknown libcurl error";
}
// log_body is a helper to log about a body.
static void log_body(const std::string &prefix, const std::string &body,
std::vector<std::string> &logs) noexcept {
std::stringstream ss;
ss << prefix << " body: " << body;
logs.push_back(ss.str());
}
Response perform(const Request &request) noexcept {
Response response;
curl::Request curl_request;
curl_request.ca_path = request.ca_bundle_path;
curl_request.timeout = request.timeout;
curl_request.method = "POST";
{
std::string url = request.base_url;
url += "/bouncer/net-tests";
std::swap(url, curl_request.url);
}
{
nlohmann::json doc;
nlohmann::json nettest;
nettest["input-hashes"] = nullptr;
nettest["name"] = request.name;
nettest["test-helpers"] = request.helpers;
nettest["version"] = request.version;
doc["net-tests"].push_back(std::move(nettest));
std::string body;
try {
body = doc.dump();
} catch (const std::exception &exc) {
response.logs.push_back(exc.what());
response.reason = exc.what();
return response;
}
log_body("Request", body, response.logs);
std::swap(body, curl_request.body);
}
curl::Response curl_response = curl::perform(curl_request);
for (auto &entry : curl_response.logs) {
response.logs.push_back(std::move(entry.line));
}
MKBOUNCER_HOOK(curl_response_error, curl_response.error);
MKBOUNCER_HOOK(curl_response_status_code, curl_response.status_code);
if (curl_response.error != 0 || curl_response.status_code != 200) {
response.reason = curl_reason_for_failure(curl_response);
return response;
}
MKBOUNCER_HOOK(curl_response_body, curl_response.body);
{
log_body("Response", curl_response.body, response.logs);
try {
nlohmann::json doc = nlohmann::json::parse(curl_response.body);
const nlohmann::json &net_tests = doc.at("net-tests")[0];
if (net_tests.count("collector") > 0) {
Record record;
record.address = net_tests.at("collector");
record.type = "onion";
response.collectors.push_back(std::move(record));
}
if (net_tests.count("collector-alternate") > 0) {
for (const nlohmann::json &e : net_tests.at("collector-alternate")) {
Record record;
record.address = e.at("address");
record.type = e.at("type");
if (e.count("front") > 0) {
record.front = e.at("front");
}
response.collectors.push_back(std::move(record));
}
}
if (net_tests.count("test-helpers") > 0) {
for (auto &e : net_tests.at("test-helpers").items()) {
Record record;
record.address = e.value();
response.helpers[e.key()].push_back(std::move(record));
}
}
if (net_tests.count("test-helpers-alternate") > 0) {
for (auto &e : net_tests.at("test-helpers-alternate").items()) {
for (auto &v : e.value()) {
Record record;
record.address = v.at("address");
record.type = v.at("type");
if (v.count("front") > 0) {
record.front = v.at("front");
}
response.helpers[e.key()].push_back(std::move(record));
}
}
}
} catch (const std::exception &exc) {
response.logs.push_back(exc.what());
response.reason = exc.what();
return response;
}
}
response.good = true;
return response;
}
} // inline namespace MKBOUNCER_INLINE_NAMESPACE
} // namespace bouncer
} // namespace mk
#endif // MKBOUNCER_INLINE_IMPL
#endif // MEASUREMENT_KIT_MKBOUNCER_HPP