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.
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
2021-10-14 15:36:49 +00:00
|
|
|
#include <array>
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <cinttypes> // PRIu64
|
|
|
|
#include <climits> // INT_MAX
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <cstdio>
|
2021-12-16 22:58:58 +00:00
|
|
|
#include <cstring>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <ctime>
|
2022-01-23 05:41:01 +00:00
|
|
|
#include <deque>
|
2022-05-27 01:29:10 +00:00
|
|
|
#include <iterator>
|
2021-12-23 17:16:05 +00:00
|
|
|
#include <map>
|
2022-04-28 15:52:26 +00:00
|
|
|
#include <numeric>
|
2021-09-29 23:42:50 +00:00
|
|
|
#include <set>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
2021-10-14 15:36:49 +00:00
|
|
|
#include <string_view>
|
2021-09-30 18:18:46 +00:00
|
|
|
#include <vector>
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/buffer.h>
|
2011-03-11 04:19:01 +00:00
|
|
|
#include <event2/event.h> /* evtimer */
|
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#define LIBTRANSMISSION_ANNOUNCER_MODULE
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
2021-12-14 20:59:40 +00:00
|
|
|
|
|
|
|
#include "announce-list.h"
|
2011-03-11 04:19:01 +00:00
|
|
|
#include "announcer-common.h"
|
2021-12-14 20:59:40 +00:00
|
|
|
#include "announcer.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "crypto-utils.h" /* tr_rand_int(), tr_rand_int_weak() */
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "peer-mgr.h" /* tr_peerMgrCompactToPex() */
|
2009-09-25 21:05:59 +00:00
|
|
|
#include "session.h"
|
|
|
|
#include "torrent.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2009-09-25 21:05:59 +00:00
|
|
|
#include "utils.h"
|
2021-11-09 03:30:03 +00:00
|
|
|
#include "web-utils.h"
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
#define tr_logAddErrorTier(tier, msg) tr_logAddError(msg, (tier)->buildLogName())
|
|
|
|
#define tr_logAddWarnTier(tier, msg) tr_logAddWarn(msg, (tier)->buildLogName())
|
|
|
|
#define tr_logAddDebugTier(tier, msg) tr_logAddDebug(msg, (tier)->buildLogName())
|
|
|
|
#define tr_logAddTraceTier(tier, msg) tr_logAddTrace(msg, (tier)->buildLogName())
|
2022-03-10 05:51:14 +00:00
|
|
|
|
2021-10-21 00:39:05 +00:00
|
|
|
/* unless the tracker says otherwise, rescrape this frequently */
|
|
|
|
static auto constexpr DefaultScrapeIntervalSec = int{ 60 * 30 };
|
|
|
|
/* unless the tracker says otherwise, this is the announce interval */
|
|
|
|
static auto constexpr DefaultAnnounceIntervalSec = int{ 60 * 10 };
|
|
|
|
/* unless the tracker says otherwise, this is the announce min_interval */
|
|
|
|
static auto constexpr DefaultAnnounceMinIntervalSec = int{ 60 * 2 };
|
|
|
|
/* the value of the 'numwant' argument passed in tracker requests. */
|
|
|
|
static auto constexpr Numwant = int{ 80 };
|
|
|
|
|
|
|
|
/* how often to announce & scrape */
|
|
|
|
static auto constexpr UpkeepIntervalMsec = int{ 500 };
|
|
|
|
static auto constexpr MaxAnnouncesPerUpkeep = int{ 20 };
|
|
|
|
static auto constexpr MaxScrapesPerUpkeep = int{ 20 };
|
|
|
|
|
|
|
|
/* this is how often to call the UDP tracker upkeep */
|
|
|
|
static auto constexpr TauUpkeepIntervalSecs = int{ 5 };
|
|
|
|
|
|
|
|
/* how many infohashes to remove when we get a scrape-too-long error */
|
|
|
|
static auto constexpr TrMultiscrapeStep = int{ 5 };
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_announce_event_get_string(tr_announce_event e)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (e)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_ANNOUNCE_EVENT_COMPLETED:
|
|
|
|
return "completed";
|
|
|
|
|
|
|
|
case TR_ANNOUNCE_EVENT_STARTED:
|
|
|
|
return "started";
|
|
|
|
|
|
|
|
case TR_ANNOUNCE_EVENT_STOPPED:
|
|
|
|
return "stopped";
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "";
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 23:42:50 +00:00
|
|
|
namespace
|
2011-02-05 18:46:10 +00:00
|
|
|
{
|
|
|
|
|
2021-09-29 23:42:50 +00:00
|
|
|
struct StopsCompare
|
|
|
|
{
|
2022-04-05 03:51:56 +00:00
|
|
|
[[nodiscard]] static int compare(tr_announce_request const* a, tr_announce_request const* b) noexcept // <=>
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-29 23:42:50 +00:00
|
|
|
// primary key: volume of data transferred
|
2021-11-04 00:55:04 +00:00
|
|
|
auto const ax = a->up + a->down;
|
|
|
|
auto const bx = b->up + b->down;
|
|
|
|
if (ax < bx)
|
2021-09-29 23:42:50 +00:00
|
|
|
{
|
2021-11-04 00:55:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ax > bx)
|
|
|
|
{
|
|
|
|
return 1;
|
2021-09-29 23:42:50 +00:00
|
|
|
}
|
2011-02-05 18:46:10 +00:00
|
|
|
|
2021-09-29 23:42:50 +00:00
|
|
|
// secondary key: the torrent's info_hash
|
2021-11-04 00:55:04 +00:00
|
|
|
if (a->info_hash < b->info_hash)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a->info_hash > b->info_hash)
|
2021-09-29 23:42:50 +00:00
|
|
|
{
|
2021-11-04 00:55:04 +00:00
|
|
|
return 1;
|
2021-09-29 23:42:50 +00:00
|
|
|
}
|
2011-06-19 18:26:04 +00:00
|
|
|
|
2021-09-29 23:42:50 +00:00
|
|
|
// tertiary key: the tracker's announce url
|
2021-11-04 00:55:04 +00:00
|
|
|
if (a->announce_url < b->announce_url)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a->announce_url > b->announce_url)
|
2021-11-02 23:00:01 +00:00
|
|
|
{
|
2021-11-04 00:55:04 +00:00
|
|
|
return 1;
|
2021-11-02 23:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-06-19 18:26:04 +00:00
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
[[nodiscard]] bool operator()(tr_announce_request const* a, tr_announce_request const* b) const noexcept // less than
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-29 23:42:50 +00:00
|
|
|
return compare(a, b) < 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-09-29 23:42:50 +00:00
|
|
|
};
|
2011-06-19 18:26:04 +00:00
|
|
|
|
2021-09-29 23:42:50 +00:00
|
|
|
} // namespace
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2019-02-16 20:19:38 +00:00
|
|
|
struct tr_scrape_info
|
|
|
|
{
|
|
|
|
int multiscrape_max;
|
|
|
|
|
2022-04-28 13:44:24 +00:00
|
|
|
tr_interned_string scrape_url;
|
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
tr_scrape_info(tr_interned_string scrape_url_in, int const multiscrape_max_in)
|
2022-04-28 13:44:24 +00:00
|
|
|
: multiscrape_max{ multiscrape_max_in }
|
|
|
|
, scrape_url{ scrape_url_in }
|
2021-09-30 18:18:46 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
2019-02-16 20:19:38 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
static void onUpkeepTimer(evutil_socket_t fd, short what, void* vannouncer);
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
/**
|
|
|
|
* "global" (per-tr_session) fields
|
|
|
|
*/
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_announcer
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
explicit tr_announcer(tr_session* session_in)
|
|
|
|
: session{ session_in }
|
|
|
|
, upkeep_timer{ evtimer_new(session_in->event_base, onUpkeepTimer, this) }
|
|
|
|
{
|
|
|
|
scheduleNextUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
~tr_announcer()
|
|
|
|
{
|
|
|
|
event_free(upkeep_timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void scheduleNextUpdate() const
|
|
|
|
{
|
2022-04-08 00:20:29 +00:00
|
|
|
tr_timerAddMsec(*this->upkeep_timer, UpkeepIntervalMsec);
|
2022-01-23 05:41:01 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 23:42:50 +00:00
|
|
|
std::set<tr_announce_request*, StopsCompare> stops;
|
2021-12-23 17:16:05 +00:00
|
|
|
std::map<tr_interned_string, tr_scrape_info> scrape_info;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
tr_session* const session;
|
|
|
|
event* const upkeep_timer;
|
2022-04-28 13:44:24 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
time_t tau_upkeep_at = 0;
|
2022-04-28 13:44:24 +00:00
|
|
|
|
|
|
|
int const key = tr_rand_int(INT_MAX);
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
static tr_scrape_info* tr_announcerGetScrapeInfo(tr_announcer* announcer, tr_interned_string url)
|
2019-02-16 20:19:38 +00:00
|
|
|
{
|
2021-12-23 17:16:05 +00:00
|
|
|
if (std::empty(url))
|
2019-02-16 20:19:38 +00:00
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
return nullptr;
|
2019-02-16 20:19:38 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
auto& scrapes = announcer->scrape_info;
|
2022-04-02 22:42:51 +00:00
|
|
|
auto const [it, is_new] = scrapes.try_emplace(url, url, TR_MULTISCRAPE_MAX);
|
|
|
|
return &it->second;
|
2019-02-16 20:19:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_announcerInit(tr_session* session)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isSession(session));
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
auto* a = new tr_announcer{ session };
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
session->announcer = a;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void flushCloseMessages(tr_announcer* announcer);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_announcerClose(tr_session* session)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_announcer* announcer = session->announcer;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
flushCloseMessages(announcer);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_tracker_udp_start_shutdown(session);
|
2011-03-17 18:51:31 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
session->announcer = nullptr;
|
2021-09-29 23:42:50 +00:00
|
|
|
delete announcer;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
/* a row in tr_tier's list of trackers */
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_tracker
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
explicit tr_tracker(tr_announcer* announcer, tr_announce_list::tracker_info const& info)
|
|
|
|
: host{ info.host }
|
2022-04-16 20:41:51 +00:00
|
|
|
, announce_url{ info.announce }
|
|
|
|
, sitename{ info.sitename }
|
|
|
|
, scrape_info{ std::empty(info.scrape) ? nullptr : tr_announcerGetScrapeInfo(announcer, info.scrape) }
|
2022-01-23 05:41:01 +00:00
|
|
|
, id{ info.id }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-01-23 22:47:41 +00:00
|
|
|
[[nodiscard]] int getRetryInterval() const
|
2022-01-23 05:41:01 +00:00
|
|
|
{
|
|
|
|
switch (consecutive_failures)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
return 20;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
return tr_rand_int_weak(60) + 60 * 5;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
return tr_rand_int_weak(60) + 60 * 15;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
return tr_rand_int_weak(60) + 60 * 30;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
return tr_rand_int_weak(60) + 60 * 60;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return tr_rand_int_weak(60) + 60 * 120;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_interned_string const host;
|
|
|
|
tr_interned_string const announce_url;
|
2022-02-12 17:30:27 +00:00
|
|
|
std::string_view const sitename;
|
2022-01-23 05:41:01 +00:00
|
|
|
tr_scrape_info* const scrape_info;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
std::string tracker_id;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
int seeder_count = -1;
|
|
|
|
int leecher_count = -1;
|
|
|
|
int download_count = -1;
|
|
|
|
int downloader_count = -1;
|
2010-02-13 21:58:38 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
int consecutive_failures = 0;
|
2011-01-20 22:51:05 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
tr_tracker_id_t const id;
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
// format: `${host}:${port}`
|
2021-12-23 17:16:05 +00:00
|
|
|
tr_interned_string tr_announcerGetKey(tr_url_parsed_t const& parsed)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-04-16 18:13:42 +00:00
|
|
|
auto buf = std::array<char, 1024>{};
|
|
|
|
auto* const begin = std::data(buf);
|
|
|
|
auto const* const end = fmt::format_to_n(begin, std::size(buf), "{:s}:{:d}", parsed.host, parsed.port).out;
|
|
|
|
auto const sv = std::string_view{ begin, static_cast<size_t>(end - begin) };
|
|
|
|
|
|
|
|
return tr_interned_string{ sv };
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-01-19 19:37:00 +00:00
|
|
|
/** @brief A group of trackers in a single tier, as per the multitracker spec */
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_tier
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
tr_tier(tr_announcer* announcer, tr_torrent* tor_in, std::vector<tr_announce_list::tracker_info const*> const& infos)
|
|
|
|
: tor{ tor_in }
|
|
|
|
, id{ next_key++ }
|
|
|
|
{
|
|
|
|
trackers.reserve(std::size(infos));
|
|
|
|
for (auto const* info : infos)
|
|
|
|
{
|
|
|
|
trackers.emplace_back(announcer, *info);
|
|
|
|
}
|
|
|
|
useNextTracker();
|
2022-01-28 04:17:11 +00:00
|
|
|
scrapeSoon();
|
2022-01-23 05:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] tr_tracker* currentTracker()
|
|
|
|
{
|
|
|
|
if (!current_tracker_index_)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
TR_ASSERT(*current_tracker_index_ < std::size(trackers));
|
|
|
|
return &trackers[*current_tracker_index_];
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] tr_tracker const* currentTracker() const
|
|
|
|
{
|
|
|
|
if (!current_tracker_index_)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
TR_ASSERT(*current_tracker_index_ < std::size(trackers));
|
|
|
|
return &trackers[*current_tracker_index_];
|
|
|
|
}
|
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
[[nodiscard]] constexpr bool needsToAnnounce(time_t now) const
|
2022-01-23 05:41:01 +00:00
|
|
|
{
|
|
|
|
return !isAnnouncing && !isScraping && announceAt != 0 && announceAt <= now && !std::empty(announce_events);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool needsToScrape(time_t now) const
|
|
|
|
{
|
|
|
|
auto const* const tracker = currentTracker();
|
|
|
|
|
|
|
|
return !isScraping && scrapeAt != 0 && scrapeAt <= now && tracker != nullptr && tracker->scrape_info != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] auto countDownloaders() const
|
|
|
|
{
|
|
|
|
auto const* const tracker = currentTracker();
|
|
|
|
|
|
|
|
return tracker == nullptr ? 0 : tracker->downloader_count + tracker->leecher_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_tracker* useNextTracker()
|
|
|
|
{
|
|
|
|
// move our index to the next tracker in the tier
|
|
|
|
if (std::empty(trackers))
|
|
|
|
{
|
|
|
|
current_tracker_index_ = std::nullopt;
|
|
|
|
}
|
|
|
|
else if (!current_tracker_index_)
|
|
|
|
{
|
|
|
|
current_tracker_index_ = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
current_tracker_index_ = (*current_tracker_index_ + 1) % std::size(trackers);
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset some of the tier's fields
|
|
|
|
scrapeIntervalSec = DefaultScrapeIntervalSec;
|
|
|
|
announceIntervalSec = DefaultAnnounceIntervalSec;
|
|
|
|
announceMinIntervalSec = DefaultAnnounceMinIntervalSec;
|
|
|
|
isAnnouncing = false;
|
|
|
|
isScraping = false;
|
|
|
|
lastAnnounceStartTime = 0;
|
|
|
|
lastScrapeStartTime = 0;
|
|
|
|
|
|
|
|
return currentTracker();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::optional<size_t> indexOf(tr_interned_string const& announce_url) const
|
|
|
|
{
|
|
|
|
for (size_t i = 0, n = std::size(trackers); i < n; ++i)
|
|
|
|
{
|
|
|
|
if (announce_url == trackers[i].announce_url)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2022-03-11 21:09:22 +00:00
|
|
|
[[nodiscard]] std::string buildLogName() const
|
|
|
|
{
|
|
|
|
auto buf = std::array<char, 512>{};
|
|
|
|
buildLogName(std::data(buf), std::size(buf));
|
|
|
|
return std::string{ std::data(buf) };
|
|
|
|
}
|
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
void buildLogName(char* buf, size_t buflen) const
|
|
|
|
{
|
|
|
|
auto const* const torrent_name = tr_torrentName(tor);
|
|
|
|
auto const* const current_tracker = currentTracker();
|
|
|
|
auto const host_sv = current_tracker == nullptr ? "?"sv : current_tracker->host.sv();
|
2022-04-03 18:23:00 +00:00
|
|
|
*fmt::format_to_n(buf, buflen - 1, FMT_STRING("{:s} at {:s}"), torrent_name, host_sv).out = '\0';
|
2022-01-23 05:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool canManualAnnounce() const
|
|
|
|
{
|
|
|
|
return this->manualAnnounceAllowedAt <= tr_time();
|
|
|
|
}
|
|
|
|
|
2022-01-28 04:17:11 +00:00
|
|
|
void scheduleNextScrape()
|
|
|
|
{
|
|
|
|
scheduleNextScrape(this->scrapeIntervalSec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void scrapeSoon()
|
|
|
|
{
|
|
|
|
scheduleNextScrape(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void scheduleNextScrape(int interval)
|
|
|
|
{
|
|
|
|
this->scrapeAt = getNextScrapeTime(tor->session, this, interval);
|
|
|
|
}
|
|
|
|
|
2022-04-28 13:44:24 +00:00
|
|
|
std::deque<tr_announce_event> announce_events;
|
|
|
|
|
|
|
|
std::string last_announce_str;
|
|
|
|
std::string last_scrape_str;
|
2022-01-23 05:41:01 +00:00
|
|
|
|
2010-02-08 16:47:30 +00:00
|
|
|
/* number of up/down/corrupt bytes since the last time we sent an
|
|
|
|
* "event=stopped" message that was acknowledged by the tracker */
|
2022-01-23 05:41:01 +00:00
|
|
|
std::array<uint64_t, 3> byteCounts = {};
|
2010-02-08 16:47:30 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
std::vector<tr_tracker> trackers;
|
2022-04-28 13:44:24 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
std::optional<size_t> current_tracker_index_;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-04-28 13:44:24 +00:00
|
|
|
tr_torrent* const tor;
|
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
time_t scrapeAt = 0;
|
|
|
|
time_t lastScrapeStartTime = 0;
|
|
|
|
time_t lastScrapeTime = 0;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
time_t announceAt = 0;
|
|
|
|
time_t manualAnnounceAllowedAt = 0;
|
|
|
|
time_t lastAnnounceStartTime = 0;
|
|
|
|
time_t lastAnnounceTime = 0;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
int const id;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-04-28 13:44:24 +00:00
|
|
|
int announce_event_priority = 0;
|
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
int scrapeIntervalSec = DefaultScrapeIntervalSec;
|
|
|
|
int announceIntervalSec = DefaultAnnounceIntervalSec;
|
|
|
|
int announceMinIntervalSec = DefaultAnnounceMinIntervalSec;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-05-14 05:31:24 +00:00
|
|
|
size_t lastAnnouncePeerCount = 0;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-04-28 13:44:24 +00:00
|
|
|
bool lastScrapeSucceeded = false;
|
|
|
|
bool lastScrapeTimedOut = false;
|
|
|
|
|
|
|
|
bool lastAnnounceSucceeded = false;
|
|
|
|
bool lastAnnounceTimedOut = false;
|
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
bool isRunning = false;
|
|
|
|
bool isAnnouncing = false;
|
|
|
|
bool isScraping = false;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
private:
|
2022-01-28 04:17:11 +00:00
|
|
|
[[nodiscard]] static time_t getNextScrapeTime(tr_session const* session, tr_tier const* tier, int interval)
|
|
|
|
{
|
|
|
|
// Maybe don't scrape paused torrents
|
|
|
|
if (!tier->isRunning && !session->scrapePausedTorrents)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-01-28 04:17:11 +00:00
|
|
|
/* Add the interval, and then increment to the nearest 10th second.
|
|
|
|
* The latter step is to increase the odds of several torrents coming
|
|
|
|
* due at the same time to improve multiscrape. */
|
|
|
|
auto ret = tr_time() + interval;
|
|
|
|
while (ret % 10 != 0)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
}
|
2022-01-23 05:41:01 +00:00
|
|
|
|
2022-01-28 04:17:11 +00:00
|
|
|
return ret;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-14 14:22:28 +00:00
|
|
|
|
2022-01-28 04:17:11 +00:00
|
|
|
static int next_key;
|
|
|
|
};
|
2011-07-23 17:14:12 +00:00
|
|
|
|
2022-01-28 04:17:11 +00:00
|
|
|
int tr_tier::next_key = 0;
|
2011-04-02 07:44:19 +00:00
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-01-19 19:37:00 +00:00
|
|
|
/**
|
|
|
|
* @brief Opaque, per-torrent data structure for tracker announce information
|
|
|
|
*
|
2009-09-25 21:05:59 +00:00
|
|
|
* this opaque data structure can be found in tr_torrent.tiers
|
|
|
|
*/
|
2022-01-23 05:41:01 +00:00
|
|
|
struct tr_torrent_announcer
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
tr_torrent_announcer(tr_announcer* announcer, tr_torrent* tor)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
// build the trackers
|
|
|
|
auto tier_to_infos = std::map<tr_tracker_tier_t, std::vector<tr_announce_list::tracker_info const*>>{};
|
2022-02-20 19:00:52 +00:00
|
|
|
auto const announce_list = getAnnounceList(tor);
|
2022-01-23 05:41:01 +00:00
|
|
|
for (auto const& info : announce_list)
|
|
|
|
{
|
|
|
|
tier_to_infos[info.tier].emplace_back(&info);
|
|
|
|
}
|
2011-03-31 20:26:43 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
for (auto const& tt : tier_to_infos)
|
|
|
|
{
|
|
|
|
tiers.emplace_back(announcer, tor, tt.second);
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
tr_tier* getTier(int tier_id)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
for (auto& tier : tiers)
|
|
|
|
{
|
|
|
|
if (tier.id == tier_id)
|
|
|
|
{
|
|
|
|
return &tier;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
tr_tier* getTierFromScrape(tr_interned_string const& scrape_url)
|
|
|
|
{
|
|
|
|
for (auto& tier : tiers)
|
|
|
|
{
|
|
|
|
auto const* const tracker = tier.currentTracker();
|
2011-03-31 20:26:43 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
if (tracker != nullptr && tracker->scrape_info != nullptr && tracker->scrape_info->scrape_url == scrape_url)
|
|
|
|
{
|
|
|
|
return &tier;
|
|
|
|
}
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-01-23 22:47:41 +00:00
|
|
|
[[nodiscard]] bool canManualAnnounce() const
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
return std::any_of(std::begin(tiers), std::end(tiers), [](auto const& tier) { return tier.canManualAnnounce(); });
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-01-23 22:47:41 +00:00
|
|
|
[[nodiscard]] bool findTracker(
|
|
|
|
tr_interned_string const& announce_url,
|
|
|
|
tr_tier const** setme_tier,
|
|
|
|
tr_tracker const** setme_tracker) const
|
2022-01-23 05:41:01 +00:00
|
|
|
{
|
|
|
|
for (auto const& tier : tiers)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
for (auto const& tracker : tier.trackers)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
if (tracker.announce_url == announce_url)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
*setme_tier = &tier;
|
|
|
|
*setme_tracker = &tracker;
|
|
|
|
return true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
2022-01-23 05:41:01 +00:00
|
|
|
|
|
|
|
return false;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
std::vector<tr_tier> tiers;
|
|
|
|
|
|
|
|
tr_tracker_callback callback = nullptr;
|
|
|
|
void* callback_data = nullptr;
|
2022-02-20 19:00:52 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
[[nodiscard]] static tr_announce_list getAnnounceList(tr_torrent const* tor)
|
|
|
|
{
|
|
|
|
auto announce_list = tor->announceList();
|
|
|
|
|
2022-02-21 03:25:49 +00:00
|
|
|
// if it's a public torrent, add the default trackers
|
|
|
|
if (tor->isPublic())
|
2022-02-20 19:00:52 +00:00
|
|
|
{
|
|
|
|
announce_list.add(tor->session->defaultTrackers());
|
|
|
|
}
|
|
|
|
|
|
|
|
return announce_list;
|
|
|
|
}
|
2022-01-23 05:41:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static tr_tier* getTier(tr_announcer* announcer, tr_sha1_digest_t const& info_hash, int tier_id)
|
|
|
|
{
|
|
|
|
if (announcer == nullptr)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-03-01 23:06:29 +00:00
|
|
|
auto* const tor = announcer->session->torrents().get(info_hash);
|
2022-01-23 05:41:01 +00:00
|
|
|
if (tor == nullptr || tor->torrent_announcer == nullptr)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tor->torrent_announcer->getTier(tier_id);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** PUBLISH
|
|
|
|
***/
|
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
static void publishMessage(tr_tier* tier, std::string_view msg, TrackerEventType type)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
if (tier != nullptr && tier->tor != nullptr && tier->tor->torrent_announcer != nullptr &&
|
|
|
|
tier->tor->torrent_announcer->callback != nullptr)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
auto* const ta = tier->tor->torrent_announcer;
|
2021-09-12 17:41:49 +00:00
|
|
|
auto event = tr_tracker_event{};
|
2009-09-25 21:05:59 +00:00
|
|
|
event.messageType = type;
|
|
|
|
event.text = msg;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-01-24 00:53:35 +00:00
|
|
|
if (auto const* const current_tracker = tier->currentTracker(); current_tracker != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
event.announce_url = current_tracker->announce_url;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-06-19 14:33:10 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
(*ta->callback)(tier->tor, &event, ta->callback_data);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void publishErrorClear(tr_tier* tier)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-12-23 17:16:05 +00:00
|
|
|
publishMessage(tier, ""sv, TR_TRACKER_ERROR_CLEAR);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
static void publishWarning(tr_tier* tier, std::string_view msg)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
publishMessage(tier, msg, TR_TRACKER_WARNING);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
static void publishError(tr_tier* tier, std::string_view msg)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
publishMessage(tier, msg, TR_TRACKER_ERROR);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 20:25:30 +00:00
|
|
|
static void publishPeerCounts(tr_tier* tier, int seeders, int leechers)
|
2010-05-18 15:14:54 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
if (tier->tor->torrent_announcer->callback != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto e = tr_tracker_event{};
|
2021-09-09 20:25:30 +00:00
|
|
|
e.messageType = TR_TRACKER_COUNTS;
|
|
|
|
e.seeders = seeders;
|
|
|
|
e.leechers = leechers;
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddDebugTier(tier, fmt::format("peer counts: {} seeders, {} leechers.", seeders, leechers));
|
2011-03-11 04:57:53 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
(*tier->tor->torrent_announcer->callback)(tier->tor, &e, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-18 15:14:54 +00:00
|
|
|
}
|
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
static void publishPeersPex(tr_tier* tier, int seeders, int leechers, std::vector<tr_pex> const& pex)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
if (tier->tor->torrent_announcer->callback != nullptr)
|
2011-03-14 02:40:39 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto e = tr_tracker_event{};
|
2011-03-14 02:40:39 +00:00
|
|
|
e.messageType = TR_TRACKER_PEERS;
|
2021-09-09 20:25:30 +00:00
|
|
|
e.seeders = seeders;
|
|
|
|
e.leechers = leechers;
|
2011-03-14 02:40:39 +00:00
|
|
|
e.pex = pex;
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddDebugTier(
|
2022-01-23 05:41:01 +00:00
|
|
|
tier,
|
2022-03-14 04:43:35 +00:00
|
|
|
fmt::format(
|
|
|
|
"tracker knows of {} seeders and {} leechers and gave a list of {} peers.",
|
|
|
|
seeders,
|
|
|
|
leechers,
|
|
|
|
std::size(pex)));
|
2011-03-11 04:57:53 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
(*tier->tor->torrent_announcer->callback)(tier->tor, &e, nullptr);
|
2011-03-14 02:40:39 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
tr_torrent_announcer* tr_announcerAddTorrent(tr_torrent* tor, tr_tracker_callback callback, void* callback_data)
|
2009-10-02 04:54:02 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isTorrent(tor));
|
2009-10-02 04:54:02 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
auto* ta = new tr_torrent_announcer(tor->session->announcer, tor);
|
|
|
|
ta->callback = callback;
|
|
|
|
ta->callback_data = callback_data;
|
|
|
|
return ta;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2011-02-05 16:45:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2009-10-02 04:54:02 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_announcerCanManualAnnounce(tr_torrent const* tor)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isTorrent(tor));
|
2022-01-23 05:41:01 +00:00
|
|
|
TR_ASSERT(tor->torrent_announcer != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
return tor->isRunning && tor->torrent_announcer->canManualAnnounce();
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t tr_announcerNextManualAnnounce(tr_torrent const* tor)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
time_t ret = ~(time_t)0;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
for (auto const& tier : tor->torrent_announcer->tiers)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
if (tier.isRunning)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
ret = std::min(ret, tier.manualAnnounceAllowedAt);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-11 21:09:22 +00:00
|
|
|
static void tr_logAddTrace_tier_announce_queue(tr_tier const* tier)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
if (!tr_logLevelIsActive(TR_LOG_TRACE) || std::empty(tier->announce_events))
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-03-10 05:51:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-22 18:05:49 +00:00
|
|
|
auto buf = std::string{};
|
|
|
|
auto const& events = tier->announce_events;
|
|
|
|
buf.reserve(std::size(events) * 20);
|
|
|
|
for (size_t i = 0, n = std::size(events); i < n; ++i)
|
2022-03-10 05:51:14 +00:00
|
|
|
{
|
2022-04-22 18:05:49 +00:00
|
|
|
fmt::format_to(std::back_inserter(buf), FMT_STRING("[{:d}:{:s}]"), i, tr_announce_event_get_string(events[i]));
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2022-03-10 05:51:14 +00:00
|
|
|
|
2022-04-22 18:05:49 +00:00
|
|
|
tr_logAddTraceTier(tier, buf);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 00:28:22 +00:00
|
|
|
// higher priorities go to the front of the announce queue
|
|
|
|
static void tier_update_announce_priority(tr_tier* tier)
|
|
|
|
{
|
|
|
|
int priority = -1;
|
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
for (auto const& event : tier->announce_events)
|
2021-09-10 00:28:22 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
priority = std::max(priority, int{ event });
|
2021-09-10 00:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tier->announce_event_priority = priority;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tier_announce_remove_trailing(tr_tier* tier, tr_announce_event e)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
while (!std::empty(tier->announce_events) && tier->announce_events.back() == e)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
tier->announce_events.resize(std::size(tier->announce_events) - 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-09-10 00:28:22 +00:00
|
|
|
|
|
|
|
tier_update_announce_priority(tier);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tier_announce_event_push(tr_tier* tier, tr_announce_event e, time_t announceAt)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(tier != nullptr);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTrace_tier_announce_queue(tier);
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceTier(tier, fmt::format("queued '{}'", tr_announce_event_get_string(e)));
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
auto& events = tier->announce_events;
|
|
|
|
if (!std::empty(events))
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
/* special case #1: if we're adding a "stopped" event,
|
|
|
|
* dump everything leading up to it except "completed" */
|
2017-04-19 12:04:45 +00:00
|
|
|
if (e == TR_ANNOUNCE_EVENT_STOPPED)
|
|
|
|
{
|
2022-02-07 04:28:36 +00:00
|
|
|
bool has_completed = std::count(std::begin(events), std::end(events), TR_ANNOUNCE_EVENT_COMPLETED) != 0;
|
2022-01-23 05:41:01 +00:00
|
|
|
events.clear();
|
2012-12-05 17:29:46 +00:00
|
|
|
if (has_completed)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
events.push_back(TR_ANNOUNCE_EVENT_COMPLETED);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* special case #2: dump all empty strings leading up to this event */
|
2017-04-19 12:04:45 +00:00
|
|
|
tier_announce_remove_trailing(tier, TR_ANNOUNCE_EVENT_NONE);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
/* special case #3: no consecutive duplicates */
|
2017-04-19 12:04:45 +00:00
|
|
|
tier_announce_remove_trailing(tier, e);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add it */
|
2022-01-23 05:41:01 +00:00
|
|
|
events.push_back(e);
|
2009-09-25 21:05:59 +00:00
|
|
|
tier->announceAt = announceAt;
|
2021-09-10 00:28:22 +00:00
|
|
|
tier_update_announce_priority(tier);
|
2010-02-10 02:59:15 +00:00
|
|
|
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTrace_tier_announce_queue(tier);
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceTier(tier, fmt::format("announcing in {} seconds", difftime(announceAt, tr_time())));
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
static auto tier_announce_event_pull(tr_tier* tier)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
auto const e = tier->announce_events.front();
|
|
|
|
tier->announce_events.pop_front();
|
2021-09-10 00:28:22 +00:00
|
|
|
tier_update_announce_priority(tier);
|
2011-03-11 05:48:57 +00:00
|
|
|
return e;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void torrentAddAnnounce(tr_torrent* tor, tr_announce_event e, time_t announceAt)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
// tell each tier to announce
|
|
|
|
for (auto& tier : tor->torrent_announcer->tiers)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
tier_announce_event_push(&tier, e, announceAt);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_announcerTorrentStarted(tr_torrent* tor)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
torrentAddAnnounce(tor, TR_ANNOUNCE_EVENT_STARTED, tr_time());
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
void tr_announcerManualAnnounce(tr_torrent* tor)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
torrentAddAnnounce(tor, TR_ANNOUNCE_EVENT_NONE, tr_time());
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
void tr_announcerTorrentStopped(tr_torrent* tor)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
torrentAddAnnounce(tor, TR_ANNOUNCE_EVENT_STOPPED, tr_time());
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
void tr_announcerTorrentCompleted(tr_torrent* tor)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
torrentAddAnnounce(tor, TR_ANNOUNCE_EVENT_COMPLETED, tr_time());
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
void tr_announcerChangeMyPort(tr_torrent* tor)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_announcerTorrentStarted(tor);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2010-02-08 16:47:30 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_announcerAddBytes(tr_torrent* tor, int type, uint32_t byteCount)
|
2010-02-08 16:47:30 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isTorrent(tor));
|
|
|
|
TR_ASSERT(type == TR_ANN_UP || type == TR_ANN_DOWN || type == TR_ANN_CORRUPT);
|
2010-02-08 16:47:30 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
for (auto& tier : tor->torrent_announcer->tiers)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
tier.byteCounts[type] += byteCount;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-02-08 16:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static tr_announce_request* announce_request_new(
|
|
|
|
tr_announcer const* announcer,
|
|
|
|
tr_torrent* tor,
|
|
|
|
tr_tier const* tier,
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_announce_event event)
|
|
|
|
{
|
2022-02-26 17:53:34 +00:00
|
|
|
auto const* const current_tracker = tier->currentTracker();
|
|
|
|
TR_ASSERT(current_tracker != nullptr);
|
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
auto* const req = new tr_announce_request();
|
2022-04-21 15:58:13 +00:00
|
|
|
req->port = announcer->session->peerPort();
|
2022-02-26 17:53:34 +00:00
|
|
|
req->announce_url = current_tracker->announce_url;
|
|
|
|
req->tracker_id = current_tracker->tracker_id;
|
2021-12-24 21:12:33 +00:00
|
|
|
req->info_hash = tor->infoHash();
|
2021-10-22 02:40:55 +00:00
|
|
|
req->peer_id = tr_torrentGetPeerId(tor);
|
2011-03-11 04:19:01 +00:00
|
|
|
req->up = tier->byteCounts[TR_ANN_UP];
|
|
|
|
req->down = tier->byteCounts[TR_ANN_DOWN];
|
|
|
|
req->corrupt = tier->byteCounts[TR_ANN_CORRUPT];
|
2022-03-29 04:29:35 +00:00
|
|
|
req->leftUntilComplete = tor->hasMetainfo() ? tor->totalSize() - tor->hasTotal() : INT64_MAX;
|
2011-03-11 04:19:01 +00:00
|
|
|
req->event = event;
|
2021-10-21 00:39:05 +00:00
|
|
|
req->numwant = event == TR_ANNOUNCE_EVENT_STOPPED ? 0 : Numwant;
|
2011-03-11 04:19:01 +00:00
|
|
|
req->key = announcer->key;
|
2021-12-16 08:49:04 +00:00
|
|
|
req->partial_seed = tor->isPartialSeed();
|
2022-01-23 05:41:01 +00:00
|
|
|
tier->buildLogName(req->log_name, sizeof(req->log_name));
|
2011-03-11 04:19:01 +00:00
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_announcerRemoveTorrent(tr_announcer* announcer, tr_torrent* tor)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
auto* const ta = tor->torrent_announcer;
|
|
|
|
if (ta == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-01-24 00:53:35 +00:00
|
|
|
for (auto const& tier : ta->tiers)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
if (tier.isRunning)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
auto const e = TR_ANNOUNCE_EVENT_STOPPED;
|
|
|
|
auto* req = announce_request_new(announcer, tor, &tier, e);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-07 04:28:36 +00:00
|
|
|
if (announcer->stops.count(req) != 0U)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
delete req;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
announcer->stops.insert(req);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
tor->torrent_announcer = nullptr;
|
|
|
|
delete ta;
|
2011-02-05 16:45:06 +00:00
|
|
|
}
|
|
|
|
|
2011-02-05 18:46:10 +00:00
|
|
|
struct announce_data
|
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
int const tier_id;
|
|
|
|
time_t const time_sent;
|
|
|
|
tr_announce_event event = {};
|
|
|
|
tr_session* session = nullptr;
|
2011-02-05 18:46:10 +00:00
|
|
|
|
|
|
|
/** If the request succeeds, the value for tier's "isRunning" flag */
|
2022-01-23 05:41:01 +00:00
|
|
|
bool is_running_on_success = false;
|
2011-02-05 18:46:10 +00:00
|
|
|
};
|
|
|
|
|
2022-02-25 06:26:19 +00:00
|
|
|
static bool isUnregistered(char const* errmsg)
|
|
|
|
{
|
|
|
|
auto const lower = tr_strlower(errmsg != nullptr ? errmsg : "");
|
|
|
|
|
|
|
|
auto constexpr Keys = std::array<std::string_view, 2>{ "unregistered torrent"sv, "torrent not registered"sv };
|
|
|
|
|
|
|
|
return std::any_of(std::begin(Keys), std::end(Keys), [&lower](auto const& key) { return tr_strvContains(lower, key); });
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void on_announce_error(tr_tier* tier, char const* err, tr_announce_event e)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2011-03-14 16:52:47 +00:00
|
|
|
/* increment the error count */
|
2022-01-23 05:41:01 +00:00
|
|
|
auto* current_tracker = tier->currentTracker();
|
|
|
|
if (current_tracker != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
++current_tracker->consecutive_failures;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-14 16:52:47 +00:00
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
/* set the error message */
|
2022-01-23 05:41:01 +00:00
|
|
|
tier->last_announce_str = err;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
/* switch to the next tracker */
|
2022-01-23 05:41:01 +00:00
|
|
|
current_tracker = tier->useNextTracker();
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2022-02-25 06:26:19 +00:00
|
|
|
if (isUnregistered(err))
|
|
|
|
{
|
2022-03-16 00:51:36 +00:00
|
|
|
tr_logAddErrorTier(tier, fmt::format(_("Announce error: {error}"), fmt::arg("error", err)));
|
2022-02-25 06:26:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* schedule a reannounce */
|
|
|
|
int const interval = current_tracker->getRetryInterval();
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddWarnTier(
|
|
|
|
tier,
|
|
|
|
fmt::format(
|
2022-03-15 14:52:16 +00:00
|
|
|
ngettext(
|
2022-03-16 00:51:36 +00:00
|
|
|
"Announce error: {error} (Retrying in {count} second)",
|
|
|
|
"Announce error: {error} (Retrying in {count} seconds)",
|
2022-03-14 04:43:35 +00:00
|
|
|
interval),
|
2022-03-16 00:51:36 +00:00
|
|
|
fmt::arg("error", err),
|
2022-03-14 04:43:35 +00:00
|
|
|
fmt::arg("count", interval)));
|
2022-02-25 06:26:19 +00:00
|
|
|
tier_announce_event_push(tier, e, tr_time() + interval);
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void on_announce_done(tr_announce_response const* response, void* vdata)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
auto* const data = static_cast<announce_data*>(vdata);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_announcer* announcer = data->session->announcer;
|
2022-01-23 05:41:01 +00:00
|
|
|
tr_tier* tier = getTier(announcer, response->info_hash, data->tier_id);
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t const now = tr_time();
|
|
|
|
tr_announce_event const event = data->event;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tier != nullptr)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceTier(
|
2021-08-15 09:41:48 +00:00
|
|
|
tier,
|
2022-03-14 04:43:35 +00:00
|
|
|
fmt::format(
|
|
|
|
"Got announce response: "
|
|
|
|
"connected:{} "
|
|
|
|
"timeout:{} "
|
|
|
|
"seeders:{} "
|
|
|
|
"leechers:{} "
|
|
|
|
"downloads:{} "
|
|
|
|
"interval:{} "
|
|
|
|
"min_interval:{} "
|
|
|
|
"tracker_id_str:{} "
|
|
|
|
"pex:{} "
|
|
|
|
"pex6:{} "
|
|
|
|
"err:{} "
|
|
|
|
"warn:{}",
|
|
|
|
response->did_connect,
|
|
|
|
response->did_timeout,
|
|
|
|
response->seeders,
|
|
|
|
response->leechers,
|
|
|
|
response->downloads,
|
|
|
|
response->interval,
|
|
|
|
response->min_interval,
|
|
|
|
(!std::empty(response->tracker_id) ? response->tracker_id.c_str() : "none"),
|
|
|
|
std::size(response->pex),
|
|
|
|
std::size(response->pex6),
|
|
|
|
(!std::empty(response->errmsg) ? response->errmsg.c_str() : "none"),
|
|
|
|
(!std::empty(response->warning) ? response->warning.c_str() : "none")));
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2010-02-01 00:28:04 +00:00
|
|
|
tier->lastAnnounceTime = now;
|
2011-03-11 04:19:01 +00:00
|
|
|
tier->lastAnnounceTimedOut = response->did_timeout;
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->lastAnnounceSucceeded = false;
|
|
|
|
tier->isAnnouncing = false;
|
2011-01-29 19:04:02 +00:00
|
|
|
tier->manualAnnounceAllowedAt = now + tier->announceMinIntervalSec;
|
2010-02-01 00:28:04 +00:00
|
|
|
|
2022-02-24 13:59:58 +00:00
|
|
|
if (response->external_ip)
|
|
|
|
{
|
|
|
|
data->session->setExternalIP(*response->external_ip);
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!response->did_connect)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
on_announce_error(tier, _("Could not connect to tracker"), event);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (response->did_timeout)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
on_announce_error(tier, _("Tracker did not respond"), event);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2021-12-23 17:16:05 +00:00
|
|
|
else if (!std::empty(response->errmsg))
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-06-25 18:47:35 +00:00
|
|
|
/* If the torrent's only tracker returned an error, publish it.
|
|
|
|
Don't bother publishing if there are other trackers -- it's
|
|
|
|
all too common for people to load up dozens of dead trackers
|
|
|
|
in a torrent's metainfo... */
|
2021-12-14 20:59:40 +00:00
|
|
|
if (tier->tor->trackerCount() < 2)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
publishError(tier, response->errmsg);
|
|
|
|
}
|
2011-06-25 18:47:35 +00:00
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
on_announce_error(tier, response->errmsg.c_str(), event);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-14 14:22:28 +00:00
|
|
|
auto const isStopped = event == TR_ANNOUNCE_EVENT_STOPPED;
|
|
|
|
auto leechers = int{};
|
|
|
|
auto scrape_fields = int{};
|
|
|
|
auto seeders = int{};
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
publishErrorClear(tier);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-02-26 17:53:34 +00:00
|
|
|
auto* const tracker = tier->currentTracker();
|
2021-10-14 14:22:28 +00:00
|
|
|
if (tracker != nullptr)
|
2011-03-31 03:37:50 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
tracker->consecutive_failures = 0;
|
2011-07-31 00:58:05 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (response->seeders >= 0)
|
2011-07-31 00:58:05 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
tracker->seeder_count = seeders = response->seeders;
|
2011-10-09 00:18:18 +00:00
|
|
|
++scrape_fields;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (response->leechers >= 0)
|
2011-10-09 00:18:18 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
tracker->leecher_count = leechers = response->leechers;
|
2011-10-09 00:18:18 +00:00
|
|
|
++scrape_fields;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (response->downloads >= 0)
|
2011-10-09 00:18:18 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
tracker->download_count = response->downloads;
|
2011-10-09 00:18:18 +00:00
|
|
|
++scrape_fields;
|
2011-07-31 00:58:05 +00:00
|
|
|
}
|
2011-03-31 03:37:50 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
if (!std::empty(response->tracker_id))
|
2011-03-31 03:37:50 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
tracker->tracker_id = response->tracker_id;
|
2011-03-31 03:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-12-25 02:07:33 +00:00
|
|
|
if (auto const& warning = response->warning; !std::empty(warning))
|
2011-01-29 19:04:02 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
tier->last_announce_str = warning;
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceTier(tier, fmt::format("tracker gave '{}'", warning));
|
2021-12-23 17:16:05 +00:00
|
|
|
publishWarning(tier, warning);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2011-10-09 00:21:36 +00:00
|
|
|
else
|
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
tier->last_announce_str = _("Success");
|
2011-10-09 00:21:36 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-10-14 14:22:28 +00:00
|
|
|
if (response->min_interval != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-14 14:22:28 +00:00
|
|
|
tier->announceMinIntervalSec = response->min_interval;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-10-14 14:22:28 +00:00
|
|
|
if (response->interval != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-14 14:22:28 +00:00
|
|
|
tier->announceIntervalSec = response->interval;
|
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(response->pex))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
publishPeersPex(tier, seeders, leechers, response->pex);
|
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(response->pex6))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
publishPeersPex(tier, seeders, leechers, response->pex6);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-09-09 20:25:30 +00:00
|
|
|
publishPeerCounts(tier, seeders, leechers);
|
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
tier->isRunning = data->is_running_on_success;
|
2011-07-31 00:58:05 +00:00
|
|
|
|
2011-10-09 00:18:18 +00:00
|
|
|
/* if the tracker included scrape fields in its announce response,
|
|
|
|
then a separate scrape isn't needed */
|
2021-09-15 00:18:09 +00:00
|
|
|
if (scrape_fields >= 3 || (scrape_fields >= 1 && tracker->scrape_info == nullptr))
|
2011-07-31 00:58:05 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceTier(
|
|
|
|
tier,
|
2022-03-14 04:43:35 +00:00
|
|
|
fmt::format(
|
|
|
|
"Announce response has scrape info; bumping next scrape to {} seconds from now.",
|
|
|
|
tier->scrapeIntervalSec));
|
2022-01-28 04:17:11 +00:00
|
|
|
tier->scheduleNextScrape();
|
2011-07-31 00:58:05 +00:00
|
|
|
tier->lastScrapeTime = now;
|
|
|
|
tier->lastScrapeSucceeded = true;
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (tier->lastScrapeTime + tier->scrapeIntervalSec <= now)
|
2011-07-31 00:58:05 +00:00
|
|
|
{
|
2022-01-28 04:17:11 +00:00
|
|
|
tier->scrapeSoon();
|
2011-07-31 00:58:05 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->lastAnnounceSucceeded = true;
|
2022-01-23 05:41:01 +00:00
|
|
|
tier->lastAnnouncePeerCount = std::size(response->pex) + std::size(response->pex6);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (isStopped)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-01-29 19:04:02 +00:00
|
|
|
/* now that we've successfully stopped the torrent,
|
|
|
|
* we can reset the up/down/corrupt count we've kept
|
|
|
|
* for this tracker */
|
2017-04-19 12:04:45 +00:00
|
|
|
tier->byteCounts[TR_ANN_UP] = 0;
|
|
|
|
tier->byteCounts[TR_ANN_DOWN] = 0;
|
|
|
|
tier->byteCounts[TR_ANN_CORRUPT] = 0;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
if (!isStopped && std::empty(tier->announce_events))
|
2010-02-10 02:59:15 +00:00
|
|
|
{
|
2022-04-21 14:28:38 +00:00
|
|
|
/* the queue is empty, so enqueue a periodic update */
|
2021-10-14 14:22:28 +00:00
|
|
|
int const i = tier->announceIntervalSec;
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceTier(tier, fmt::format("Sending periodic reannounce in {} seconds", i));
|
2017-04-19 12:04:45 +00:00
|
|
|
tier_announce_event_push(tier, TR_ANNOUNCE_EVENT_NONE, now + i);
|
2011-01-29 19:04:02 +00:00
|
|
|
}
|
2010-02-10 02:59:15 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
delete data;
|
2011-06-19 18:26:04 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static void announce_request_delegate(
|
|
|
|
tr_announcer* announcer,
|
|
|
|
tr_announce_request* request,
|
|
|
|
tr_announce_response_func callback,
|
2022-01-23 05:41:01 +00:00
|
|
|
announce_data* callback_data)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_session* session = announcer->session;
|
2010-02-10 02:59:15 +00:00
|
|
|
|
2013-01-30 19:59:55 +00:00
|
|
|
#if 0
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "ANNOUNCE: event %s isPartialSeed %d port %d key %d numwant %d up %" PRIu64 " down %" PRIu64
|
|
|
|
" corrupt %" PRIu64 " left %" PRIu64 " url [%s] tracker_id_str [%s] peer_id [%20.20s]\n",
|
|
|
|
tr_announce_event_get_string(request->event), (int)request->partial_seed, (int)request->port, request->key,
|
|
|
|
request->numwant, request->up, request->down, request->corrupt, request->leftUntilComplete, request->url,
|
|
|
|
request->tracker_id_str, request->peer_id);
|
|
|
|
|
2013-01-30 19:59:55 +00:00
|
|
|
#endif
|
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
if (auto const announce_sv = request->announce_url.sv();
|
2021-12-01 23:11:57 +00:00
|
|
|
tr_strvStartsWith(announce_sv, "http://"sv) || tr_strvStartsWith(announce_sv, "https://"sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_tracker_http_announce(session, request, callback, callback_data);
|
|
|
|
}
|
2021-11-10 02:42:18 +00:00
|
|
|
else if (tr_strvStartsWith(announce_sv, "udp://"sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_tracker_udp_announce(session, request, callback, callback_data);
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-16 00:51:36 +00:00
|
|
|
tr_logAddWarn(fmt::format(_("Unsupported URL: '{url}'"), fmt::arg("url", announce_sv)));
|
2022-01-23 05:41:01 +00:00
|
|
|
delete callback_data;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
delete request;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tierAnnounce(tr_announcer* announcer, tr_tier* tier)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(!tier->isAnnouncing);
|
2022-01-23 05:41:01 +00:00
|
|
|
TR_ASSERT(!std::empty(tier->announce_events));
|
2022-02-26 17:53:34 +00:00
|
|
|
TR_ASSERT(tier->currentTracker() != nullptr);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
time_t const now = tr_time();
|
|
|
|
|
|
|
|
tr_torrent* tor = tier->tor;
|
|
|
|
tr_announce_event announce_event = tier_announce_event_pull(tier);
|
|
|
|
tr_announce_request* req = announce_request_new(announcer, tor, tier, announce_event);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
auto* const data = new announce_data{ tier->id, now, announce_event, announcer->session, tor->isRunning };
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->isAnnouncing = true;
|
2011-03-11 05:48:57 +00:00
|
|
|
tier->lastAnnounceStartTime = now;
|
2010-02-10 02:59:15 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
announce_request_delegate(announcer, req, on_announce_done, data);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2011-02-05 16:45:06 +00:00
|
|
|
/***
|
|
|
|
****
|
2011-03-11 04:19:01 +00:00
|
|
|
**** SCRAPE
|
|
|
|
****
|
2011-02-05 16:45:06 +00:00
|
|
|
***/
|
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
static bool multiscrape_too_big(std::string_view errmsg)
|
2019-02-16 20:19:38 +00:00
|
|
|
{
|
|
|
|
/* Found a tracker that returns some bespoke string for this case?
|
|
|
|
Add your patch here and open a PR */
|
2021-10-14 15:36:49 +00:00
|
|
|
auto constexpr TooLongErrors = std::array<std::string_view, 3>{
|
2019-02-16 20:19:38 +00:00
|
|
|
"Bad Request",
|
|
|
|
"GET string too long",
|
2021-08-15 09:41:48 +00:00
|
|
|
"Request-URI Too Long",
|
2019-02-16 20:19:38 +00:00
|
|
|
};
|
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
return std::any_of(
|
|
|
|
std::begin(TooLongErrors),
|
|
|
|
std::end(TooLongErrors),
|
|
|
|
[&errmsg](auto const& substr) { return tr_strvContains(errmsg, substr); });
|
2019-02-16 20:19:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 04:17:11 +00:00
|
|
|
static void on_scrape_error(tr_session const* /*session*/, tr_tier* tier, char const* errmsg)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
// increment the error count
|
|
|
|
auto* current_tracker = tier->currentTracker();
|
|
|
|
if (current_tracker != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
++current_tracker->consecutive_failures;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-14 16:52:47 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
// set the error message
|
|
|
|
tier->last_scrape_str = errmsg != nullptr ? errmsg : "";
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
// switch to the next tracker
|
|
|
|
current_tracker = tier->useNextTracker();
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
// schedule a rescrape
|
|
|
|
auto const interval = current_tracker->getRetryInterval();
|
|
|
|
auto const* const host_cstr = current_tracker->host.c_str();
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddDebugTier(
|
|
|
|
tier,
|
|
|
|
fmt::format("Tracker '{}' scrape error: {} (Retrying in {} seconds)", host_cstr, errmsg, interval));
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->lastScrapeSucceeded = false;
|
2022-01-28 04:17:11 +00:00
|
|
|
tier->scheduleNextScrape(interval);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
static void checkMultiscrapeMax(tr_announcer* announcer, tr_scrape_response const* response)
|
|
|
|
{
|
|
|
|
if (!multiscrape_too_big(response->errmsg))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const& url = response->scrape_url;
|
2022-01-23 05:41:01 +00:00
|
|
|
auto* const scrape_info = tr_announcerGetScrapeInfo(announcer, url);
|
2021-11-02 23:00:01 +00:00
|
|
|
if (scrape_info == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lower the max only if it hasn't already lowered for a similar
|
|
|
|
// error. So if N parallel multiscrapes all have the same `max`
|
|
|
|
// and error out, lower the value once for that batch, not N times.
|
|
|
|
int& multiscrape_max = scrape_info->multiscrape_max;
|
|
|
|
if (multiscrape_max < response->row_count)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int const n = std::max(1, int{ multiscrape_max - TrMultiscrapeStep });
|
|
|
|
if (multiscrape_max != n)
|
|
|
|
{
|
|
|
|
// don't log the full URL, since that might have a personal announce id
|
|
|
|
// (note: we know 'parsed' will be successful since this url has a scrape_info)
|
2022-06-01 16:56:59 +00:00
|
|
|
if (auto const parsed = tr_urlParse(url); parsed)
|
|
|
|
{
|
|
|
|
tr_logAddDebug(
|
|
|
|
fmt::format(FMT_STRING("Reducing multiscrape max to {:d}"), n),
|
|
|
|
fmt::format(FMT_STRING("{:s}://{:s}:{:d}"), parsed->scheme, parsed->host, parsed->port));
|
|
|
|
}
|
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
multiscrape_max = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void on_scrape_done(tr_scrape_response const* response, void* vsession)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
auto const now = tr_time();
|
|
|
|
auto* const session = static_cast<tr_session*>(vsession);
|
|
|
|
auto* const announcer = session->announcer;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < response->row_count; ++i)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
auto const& row = response->rows[i];
|
2022-03-01 23:06:29 +00:00
|
|
|
auto* const tor = session->torrents().get(row.info_hash);
|
2009-11-10 17:03:23 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tor != nullptr)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
auto* tier = tor->torrent_announcer->getTierFromScrape(response->scrape_url);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
if (tier == nullptr)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceTier(
|
2022-01-23 05:41:01 +00:00
|
|
|
tier,
|
2022-03-14 04:43:35 +00:00
|
|
|
fmt::format(
|
|
|
|
"scraped url:{} "
|
|
|
|
" -- "
|
|
|
|
"did_connect:{} "
|
|
|
|
"did_timeout:{} "
|
|
|
|
"seeders:{} "
|
|
|
|
"leechers:{} "
|
|
|
|
"downloads:{} "
|
|
|
|
"downloaders:{} "
|
|
|
|
"min_request_interval:{} "
|
|
|
|
"err:{} ",
|
2022-03-28 22:13:32 +00:00
|
|
|
response->scrape_url.sv(),
|
2022-03-14 04:43:35 +00:00
|
|
|
response->did_connect,
|
|
|
|
response->did_timeout,
|
|
|
|
row.seeders,
|
|
|
|
row.leechers,
|
|
|
|
row.downloads,
|
|
|
|
row.downloaders,
|
|
|
|
response->min_request_interval,
|
|
|
|
std::empty(response->errmsg) ? "none"sv : response->errmsg));
|
2022-01-23 05:41:01 +00:00
|
|
|
|
|
|
|
tier->isScraping = false;
|
|
|
|
tier->lastScrapeTime = now;
|
|
|
|
tier->lastScrapeSucceeded = false;
|
|
|
|
tier->lastScrapeTimedOut = response->did_timeout;
|
|
|
|
|
|
|
|
if (!response->did_connect)
|
|
|
|
{
|
|
|
|
on_scrape_error(session, tier, _("Could not connect to tracker"));
|
|
|
|
}
|
|
|
|
else if (response->did_timeout)
|
|
|
|
{
|
|
|
|
on_scrape_error(session, tier, _("Tracker did not respond"));
|
|
|
|
}
|
|
|
|
else if (!std::empty(response->errmsg))
|
|
|
|
{
|
|
|
|
on_scrape_error(session, tier, response->errmsg.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tier->lastScrapeSucceeded = true;
|
|
|
|
tier->scrapeIntervalSec = std::max(int{ DefaultScrapeIntervalSec }, response->min_request_interval);
|
2022-01-28 04:17:11 +00:00
|
|
|
tier->scheduleNextScrape();
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceTier(tier, fmt::format("Scrape successful. Rescraping in {} seconds.", tier->scrapeIntervalSec));
|
2022-01-23 05:41:01 +00:00
|
|
|
|
2022-02-02 01:09:11 +00:00
|
|
|
if (tr_tracker* const tracker = tier->currentTracker(); tracker != nullptr)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
if (row.seeders >= 0)
|
|
|
|
{
|
|
|
|
tracker->seeder_count = row.seeders;
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
if (row.leechers >= 0)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
tracker->leecher_count = row.leechers;
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2021-09-09 20:25:30 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
if (row.downloads >= 0)
|
2021-09-09 20:25:30 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
tracker->download_count = row.downloads;
|
2021-09-09 20:25:30 +00:00
|
|
|
}
|
2022-01-23 05:41:01 +00:00
|
|
|
|
|
|
|
tracker->downloader_count = row.downloaders;
|
|
|
|
tracker->consecutive_failures = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (row.seeders >= 0 && row.leechers >= 0 && row.downloads >= 0)
|
|
|
|
{
|
|
|
|
publishPeerCounts(tier, row.seeders, row.leechers);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
checkMultiscrapeMax(announcer, response);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static void scrape_request_delegate(
|
|
|
|
tr_announcer* announcer,
|
|
|
|
tr_scrape_request const* request,
|
|
|
|
tr_scrape_response_func callback,
|
2017-04-19 12:04:45 +00:00
|
|
|
void* callback_data)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_session* session = announcer->session;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
auto const scrape_sv = request->scrape_url.sv();
|
2021-11-02 23:00:01 +00:00
|
|
|
|
2021-11-10 02:42:18 +00:00
|
|
|
if (tr_strvStartsWith(scrape_sv, "http://"sv) || tr_strvStartsWith(scrape_sv, "https://"sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_tracker_http_scrape(session, request, callback, callback_data);
|
|
|
|
}
|
2021-11-10 02:42:18 +00:00
|
|
|
else if (tr_strvStartsWith(scrape_sv, "udp://"sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_tracker_udp_scrape(session, request, callback, callback_data);
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-15 14:52:16 +00:00
|
|
|
tr_logAddError(fmt::format(_("Unsupported URL: '{url}'"), fmt::arg("url", scrape_sv)));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 14:32:07 +00:00
|
|
|
static void multiscrape(tr_announcer* announcer, std::vector<tr_tier*> const& tiers)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
auto const now = tr_time();
|
|
|
|
auto requests = std::array<tr_scrape_request, MaxScrapesPerUpkeep>{};
|
|
|
|
auto request_count = size_t{};
|
2011-03-11 15:11:37 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
// batch as many info_hashes into a request as we can
|
2021-09-15 14:32:07 +00:00
|
|
|
for (auto* tier : tiers)
|
2011-03-11 15:11:37 +00:00
|
|
|
{
|
2022-01-25 01:05:24 +00:00
|
|
|
auto const* const scrape_info = tier->currentTracker()->scrape_info;
|
2017-05-13 22:38:31 +00:00
|
|
|
bool found = false;
|
2011-03-11 15:11:37 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(scrape_info != nullptr);
|
2017-07-04 18:22:26 +00:00
|
|
|
|
2011-03-11 15:11:37 +00:00
|
|
|
/* if there's a request with this scrape URL and a free slot, use it */
|
2021-09-10 00:28:22 +00:00
|
|
|
for (size_t j = 0; !found && j < request_count; ++j)
|
2011-03-11 15:11:37 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
auto* const req = &requests[j];
|
2011-03-11 15:11:37 +00:00
|
|
|
|
2019-02-16 20:19:38 +00:00
|
|
|
if (req->info_hash_count >= scrape_info->multiscrape_max)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-11 15:11:37 +00:00
|
|
|
continue;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
if (scrape_info->scrape_url != req->scrape_url)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-11 15:11:37 +00:00
|
|
|
continue;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-11 15:11:37 +00:00
|
|
|
|
2021-12-24 21:12:33 +00:00
|
|
|
req->info_hash[req->info_hash_count] = tier->tor->infoHash();
|
2021-11-25 22:39:19 +00:00
|
|
|
++req->info_hash_count;
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->isScraping = true;
|
2011-03-11 15:11:37 +00:00
|
|
|
tier->lastScrapeStartTime = now;
|
2017-05-13 22:38:31 +00:00
|
|
|
found = true;
|
2011-03-11 15:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* otherwise, if there's room for another request, build a new one */
|
2021-10-21 00:39:05 +00:00
|
|
|
if (!found && request_count < MaxScrapesPerUpkeep)
|
2011-03-11 15:11:37 +00:00
|
|
|
{
|
2022-01-24 00:53:35 +00:00
|
|
|
auto* const req = &requests[request_count];
|
2021-11-02 23:00:01 +00:00
|
|
|
req->scrape_url = scrape_info->scrape_url;
|
2022-01-23 05:41:01 +00:00
|
|
|
tier->buildLogName(req->log_name, sizeof(req->log_name));
|
2011-03-11 15:11:37 +00:00
|
|
|
|
2021-12-24 21:12:33 +00:00
|
|
|
req->info_hash[req->info_hash_count] = tier->tor->infoHash();
|
2021-11-25 22:39:19 +00:00
|
|
|
++req->info_hash_count;
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->isScraping = true;
|
2011-03-11 15:11:37 +00:00
|
|
|
tier->lastScrapeStartTime = now;
|
2022-01-24 00:53:35 +00:00
|
|
|
|
|
|
|
++request_count;
|
2011-03-11 15:11:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send the requests we just built */
|
2021-09-10 00:28:22 +00:00
|
|
|
for (size_t i = 0; i < request_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
scrape_request_delegate(announcer, &requests[i], on_scrape_done, announcer->session);
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void flushCloseMessages(tr_announcer* announcer)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-09-29 23:42:50 +00:00
|
|
|
auto& stops = announcer->stops;
|
|
|
|
std::for_each(
|
|
|
|
std::begin(stops),
|
|
|
|
std::end(stops),
|
|
|
|
[&announcer](auto* stop) { announce_request_delegate(announcer, stop, nullptr, nullptr); });
|
|
|
|
stops.clear();
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 14:32:07 +00:00
|
|
|
static int compareAnnounceTiers(tr_tier const* a, tr_tier const* b)
|
2021-09-10 00:28:22 +00:00
|
|
|
{
|
|
|
|
/* prefer higher-priority events */
|
2021-12-16 22:58:58 +00:00
|
|
|
if (auto const priority_a = a->announce_event_priority, priority_b = b->announce_event_priority; priority_a != priority_b)
|
2021-09-10 00:28:22 +00:00
|
|
|
{
|
|
|
|
return priority_a > priority_b ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* prefer swarms where we might upload */
|
2022-01-23 05:41:01 +00:00
|
|
|
if (auto const leechers_a = a->countDownloaders(), leechers_b = b->countDownloaders(); leechers_a != leechers_b)
|
2021-09-10 00:28:22 +00:00
|
|
|
{
|
2021-12-16 22:58:58 +00:00
|
|
|
return leechers_a > leechers_b ? -1 : 1;
|
2021-09-10 00:28:22 +00:00
|
|
|
}
|
2011-02-05 16:45:06 +00:00
|
|
|
|
2021-09-10 00:28:22 +00:00
|
|
|
/* prefer swarms where we might download */
|
2021-12-16 22:58:58 +00:00
|
|
|
if (auto const is_done_a = a->tor->isDone(), is_done_b = b->tor->isDone(); is_done_a != is_done_b)
|
2021-09-10 00:28:22 +00:00
|
|
|
{
|
2021-12-15 07:04:26 +00:00
|
|
|
return is_done_a ? 1 : -1;
|
2021-09-10 00:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* prefer larger stats, to help ensure stats get recorded when stopping on shutdown */
|
2021-12-16 22:58:58 +00:00
|
|
|
if (auto const xa = a->byteCounts[TR_ANN_UP] + a->byteCounts[TR_ANN_DOWN],
|
|
|
|
xb = b->byteCounts[TR_ANN_UP] + b->byteCounts[TR_ANN_DOWN];
|
|
|
|
xa != xb)
|
2021-09-10 00:28:22 +00:00
|
|
|
{
|
2021-09-29 23:42:50 +00:00
|
|
|
return xa > xb ? -1 : 1;
|
2021-09-10 00:28:22 +00:00
|
|
|
}
|
2011-02-05 16:45:06 +00:00
|
|
|
|
2021-09-10 00:28:22 +00:00
|
|
|
// announcements that have been waiting longer go first
|
|
|
|
if (a->announceAt != b->announceAt)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-10 00:28:22 +00:00
|
|
|
return a->announceAt < b->announceAt ? -1 : 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-04 23:26:10 +00:00
|
|
|
|
2021-09-11 13:11:46 +00:00
|
|
|
// the tiers are effectively equal priority, but add an arbitrary
|
|
|
|
// differentiation because ptrArray sorted mode hates equal items.
|
|
|
|
return a < b ? -1 : 1;
|
2011-02-05 16:45:06 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 00:28:22 +00:00
|
|
|
static void scrapeAndAnnounceMore(tr_announcer* announcer)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t const now = tr_time();
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-14 02:40:39 +00:00
|
|
|
/* build a list of tiers that need to be announced */
|
2021-09-15 14:32:07 +00:00
|
|
|
auto announce_me = std::vector<tr_tier*>{};
|
|
|
|
auto scrape_me = std::vector<tr_tier*>{};
|
2022-03-01 23:06:29 +00:00
|
|
|
for (auto* const tor : announcer->session->torrents())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
for (auto& tier : tor->torrent_announcer->tiers)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
if (tier.needsToAnnounce(now))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
announce_me.push_back(&tier);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-09-10 00:28:22 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
if (tier.needsToScrape(now))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
scrape_me.push_back(&tier);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
2011-03-14 02:40:39 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-09-10 00:28:22 +00:00
|
|
|
/* First, scrape what we can. We handle scrapes first because
|
|
|
|
* we can work through that queue much faster than announces
|
|
|
|
* (thanks to multiscrape) _and_ the scrape responses will tell
|
|
|
|
* us which swarms are interesting and should be announced next. */
|
2021-09-15 14:32:07 +00:00
|
|
|
multiscrape(announcer, scrape_me);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-09-10 00:28:22 +00:00
|
|
|
/* Second, announce what we can. If there aren't enough slots
|
|
|
|
* available, use compareAnnounceTiers to prioritize. */
|
2021-10-21 00:39:05 +00:00
|
|
|
if (announce_me.size() > MaxAnnouncesPerUpkeep)
|
2021-09-15 14:32:07 +00:00
|
|
|
{
|
|
|
|
std::partial_sort(
|
|
|
|
std::begin(announce_me),
|
2021-10-21 00:39:05 +00:00
|
|
|
std::begin(announce_me) + MaxAnnouncesPerUpkeep,
|
2021-09-15 14:32:07 +00:00
|
|
|
std::end(announce_me),
|
|
|
|
[](auto const* a, auto const* b) { return compareAnnounceTiers(a, b) < 0; });
|
2021-10-21 00:39:05 +00:00
|
|
|
announce_me.resize(MaxAnnouncesPerUpkeep);
|
2021-09-15 14:32:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto*& tier : announce_me)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceTier(tier, "Announcing to tracker");
|
2017-04-19 12:04:45 +00:00
|
|
|
tierAnnounce(announcer, tier);
|
2011-03-14 02:40:39 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-12 23:16:51 +00:00
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void onUpkeepTimer(evutil_socket_t /*fd*/, short /*what*/, void* vannouncer)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* announcer = static_cast<tr_announcer*>(vannouncer);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_session* session = announcer->session;
|
2021-11-20 21:20:45 +00:00
|
|
|
auto const lock = session->unique_lock();
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const is_closing = session->isClosed;
|
|
|
|
time_t const now = tr_time();
|
2011-03-17 18:51:31 +00:00
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
/* maybe send out some "stopped" messages for closed torrents */
|
2017-04-19 12:04:45 +00:00
|
|
|
flushCloseMessages(announcer);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-09-10 00:28:22 +00:00
|
|
|
/* maybe kick off some scrapes / announces whose time has come */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!is_closing)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-10 00:28:22 +00:00
|
|
|
scrapeAndAnnounceMore(announcer);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-13 06:38:54 +00:00
|
|
|
/* TAU upkeep */
|
2022-01-23 05:41:01 +00:00
|
|
|
if (announcer->tau_upkeep_at <= now)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
announcer->tau_upkeep_at = now + TauUpkeepIntervalSecs;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_tracker_udp_upkeep(session);
|
2011-03-13 06:38:54 +00:00
|
|
|
}
|
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
// set up the next timer
|
|
|
|
announcer->scheduleNextUpdate();
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-12-08 16:55:52 +00:00
|
|
|
static tr_tracker_view trackerView(tr_torrent const& tor, int tier_index, tr_tier const& tier, tr_tracker const& tracker)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-12-08 16:55:52 +00:00
|
|
|
auto const now = tr_time();
|
|
|
|
auto view = tr_tracker_view{};
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
view.host = tracker.host.c_str();
|
2021-12-23 17:16:05 +00:00
|
|
|
view.announce = tracker.announce_url.c_str();
|
|
|
|
view.scrape = tracker.scrape_info == nullptr ? "" : tracker.scrape_info->scrape_url.c_str();
|
2022-02-12 17:30:27 +00:00
|
|
|
*std::copy_n(
|
|
|
|
std::begin(tracker.sitename),
|
|
|
|
std::min(std::size(tracker.sitename), sizeof(view.sitename) - 1),
|
|
|
|
view.sitename) = '\0';
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-12-08 16:55:52 +00:00
|
|
|
view.id = tracker.id;
|
|
|
|
view.tier = tier_index;
|
2022-01-23 05:41:01 +00:00
|
|
|
view.isBackup = &tracker != tier.currentTracker();
|
2021-12-08 16:55:52 +00:00
|
|
|
view.lastScrapeStartTime = tier.lastScrapeStartTime;
|
2022-01-23 05:41:01 +00:00
|
|
|
view.seederCount = tracker.seeder_count;
|
|
|
|
view.leecherCount = tracker.leecher_count;
|
|
|
|
view.downloadCount = tracker.download_count;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-12-08 16:55:52 +00:00
|
|
|
if (view.isBackup)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-12-08 16:55:52 +00:00
|
|
|
view.scrapeState = TR_TRACKER_INACTIVE;
|
|
|
|
view.announceState = TR_TRACKER_INACTIVE;
|
|
|
|
view.nextScrapeTime = 0;
|
|
|
|
view.nextAnnounceTime = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-07 04:28:36 +00:00
|
|
|
view.hasScraped = tier.lastScrapeTime != 0;
|
|
|
|
if (view.hasScraped)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-12-08 16:55:52 +00:00
|
|
|
view.lastScrapeTime = tier.lastScrapeTime;
|
|
|
|
view.lastScrapeSucceeded = tier.lastScrapeSucceeded;
|
|
|
|
view.lastScrapeTimedOut = tier.lastScrapeTimedOut;
|
2022-01-23 05:41:01 +00:00
|
|
|
tr_strlcpy(view.lastScrapeResult, tier.last_scrape_str.c_str(), sizeof(view.lastScrapeResult));
|
2021-12-08 16:55:52 +00:00
|
|
|
}
|
2009-09-30 20:49:52 +00:00
|
|
|
|
2021-12-08 16:55:52 +00:00
|
|
|
if (tier.isScraping)
|
|
|
|
{
|
|
|
|
view.scrapeState = TR_TRACKER_ACTIVE;
|
|
|
|
}
|
|
|
|
else if (tier.scrapeAt == 0)
|
|
|
|
{
|
|
|
|
view.scrapeState = TR_TRACKER_INACTIVE;
|
|
|
|
}
|
|
|
|
else if (tier.scrapeAt > now)
|
|
|
|
{
|
|
|
|
view.scrapeState = TR_TRACKER_WAITING;
|
|
|
|
view.nextScrapeTime = tier.scrapeAt;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
view.scrapeState = TR_TRACKER_QUEUED;
|
|
|
|
}
|
2009-09-30 20:49:52 +00:00
|
|
|
|
2021-12-08 16:55:52 +00:00
|
|
|
view.lastAnnounceStartTime = tier.lastAnnounceStartTime;
|
2009-09-30 20:49:52 +00:00
|
|
|
|
2022-02-07 04:28:36 +00:00
|
|
|
view.hasAnnounced = tier.lastAnnounceTime != 0;
|
|
|
|
if (view.hasAnnounced)
|
2021-12-08 16:55:52 +00:00
|
|
|
{
|
|
|
|
view.lastAnnounceTime = tier.lastAnnounceTime;
|
|
|
|
view.lastAnnounceSucceeded = tier.lastAnnounceSucceeded;
|
|
|
|
view.lastAnnounceTimedOut = tier.lastAnnounceTimedOut;
|
|
|
|
view.lastAnnouncePeerCount = tier.lastAnnouncePeerCount;
|
2022-01-23 05:41:01 +00:00
|
|
|
tr_strlcpy(view.lastAnnounceResult, tier.last_announce_str.c_str(), sizeof(view.lastAnnounceResult));
|
2021-12-08 16:55:52 +00:00
|
|
|
}
|
2009-09-30 20:49:52 +00:00
|
|
|
|
2021-12-08 16:55:52 +00:00
|
|
|
if (tier.isAnnouncing)
|
|
|
|
{
|
|
|
|
view.announceState = TR_TRACKER_ACTIVE;
|
|
|
|
}
|
|
|
|
else if (!tor.isRunning || tier.announceAt == 0)
|
|
|
|
{
|
|
|
|
view.announceState = TR_TRACKER_INACTIVE;
|
|
|
|
}
|
|
|
|
else if (tier.announceAt > now)
|
|
|
|
{
|
|
|
|
view.announceState = TR_TRACKER_WAITING;
|
|
|
|
view.nextAnnounceTime = tier.announceAt;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
view.announceState = TR_TRACKER_QUEUED;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:55:52 +00:00
|
|
|
TR_ASSERT(0 <= view.tier);
|
|
|
|
return view;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 02:03:26 +00:00
|
|
|
size_t tr_announcerTrackerCount(tr_torrent const* tor)
|
|
|
|
{
|
|
|
|
TR_ASSERT(tr_isTorrent(tor));
|
2022-01-23 05:41:01 +00:00
|
|
|
TR_ASSERT(tor->torrent_announcer != nullptr);
|
2021-12-10 02:03:26 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
auto const& tiers = tor->torrent_announcer->tiers;
|
|
|
|
return std::accumulate(
|
|
|
|
std::begin(tiers),
|
|
|
|
std::end(tiers),
|
|
|
|
size_t{},
|
|
|
|
[](size_t acc, auto const& cur) { return acc + std::size(cur.trackers); });
|
2021-12-10 02:03:26 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 16:55:52 +00:00
|
|
|
tr_tracker_view tr_announcerTracker(tr_torrent const* tor, size_t nth)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-12-08 16:55:52 +00:00
|
|
|
TR_ASSERT(tr_isTorrent(tor));
|
2022-01-23 05:41:01 +00:00
|
|
|
TR_ASSERT(tor->torrent_announcer != nullptr);
|
2021-12-08 16:55:52 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
auto i = size_t{ 0 };
|
|
|
|
for (auto const& tier : tor->torrent_announcer->tiers)
|
2021-12-08 16:55:52 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
for (auto const& tracker : tier.trackers)
|
2021-12-08 16:55:52 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
if (i == nth)
|
2021-12-08 16:55:52 +00:00
|
|
|
{
|
|
|
|
return trackerView(*tor, i, tier, tracker);
|
|
|
|
}
|
2022-01-23 05:41:01 +00:00
|
|
|
|
|
|
|
++i;
|
2021-12-08 16:55:52 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-23 05:41:01 +00:00
|
|
|
|
|
|
|
TR_ASSERT(false);
|
2021-12-08 16:55:52 +00:00
|
|
|
return {};
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
2011-02-05 16:45:06 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
// called after the torrent's announceList was rebuilt --
|
|
|
|
// so announcer needs to update the tr_tier / tr_trackers to match
|
|
|
|
void tr_announcerResetTorrent(tr_announcer* /*announcer*/, tr_torrent* tor)
|
2011-02-05 16:45:06 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
// make a new tr_announcer_tier
|
|
|
|
auto* const older = tor->torrent_announcer;
|
|
|
|
tor->torrent_announcer = new tr_torrent_announcer(tor->session->announcer, tor);
|
|
|
|
auto* const newer = tor->torrent_announcer;
|
2011-02-05 16:45:06 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
// copy the tracker counts into the new replacementa
|
|
|
|
if (older != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
for (auto& new_tier : newer->tiers)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
for (auto& new_tracker : new_tier.trackers)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
tr_tier const* old_tier = nullptr;
|
|
|
|
tr_tracker const* old_tracker = nullptr;
|
|
|
|
if (older->findTracker(new_tracker.announce_url, &old_tier, &old_tracker))
|
|
|
|
{
|
|
|
|
new_tracker.seeder_count = old_tracker->seeder_count;
|
|
|
|
new_tracker.leecher_count = old_tracker->leecher_count;
|
|
|
|
new_tracker.download_count = old_tracker->download_count;
|
|
|
|
new_tracker.downloader_count = old_tracker->downloader_count;
|
2011-02-05 16:45:06 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
new_tier.announce_events = old_tier->announce_events;
|
|
|
|
new_tier.announce_event_priority = old_tier->announce_event_priority;
|
2011-02-05 16:45:06 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
auto const* const old_current = old_tier->currentTracker();
|
|
|
|
new_tier.current_tracker_index_ = old_current == nullptr ? std::nullopt :
|
|
|
|
new_tier.indexOf(old_current->announce_url);
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-05 16:45:06 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
// kickstart any tiers that didn't get started
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tor->isRunning)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
auto const now = tr_time();
|
|
|
|
for (auto& tier : newer->tiers)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
if (!tier.current_tracker_index_)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-26 17:53:34 +00:00
|
|
|
tier.useNextTracker();
|
2022-01-23 05:41:01 +00:00
|
|
|
tier_announce_event_push(&tier, TR_ANNOUNCE_EVENT_STARTED, now);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-05 16:45:06 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
delete older;
|
2011-02-05 16:45:06 +00:00
|
|
|
}
|