2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © 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
|
|
|
|
2022-07-26 02:45:54 +00:00
|
|
|
#include <algorithm> // std::copy_n()
|
2022-09-21 23:34:18 +00:00
|
|
|
#include <cctype>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <cstddef> // std::byte, size_t
|
|
|
|
#include <cstdint> // int64_t, uint8_t, uint...
|
2022-01-25 04:25:55 +00:00
|
|
|
#include <iomanip>
|
2022-03-28 22:13:32 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <iterator>
|
2022-06-18 01:33:11 +00:00
|
|
|
#include <optional>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <string_view>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2011-03-13 14:33:28 +00:00
|
|
|
|
2022-06-18 01:33:11 +00:00
|
|
|
#include <curl/curl.h>
|
|
|
|
|
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
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
|
|
|
|
|
|
|
#include "libtransmission/announcer-common.h"
|
|
|
|
#include "libtransmission/benc.h"
|
|
|
|
#include "libtransmission/crypto-utils.h"
|
|
|
|
#include "libtransmission/error.h"
|
|
|
|
#include "libtransmission/log.h"
|
|
|
|
#include "libtransmission/net.h"
|
|
|
|
#include "libtransmission/peer-mgr.h" /* pex */
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/session.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/tr-assert.h"
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/tr-macros.h"
|
|
|
|
#include "libtransmission/tr-strbuf.h" // tr_strbuf, tr_urlbuf
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/utils.h"
|
|
|
|
#include "libtransmission/web-utils.h"
|
|
|
|
#include "libtransmission/web.h"
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-12-28 15:08:04 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
namespace
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2023-01-07 18:58:16 +00:00
|
|
|
void verboseLog(std::string_view description, tr_direction direction, std::string_view message)
|
2022-01-25 04:25:55 +00:00
|
|
|
{
|
2024-02-18 04:43:24 +00:00
|
|
|
if (static bool const verbose = tr_env_key_exists("TR_CURL_VERBOSE"); !verbose)
|
2022-01-25 04:25:55 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const direction_sv = direction == TR_DOWN ? "<< "sv : ">> "sv;
|
2024-02-18 04:43:24 +00:00
|
|
|
auto& out = std::cerr;
|
2023-11-21 15:02:03 +00:00
|
|
|
out << description << '\n' << "[raw]"sv << direction_sv;
|
2022-07-27 14:03:13 +00:00
|
|
|
for (unsigned char const ch : message)
|
2022-01-25 04:25:55 +00:00
|
|
|
{
|
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(' ');
|
|
|
|
}
|
|
|
|
}
|
2023-11-21 15:02:03 +00:00
|
|
|
out << '\n' << "[b64]"sv << direction_sv << tr_base64_encode(message) << '\n';
|
2022-01-25 04:25:55 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
auto constexpr MaxBencDepth = 8;
|
|
|
|
} // namespace
|
2022-01-28 18:39:45 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Announce
|
2022-01-25 04:25:55 +00:00
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
namespace announce_helpers
|
|
|
|
{
|
|
|
|
[[nodiscard]] constexpr std::string_view get_event_string(tr_announce_request const& req)
|
|
|
|
{
|
|
|
|
return req.partial_seed && (req.event != TR_ANNOUNCE_EVENT_STOPPED) ? "paused"sv : tr_announce_event_get_string(req.event);
|
2022-01-25 04:25:55 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 15:47:45 +00:00
|
|
|
struct http_announce_data
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-11-19 05:00:25 +00:00
|
|
|
http_announce_data(tr_sha1_digest_t info_hash_in, tr_announce_response_func on_response_in, std::string_view log_name_in)
|
|
|
|
: info_hash{ info_hash_in }
|
|
|
|
, on_response{ std::move(on_response_in) }
|
|
|
|
, log_name{ log_name_in }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-23 05:51:15 +00:00
|
|
|
tr_sha1_digest_t info_hash = {};
|
2022-06-18 01:33:11 +00:00
|
|
|
std::optional<tr_announce_response> previous_response;
|
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
tr_announce_response_func on_response;
|
2022-06-18 01:33:11 +00:00
|
|
|
|
2022-09-23 05:51:15 +00:00
|
|
|
uint8_t requests_sent_count = {};
|
|
|
|
uint8_t requests_answered_count = {};
|
2022-06-18 01:33:11 +00:00
|
|
|
|
2022-08-26 18:35:28 +00:00
|
|
|
std::string log_name;
|
2022-02-16 18:33:50 +00:00
|
|
|
};
|
|
|
|
|
2023-11-08 15:54:19 +00:00
|
|
|
bool handleAnnounceResponse(tr_web::FetchResponse const& web_response, tr_announce_response& response)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2023-11-23 05:02:21 +00:00
|
|
|
auto const& [status, body, primary_ip, did_connect, did_timeout, vdata] = web_response;
|
2022-11-28 15:45:39 +00:00
|
|
|
auto const& log_name = static_cast<http_announce_data const*>(vdata)->log_name;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2023-11-08 15:54:19 +00:00
|
|
|
response.did_connect = did_connect;
|
|
|
|
response.did_timeout = did_timeout;
|
2022-11-28 15:45:39 +00:00
|
|
|
tr_logAddTrace("Got announce response", 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);
|
2023-11-08 15:54:19 +00:00
|
|
|
response.errmsg = fmt::format("Tracker HTTP response {:d} ({:s})", status, response_str);
|
2022-06-18 01:33:11 +00:00
|
|
|
|
|
|
|
return false;
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2022-06-18 01:33:11 +00:00
|
|
|
|
2023-11-08 15:54:19 +00:00
|
|
|
tr_announcerParseHttpAnnounceResponse(response, body, log_name);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2023-11-08 15:54:19 +00:00
|
|
|
if (!std::empty(response.pex6))
|
2022-01-25 04:25:55 +00:00
|
|
|
{
|
2023-11-08 15:54:19 +00:00
|
|
|
tr_logAddTrace(fmt::format("got a peers6 length of {}", std::size(response.pex6)), log_name);
|
2022-01-25 04:25:55 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2023-11-08 15:54:19 +00:00
|
|
|
if (!std::empty(response.pex))
|
2022-01-25 04:25:55 +00:00
|
|
|
{
|
2023-11-08 15:54:19 +00:00
|
|
|
tr_logAddTrace(fmt::format("got a peers length of {}", std::size(response.pex)), log_name);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 01:33:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
void onAnnounceDone(tr_web::FetchResponse const& web_response)
|
2022-06-18 01:33:11 +00:00
|
|
|
{
|
2023-11-23 05:02:21 +00:00
|
|
|
auto const& [status, body, primary_ip, did_connect, did_timeout, vdata] = web_response;
|
2023-11-08 15:54:19 +00:00
|
|
|
auto* data = static_cast<http_announce_data*>(vdata);
|
2022-06-18 01:33:11 +00:00
|
|
|
|
2023-11-08 15:54:19 +00:00
|
|
|
auto const got_all_responses = ++data->requests_answered_count == data->requests_sent_count;
|
2022-06-18 01:33:11 +00:00
|
|
|
|
2023-11-08 15:54:19 +00:00
|
|
|
TR_ASSERT(data->on_response);
|
|
|
|
if (data->on_response)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-06-18 01:33:11 +00:00
|
|
|
tr_announce_response response;
|
|
|
|
response.info_hash = data->info_hash;
|
|
|
|
|
2023-11-08 15:54:19 +00:00
|
|
|
if (handleAnnounceResponse(web_response, response))
|
2022-06-18 01:33:11 +00:00
|
|
|
{
|
2022-11-19 05:00:25 +00:00
|
|
|
data->on_response(response);
|
2022-06-18 01:33:11 +00:00
|
|
|
}
|
2023-11-08 15:54:19 +00:00
|
|
|
else if (got_all_responses)
|
2022-06-18 01:33:11 +00:00
|
|
|
{
|
|
|
|
auto const* response_used = &response;
|
|
|
|
|
2022-08-31 16:28:54 +00:00
|
|
|
// All requests have been answered, but none were successful.
|
2022-06-18 01:33:11 +00:00
|
|
|
// Choose the one that went further to report.
|
|
|
|
if (data->previous_response && !response.did_connect && !response.did_timeout)
|
|
|
|
{
|
|
|
|
response_used = &*data->previous_response;
|
|
|
|
}
|
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
data->on_response(*response_used);
|
2022-06-18 01:33:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// There is still one request pending that might succeed, so store
|
|
|
|
// the response for later. There is only room for 1 previous response,
|
|
|
|
// because there can be at most 2 requests.
|
|
|
|
TR_ASSERT(!data->previous_response);
|
|
|
|
data->previous_response = std::move(response);
|
|
|
|
}
|
|
|
|
}
|
2022-02-16 18:33:50 +00:00
|
|
|
|
2023-11-08 15:54:19 +00:00
|
|
|
// Free data if no more responses are expected
|
|
|
|
if (got_all_responses)
|
2022-06-18 01:33:11 +00:00
|
|
|
{
|
|
|
|
delete data;
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 02:27:52 +00:00
|
|
|
void announce_url_new(tr_urlbuf& url, tr_session const* session, tr_announce_request const& req)
|
|
|
|
{
|
|
|
|
url.clear();
|
|
|
|
auto out = std::back_inserter(url);
|
|
|
|
|
|
|
|
auto escaped_info_hash = tr_urlbuf{};
|
|
|
|
tr_urlPercentEncode(std::back_inserter(escaped_info_hash), req.info_hash);
|
|
|
|
|
|
|
|
fmt::format_to(
|
|
|
|
out,
|
|
|
|
"{url}"
|
|
|
|
"{sep}info_hash={info_hash}"
|
|
|
|
"&peer_id={peer_id}"
|
|
|
|
"&port={port}"
|
|
|
|
"&uploaded={uploaded}"
|
|
|
|
"&downloaded={downloaded}"
|
|
|
|
"&left={left}"
|
|
|
|
"&numwant={numwant}"
|
2023-06-21 16:53:06 +00:00
|
|
|
"&key={key:08X}"
|
2022-12-09 02:27:52 +00:00
|
|
|
"&compact=1"
|
|
|
|
"&supportcrypto=1",
|
|
|
|
fmt::arg("url", req.announce_url),
|
2023-06-30 14:49:58 +00:00
|
|
|
fmt::arg("sep", tr_strv_contains(req.announce_url.sv(), '?') ? '&' : '?'),
|
2022-12-09 02:27:52 +00:00
|
|
|
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) }),
|
|
|
|
fmt::arg("port", req.port.host()),
|
|
|
|
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));
|
|
|
|
|
|
|
|
if (session->encryptionMode() == TR_ENCRYPTION_REQUIRED)
|
|
|
|
{
|
|
|
|
fmt::format_to(out, "&requirecrypto=1");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.corrupt != 0)
|
|
|
|
{
|
|
|
|
fmt::format_to(out, "&corrupt={}", req.corrupt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto const str = get_event_string(req); !std::empty(str))
|
|
|
|
{
|
|
|
|
fmt::format_to(out, "&event={}", str);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!std::empty(req.tracker_id))
|
|
|
|
{
|
|
|
|
fmt::format_to(out, "&trackerid={}", req.tracker_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::string format_ip_arg(std::string_view ip)
|
|
|
|
{
|
2022-12-31 19:13:59 +00:00
|
|
|
return fmt::format("&ip={:s}", ip);
|
2022-12-09 02:27:52 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
} // namespace announce_helpers
|
|
|
|
} // namespace
|
2022-12-09 02:27:52 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
void tr_tracker_http_announce(
|
2022-09-06 17:52:58 +00:00
|
|
|
tr_session const* session,
|
2022-11-19 05:00:25 +00:00
|
|
|
tr_announce_request const& request,
|
|
|
|
tr_announce_response_func on_response)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2023-01-07 18:58:16 +00:00
|
|
|
using namespace announce_helpers;
|
2022-12-09 02:27:52 +00:00
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
auto* const d = new http_announce_data{ request.info_hash, std::move(on_response), request.log_name };
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2022-06-18 01:33:11 +00:00
|
|
|
/* There are two alternative techniques for announcing both IPv4 and
|
|
|
|
IPv6 addresses. Previous version of BEP-7 suggests adding "ipv4="
|
|
|
|
and "ipv6=" parameters to the announce URL, while OpenTracker and
|
|
|
|
newer version of BEP-7 requires that peers announce once per each
|
|
|
|
public address they want to use.
|
|
|
|
|
|
|
|
We should ensure that we send the announce both via IPv6 and IPv4,
|
2023-03-28 17:59:42 +00:00
|
|
|
but no longer use the "ipv4=" and "ipv6=" parameters. So, we no
|
|
|
|
longer need to compute the global IPv4 and IPv6 addresses.
|
2022-06-18 01:33:11 +00:00
|
|
|
*/
|
2022-08-19 19:33:06 +00:00
|
|
|
auto url = tr_urlbuf{};
|
|
|
|
announce_url_new(url, session, request);
|
|
|
|
auto options = tr_web::FetchOptions{ url.sv(), onAnnounceDone, d };
|
2022-11-30 01:38:53 +00:00
|
|
|
options.timeout_secs = TR_ANNOUNCE_TIMEOUT_SEC;
|
2022-06-08 22:51:54 +00:00
|
|
|
options.sndbuf = 4096;
|
|
|
|
options.rcvbuf = 4096;
|
2022-06-18 01:33:11 +00:00
|
|
|
|
|
|
|
auto do_make_request = [&](std::string_view const& protocol_name, tr_web::FetchOptions&& opt)
|
|
|
|
{
|
2022-11-19 05:00:25 +00:00
|
|
|
tr_logAddTrace(fmt::format("Sending {} announce to libcurl: '{}'", protocol_name, opt.url), request.log_name);
|
2022-10-14 02:25:02 +00:00
|
|
|
session->fetch(std::move(opt));
|
2022-06-18 01:33:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Before Curl 7.77.0, if we explicitly choose the IP version we want
|
|
|
|
* to use, it is still possible that the wrong one is used. The workaround
|
|
|
|
* is expensive (disabling DNS cache), so instead we have to make do with
|
2023-11-08 15:54:19 +00:00
|
|
|
* a request that we don't know whether it will go through IPv6 or IPv4.
|
2022-06-18 01:33:11 +00:00
|
|
|
*/
|
2023-01-26 17:25:53 +00:00
|
|
|
static auto const use_curl_workaround = curl_version_info(CURLVERSION_NOW)->version_num < 0x074D00 /* 7.77.0 */;
|
2023-03-28 17:59:42 +00:00
|
|
|
if (use_curl_workaround || session->useAnnounceIP())
|
2022-06-18 01:33:11 +00:00
|
|
|
{
|
2022-07-20 19:04:52 +00:00
|
|
|
if (session->useAnnounceIP())
|
|
|
|
{
|
|
|
|
options.url += format_ip_arg(session->announceIP());
|
|
|
|
}
|
2022-06-18 01:33:11 +00:00
|
|
|
|
|
|
|
d->requests_sent_count = 1;
|
|
|
|
do_make_request(""sv, std::move(options));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-28 17:59:42 +00:00
|
|
|
d->requests_sent_count = 2;
|
2022-06-18 01:33:11 +00:00
|
|
|
|
2023-11-08 15:54:19 +00:00
|
|
|
// First try to send the announce via IPv4
|
2023-03-28 17:59:42 +00:00
|
|
|
auto ipv4_options = options;
|
|
|
|
ipv4_options.ip_proto = tr_web::FetchOptions::IPProtocol::V4;
|
|
|
|
do_make_request("IPv4"sv, std::move(ipv4_options));
|
2022-06-18 01:33:11 +00:00
|
|
|
|
2023-11-08 15:54:19 +00:00
|
|
|
// Then try to send via IPv6
|
2023-03-28 17:59:42 +00:00
|
|
|
options.ip_proto = tr_web::FetchOptions::IPProtocol::V6;
|
|
|
|
do_make_request("IPv6"sv, std::move(options));
|
2022-06-18 01:33:11 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
void tr_announcerParseHttpAnnounceResponse(tr_announce_response& response, std::string_view benc, std::string_view log_name)
|
2022-01-27 23:18:50 +00:00
|
|
|
{
|
2023-01-07 18:58:16 +00:00
|
|
|
verboseLog("Announce response:", TR_DOWN, benc);
|
2022-01-27 23:18:50 +00:00
|
|
|
|
2023-01-07 18:58:16 +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-27 23:18:50 +00:00
|
|
|
|
2023-01-07 18:58:16 +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_;
|
2024-02-13 16:42:19 +00:00
|
|
|
tr_pex pex_;
|
2022-01-27 23:18:50 +00:00
|
|
|
|
2023-01-07 18:58:16 +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-28 18:39:45 +00:00
|
|
|
{
|
|
|
|
}
|
2022-01-27 23:18:50 +00:00
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
bool StartDict(Context const& context) override
|
2022-01-27 23:18:50 +00:00
|
|
|
{
|
2023-01-07 18:58:16 +00:00
|
|
|
BasicHandler::StartDict(context);
|
2022-01-28 18:39:45 +00:00
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
pex_ = {};
|
2022-01-27 23:18:50 +00:00
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EndDict(Context const& context) override
|
|
|
|
{
|
|
|
|
BasicHandler::EndDict(context);
|
|
|
|
|
2024-03-24 22:09:51 +00:00
|
|
|
if (pex_.is_valid())
|
2023-01-07 18:58:16 +00:00
|
|
|
{
|
|
|
|
response_.pex.push_back(pex_);
|
|
|
|
pex_ = {};
|
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
|
|
|
{
|
2023-01-07 18:58:16 +00:00
|
|
|
if (auto const key = currentKey(); key == "interval")
|
2022-01-28 18:39:45 +00:00
|
|
|
{
|
2023-01-07 18:58:16 +00:00
|
|
|
response_.interval = static_cast<int>(value);
|
2022-01-28 18:39:45 +00:00
|
|
|
}
|
2023-01-07 18:58:16 +00:00
|
|
|
else if (key == "min interval"sv)
|
2022-01-28 18:39:45 +00:00
|
|
|
{
|
2023-01-07 18:58:16 +00:00
|
|
|
response_.min_interval = static_cast<int>(value);
|
2022-01-28 18:39:45 +00:00
|
|
|
}
|
2023-01-07 18:58:16 +00:00
|
|
|
else if (key == "complete"sv)
|
2022-01-28 18:39:45 +00:00
|
|
|
{
|
2023-09-01 21:51:58 +00:00
|
|
|
response_.seeders = value;
|
2022-01-28 18:39:45 +00:00
|
|
|
}
|
2023-01-07 18:58:16 +00:00
|
|
|
else if (key == "incomplete"sv)
|
2022-02-24 13:59:58 +00:00
|
|
|
{
|
2023-09-01 21:51:58 +00:00
|
|
|
response_.leechers = value;
|
2022-02-24 13:59:58 +00:00
|
|
|
}
|
2023-01-07 18:58:16 +00:00
|
|
|
else if (key == "downloaded"sv)
|
2022-02-24 13:59:58 +00:00
|
|
|
{
|
2023-09-01 21:51:58 +00:00
|
|
|
response_.downloads = value;
|
2023-01-07 18:58:16 +00:00
|
|
|
}
|
|
|
|
else if (key == "port"sv)
|
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
pex_.socket_address.port_.set_host(static_cast<uint16_t>(value));
|
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-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
|
|
|
{
|
2023-01-07 18:58:16 +00:00
|
|
|
if (auto const key = currentKey(); key == "failure reason"sv)
|
2022-01-28 18:39:45 +00:00
|
|
|
{
|
|
|
|
response_.errmsg = value;
|
|
|
|
}
|
2023-01-07 18:58:16 +00:00
|
|
|
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_pex::from_compact_ipv4(std::data(value), std::size(value), nullptr, 0);
|
|
|
|
}
|
|
|
|
else if (key == "peers6"sv)
|
|
|
|
{
|
|
|
|
response_.pex6 = tr_pex::from_compact_ipv6(std::data(value), std::size(value), nullptr, 0);
|
|
|
|
}
|
|
|
|
else if (key == "ip")
|
|
|
|
{
|
|
|
|
if (auto const addr = tr_address::from_string(value); addr)
|
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
pex_.socket_address.address_ = *addr;
|
2023-01-07 18:58:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (key == "peer id")
|
|
|
|
{
|
|
|
|
// unused
|
|
|
|
}
|
|
|
|
else if (key == "external ip"sv && std::size(value) == 4)
|
|
|
|
{
|
|
|
|
auto const [addr, out] = tr_address::from_compact_ipv4(reinterpret_cast<std::byte const*>(std::data(value)));
|
|
|
|
response_.external_ip = addr;
|
|
|
|
}
|
2022-03-18 00:31:24 +00:00
|
|
|
else
|
2022-01-28 22:46:14 +00:00
|
|
|
{
|
2023-01-07 18:58:16 +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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto stack = transmission::benc::ParserStack<MaxBencDepth>{};
|
2023-01-07 18:58:16 +00:00
|
|
|
auto handler = AnnounceHandler{ response, log_name };
|
2023-11-04 16:39:41 +00:00
|
|
|
auto error = tr_error{};
|
2022-01-28 18:39:45 +00:00
|
|
|
transmission::benc::parse(benc, stack, handler, nullptr, &error);
|
2023-11-04 16:39:41 +00:00
|
|
|
if (error)
|
2022-01-28 18:39:45 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddWarn(
|
2022-03-16 00:51:36 +00:00
|
|
|
fmt::format(
|
2023-01-07 18:58:16 +00:00
|
|
|
_("Couldn't parse announce response: {error} ({error_code})"),
|
2023-11-04 16:39:41 +00:00
|
|
|
fmt::arg("error", error.message()),
|
|
|
|
fmt::arg("error_code", error.code())),
|
2022-03-17 22:39:06 +00:00
|
|
|
log_name);
|
2022-01-28 18:39:45 +00:00
|
|
|
}
|
2022-01-27 23:18:50 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
// ---
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
namespace scrape_helpers
|
|
|
|
{
|
2022-11-19 05:00:25 +00:00
|
|
|
class scrape_data
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-11-19 05:00:25 +00:00
|
|
|
public:
|
|
|
|
scrape_data(tr_scrape_response_func response_func, std::string_view log_name)
|
|
|
|
: response_func_{ std::move(response_func) }
|
|
|
|
, log_name_{ log_name }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto& response() noexcept
|
|
|
|
{
|
|
|
|
return response_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto const& log_name() const noexcept
|
|
|
|
{
|
|
|
|
return log_name_;
|
|
|
|
}
|
|
|
|
|
2022-11-24 16:17:02 +00:00
|
|
|
void invoke_callback() const
|
2022-11-19 05:00:25 +00:00
|
|
|
{
|
|
|
|
if (response_func_)
|
|
|
|
{
|
|
|
|
response_func_(response_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
tr_scrape_response response_ = {};
|
2024-02-13 16:42:19 +00:00
|
|
|
tr_scrape_response_func response_func_;
|
2022-11-19 05:00:25 +00:00
|
|
|
std::string log_name_;
|
2022-02-16 18:33:50 +00:00
|
|
|
};
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
void onScrapeDone(tr_web::FetchResponse const& web_response)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2023-11-23 05:02:21 +00:00
|
|
|
auto const& [status, body, primary_ip, did_connect, did_timeout, vdata] = web_response;
|
2022-11-19 05:00:25 +00:00
|
|
|
auto* const data = static_cast<scrape_data*>(vdata);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
auto& response = data->response();
|
2022-01-27 23:18:50 +00:00
|
|
|
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-11-19 05:00:25 +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);
|
2023-11-21 15:02:03 +00:00
|
|
|
response.errmsg = fmt::format("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-11-19 05:00:25 +00:00
|
|
|
tr_announcerParseHttpScrapeResponse(response, body, data->log_name());
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
data->invoke_callback();
|
2022-02-16 18:33:50 +00:00
|
|
|
delete data;
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
void scrape_url_new(tr_pathbuf& scrape_url, tr_scrape_request const& req)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-11-19 05:00:25 +00:00
|
|
|
scrape_url = req.scrape_url.sv();
|
2023-06-30 14:49:58 +00:00
|
|
|
char delimiter = tr_strv_contains(scrape_url, '?') ? '&' : '?';
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
for (int i = 0; i < req.info_hash_count; ++i)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-08-19 19:33:06 +00:00
|
|
|
scrape_url.append(delimiter, "info_hash=");
|
2022-11-19 05:00:25 +00:00
|
|
|
tr_urlPercentEncode(std::back_inserter(scrape_url), req.info_hash[i]);
|
2011-03-11 04:19:01 +00:00
|
|
|
delimiter = '&';
|
|
|
|
}
|
|
|
|
}
|
2023-01-07 18:58:16 +00:00
|
|
|
} // namespace scrape_helpers
|
|
|
|
} // namespace
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
void tr_tracker_http_scrape(tr_session const* session, tr_scrape_request const& request, tr_scrape_response_func on_response)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2023-01-07 18:58:16 +00:00
|
|
|
using namespace scrape_helpers;
|
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
auto* d = new scrape_data{ std::move(on_response), request.log_name };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
auto& response = d->response();
|
|
|
|
response.scrape_url = request.scrape_url;
|
|
|
|
response.row_count = request.info_hash_count;
|
|
|
|
for (int i = 0; i < response.row_count; ++i)
|
2011-10-14 00:27:14 +00:00
|
|
|
{
|
2022-11-19 05:00:25 +00:00
|
|
|
response.rows[i].info_hash = request.info_hash[i];
|
2011-10-14 00:27:14 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2022-08-19 19:33:06 +00:00
|
|
|
auto scrape_url = tr_pathbuf{};
|
|
|
|
scrape_url_new(scrape_url, request);
|
2022-11-19 05:00:25 +00:00
|
|
|
tr_logAddTrace(fmt::format("Sending scrape to libcurl: '{}'", scrape_url), request.log_name);
|
2022-08-19 19:33:06 +00:00
|
|
|
auto options = tr_web::FetchOptions{ scrape_url, onScrapeDone, d };
|
2022-11-30 01:38:53 +00:00
|
|
|
options.timeout_secs = TR_SCRAPE_TIMEOUT_SEC;
|
2022-02-16 18:33:50 +00:00
|
|
|
options.sndbuf = 4096;
|
|
|
|
options.rcvbuf = 4096;
|
2022-10-14 02:25:02 +00:00
|
|
|
session->fetch(std::move(options));
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2023-01-07 18:58:16 +00:00
|
|
|
|
|
|
|
void tr_announcerParseHttpScrapeResponse(tr_scrape_response& response, std::string_view benc, std::string_view log_name)
|
|
|
|
{
|
|
|
|
verboseLog("Scrape response:", TR_DOWN, benc);
|
|
|
|
|
|
|
|
struct ScrapeHandler final : public transmission::benc::BasicHandler<MaxBencDepth>
|
|
|
|
{
|
|
|
|
using BasicHandler = transmission::benc::BasicHandler<MaxBencDepth>;
|
|
|
|
|
|
|
|
tr_scrape_response& response_;
|
|
|
|
std::string_view const log_name_;
|
|
|
|
std::optional<size_t> row_;
|
|
|
|
|
|
|
|
explicit ScrapeHandler(tr_scrape_response& response, std::string_view const log_name)
|
|
|
|
: response_{ response }
|
|
|
|
, log_name_{ log_name }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Key(std::string_view value, Context const& context) override
|
|
|
|
{
|
|
|
|
BasicHandler::Key(value, context);
|
|
|
|
|
|
|
|
if (auto needle = tr_sha1_digest_t{}; depth() == 2 && key(1) == "files"sv && std::size(value) == std::size(needle))
|
|
|
|
{
|
|
|
|
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; });
|
|
|
|
|
|
|
|
if (it == std::end(response_.rows))
|
|
|
|
{
|
|
|
|
row_.reset();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
row_ = std::distance(std::begin(response_.rows), it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Int64(int64_t value, Context const& /*context*/) override
|
|
|
|
{
|
|
|
|
if (auto const key = currentKey(); row_ && key == "complete"sv)
|
|
|
|
{
|
2023-09-01 21:51:58 +00:00
|
|
|
response_.rows[*row_].seeders = value;
|
2023-01-07 18:58:16 +00:00
|
|
|
}
|
|
|
|
else if (row_ && key == "downloaded"sv)
|
|
|
|
{
|
2023-09-01 21:51:58 +00:00
|
|
|
response_.rows[*row_].downloads = value;
|
2023-01-07 18:58:16 +00:00
|
|
|
}
|
|
|
|
else if (row_ && key == "incomplete"sv)
|
|
|
|
{
|
2023-09-01 21:51:58 +00:00
|
|
|
response_.rows[*row_].leechers = value;
|
2023-01-07 18:58:16 +00:00
|
|
|
}
|
|
|
|
else if (row_ && key == "downloaders"sv)
|
|
|
|
{
|
2023-09-01 21:51:58 +00:00
|
|
|
response_.rows[*row_].downloaders = value;
|
2023-01-07 18:58:16 +00:00
|
|
|
}
|
|
|
|
else if (key == "min_request_interval"sv)
|
|
|
|
{
|
|
|
|
response_.min_request_interval = static_cast<int>(value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_logAddDebug(fmt::format("unexpected key '{}' int '{}'", key, value), log_name_);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool String(std::string_view value, Context const& /*context*/) override
|
|
|
|
{
|
|
|
|
if (auto const key = currentKey(); depth() == 1 && key == "failure reason"sv)
|
|
|
|
{
|
|
|
|
response_.errmsg = value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_logAddDebug(fmt::format("unexpected key '{}' str '{}'", key, value), log_name_);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto stack = transmission::benc::ParserStack<MaxBencDepth>{};
|
|
|
|
auto handler = ScrapeHandler{ response, log_name };
|
2023-11-04 16:39:41 +00:00
|
|
|
auto error = tr_error{};
|
2023-01-07 18:58:16 +00:00
|
|
|
transmission::benc::parse(benc, stack, handler, nullptr, &error);
|
2023-11-04 16:39:41 +00:00
|
|
|
if (error)
|
2023-01-07 18:58:16 +00:00
|
|
|
{
|
|
|
|
tr_logAddWarn(
|
|
|
|
fmt::format(
|
|
|
|
_("Couldn't parse scrape response: {error} ({error_code})"),
|
2023-11-04 16:39:41 +00:00
|
|
|
fmt::arg("error", error.message()),
|
|
|
|
fmt::arg("error_code", error.code())),
|
2023-01-07 18:58:16 +00:00
|
|
|
log_name);
|
|
|
|
}
|
|
|
|
}
|