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 <iostream>
|
|
|
|
#include <iomanip>
|
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/buffer.h>
|
|
|
|
#include <event2/http.h> /* for HTTP_OK */
|
|
|
|
|
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"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "trevent.h" /* tr_runInEventThread() */
|
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
|
|
|
|
2017-05-22 20:12:57 +00:00
|
|
|
#define dbgmsg(name, ...) tr_logAddDeepNamed(name, __VA_ARGS__)
|
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
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
static std::string announce_url_new(tr_session const* session, tr_announce_request const* req)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2021-12-23 17:16:05 +00:00
|
|
|
auto const announce_sv = req->announce_url.sv();
|
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
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
auto* const buf = evbuffer_new();
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_expand(buf, 1024);
|
2021-08-15 09:41:48 +00:00
|
|
|
evbuffer_add_printf(
|
|
|
|
buf,
|
2021-11-02 23:00:01 +00:00
|
|
|
"%" TR_PRIsv
|
2017-04-19 12:04:45 +00:00
|
|
|
"%c"
|
|
|
|
"info_hash=%s"
|
2021-11-01 22:14:17 +00:00
|
|
|
"&peer_id=%" TR_PRIsv
|
2017-04-19 12:04:45 +00:00
|
|
|
"&port=%d"
|
2021-08-15 09:41:48 +00:00
|
|
|
"&uploaded=%" PRIu64 //
|
|
|
|
"&downloaded=%" PRIu64 //
|
2017-05-01 09:31:01 +00:00
|
|
|
"&left=%" PRIu64
|
2017-04-19 12:04:45 +00:00
|
|
|
"&numwant=%d"
|
|
|
|
"&key=%x"
|
|
|
|
"&compact=1"
|
|
|
|
"&supportcrypto=1",
|
2021-11-02 23:00:01 +00:00
|
|
|
TR_PRIsv_ARG(announce_sv),
|
2022-02-07 04:28:36 +00:00
|
|
|
announce_sv.find('?') == std::string_view::npos ? '?' : '&',
|
2022-01-23 05:41:01 +00:00
|
|
|
std::data(escaped_info_hash),
|
2021-11-01 22:14:17 +00:00
|
|
|
TR_PRIsv_ARG(req->peer_id),
|
2017-04-19 12:04:45 +00:00
|
|
|
req->port,
|
|
|
|
req->up,
|
|
|
|
req->down,
|
2017-05-01 09:31:01 +00:00
|
|
|
req->leftUntilComplete,
|
2017-04-19 12:04:45 +00:00
|
|
|
req->numwant,
|
|
|
|
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
|
|
|
{
|
|
|
|
evbuffer_add_printf(buf, "&requirecrypto=1");
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
evbuffer_add_printf(buf, "&corrupt=%" PRIu64, req->corrupt);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
evbuffer_add_printf(buf, "&event=%s", str);
|
|
|
|
}
|
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-01-23 05:41:01 +00:00
|
|
|
evbuffer_add_printf(buf, "&trackerid=%" TR_PRIsv, TR_PRIsv_ARG(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));
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add_printf(buf, "&ipv6=");
|
2022-01-28 18:39:45 +00:00
|
|
|
tr_http_escape(buf, std::data(ipv6_readable), true);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
return evbuffer_free_to_str(buf);
|
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;
|
|
|
|
|
|
|
|
void tr_announcerParseHttpAnnounceResponse(tr_announce_response& response, std::string_view benc)
|
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_;
|
|
|
|
std::optional<size_t> row_;
|
|
|
|
tr_pex pex_ = {};
|
2022-01-25 04:25:55 +00:00
|
|
|
|
2022-01-28 18:39:45 +00:00
|
|
|
explicit AnnounceHandler(tr_announce_response& response)
|
|
|
|
: response_{ response }
|
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-01-28 22:46:14 +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)
|
|
|
|
{
|
|
|
|
pex_.port = htons(uint16_t(value));
|
|
|
|
}
|
2022-01-28 22:46:14 +00:00
|
|
|
else if (!tr_error_is_set(context.error))
|
|
|
|
{
|
|
|
|
auto const msg = tr_strvJoin("unexpected int: key["sv, key, "] value["sv, std::to_string(value), "]"sv);
|
|
|
|
tr_error_set(context.error, EINVAL, msg);
|
|
|
|
}
|
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-01-28 22:46:14 +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
|
|
|
|
}
|
|
|
|
else if (!tr_error_is_set(context.error))
|
|
|
|
{
|
|
|
|
tr_error_set(context.error, EINVAL, tr_strvJoin("unexpected str: key["sv, key, "] value["sv, value, "]"sv));
|
|
|
|
}
|
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>{};
|
|
|
|
auto handler = AnnounceHandler{ response };
|
2022-01-28 22:46:14 +00:00
|
|
|
tr_error* error = nullptr;
|
|
|
|
transmission::benc::parse(benc, stack, handler, nullptr, &error);
|
|
|
|
if (error != nullptr)
|
|
|
|
{
|
|
|
|
tr_logAddError("%s", error->message);
|
|
|
|
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];
|
|
|
|
};
|
|
|
|
|
|
|
|
static void onAnnounceDone(tr_web::FetchResponse&& web_response)
|
|
|
|
{
|
|
|
|
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;
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(data->log_name, "Got announce response");
|
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);
|
|
|
|
response->errmsg = tr_strvJoin("Tracker HTTP response "sv, std::to_string(status), " ("sv, response_str, ")"sv);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
tr_announcerParseHttpAnnounceResponse(*response, body);
|
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))
|
|
|
|
{
|
|
|
|
dbgmsg(data->log_name, "got a peers6 length of %zu", std::size(response->pex6));
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2022-01-25 04:25:55 +00:00
|
|
|
if (!std::empty(response->pex))
|
|
|
|
{
|
|
|
|
dbgmsg(data->log_name, "got a peers length of %zu", std::size(response->pex));
|
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);
|
|
|
|
dbgmsg(request->log_name, "Sending announce to libcurl: \"%" TR_PRIsv "\"", TR_PRIsv_ARG(url));
|
2022-02-16 18:33:50 +00:00
|
|
|
|
|
|
|
auto options = tr_web::FetchOptions{ url, onAnnounceDone, d };
|
|
|
|
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-01-28 18:39:45 +00:00
|
|
|
void tr_announcerParseHttpScrapeResponse(tr_scrape_response& response, std::string_view benc)
|
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_;
|
|
|
|
std::optional<size_t> row_;
|
2022-01-27 23:18:50 +00:00
|
|
|
|
2022-01-28 18:39:45 +00:00
|
|
|
explicit ScrapeHandler(tr_scrape_response& response)
|
|
|
|
: response_{ response }
|
|
|
|
{
|
|
|
|
}
|
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-01-28 22:46:14 +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-01-28 22:46:14 +00:00
|
|
|
else if (!tr_error_is_set(context.error))
|
|
|
|
{
|
|
|
|
auto const errmsg = tr_strvJoin("unexpected int: key["sv, key, "] value["sv, std::to_string(value), "]"sv);
|
|
|
|
tr_error_set(context.error, EINVAL, errmsg);
|
|
|
|
}
|
2022-01-28 18:39:45 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-28 22:46:14 +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-01-28 22:46:14 +00:00
|
|
|
else if (!tr_error_is_set(context.error))
|
|
|
|
{
|
|
|
|
tr_error_set(context.error, EINVAL, tr_strvJoin("unexpected string: key["sv, key, "] value["sv, value, "]"sv));
|
|
|
|
}
|
2022-01-28 18:39:45 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto stack = transmission::benc::ParserStack<MaxBencDepth>{};
|
|
|
|
auto handler = ScrapeHandler{ response };
|
|
|
|
tr_error* error = nullptr;
|
|
|
|
transmission::benc::parse(benc, stack, handler, nullptr, &error);
|
|
|
|
if (error != nullptr)
|
|
|
|
{
|
2022-01-28 22:46:14 +00:00
|
|
|
tr_logAddError("%s", error->message);
|
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];
|
|
|
|
};
|
|
|
|
|
|
|
|
static void onScrapeDone(tr_web::FetchResponse&& web_response)
|
|
|
|
{
|
|
|
|
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();
|
2021-11-02 23:00:01 +00:00
|
|
|
dbgmsg(data->log_name, "Got scrape response for \"%" TR_PRIsv "\"", TR_PRIsv_ARG(scrape_url_sv));
|
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);
|
|
|
|
response.errmsg = tr_strvJoin("Tracker HTTP response "sv, std::to_string(status), " ("sv, response_str, ")"sv);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
tr_announcerParseHttpScrapeResponse(response, body);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
static std::string 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();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
auto* const buf = evbuffer_new();
|
|
|
|
evbuffer_add(buf, std::data(sv), std::size(sv));
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
char delimiter = sv.find('?') == std::string_view::npos ? '?' : '&';
|
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]);
|
|
|
|
evbuffer_add_printf(buf, "%cinfo_hash=%s", delimiter, str);
|
2011-03-11 04:19:01 +00:00
|
|
|
delimiter = '&';
|
|
|
|
}
|
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
return evbuffer_free_to_str(buf);
|
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);
|
|
|
|
dbgmsg(request->log_name, "Sending scrape to libcurl: \"%" TR_PRIsv "\"", TR_PRIsv_ARG(url));
|
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
|
|
|
}
|