2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2010-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <climits> /* USHRT_MAX */
|
|
|
|
#include <cstdio> /* fprintf() */
|
|
|
|
#include <cstring> /* strchr(), memcmp(), memcpy() */
|
2022-01-25 04:25:55 +00:00
|
|
|
#include <iomanip>
|
2022-03-28 22:13:32 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <iterator>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <string_view>
|
2011-03-13 14:33:28 +00:00
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
#include <event2/http.h> /* for HTTP_OK */
|
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#define LIBTRANSMISSION_ANNOUNCER_MODULE
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
2022-01-25 04:25:55 +00:00
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
#include "announcer-common.h"
|
2022-01-28 18:39:45 +00:00
|
|
|
#include "benc.h"
|
2022-01-25 04:25:55 +00:00
|
|
|
#include "crypto-utils.h"
|
|
|
|
#include "error.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "net.h" /* tr_globalIPv6() */
|
2011-03-11 04:19:01 +00:00
|
|
|
#include "peer-mgr.h" /* pex */
|
2022-01-13 02:13:58 +00:00
|
|
|
#include "quark.h"
|
2011-03-11 04:19:01 +00:00
|
|
|
#include "torrent.h"
|
2011-03-13 14:33:28 +00:00
|
|
|
#include "utils.h"
|
2021-11-09 03:30:03 +00:00
|
|
|
#include "web-utils.h"
|
2022-01-25 04:25:55 +00:00
|
|
|
#include "web.h"
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-12-28 15:08:04 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
***** ANNOUNCE
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* get_event_string(tr_announce_request const* req)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
return req->partial_seed && (req->event != TR_ANNOUNCE_EVENT_STOPPED) ? "paused" : tr_announce_event_get_string(req->event);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2011-03-11 15:44:24 +00:00
|
|
|
|
2022-03-28 22:13:32 +00:00
|
|
|
static tr_urlbuf announce_url_new(tr_session const* session, tr_announce_request const* req)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-03-28 22:13:32 +00:00
|
|
|
auto url = tr_urlbuf{};
|
|
|
|
auto out = std::back_inserter(url);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
auto escaped_info_hash = std::array<char, SHA_DIGEST_LENGTH * 3 + 1>{};
|
|
|
|
tr_http_escape_sha1(std::data(escaped_info_hash), req->info_hash);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-03-28 22:13:32 +00:00
|
|
|
fmt::format_to(
|
|
|
|
out,
|
|
|
|
"{url}"
|
|
|
|
"{sep}info_hash={info_hash}"
|
|
|
|
"&peer_id={peer_id}"
|
|
|
|
"&port={port}"
|
|
|
|
"&uploaded={uploaded}"
|
|
|
|
"&downloaded={downloaded}"
|
|
|
|
"&left={left}"
|
|
|
|
"&numwant={numwant}"
|
|
|
|
"&key={key}"
|
2017-04-19 12:04:45 +00:00
|
|
|
"&compact=1"
|
|
|
|
"&supportcrypto=1",
|
2022-03-28 22:13:32 +00:00
|
|
|
fmt::arg("url", req->announce_url),
|
|
|
|
fmt::arg("sep", tr_strvContains(req->announce_url.sv(), '?') ? '&' : '?'),
|
|
|
|
fmt::arg("info_hash", std::data(escaped_info_hash)),
|
|
|
|
fmt::arg("peer_id", std::string_view{ std::data(req->peer_id), std::size(req->peer_id) }),
|
2022-04-21 15:58:13 +00:00
|
|
|
fmt::arg("port", req->port.host()),
|
2022-03-28 22:13:32 +00:00
|
|
|
fmt::arg("uploaded", req->up),
|
|
|
|
fmt::arg("downloaded", req->down),
|
|
|
|
fmt::arg("left", req->leftUntilComplete),
|
|
|
|
fmt::arg("numwant", req->numwant),
|
|
|
|
fmt::arg("key", req->key));
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (session->encryptionMode == TR_ENCRYPTION_REQUIRED)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-28 22:13:32 +00:00
|
|
|
fmt::format_to(out, "&requirecrypto=1");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (req->corrupt != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-28 22:13:32 +00:00
|
|
|
fmt::format_to(out, "&corrupt={}", req->corrupt);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 00:53:35 +00:00
|
|
|
if (char const* str = get_event_string(req); !tr_str_is_empty(str))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-28 22:13:32 +00:00
|
|
|
fmt::format_to(out, "&event={}", str);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
if (!std::empty(req->tracker_id))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-28 22:13:32 +00:00
|
|
|
fmt::format_to(out, "&trackerid={}", req->tracker_id);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
/* There are two incompatible techniques for announcing an IPv6 address.
|
|
|
|
BEP-7 suggests adding an "ipv6=" parameter to the announce URL,
|
|
|
|
while OpenTracker requires that peers announce twice, once over IPv4
|
|
|
|
and once over IPv6.
|
|
|
|
|
|
|
|
To be safe, we should do both: add the "ipv6=" parameter and
|
|
|
|
announce twice. At any rate, we're already computing our IPv6
|
|
|
|
address (for the LTEP handshake), so this comes for free. */
|
|
|
|
|
2022-02-02 01:09:11 +00:00
|
|
|
if (auto const* const ipv6 = tr_globalIPv6(session); ipv6 != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-28 18:39:45 +00:00
|
|
|
auto ipv6_readable = std::array<char, INET6_ADDRSTRLEN>{};
|
|
|
|
evutil_inet_ntop(AF_INET6, ipv6, std::data(ipv6_readable), std::size(ipv6_readable));
|
2022-03-28 22:13:32 +00:00
|
|
|
fmt::format_to(out, "&ipv6=");
|
|
|
|
tr_http_escape(out, std::data(ipv6_readable), true);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 22:13:32 +00:00
|
|
|
return url;
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 23:18:50 +00:00
|
|
|
static void verboseLog(std::string_view description, tr_direction direction, std::string_view message)
|
2022-01-25 04:25:55 +00:00
|
|
|
{
|
|
|
|
auto& out = std::cerr;
|
|
|
|
static bool const verbose = tr_env_key_exists("TR_CURL_VERBOSE");
|
|
|
|
if (!verbose)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const direction_sv = direction == TR_DOWN ? "<< "sv : ">> "sv;
|
|
|
|
out << description << std::endl << "[raw]"sv << direction_sv;
|
|
|
|
for (unsigned char ch : message)
|
|
|
|
{
|
2022-02-07 04:28:36 +00:00
|
|
|
if (isprint(ch) != 0)
|
2022-01-25 04:25:55 +00:00
|
|
|
{
|
|
|
|
out << ch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-07 04:28:36 +00:00
|
|
|
out << R"(\x)" << std::hex << std::setw(2) << std::setfill('0') << unsigned(ch) << std::dec << std::setw(1)
|
2022-01-25 04:25:55 +00:00
|
|
|
<< std::setfill(' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out << std::endl << "[b64]"sv << direction_sv << tr_base64_encode(message) << std::endl;
|
|
|
|
}
|
|
|
|
|
2022-01-28 18:39:45 +00:00
|
|
|
static auto constexpr MaxBencDepth = 8;
|
|
|
|
|
2022-03-10 05:51:14 +00:00
|
|
|
void tr_announcerParseHttpAnnounceResponse(tr_announce_response& response, std::string_view benc, char const* log_name)
|
2022-01-25 04:25:55 +00:00
|
|
|
{
|
2022-01-28 18:39:45 +00:00
|
|
|
verboseLog("Announce response:", TR_DOWN, benc);
|
2022-01-25 04:25:55 +00:00
|
|
|
|
2022-01-28 18:39:45 +00:00
|
|
|
struct AnnounceHandler final : public transmission::benc::BasicHandler<MaxBencDepth>
|
2022-01-27 23:18:50 +00:00
|
|
|
{
|
2022-01-28 18:39:45 +00:00
|
|
|
using BasicHandler = transmission::benc::BasicHandler<MaxBencDepth>;
|
2022-01-25 04:25:55 +00:00
|
|
|
|
2022-01-28 18:39:45 +00:00
|
|
|
tr_announce_response& response_;
|
2022-03-18 00:31:24 +00:00
|
|
|
std::string_view const log_name_;
|
2022-01-28 18:39:45 +00:00
|
|
|
std::optional<size_t> row_;
|
|
|
|
tr_pex pex_ = {};
|
2022-01-25 04:25:55 +00:00
|
|
|
|
2022-03-18 00:31:24 +00:00
|
|
|
explicit AnnounceHandler(tr_announce_response& response, std::string_view log_name)
|
2022-01-28 18:39:45 +00:00
|
|
|
: response_{ response }
|
2022-03-18 00:31:24 +00:00
|
|
|
, log_name_{ log_name }
|
2022-01-25 04:25:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
bool StartDict(Context const& context) override
|
2022-01-25 04:25:55 +00:00
|
|
|
{
|
2022-01-28 22:46:14 +00:00
|
|
|
BasicHandler::StartDict(context);
|
2022-01-25 04:25:55 +00:00
|
|
|
|
2022-01-28 18:39:45 +00:00
|
|
|
pex_ = {};
|
2022-01-25 04:25:55 +00:00
|
|
|
|
2022-01-28 18:39:45 +00:00
|
|
|
return true;
|
2022-01-25 04:25:55 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
bool EndDict(Context const& context) override
|
2022-01-25 04:25:55 +00:00
|
|
|
{
|
2022-01-28 22:46:14 +00:00
|
|
|
BasicHandler::EndDict(context);
|
2022-01-25 04:25:55 +00:00
|
|
|
|
2022-01-28 18:39:45 +00:00
|
|
|
if (tr_address_is_valid_for_peers(&pex_.addr, pex_.port))
|
|
|
|
{
|
|
|
|
response_.pex.push_back(pex_);
|
|
|
|
pex_ = {};
|
|
|
|
}
|
2022-01-25 04:25:55 +00:00
|
|
|
|
2022-01-28 18:39:45 +00:00
|
|
|
return true;
|
2022-01-25 04:25:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 00:31:24 +00:00
|
|
|
bool Int64(int64_t value, Context const& /*context*/) override
|
2022-01-25 04:25:55 +00:00
|
|
|
{
|
2022-01-29 22:26:09 +00:00
|
|
|
if (auto const key = currentKey(); key == "interval")
|
2022-01-28 18:39:45 +00:00
|
|
|
{
|
|
|
|
response_.interval = value;
|
|
|
|
}
|
|
|
|
else if (key == "min interval"sv)
|
|
|
|
{
|
|
|
|
response_.min_interval = value;
|
|
|
|
}
|
|
|
|
else if (key == "complete"sv)
|
|
|
|
{
|
|
|
|
response_.seeders = value;
|
|
|
|
}
|
|
|
|
else if (key == "incomplete"sv)
|
|
|
|
{
|
|
|
|
response_.leechers = value;
|
|
|
|
}
|
|
|
|
else if (key == "downloaded"sv)
|
|
|
|
{
|
|
|
|
response_.downloads = value;
|
|
|
|
}
|
|
|
|
else if (key == "port"sv)
|
|
|
|
{
|
2022-04-21 15:58:13 +00:00
|
|
|
pex_.port.setHost(static_cast<uint16_t>(value));
|
2022-01-28 18:39:45 +00:00
|
|
|
}
|
2022-03-18 00:31:24 +00:00
|
|
|
else
|
2022-01-28 22:46:14 +00:00
|
|
|
{
|
2022-03-18 00:31:24 +00:00
|
|
|
tr_logAddDebug(fmt::format("unexpected key '{}' int '{}'", key, value), log_name_);
|
2022-01-28 22:46:14 +00:00
|
|
|
}
|
2022-01-25 04:25:55 +00:00
|
|
|
|
2022-01-28 18:39:45 +00:00
|
|
|
return true;
|
2022-01-25 04:25:55 +00:00
|
|
|
}
|
2022-01-28 18:39:45 +00:00
|
|
|
|
2022-03-18 00:31:24 +00:00
|
|
|
bool String(std::string_view value, Context const& /*context*/) override
|
2022-01-25 04:25:55 +00:00
|
|
|
{
|
2022-01-29 22:26:09 +00:00
|
|
|
if (auto const key = currentKey(); key == "failure reason"sv)
|
2022-01-28 18:39:45 +00:00
|
|
|
{
|
|
|
|
response_.errmsg = value;
|
|
|
|
}
|
|
|
|
else if (key == "warning message"sv)
|
|
|
|
{
|
|
|
|
response_.warning = value;
|
|
|
|
}
|
|
|
|
else if (key == "tracker id"sv)
|
|
|
|
{
|
|
|
|
response_.tracker_id = value;
|
|
|
|
}
|
|
|
|
else if (key == "peers"sv)
|
|
|
|
{
|
|
|
|
response_.pex = tr_peerMgrCompactToPex(std::data(value), std::size(value), nullptr, 0);
|
|
|
|
}
|
|
|
|
else if (key == "peers6"sv)
|
|
|
|
{
|
|
|
|
response_.pex6 = tr_peerMgrCompact6ToPex(std::data(value), std::size(value), nullptr, 0);
|
|
|
|
}
|
|
|
|
else if (key == "ip")
|
|
|
|
{
|
|
|
|
tr_address_from_string(&pex_.addr, value);
|
|
|
|
}
|
2022-01-28 22:46:14 +00:00
|
|
|
else if (key == "peer id")
|
|
|
|
{
|
|
|
|
// unused
|
|
|
|
}
|
2022-02-24 13:59:58 +00:00
|
|
|
else if (key == "external ip"sv && std::size(value) == 4)
|
|
|
|
{
|
2022-04-21 22:06:00 +00:00
|
|
|
auto const [addr, out] = tr_address::fromCompact4(reinterpret_cast<uint8_t const*>(std::data(value)));
|
|
|
|
response_.external_ip = addr;
|
2022-02-24 13:59:58 +00:00
|
|
|
}
|
2022-03-18 00:31:24 +00:00
|
|
|
else
|
2022-01-28 22:46:14 +00:00
|
|
|
{
|
2022-03-18 00:31:24 +00:00
|
|
|
tr_logAddDebug(fmt::format("unexpected key '{}' int '{}'", key, value), log_name_);
|
2022-01-28 22:46:14 +00:00
|
|
|
}
|
2022-01-28 18:39:45 +00:00
|
|
|
|
|
|
|
return true;
|
2022-01-25 04:25:55 +00:00
|
|
|
}
|
2022-01-28 18:39:45 +00:00
|
|
|
};
|
2022-01-25 04:25:55 +00:00
|
|
|
|
2022-01-28 18:39:45 +00:00
|
|
|
auto stack = transmission::benc::ParserStack<MaxBencDepth>{};
|
2022-03-18 00:31:24 +00:00
|
|
|
auto handler = AnnounceHandler{ response, log_name };
|
2022-01-28 22:46:14 +00:00
|
|
|
tr_error* error = nullptr;
|
|
|
|
transmission::benc::parse(benc, stack, handler, nullptr, &error);
|
|
|
|
if (error != nullptr)
|
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddWarn(
|
|
|
|
fmt::format(
|
|
|
|
_("Couldn't parse announce response: {error} ({error_code})"),
|
|
|
|
fmt::arg("error", error->message),
|
|
|
|
fmt::arg("error_code", error->code)),
|
|
|
|
log_name);
|
2022-01-28 22:46:14 +00:00
|
|
|
tr_error_clear(&error);
|
|
|
|
}
|
2022-01-25 04:25:55 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
struct announce_data
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
tr_announce_response response;
|
|
|
|
tr_announce_response_func response_func;
|
|
|
|
void* response_func_user_data;
|
|
|
|
char log_name[128];
|
|
|
|
};
|
|
|
|
|
2022-02-17 23:35:57 +00:00
|
|
|
static void onAnnounceDone(tr_web::FetchResponse const& web_response)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
auto const& [status, body, did_connect, did_timeout, vdata] = web_response;
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* data = static_cast<struct announce_data*>(vdata);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-10-14 14:22:28 +00:00
|
|
|
tr_announce_response* const response = &data->response;
|
2011-03-11 04:19:01 +00:00
|
|
|
response->did_connect = did_connect;
|
|
|
|
response->did_timeout = did_timeout;
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace("Got announce response", data->log_name);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
if (status != HTTP_OK)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
auto const* const response_str = tr_webGetResponseStr(status);
|
2022-03-30 19:59:13 +00:00
|
|
|
response->errmsg = fmt::format(FMT_STRING("Tracker HTTP response {:d} ({:s}"), status, response_str);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-10 05:51:14 +00:00
|
|
|
tr_announcerParseHttpAnnounceResponse(*response, body, data->log_name);
|
2022-01-25 04:25:55 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2022-01-25 04:25:55 +00:00
|
|
|
if (!std::empty(response->pex6))
|
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace(fmt::format("got a peers6 length of {}", std::size(response->pex6)), data->log_name);
|
2022-01-25 04:25:55 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2022-01-25 04:25:55 +00:00
|
|
|
if (!std::empty(response->pex))
|
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace(fmt::format("got a peers length of {}", std::size(response->pex)), data->log_name);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
if (data->response_func != nullptr)
|
|
|
|
{
|
|
|
|
data->response_func(&data->response, data->response_func_user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete data;
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
void tr_tracker_http_announce(
|
|
|
|
tr_session* session,
|
|
|
|
tr_announce_request const* request,
|
|
|
|
tr_announce_response_func response_func,
|
2017-04-19 12:04:45 +00:00
|
|
|
void* response_func_user_data)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-01-25 04:25:55 +00:00
|
|
|
auto* const d = new announce_data();
|
2011-03-11 04:19:01 +00:00
|
|
|
d->response_func = response_func;
|
|
|
|
d->response_func_user_data = response_func_user_data;
|
2021-11-04 00:55:04 +00:00
|
|
|
d->response.info_hash = request->info_hash;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_strlcpy(d->log_name, request->log_name, sizeof(d->log_name));
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
auto const url = announce_url_new(session, request);
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace(fmt::format("Sending announce to libcurl: '{}'", url), request->log_name);
|
2022-02-16 18:33:50 +00:00
|
|
|
|
2022-03-28 22:13:32 +00:00
|
|
|
auto options = tr_web::FetchOptions{ url.sv(), onAnnounceDone, d };
|
2022-02-16 18:33:50 +00:00
|
|
|
options.timeout_secs = 90L;
|
|
|
|
options.sndbuf = 1024;
|
|
|
|
options.rcvbuf = 3072;
|
|
|
|
session->web->fetch(std::move(options));
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
***** SCRAPE
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2022-03-10 05:51:14 +00:00
|
|
|
void tr_announcerParseHttpScrapeResponse(tr_scrape_response& response, std::string_view benc, char const* log_name)
|
2022-01-27 23:18:50 +00:00
|
|
|
{
|
2022-01-28 18:39:45 +00:00
|
|
|
verboseLog("Scrape response:", TR_DOWN, benc);
|
2022-01-27 23:18:50 +00:00
|
|
|
|
2022-01-28 18:39:45 +00:00
|
|
|
struct ScrapeHandler final : public transmission::benc::BasicHandler<MaxBencDepth>
|
2022-01-27 23:18:50 +00:00
|
|
|
{
|
2022-01-28 18:39:45 +00:00
|
|
|
using BasicHandler = transmission::benc::BasicHandler<MaxBencDepth>;
|
2022-01-27 23:18:50 +00:00
|
|
|
|
2022-01-28 18:39:45 +00:00
|
|
|
tr_scrape_response& response_;
|
2022-03-18 00:31:24 +00:00
|
|
|
std::string_view const log_name_;
|
2022-01-28 18:39:45 +00:00
|
|
|
std::optional<size_t> row_;
|
2022-01-27 23:18:50 +00:00
|
|
|
|
2022-03-18 00:31:24 +00:00
|
|
|
explicit ScrapeHandler(tr_scrape_response& response, std::string_view const log_name)
|
2022-01-28 18:39:45 +00:00
|
|
|
: response_{ response }
|
2022-03-18 00:31:24 +00:00
|
|
|
, log_name_{ log_name }
|
2022-01-28 18:39:45 +00:00
|
|
|
{
|
|
|
|
}
|
2022-01-27 23:18:50 +00:00
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
bool Key(std::string_view value, Context const& context) override
|
2022-01-27 23:18:50 +00:00
|
|
|
{
|
2022-01-28 22:46:14 +00:00
|
|
|
BasicHandler::Key(value, context);
|
2022-01-28 18:39:45 +00:00
|
|
|
|
2022-01-29 22:26:09 +00:00
|
|
|
if (auto needle = tr_sha1_digest_t{}; depth() == 2 && key(1) == "files"sv && std::size(value) == std::size(needle))
|
2022-01-27 23:18:50 +00:00
|
|
|
{
|
2022-01-28 18:39:45 +00:00
|
|
|
std::copy_n(reinterpret_cast<std::byte const*>(std::data(value)), std::size(value), std::data(needle));
|
|
|
|
auto const it = std::find_if(
|
|
|
|
std::begin(response_.rows),
|
|
|
|
std::end(response_.rows),
|
|
|
|
[needle](auto const& row) { return row.info_hash == needle; });
|
2022-01-27 23:18:50 +00:00
|
|
|
|
2022-01-28 18:39:45 +00:00
|
|
|
if (it == std::end(response_.rows))
|
2022-01-27 23:18:50 +00:00
|
|
|
{
|
2022-01-28 18:39:45 +00:00
|
|
|
row_.reset();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
row_ = std::distance(std::begin(response_.rows), it);
|
2022-01-27 23:18:50 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-28 18:39:45 +00:00
|
|
|
|
|
|
|
return true;
|
2022-01-27 23:18:50 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 00:31:24 +00:00
|
|
|
bool Int64(int64_t value, Context const& /*context*/) override
|
2022-01-28 18:39:45 +00:00
|
|
|
{
|
2022-02-02 01:09:11 +00:00
|
|
|
if (auto const key = currentKey(); row_ && key == "complete"sv)
|
2022-01-28 18:39:45 +00:00
|
|
|
{
|
|
|
|
response_.rows[*row_].seeders = value;
|
|
|
|
}
|
2022-01-28 22:46:14 +00:00
|
|
|
else if (row_ && key == "downloaded"sv)
|
2022-01-28 18:39:45 +00:00
|
|
|
{
|
|
|
|
response_.rows[*row_].downloads = value;
|
|
|
|
}
|
2022-01-28 22:46:14 +00:00
|
|
|
else if (row_ && key == "incomplete"sv)
|
2022-01-28 18:39:45 +00:00
|
|
|
{
|
|
|
|
response_.rows[*row_].leechers = value;
|
|
|
|
}
|
2022-02-24 13:59:58 +00:00
|
|
|
else if (row_ && key == "downloaders"sv)
|
|
|
|
{
|
|
|
|
response_.rows[*row_].downloaders = value;
|
|
|
|
}
|
|
|
|
else if (key == "min_request_interval"sv)
|
|
|
|
{
|
|
|
|
response_.min_request_interval = value;
|
|
|
|
}
|
2022-03-18 00:31:24 +00:00
|
|
|
else
|
2022-01-28 22:46:14 +00:00
|
|
|
{
|
2022-03-18 00:31:24 +00:00
|
|
|
tr_logAddDebug(fmt::format("unexpected key '{}' int '{}'", key, value), log_name_);
|
2022-01-28 22:46:14 +00:00
|
|
|
}
|
2022-01-28 18:39:45 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-18 00:31:24 +00:00
|
|
|
bool String(std::string_view value, Context const& /*context*/) override
|
2022-01-28 18:39:45 +00:00
|
|
|
{
|
2022-02-02 01:09:11 +00:00
|
|
|
if (auto const key = currentKey(); depth() == 1 && key == "failure reason"sv)
|
2022-01-28 18:39:45 +00:00
|
|
|
{
|
|
|
|
response_.errmsg = value;
|
|
|
|
}
|
2022-03-18 00:31:24 +00:00
|
|
|
else
|
2022-01-28 22:46:14 +00:00
|
|
|
{
|
2022-03-18 00:31:24 +00:00
|
|
|
tr_logAddDebug(fmt::format("unexpected key '{}' str '{}'", key, value), log_name_);
|
2022-01-28 22:46:14 +00:00
|
|
|
}
|
2022-01-28 18:39:45 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto stack = transmission::benc::ParserStack<MaxBencDepth>{};
|
2022-03-18 00:31:24 +00:00
|
|
|
auto handler = ScrapeHandler{ response, log_name };
|
2022-01-28 18:39:45 +00:00
|
|
|
tr_error* error = nullptr;
|
|
|
|
transmission::benc::parse(benc, stack, handler, nullptr, &error);
|
|
|
|
if (error != nullptr)
|
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddWarn(
|
2022-03-16 00:51:36 +00:00
|
|
|
fmt::format(
|
|
|
|
_("Couldn't parse scrape response: {error} ({error_code})"),
|
|
|
|
fmt::arg("error", error->message),
|
2022-03-17 22:39:06 +00:00
|
|
|
fmt::arg("error_code", error->code)),
|
|
|
|
log_name);
|
2022-01-28 18:39:45 +00:00
|
|
|
tr_error_clear(&error);
|
|
|
|
}
|
2022-01-27 23:18:50 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
struct scrape_data
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
tr_scrape_response response;
|
|
|
|
tr_scrape_response_func response_func;
|
|
|
|
void* response_func_user_data;
|
|
|
|
char log_name[128];
|
|
|
|
};
|
|
|
|
|
2022-02-17 23:35:57 +00:00
|
|
|
static void onScrapeDone(tr_web::FetchResponse const& web_response)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
auto const& [status, body, did_connect, did_timeout, vdata] = web_response;
|
|
|
|
auto* const data = static_cast<struct scrape_data*>(vdata);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2022-01-27 23:18:50 +00:00
|
|
|
tr_scrape_response& response = data->response;
|
|
|
|
response.did_connect = did_connect;
|
|
|
|
response.did_timeout = did_timeout;
|
2021-11-02 23:00:01 +00:00
|
|
|
|
2022-01-27 23:18:50 +00:00
|
|
|
auto const scrape_url_sv = response.scrape_url.sv();
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace(fmt::format("Got scrape response for '{}'", scrape_url_sv), data->log_name);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
if (status != HTTP_OK)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
auto const* const response_str = tr_webGetResponseStr(status);
|
2022-03-30 19:59:13 +00:00
|
|
|
response.errmsg = fmt::format(FMT_STRING("Tracker HTTP response {:d} ({:s})"), status, response_str);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2022-03-11 21:09:22 +00:00
|
|
|
else if (!std::empty(body))
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-03-10 05:51:14 +00:00
|
|
|
tr_announcerParseHttpScrapeResponse(response, body, data->log_name);
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (data->response_func != nullptr)
|
|
|
|
{
|
|
|
|
data->response_func(&data->response, data->response_func_user_data);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
delete data;
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 22:13:32 +00:00
|
|
|
static auto scrape_url_new(tr_scrape_request const* req)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2021-12-23 17:16:05 +00:00
|
|
|
auto const sv = req->scrape_url.sv();
|
2022-03-28 22:13:32 +00:00
|
|
|
char delimiter = tr_strvContains(sv, '?') ? '&' : '?';
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-03-28 22:13:32 +00:00
|
|
|
auto scrape_url = tr_pathbuf{ sv };
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < req->info_hash_count; ++i)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char str[SHA_DIGEST_LENGTH * 3 + 1];
|
|
|
|
tr_http_escape_sha1(str, req->info_hash[i]);
|
2022-03-28 22:13:32 +00:00
|
|
|
scrape_url.append(delimiter, "info_hash=", str);
|
2011-03-11 04:19:01 +00:00
|
|
|
delimiter = '&';
|
|
|
|
}
|
|
|
|
|
2022-03-28 22:13:32 +00:00
|
|
|
return scrape_url;
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
void tr_tracker_http_scrape(
|
|
|
|
tr_session* session,
|
|
|
|
tr_scrape_request const* request,
|
|
|
|
tr_scrape_response_func response_func,
|
2017-04-19 12:04:45 +00:00
|
|
|
void* response_func_user_data)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-01-25 04:25:55 +00:00
|
|
|
auto* d = new scrape_data();
|
2021-11-02 23:00:01 +00:00
|
|
|
d->response.scrape_url = request->scrape_url;
|
2011-03-11 04:19:01 +00:00
|
|
|
d->response_func = response_func;
|
|
|
|
d->response_func_user_data = response_func_user_data;
|
|
|
|
d->response.row_count = request->info_hash_count;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < d->response.row_count; ++i)
|
2011-10-14 00:27:14 +00:00
|
|
|
{
|
2021-11-04 00:55:04 +00:00
|
|
|
d->response.rows[i].info_hash = request->info_hash[i];
|
2011-10-14 00:27:14 +00:00
|
|
|
d->response.rows[i].seeders = -1;
|
|
|
|
d->response.rows[i].leechers = -1;
|
|
|
|
d->response.rows[i].downloads = -1;
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_strlcpy(d->log_name, request->log_name, sizeof(d->log_name));
|
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
auto const url = scrape_url_new(request);
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace(fmt::format("Sending scrape to libcurl: '{}'", url), request->log_name);
|
2022-02-16 18:33:50 +00:00
|
|
|
|
|
|
|
auto options = tr_web::FetchOptions{ url, onScrapeDone, d };
|
|
|
|
options.timeout_secs = 30L;
|
|
|
|
options.sndbuf = 4096;
|
|
|
|
options.rcvbuf = 4096;
|
|
|
|
session->web->fetch(std::move(options));
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|