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 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
|
/*
* Phusion Passenger - https://www.phusionpassenger.com/
* Copyright (c) 2010-2014 Phusion
*
* "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef _PASSENGER_REMOTE_SENDER_H_
#define _PASSENGER_REMOTE_SENDER_H_
#include <sys/types.h>
#include <ctime>
#include <cassert>
#include <curl/curl.h>
#include <zlib.h>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#include <oxt/thread.hpp>
#include <string>
#include <list>
#include <Logging.h>
#include <StaticString.h>
#include <Utils.h>
#include <Utils/BlockingQueue.h>
#include <Utils/SystemTime.h>
#include <Utils/ScopeGuard.h>
#include <Utils/Base64.h>
#include <json/json.h>
#include <Utils/Curl.h>
namespace Passenger {
using namespace std;
using namespace boost;
using namespace oxt;
class RemoteSender {
private:
struct Item {
bool exit;
bool compressed;
string unionStationKey;
string nodeName;
string category;
string data;
Item() {
exit = false;
compressed = false;
}
};
class Server {
private:
string ip;
unsigned short port;
string certificate;
const CurlProxyInfo *proxyInfo;
CURL *curl;
struct curl_slist *headers;
char lastErrorMessage[CURL_ERROR_SIZE];
string hostHeader;
string responseBody;
string pingURL;
string sinkURL;
void resetConnection() {
if (curl != NULL) {
#ifdef HAS_CURL_EASY_RESET
curl_easy_reset(curl);
#else
curl_easy_cleanup(curl);
curl = NULL;
#endif
}
if (curl == NULL) {
curl = curl_easy_init();
if (curl == NULL) {
throw IOException("Unable to create a CURL handle");
}
}
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, lastErrorMessage);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlDataReceived);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
if (certificate.empty()) {
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
} else {
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_easy_setopt(curl, CURLOPT_CAINFO, certificate.c_str());
}
/* No host name verification because Curl thinks the
* host name is the IP address. But if we have the
* certificate then it doesn't matter.
*/
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
setCurlProxy(curl, *proxyInfo);
responseBody.clear();
}
void prepareRequest(const string &url) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
responseBody.clear();
}
static bool validateResponse(const Json::Value &response) {
if (response.isObject() && response["status"].isString()) {
string status = response["status"].asString();
if (status == "ok") {
return true;
} else if (status == "error") {
return response["message"].isString();
} else {
return false;
}
} else {
return false;
}
}
bool handleSendResponse() {
Json::Reader reader;
Json::Value response;
long httpCode = -1;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
if (!reader.parse(responseBody, response, false) || !validateResponse(response)) {
P_ERROR("The Union Station gateway server " << ip << " encountered an error "
"while processing sent analytics data. It sent an invalid response "
"(parse error: " << reader.getFormattedErrorMessages().c_str() <<
"; HTTP code: " << httpCode <<
"; data: \"" << cEscapeString(responseBody) << "\").");
return false;
} else if (response["status"].asString() == "ok") {
if (httpCode == 200) {
P_DEBUG("The Union Station gateway server " << ip << " accepted the packet.");
return true;
} else {
P_ERROR("The Union Station gateway server " << ip << " encountered an error "
"while processing sent analytics data. It sent an invalid response "
"(HTTP code: " << httpCode <<
"; data: \"" << cEscapeString(responseBody) << "\").");
return false;
}
} else {
// response == error
P_ERROR("The Union Station gateway server " << ip << " did not accept the "
"sent analytics data. Error message: " << response["message"].asString());
// Return value of true is intentional. See comment for send().
return true;
}
}
void handleSendError() {
P_ERROR("Could not send data to Union Station gateway server " << ip
<< ": " << lastErrorMessage);
}
static size_t curlDataReceived(void *buffer, size_t size, size_t nmemb, void *userData) {
Server *self = (Server *) userData;
self->responseBody.append((const char *) buffer, size * nmemb);
return size * nmemb;
}
public:
Server(const string &ip, const string &hostName, unsigned short port, const string &cert,
const CurlProxyInfo *proxyInfo)
{
this->ip = ip;
this->port = port;
certificate = cert;
this->proxyInfo = proxyInfo;
hostHeader = "Host: " + hostName;
headers = NULL;
headers = curl_slist_append(headers, hostHeader.c_str());
if (headers == NULL) {
throw IOException("Unable to create a CURL linked list");
}
// Older libcurl versions didn't strdup() any option
// strings so we need to keep these in memory.
pingURL = string("https://") + ip + ":" + toString(port) +
"/ping";
sinkURL = string("https://") + ip + ":" + toString(port) +
"/sink";
curl = NULL;
resetConnection();
}
~Server() {
if (curl != NULL) {
curl_easy_cleanup(curl);
}
curl_slist_free_all(headers);
}
string name() const {
return ip + ":" + toString(port);
}
bool ping() {
P_DEBUG("Pinging Union Station gateway " << ip << ":" << port);
ScopeGuard guard(boost::bind(&Server::resetConnection, this));
prepareRequest(pingURL);
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
if (curl_easy_perform(curl) != 0) {
P_DEBUG("Could not ping Union Station gateway server " << ip
<< ": " << lastErrorMessage);
return false;
}
if (responseBody == "pong") {
guard.clear();
return true;
} else {
P_DEBUG("Union Station gateway server " << ip <<
" returned an unexpected ping message: " <<
responseBody);
return false;
}
}
/** Returns true if the server is up, false if the server is down.
* The return value does NOT indicate whether the server accepted the data!
* Thus, if (for example) the Union Station key is invalid or disabled,
* but the connection is fine, then this method still returns true.
* This is because the return value is used to determine whether a different
* gateway server should be used. If the server is up but rejects the data
* then we'll want the code to keep sending future packets.
*/
bool send(const Item &item) {
ScopeGuard guard(boost::bind(&Server::resetConnection, this));
prepareRequest(sinkURL);
struct curl_httppost *post = NULL;
struct curl_httppost *last = NULL;
string base64_data;
curl_formadd(&post, &last,
CURLFORM_PTRNAME, "key",
CURLFORM_PTRCONTENTS, item.unionStationKey.c_str(),
CURLFORM_CONTENTSLENGTH, (long) item.unionStationKey.size(),
CURLFORM_END);
curl_formadd(&post, &last,
CURLFORM_PTRNAME, "node_name",
CURLFORM_PTRCONTENTS, item.nodeName.c_str(),
CURLFORM_CONTENTSLENGTH, (long) item.nodeName.size(),
CURLFORM_END);
curl_formadd(&post, &last,
CURLFORM_PTRNAME, "category",
CURLFORM_PTRCONTENTS, item.category.c_str(),
CURLFORM_CONTENTSLENGTH, (long) item.category.size(),
CURLFORM_END);
if (item.compressed) {
base64_data = Base64::encode(item.data);
curl_formadd(&post, &last,
CURLFORM_PTRNAME, "data",
CURLFORM_PTRCONTENTS, base64_data.c_str(),
CURLFORM_CONTENTSLENGTH, (long) base64_data.size(),
CURLFORM_END);
curl_formadd(&post, &last,
CURLFORM_PTRNAME, "compressed",
CURLFORM_PTRCONTENTS, "1",
CURLFORM_END);
} else {
curl_formadd(&post, &last,
CURLFORM_PTRNAME, "data",
CURLFORM_PTRCONTENTS, item.data.c_str(),
CURLFORM_CONTENTSLENGTH, (long) item.data.size(),
CURLFORM_END);
}
curl_easy_setopt(curl, CURLOPT_HTTPGET, 0);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
P_DEBUG("Sending Union Station packet: key=" << item.unionStationKey <<
", node=" << item.nodeName << ", category=" << item.category <<
", compressedDataSize=" << item.data.size());
CURLcode code = curl_easy_perform(curl);
curl_formfree(post);
if (code == CURLE_OK) {
guard.clear();
return handleSendResponse();
} else {
handleSendError();
return false;
}
}
};
typedef boost::shared_ptr<Server> ServerPtr;
string gatewayAddress;
unsigned short gatewayPort;
string certificate;
CurlProxyInfo proxyInfo;
BlockingQueue<Item> queue;
oxt::thread *thr;
mutable boost::mutex syncher;
list<ServerPtr> servers;
time_t nextCheckupTime;
unsigned int packetsSent, packetsDropped;
void threadMain() {
ScopeGuard guard(boost::bind(&RemoteSender::freeThreadData, this));
while (true) {
Item item;
bool hasItem;
if (firstStarted()) {
item = queue.get();
hasItem = true;
} else {
hasItem = queue.timedGet(item, msecUntilNextCheckup());
}
if (hasItem) {
if (item.exit) {
return;
} else {
if (timeForCheckup()) {
recheckServers();
}
sendOut(item);
}
} else if (timeForCheckup()) {
recheckServers();
}
}
}
bool firstStarted() const {
boost::lock_guard<boost::mutex> l(syncher);
return nextCheckupTime == 0;
}
void recheckServers() {
P_INFO("Rechecking Union Station gateway servers (" << gatewayAddress << ")...");
vector<string> ips;
vector<string>::const_iterator it;
list<ServerPtr> servers;
string hostName;
bool someServersAreDown = false;
ips = resolveHostname(gatewayAddress, gatewayPort);
P_INFO(ips.size() << " Union Station gateway servers found");
for (it = ips.begin(); it != ips.end(); it++) {
ServerPtr server = boost::make_shared<Server>(*it, gatewayAddress, gatewayPort,
certificate, &proxyInfo);
if (server->ping()) {
servers.push_back(server);
} else {
someServersAreDown = true;
}
}
P_INFO(servers.size() << " Union Station gateway servers are up");
if (servers.empty()) {
scheduleNextCheckup(5 * 60);
} else if (someServersAreDown) {
scheduleNextCheckup(60 * 60);
} else {
scheduleNextCheckup(3 * 60 * 60);
}
boost::lock_guard<boost::mutex> l(syncher);
this->servers = servers;
}
void freeThreadData() {
boost::lock_guard<boost::mutex> l(syncher);
servers.clear(); // Invoke destructors inside this thread.
}
/**
* Schedules the next checkup to be run after the given number
* of seconds, unless there's already a checkup scheduled for
* earlier.
*/
void scheduleNextCheckup(unsigned int seconds) {
time_t now = SystemTime::get();
if (now >= nextCheckupTime || (time_t) (now + seconds) < nextCheckupTime) {
nextCheckupTime = now + seconds;
P_DEBUG("Next checkup time in about " << seconds << " seconds");
}
}
unsigned int msecUntilNextCheckup() const {
boost::lock_guard<boost::mutex> l(syncher);
time_t now = SystemTime::get();
if (now >= nextCheckupTime) {
return 0;
} else {
return (nextCheckupTime - now) * 1000;
}
}
bool timeForCheckup() const {
boost::lock_guard<boost::mutex> l(syncher);
return SystemTime::get() >= nextCheckupTime;
}
void sendOut(const Item &item) {
boost::unique_lock<boost::mutex> l(syncher);
bool sent = false;
bool someServersWentDown = false;
while (!sent && !servers.empty()) {
// Pick first available server and put it on the back of the list
// for round-robin load balancing.
ServerPtr server = servers.front();
l.unlock();
if (server->send(item)) {
l.lock();
servers.pop_front();
servers.push_back(server);
sent = true;
packetsSent++;
} else {
l.lock();
servers.pop_front();
someServersWentDown = true;
packetsDropped++;
}
}
if (someServersWentDown) {
if (servers.empty()) {
scheduleNextCheckup(5 * 60);
} else {
scheduleNextCheckup(60 * 60);
}
}
/* If all servers went down then all items in the queue will be
* effectively dropped until after the next checkup has detected
* servers that are up.
*/
if (!sent) {
P_WARN("Dropping Union Station packet because no servers are available: "
"key=" << item.unionStationKey <<
", node=" << item.nodeName <<
", category=" << item.category <<
", compressedDataSize=" << item.data.size());
}
}
bool compress(const StaticString data[], unsigned int count, string &output) {
if (count == 0) {
StaticString newdata;
return compress(&newdata, 1, output);
}
unsigned char out[128 * 1024];
z_stream strm;
int ret, flush;
unsigned int i, have;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
if (ret != Z_OK) {
return false;
}
for (i = 0; i < count; i++) {
strm.avail_in = data[i].size();
strm.next_in = (unsigned char *) data[i].c_str();
flush = (i == count - 1) ? Z_FINISH : Z_NO_FLUSH;
do {
strm.avail_out = sizeof(out);
strm.next_out = out;
ret = deflate(&strm, flush);
assert(ret != Z_STREAM_ERROR);
have = sizeof(out) - strm.avail_out;
output.append((const char *) out, have);
} while (strm.avail_out == 0);
assert(strm.avail_in == 0);
}
assert(ret == Z_STREAM_END);
deflateEnd(&strm);
return true;
}
public:
RemoteSender(const string &gatewayAddress, unsigned short gatewayPort, const string &certificate,
const string &proxyAddress)
: queue(1024)
{
TRACE_POINT();
this->gatewayAddress = gatewayAddress;
this->gatewayPort = gatewayPort;
this->certificate = certificate;
try {
this->proxyInfo = prepareCurlProxy(proxyAddress);
} catch (const ArgumentException &e) {
throw RuntimeException("Invalid Union Station proxy address \"" +
proxyAddress + "\": " + e.what());
}
nextCheckupTime = 0;
packetsSent = 0;
packetsDropped = 0;
thr = new oxt::thread(
boost::bind(&RemoteSender::threadMain, this),
"RemoteSender thread",
1024 * 512
);
}
~RemoteSender() {
Item item;
item.exit = true;
queue.add(item);
/* Wait until the thread sends out all queued items.
* If this cannot be done within a short amount of time,
* e.g. because all servers are down, then we'll get killed
* by the watchdog anyway.
*/
thr->join();
delete thr;
}
void schedule(const string &unionStationKey, const StaticString &nodeName,
const StaticString &category, const StaticString data[],
unsigned int count)
{
Item item;
item.unionStationKey = unionStationKey;
item.nodeName = nodeName;
item.category = category;
if (compress(data, count, item.data)) {
item.compressed = true;
} else {
size_t size = 0;
unsigned int i;
for (i = 0; i < count; i++) {
size += data[i].size();
}
item.data.reserve(size);
for (i = 0; i < count; i++) {
item.data.append(data[i].c_str(), data[i].size());
}
}
P_DEBUG("Scheduling Union Station packet: key=" << unionStationKey <<
", node=" << nodeName << ", category=" << category <<
", compressedDataSize=" << item.data.size());
if (!queue.tryAdd(item)) {
P_WARN("The Union Station gateway isn't responding quickly enough; dropping packet.");
boost::lock_guard<boost::mutex> l(syncher);
packetsDropped++;
}
}
unsigned int queued() const {
return queue.size();
}
template<typename Stream>
void inspect(Stream &stream) const {
boost::lock_guard<boost::mutex> l(syncher);
stream << " Available servers (" << servers.size() << "): ";
foreach (const ServerPtr server, servers) {
stream << server->name() << " ";
}
stream << "\n";
stream << " Items in queue: " << queue.size() << "\n";
stream << " Packets sent out so far: " << packetsSent << "\n";
stream << " Packets dropped out so far: " << packetsDropped << "\n";
stream << " Next server checkup time: ";
if (nextCheckupTime == 0) {
stream << "not yet scheduled, waiting for first packet\n";
} else {
stream << "in " << distanceOfTimeInWords(nextCheckupTime) << "\n";
}
}
};
} // namespace Passenger
#endif /* _PASSENGER_REMOTE_SENDER_H_ */
|