2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2008-2023 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.
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2021-09-27 18:28:41 +00:00
|
|
|
#include <algorithm> // std::partial_sort(), std::min(), std::max()
|
2022-03-04 22:43:56 +00:00
|
|
|
#include <condition_variable>
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <csignal>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <cstddef> // size_t
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <cstdint>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <ctime>
|
2022-11-10 18:04:49 +00:00
|
|
|
#include <future>
|
2022-08-06 01:37:21 +00:00
|
|
|
#include <iterator> // for std::back_inserter
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <limits> // std::numeric_limits
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <memory>
|
2022-08-06 01:37:21 +00:00
|
|
|
#include <numeric> // for std::accumulate()
|
2022-04-08 01:50:26 +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>
|
2021-09-15 14:32:07 +00:00
|
|
|
#include <vector>
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2014-12-13 15:22:39 +00:00
|
|
|
#ifndef _WIN32
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <sys/stat.h> /* umask() */
|
2014-12-13 15:22:39 +00:00
|
|
|
#endif
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2010-12-24 08:58:41 +00:00
|
|
|
#include <event2/event.h>
|
2009-03-25 19:50:58 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <fmt/core.h> // fmt::ptr
|
2022-03-14 04:43:35 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2021-11-09 03:30:03 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/bandwidth.h"
|
|
|
|
#include "libtransmission/blocklist.h"
|
|
|
|
#include "libtransmission/cache.h"
|
|
|
|
#include "libtransmission/crypto-utils.h"
|
|
|
|
#include "libtransmission/file.h"
|
2023-05-05 17:17:40 +00:00
|
|
|
#include "libtransmission/global-ip-cache.h"
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/interned-string.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/log.h"
|
|
|
|
#include "libtransmission/net.h"
|
|
|
|
#include "libtransmission/peer-mgr.h"
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/peer-socket.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/port-forwarding.h"
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/quark.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/rpc-server.h"
|
|
|
|
#include "libtransmission/session.h"
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/session-alt-speeds.h"
|
|
|
|
#include "libtransmission/session-settings.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/timer-ev.h"
|
|
|
|
#include "libtransmission/torrent.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/tr-dht.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/tr-lpd.h"
|
|
|
|
#include "libtransmission/tr-strbuf.h"
|
|
|
|
#include "libtransmission/tr-utp.h"
|
|
|
|
#include "libtransmission/utils.h"
|
|
|
|
#include "libtransmission/variant.h"
|
|
|
|
#include "libtransmission/version.h"
|
|
|
|
#include "libtransmission/web.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
struct tr_ctor;
|
|
|
|
|
2021-11-13 00:10:04 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
namespace bandwidth_group_helpers
|
|
|
|
{
|
|
|
|
auto constexpr BandwidthGroupsFilename = "bandwidth-groups.json"sv;
|
|
|
|
|
|
|
|
void bandwidthGroupRead(tr_session* session, std::string_view config_dir)
|
|
|
|
{
|
|
|
|
auto const filename = tr_pathbuf{ config_dir, '/', BandwidthGroupsFilename };
|
2023-08-17 16:02:45 +00:00
|
|
|
if (!tr_sys_path_exists(filename))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto groups_var = tr_variant_serde::json().parse_file(filename);
|
|
|
|
if (!groups_var)
|
2023-01-07 14:27:54 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto idx = size_t{ 0 };
|
|
|
|
auto key = tr_quark{};
|
|
|
|
tr_variant* dict = nullptr;
|
2023-08-17 16:02:45 +00:00
|
|
|
while (tr_variantDictChild(&*groups_var, idx, &key, &dict))
|
2023-01-07 14:27:54 +00:00
|
|
|
{
|
|
|
|
++idx;
|
|
|
|
|
|
|
|
auto name = tr_interned_string(key);
|
|
|
|
auto& group = session->getBandwidthGroup(name);
|
|
|
|
|
|
|
|
auto limits = tr_bandwidth_limits{};
|
|
|
|
|
2023-08-22 02:59:47 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(dict, TR_KEY_uploadLimited, &val))
|
2023-01-07 14:27:54 +00:00
|
|
|
{
|
2023-08-22 02:59:47 +00:00
|
|
|
limits.up_limited = val;
|
2023-01-07 14:27:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 02:59:47 +00:00
|
|
|
if (auto val = bool{}; tr_variantDictFindBool(dict, TR_KEY_downloadLimited, &val))
|
2023-01-07 14:27:54 +00:00
|
|
|
{
|
2023-08-22 02:59:47 +00:00
|
|
|
limits.down_limited = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto val = int64_t{}; tr_variantDictFindInt(dict, TR_KEY_uploadLimit, &val))
|
|
|
|
{
|
|
|
|
limits.up_limit_KBps = static_cast<tr_kilobytes_per_second_t>(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto val = int64_t{}; tr_variantDictFindInt(dict, TR_KEY_downloadLimit, &val))
|
|
|
|
{
|
|
|
|
limits.down_limit_KBps = static_cast<tr_kilobytes_per_second_t>(val);
|
2023-01-07 14:27:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
group.set_limits(&limits);
|
2023-01-07 14:27:54 +00:00
|
|
|
|
|
|
|
if (auto honors = bool{}; tr_variantDictFindBool(dict, TR_KEY_honorsSessionLimits, &honors))
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
group.honor_parent_limits(TR_UP, honors);
|
|
|
|
group.honor_parent_limits(TR_DOWN, honors);
|
2023-01-07 14:27:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
void bandwidthGroupWrite(tr_session const* session, std::string_view config_dir)
|
2023-01-07 14:27:54 +00:00
|
|
|
{
|
|
|
|
auto const& groups = session->bandwidthGroups();
|
|
|
|
|
|
|
|
auto groups_dict = tr_variant{};
|
|
|
|
tr_variantInitDict(&groups_dict, std::size(groups));
|
|
|
|
|
|
|
|
for (auto const& [name, group] : groups)
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const limits = group->get_limits();
|
2023-01-07 14:27:54 +00:00
|
|
|
|
|
|
|
auto* const dict = tr_variantDictAddDict(&groups_dict, name.quark(), 5);
|
|
|
|
tr_variantDictAddStrView(dict, TR_KEY_name, name.sv());
|
|
|
|
tr_variantDictAddBool(dict, TR_KEY_uploadLimited, limits.up_limited);
|
|
|
|
tr_variantDictAddInt(dict, TR_KEY_uploadLimit, limits.up_limit_KBps);
|
|
|
|
tr_variantDictAddBool(dict, TR_KEY_downloadLimited, limits.down_limited);
|
|
|
|
tr_variantDictAddInt(dict, TR_KEY_downloadLimit, limits.down_limit_KBps);
|
2023-04-23 01:25:55 +00:00
|
|
|
tr_variantDictAddBool(dict, TR_KEY_honorsSessionLimits, group->are_parent_limits_honored(TR_UP));
|
2023-01-07 14:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto const filename = tr_pathbuf{ config_dir, '/', BandwidthGroupsFilename };
|
2023-08-17 16:02:45 +00:00
|
|
|
tr_variant_serde::json().to_file(groups_dict, filename);
|
2023-01-07 14:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace bandwidth_group_helpers
|
2023-01-07 22:55:00 +00:00
|
|
|
|
|
|
|
void update_bandwidth(tr_session* session, tr_direction dir)
|
|
|
|
{
|
|
|
|
if (auto const limit_bytes_per_second = session->activeSpeedLimitBps(dir); limit_bytes_per_second)
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
session->top_bandwidth_.set_limited(dir, *limit_bytes_per_second > 0U);
|
|
|
|
session->top_bandwidth_.set_desired_speed_bytes_per_second(dir, *limit_bytes_per_second);
|
2023-01-07 22:55:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
session->top_bandwidth_.set_limited(dir, false);
|
2023-01-07 22:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-07 14:27:54 +00:00
|
|
|
} // namespace
|
2022-03-18 13:11:59 +00:00
|
|
|
|
2022-08-14 01:04:36 +00:00
|
|
|
tr_port tr_session::randomPort() const
|
2008-12-13 23:17:36 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
auto const lower = std::min(settings_.peer_port_random_low.host(), settings_.peer_port_random_high.host());
|
|
|
|
auto const upper = std::max(settings_.peer_port_random_low.host(), settings_.peer_port_random_high.host());
|
2022-04-21 15:58:13 +00:00
|
|
|
auto const range = upper - lower;
|
2023-08-18 03:13:01 +00:00
|
|
|
return tr_port::from_host(lower + tr_rand_int(range + 1U));
|
2008-12-13 23:17:36 +00:00
|
|
|
}
|
2008-12-11 04:41:23 +00:00
|
|
|
|
2007-07-16 21:57:22 +00:00
|
|
|
/* Generate a peer id : "-TRxyzb-" + 12 random alphanumeric
|
|
|
|
characters, where x is the major version number, y is the
|
|
|
|
minor version number, z is the maintenance number, and b
|
|
|
|
designates beta (Azureus-style) */
|
2021-10-22 02:40:55 +00:00
|
|
|
tr_peer_id_t tr_peerIdInit()
|
2007-07-16 11:19:44 +00:00
|
|
|
{
|
2021-10-22 02:40:55 +00:00
|
|
|
auto peer_id = tr_peer_id_t{};
|
|
|
|
auto* it = std::data(peer_id);
|
|
|
|
|
|
|
|
// starts with -TRXXXX-
|
|
|
|
auto constexpr Prefix = std::string_view{ PEERID_PREFIX };
|
|
|
|
auto const* const end = it + std::size(peer_id);
|
|
|
|
it = std::copy_n(std::data(Prefix), std::size(Prefix), it);
|
|
|
|
|
|
|
|
// remainder is randomly-generated characters
|
|
|
|
auto constexpr Pool = std::string_view{ "0123456789abcdefghijklmnopqrstuvwxyz" };
|
|
|
|
auto total = int{ 0 };
|
|
|
|
tr_rand_buffer(it, end - it);
|
|
|
|
while (it + 1 < end)
|
2013-05-23 05:43:12 +00:00
|
|
|
{
|
2021-10-22 02:40:55 +00:00
|
|
|
int const val = *it % std::size(Pool);
|
2017-04-19 12:04:45 +00:00
|
|
|
total += val;
|
2021-10-22 02:40:55 +00:00
|
|
|
*it++ = Pool[val];
|
2008-01-07 06:19:34 +00:00
|
|
|
}
|
2021-10-22 02:40:55 +00:00
|
|
|
int const val = total % std::size(Pool) != 0 ? std::size(Pool) - total % std::size(Pool) : 0;
|
|
|
|
*it = Pool[val];
|
2007-07-28 04:10:09 +00:00
|
|
|
|
2021-10-22 02:40:55 +00:00
|
|
|
return peer_id;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-11-11 16:09:24 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
std::vector<tr_torrent_id_t> tr_session::DhtMediator::torrents_allowing_dht() const
|
2022-11-11 16:09:24 +00:00
|
|
|
{
|
|
|
|
auto ids = std::vector<tr_torrent_id_t>{};
|
|
|
|
auto const& torrents = session_.torrents();
|
|
|
|
|
|
|
|
ids.reserve(std::size(torrents));
|
|
|
|
for (auto const* const tor : torrents)
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
if (tor->is_running() && tor->allows_dht())
|
2022-11-11 16:09:24 +00:00
|
|
|
{
|
|
|
|
ids.push_back(tor->id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
tr_sha1_digest_t tr_session::DhtMediator::torrent_info_hash(tr_torrent_id_t id) const
|
2022-11-11 16:09:24 +00:00
|
|
|
{
|
|
|
|
if (auto const* const tor = session_.torrents().get(id); tor != nullptr)
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return tor->info_hash();
|
2022-11-11 16:09:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void tr_session::DhtMediator::add_pex(tr_sha1_digest_t const& info_hash, tr_pex const* pex, size_t n_pex)
|
2022-11-11 16:09:24 +00:00
|
|
|
{
|
|
|
|
if (auto* const tor = session_.torrents().get(info_hash); tor != nullptr)
|
|
|
|
{
|
|
|
|
tr_peerMgrAddPex(tor, TR_PEER_FROM_DHT, pex, n_pex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
bool tr_session::LpdMediator::onPeerFound(std::string_view info_hash_str, tr_address address, tr_port port)
|
|
|
|
{
|
|
|
|
auto const digest = tr_sha1_from_string(info_hash_str);
|
|
|
|
if (!digest)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_torrent* const tor = session_.torrents_.get(*digest);
|
2023-04-23 01:25:55 +00:00
|
|
|
if (!tr_isTorrent(tor) || !tor->allows_lpd())
|
2022-08-24 21:03:30 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we found a suitable peer, add it to the torrent
|
2023-08-18 03:13:01 +00:00
|
|
|
auto const socket_address = tr_socket_address{ address, port };
|
|
|
|
auto const pex = tr_pex{ socket_address };
|
2022-08-24 21:03:30 +00:00
|
|
|
tr_peerMgrAddPex(tor, TR_PEER_FROM_LPD, &pex, 1U);
|
2023-08-18 03:13:01 +00:00
|
|
|
tr_logAddDebugTor(tor, fmt::format("Found a local peer from LPD ({:s})", socket_address.display_name()));
|
2022-08-24 21:03:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<tr_lpd::Mediator::TorrentInfo> tr_session::LpdMediator::torrents() const
|
|
|
|
{
|
|
|
|
auto ret = std::vector<tr_lpd::Mediator::TorrentInfo>{};
|
|
|
|
ret.reserve(std::size(session_.torrents()));
|
|
|
|
for (auto const* const tor : session_.torrents())
|
|
|
|
{
|
|
|
|
auto info = tr_lpd::Mediator::TorrentInfo{};
|
2023-04-23 01:25:55 +00:00
|
|
|
info.info_hash_str = tor->info_hash_string();
|
2022-11-22 20:00:09 +00:00
|
|
|
info.activity = tor->activity();
|
2023-04-23 01:25:55 +00:00
|
|
|
info.allows_lpd = tor->allows_lpd();
|
2022-08-24 21:03:30 +00:00
|
|
|
info.announce_after = tor->lpdAnnounceAt;
|
|
|
|
ret.emplace_back(info);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_session::LpdMediator::setNextAnnounceTime(std::string_view info_hash_str, time_t announce_after)
|
|
|
|
{
|
|
|
|
if (auto digest = tr_sha1_from_string(info_hash_str); digest)
|
|
|
|
{
|
|
|
|
if (tr_torrent* const tor = session_.torrents_.get(*digest); tr_isTorrent(tor))
|
|
|
|
{
|
|
|
|
tor->lpdAnnounceAt = announce_after;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2022-08-24 21:03:30 +00:00
|
|
|
|
2022-02-22 15:09:24 +00:00
|
|
|
std::optional<std::string> tr_session::WebMediator::cookieFile() const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-07-23 07:13:18 +00:00
|
|
|
auto const path = tr_pathbuf{ session_->configDir(), "/cookies.txt"sv };
|
2022-03-28 22:13:32 +00:00
|
|
|
|
|
|
|
if (!tr_sys_path_exists(path))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::string{ path };
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 14:28:01 +00:00
|
|
|
std::optional<std::string_view> tr_session::WebMediator::userAgent() const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-05-31 14:28:01 +00:00
|
|
|
return TR_NAME "/" SHORT_VERSION_STRING;
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 15:31:12 +00:00
|
|
|
std::optional<std::string> tr_session::WebMediator::bind_address_V4() const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2023-06-20 15:51:07 +00:00
|
|
|
if (auto const addr = session_->bind_address(TR_AF_INET); !addr.is_any())
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-12-08 22:44:19 +00:00
|
|
|
return addr.display_name();
|
2022-08-21 23:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2023-08-14 15:31:12 +00:00
|
|
|
std::optional<std::string> tr_session::WebMediator::bind_address_V6() const
|
2022-08-21 23:37:38 +00:00
|
|
|
{
|
2023-06-20 15:51:07 +00:00
|
|
|
if (auto const addr = session_->bind_address(TR_AF_INET6); !addr.is_any())
|
2022-08-21 23:37:38 +00:00
|
|
|
{
|
2022-12-08 22:44:19 +00:00
|
|
|
return addr.display_name();
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2022-11-21 16:19:45 +00:00
|
|
|
size_t tr_session::WebMediator::clamp(int torrent_id, size_t byte_count) const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
auto const lock = session_->unique_lock();
|
2022-03-18 16:34:29 +00:00
|
|
|
|
2022-03-01 23:06:29 +00:00
|
|
|
auto const* const tor = session_->torrents().get(torrent_id);
|
2022-03-18 16:34:29 +00:00
|
|
|
return tor == nullptr ? 0U : tor->bandwidth_.clamp(TR_DOWN, byte_count);
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 15:09:24 +00:00
|
|
|
void tr_session::WebMediator::notifyBandwidthConsumed(int torrent_id, size_t byte_count)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
auto const lock = session_->unique_lock();
|
2022-03-18 16:34:29 +00:00
|
|
|
|
|
|
|
if (auto* const tor = session_->torrents().get(torrent_id); tor != nullptr)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
tor->bandwidth_.notify_bandwidth_consumed(TR_DOWN, byte_count, true, tr_time_msec());
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 15:09:24 +00:00
|
|
|
void tr_session::WebMediator::run(tr_web::FetchDoneFunc&& func, tr_web::FetchResponse&& response) const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-11-04 21:20:27 +00:00
|
|
|
session_->runInSessionThread(std::move(func), std::move(response));
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 14:03:26 +00:00
|
|
|
time_t tr_session::WebMediator::now() const
|
|
|
|
{
|
|
|
|
return tr_time();
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
void tr_sessionFetch(tr_session* session, tr_web::FetchOptions&& options)
|
|
|
|
{
|
2022-10-14 02:25:02 +00:00
|
|
|
session->fetch(std::move(options));
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2022-02-16 18:33:50 +00:00
|
|
|
|
2022-06-15 22:19:24 +00:00
|
|
|
tr_encryption_mode tr_sessionGetEncryption(tr_session const* session)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-08-14 01:04:36 +00:00
|
|
|
return session->encryptionMode();
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetEncryption(tr_session* session, tr_encryption_mode mode)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(mode == TR_ENCRYPTION_PREFERRED || mode == TR_ENCRYPTION_REQUIRED || mode == TR_CLEAR_PREFERRED);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.encryption_mode = mode;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
void tr_session::onIncomingPeerConnection(tr_socket_t fd, void* vsession)
|
2009-05-14 13:42:29 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* session = static_cast<tr_session*>(vsession);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-10-14 06:20:39 +00:00
|
|
|
if (auto const incoming_info = tr_netAccept(session, fd); incoming_info)
|
2013-05-23 05:43:12 +00:00
|
|
|
{
|
2023-07-05 01:29:12 +00:00
|
|
|
auto const& [socket_address, sock] = *incoming_info;
|
2023-08-18 03:13:01 +00:00
|
|
|
tr_logAddTrace(fmt::format("new incoming connection {} ({})", sock, socket_address.display_name()));
|
2023-07-05 01:29:12 +00:00
|
|
|
session->addIncoming({ session, socket_address, sock });
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
tr_session::BoundSocket::BoundSocket(
|
2023-05-06 04:11:05 +00:00
|
|
|
struct event_base* evbase,
|
2022-11-06 16:35:48 +00:00
|
|
|
tr_address const& addr,
|
|
|
|
tr_port port,
|
|
|
|
IncomingCallback cb,
|
|
|
|
void* cb_data)
|
|
|
|
: cb_{ cb }
|
|
|
|
, cb_data_{ cb_data }
|
|
|
|
, socket_{ tr_netBindTCP(addr, port, false) }
|
2022-11-06 21:11:30 +00:00
|
|
|
, ev_{ event_new(evbase, socket_, EV_READ | EV_PERSIST, &BoundSocket::onCanRead, this) }
|
2009-05-14 13:42:29 +00:00
|
|
|
{
|
2022-11-06 16:35:48 +00:00
|
|
|
if (socket_ == TR_BAD_SOCKET)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-08-26 02:27:11 +00:00
|
|
|
|
2023-08-18 03:13:01 +00:00
|
|
|
tr_logAddInfo(fmt::format(
|
|
|
|
_("Listening to incoming peer connections on {hostport}"),
|
|
|
|
fmt::arg("hostport", tr_socket_address::display_name(addr, port))));
|
2022-11-06 21:11:30 +00:00
|
|
|
event_add(ev_.get(), nullptr);
|
2022-11-06 16:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_session::BoundSocket::~BoundSocket()
|
|
|
|
{
|
2022-11-06 21:11:30 +00:00
|
|
|
ev_.reset();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-07-29 13:11:47 +00:00
|
|
|
if (socket_ != TR_BAD_SOCKET)
|
2013-05-23 05:43:12 +00:00
|
|
|
{
|
2023-01-04 21:37:55 +00:00
|
|
|
tr_net_close_socket(socket_);
|
2022-11-06 16:35:48 +00:00
|
|
|
socket_ = TR_BAD_SOCKET;
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
2022-07-29 13:11:47 +00:00
|
|
|
}
|
|
|
|
|
2023-06-20 15:51:07 +00:00
|
|
|
tr_address tr_session::bind_address(tr_address_type type) const noexcept
|
2009-05-14 13:42:29 +00:00
|
|
|
{
|
2022-11-06 16:35:48 +00:00
|
|
|
if (type == TR_AF_INET)
|
2022-07-09 23:44:20 +00:00
|
|
|
{
|
2022-12-28 08:03:35 +00:00
|
|
|
// if user provided an address, use it.
|
|
|
|
// otherwise, use any_ipv4 (0.0.0.0).
|
2023-05-05 17:17:40 +00:00
|
|
|
return global_ip_cache_->bind_addr(type);
|
2022-11-06 16:35:48 +00:00
|
|
|
}
|
2022-07-09 23:44:20 +00:00
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
if (type == TR_AF_INET6)
|
|
|
|
{
|
2022-12-28 08:03:35 +00:00
|
|
|
// if user provided an address, use it.
|
2023-05-06 03:05:28 +00:00
|
|
|
// otherwise, if we can determine which one to use via global_source_address(ipv6) magic, use it.
|
2022-12-28 08:03:35 +00:00
|
|
|
// otherwise, use any_ipv6 (::).
|
2023-05-05 17:17:40 +00:00
|
|
|
auto const source_addr = global_source_address(type);
|
2023-06-11 23:27:01 +00:00
|
|
|
auto const default_addr = source_addr && source_addr->is_global_unicast_address() ? *source_addr :
|
2023-08-18 03:13:01 +00:00
|
|
|
tr_address::any(TR_AF_INET6);
|
2023-06-11 23:27:01 +00:00
|
|
|
return tr_address::from_string(settings_.bind_address_ipv6).value_or(default_addr);
|
2022-07-09 23:44:20 +00:00
|
|
|
}
|
2022-11-06 16:35:48 +00:00
|
|
|
|
|
|
|
TR_ASSERT_MSG(false, "invalid type");
|
|
|
|
return {};
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
// ---
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
namespace settings_helpers
|
|
|
|
{
|
|
|
|
|
|
|
|
void get_settings_filename(tr_pathbuf& setme, char const* config_dir, char const* appname)
|
|
|
|
{
|
|
|
|
if (!tr_str_is_empty(config_dir))
|
|
|
|
{
|
|
|
|
setme.assign(std::string_view{ config_dir }, "/settings.json"sv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const default_config_dir = tr_getDefaultConfigDir(appname);
|
|
|
|
setme.assign(std::string_view{ default_config_dir }, "/settings.json"sv);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace settings_helpers
|
|
|
|
} // namespace
|
2009-05-14 13:42:29 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionGetDefaultSettings(tr_variant* setme_dictionary)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_session_settings{}.save(setme_dictionary);
|
2023-05-06 04:11:05 +00:00
|
|
|
tr_rpc_server::default_settings(setme_dictionary);
|
|
|
|
tr_session_alt_speeds::default_settings(setme_dictionary);
|
2022-11-02 00:32:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_sessionGetSettings(tr_session const* session, tr_variant* setme_dictionary)
|
|
|
|
{
|
|
|
|
session->settings_.save(setme_dictionary);
|
|
|
|
session->alt_speeds_.save(setme_dictionary);
|
|
|
|
session->rpc_server_->save(setme_dictionary);
|
2022-11-26 13:32:51 +00:00
|
|
|
|
|
|
|
tr_variantDictRemove(setme_dictionary, TR_KEY_message_level);
|
|
|
|
tr_variantDictAddInt(setme_dictionary, TR_KEY_message_level, tr_logGetLevel());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
bool tr_sessionLoadSettings(tr_variant* settings_in, char const* config_dir, char const* app_name)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-01-07 22:55:00 +00:00
|
|
|
using namespace settings_helpers;
|
|
|
|
|
2023-08-21 21:16:54 +00:00
|
|
|
TR_ASSERT(settings_in != nullptr);
|
|
|
|
TR_ASSERT(settings_in->holds_alternative<tr_variant::Map>());
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
// first, start with the libtransmission default settings
|
|
|
|
auto settings = tr_variant{};
|
|
|
|
tr_variantInitDict(&settings, 0);
|
|
|
|
tr_sessionGetDefaultSettings(&settings);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
// now use the app default settings passed in
|
|
|
|
tr_variantMergeDicts(&settings, settings_in);
|
|
|
|
|
|
|
|
// now use a settings file to override all the defaults
|
2021-10-18 12:51:02 +00:00
|
|
|
auto success = bool{};
|
2022-07-23 01:10:02 +00:00
|
|
|
auto filename = tr_pathbuf{};
|
2023-01-07 22:55:00 +00:00
|
|
|
get_settings_filename(filename, config_dir, app_name);
|
2022-07-23 01:10:02 +00:00
|
|
|
if (!tr_sys_path_exists(filename))
|
|
|
|
{
|
|
|
|
success = true;
|
|
|
|
}
|
2023-08-17 16:02:45 +00:00
|
|
|
else if (auto file_settings = tr_variant_serde::json().parse_file(filename); file_settings)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-08-21 04:15:23 +00:00
|
|
|
tr_variantMergeDicts(&settings, &*file_settings);
|
2017-04-19 12:04:45 +00:00
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-23 01:10:02 +00:00
|
|
|
success = false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
std::swap(*settings_in, settings);
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return success;
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSaveSettings(tr_session* session, char const* config_dir, tr_variant const* client_settings)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-01-07 14:27:54 +00:00
|
|
|
using namespace bandwidth_group_helpers;
|
|
|
|
|
2023-08-21 21:16:54 +00:00
|
|
|
TR_ASSERT(client_settings != nullptr);
|
|
|
|
TR_ASSERT(client_settings->holds_alternative<tr_variant::Map>());
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant settings;
|
2022-03-26 03:39:08 +00:00
|
|
|
auto const filename = tr_pathbuf{ config_dir, "/settings.json"sv };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_variantInitDict(&settings, 0);
|
|
|
|
|
|
|
|
/* the existing file settings are the fallback values */
|
2023-08-21 04:15:23 +00:00
|
|
|
if (auto const file_settings = tr_variant_serde::json().parse_file(filename); file_settings)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-08-17 16:02:45 +00:00
|
|
|
tr_variantMergeDicts(&settings, &*file_settings);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the client's settings override the file settings */
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variantMergeDicts(&settings, client_settings);
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the session's true values override the file & client settings */
|
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto session_settings = tr_variant{};
|
|
|
|
tr_variantInitDict(&session_settings, 0);
|
|
|
|
tr_sessionGetSettings(session, &session_settings);
|
|
|
|
tr_variantMergeDicts(&settings, &session_settings);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* save the result */
|
2023-08-17 16:02:45 +00:00
|
|
|
tr_variant_serde::json().to_file(settings, filename);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-03-18 13:11:59 +00:00
|
|
|
/* Write bandwidth groups limits to file */
|
|
|
|
bandwidthGroupWrite(session, config_dir);
|
2008-12-13 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
// ---
|
2009-08-13 14:47:56 +00:00
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
struct tr_session::init_data
|
2009-02-04 16:58:52 +00:00
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
bool message_queuing_enabled;
|
|
|
|
std::string_view config_dir;
|
|
|
|
tr_variant* client_settings;
|
2022-03-04 22:43:56 +00:00
|
|
|
std::condition_variable_any done_cv;
|
2009-02-04 16:58:52 +00:00
|
|
|
};
|
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_session* tr_sessionInit(char const* config_dir, bool message_queueing_enabled, tr_variant* client_settings)
|
2008-12-13 23:17:36 +00:00
|
|
|
{
|
2023-01-07 14:27:54 +00:00
|
|
|
using namespace bandwidth_group_helpers;
|
|
|
|
|
2023-08-21 21:16:54 +00:00
|
|
|
TR_ASSERT(client_settings != nullptr);
|
|
|
|
TR_ASSERT(client_settings->holds_alternative<tr_variant::Map>());
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_timeUpdate(time(nullptr));
|
2010-12-03 22:56:17 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
// nice to start logging at the very beginning
|
|
|
|
if (auto val = int64_t{}; tr_variantDictFindInt(client_settings, TR_KEY_message_level, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_logSetLevel(static_cast<tr_log_level>(val));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-10-23 05:00:16 +00:00
|
|
|
|
2023-04-03 20:37:28 +00:00
|
|
|
/* initialize the bare skeleton of the session object */
|
|
|
|
auto* const session = new tr_session{ config_dir };
|
|
|
|
bandwidthGroupRead(session, config_dir);
|
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
auto data = tr_session::init_data{};
|
2022-01-02 17:51:59 +00:00
|
|
|
data.config_dir = config_dir;
|
2022-08-13 18:36:34 +00:00
|
|
|
data.message_queuing_enabled = message_queueing_enabled;
|
|
|
|
data.client_settings = client_settings;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-04 21:20:27 +00:00
|
|
|
// run initImpl() in the libtransmission thread
|
|
|
|
auto lock = session->unique_lock();
|
|
|
|
session->runInSessionThread([&session, &data]() { session->initImpl(data); });
|
|
|
|
data.done_cv.wait(lock); // wait for the session to be ready
|
2009-02-04 16:58:52 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return session;
|
2009-02-04 16:58:52 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 17:22:23 +00:00
|
|
|
void tr_session::onNowTimer()
|
2009-11-27 02:24:48 +00:00
|
|
|
{
|
2022-08-12 17:22:23 +00:00
|
|
|
TR_ASSERT(now_timer_);
|
2023-01-11 15:26:58 +00:00
|
|
|
auto const now = std::chrono::system_clock::now();
|
2010-12-23 19:32:59 +00:00
|
|
|
|
2022-08-08 22:53:20 +00:00
|
|
|
// tr_session upkeep tasks to perform once per second
|
2023-01-11 15:26:58 +00:00
|
|
|
tr_timeUpdate(std::chrono::system_clock::to_time_t(now));
|
2023-05-06 04:11:05 +00:00
|
|
|
alt_speeds_.check_scheduler();
|
2010-12-23 19:32:59 +00:00
|
|
|
|
2022-08-11 17:28:37 +00:00
|
|
|
// set the timer to kick again right after (10ms after) the next second
|
|
|
|
auto const target_time = std::chrono::time_point_cast<std::chrono::seconds>(now) + 1s + 10ms;
|
|
|
|
auto target_interval = target_time - now;
|
|
|
|
if (target_interval < 100ms)
|
2022-08-08 22:53:20 +00:00
|
|
|
{
|
2022-08-11 17:28:37 +00:00
|
|
|
target_interval += 1s;
|
2022-08-08 22:53:20 +00:00
|
|
|
}
|
2023-05-06 04:11:05 +00:00
|
|
|
now_timer_->set_interval(std::chrono::duration_cast<std::chrono::milliseconds>(target_interval));
|
2009-11-26 18:47:08 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
void tr_session::initImpl(init_data& data)
|
2009-02-04 16:58:52 +00:00
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
auto lock = unique_lock();
|
2023-05-06 04:11:05 +00:00
|
|
|
TR_ASSERT(am_in_session_thread());
|
2009-02-04 16:58:52 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
auto* const client_settings = data.client_settings;
|
2023-08-21 21:16:54 +00:00
|
|
|
TR_ASSERT(client_settings != nullptr);
|
|
|
|
TR_ASSERT(client_settings->holds_alternative<tr_variant::Map>());
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_logAddTrace(fmt::format("tr_sessionInit: the session's top-level bandwidth object is {}", fmt::ptr(&top_bandwidth_)));
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
auto settings = tr_variant{};
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(&settings, 0);
|
|
|
|
tr_sessionGetDefaultSettings(&settings);
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_variantMergeDicts(&settings, client_settings);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifndef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Don't exit when writing on a broken socket */
|
2022-04-22 19:56:54 +00:00
|
|
|
(void)signal(SIGPIPE, SIG_IGN);
|
2007-09-20 16:32:01 +00:00
|
|
|
#endif
|
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_logSetQueueEnabled(data.message_queuing_enabled);
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
this->blocklists_ = libtransmission::Blocklist::loadBlocklists(blocklist_dir_, useBlocklist());
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddInfo(fmt::format(_("Transmission version {version} starting"), fmt::arg("version", LONG_VERSION_STRING)));
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
setSettings(client_settings, true);
|
2007-12-24 07:02:40 +00:00
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
if (this->allowsLPD())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
this->lpd_ = tr_lpd::create(lpd_mediator_, event_base());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_utpInit(this);
|
2022-07-08 15:13:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* cleanup */
|
2022-08-13 18:36:34 +00:00
|
|
|
data.done_cv.notify_one();
|
2009-10-23 03:41:36 +00:00
|
|
|
}
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
void tr_session::setSettings(tr_variant* settings_dict, bool force)
|
2009-10-23 03:41:36 +00:00
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
TR_ASSERT(am_in_session_thread());
|
2023-08-21 21:16:54 +00:00
|
|
|
TR_ASSERT(settings_dict != nullptr);
|
|
|
|
TR_ASSERT(settings_dict->holds_alternative<tr_variant::Map>());
|
2022-11-06 16:35:48 +00:00
|
|
|
|
|
|
|
// load the session settings
|
|
|
|
auto new_settings = tr_session_settings{};
|
|
|
|
new_settings.load(settings_dict);
|
|
|
|
setSettings(std::move(new_settings), force);
|
|
|
|
|
|
|
|
// delegate loading out the other settings
|
|
|
|
alt_speeds_.load(settings_dict);
|
|
|
|
rpc_server_->load(settings_dict);
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2022-11-12 15:53:09 +00:00
|
|
|
void tr_session::setSettings(tr_session_settings&& settings_in, bool force)
|
2022-11-06 16:35:48 +00:00
|
|
|
{
|
|
|
|
auto const lock = unique_lock();
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
std::swap(settings_, settings_in);
|
|
|
|
auto const& new_settings = settings_;
|
|
|
|
auto const& old_settings = settings_in;
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
// the rest of the func is session_ responding to settings changes
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (auto const& val = new_settings.log_level; force || val != old_settings.log_level)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_logSetLevel(val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-10-23 05:00:16 +00:00
|
|
|
|
2014-12-13 15:22:39 +00:00
|
|
|
#ifndef _WIN32
|
2022-11-02 00:32:26 +00:00
|
|
|
if (auto const& val = new_settings.umask; force || val != old_settings.umask)
|
2022-04-28 14:16:19 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
::umask(val);
|
2009-10-23 03:41:36 +00:00
|
|
|
}
|
2014-12-13 15:22:39 +00:00
|
|
|
#endif
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (auto const& val = new_settings.cache_size_mb; force || val != old_settings.cache_size_mb)
|
2022-02-20 17:54:20 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_sessionSetCacheLimit_MB(this, val);
|
2022-02-20 17:54:20 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 17:17:40 +00:00
|
|
|
if (auto const& val = new_settings.bind_address_ipv4; force || val != old_settings.bind_address_ipv4)
|
|
|
|
{
|
2023-06-11 23:27:01 +00:00
|
|
|
global_ip_cache_->update_addr(TR_AF_INET);
|
2023-05-05 17:17:40 +00:00
|
|
|
}
|
|
|
|
if (auto const& val = new_settings.bind_address_ipv6; force || val != old_settings.bind_address_ipv6)
|
|
|
|
{
|
2023-06-11 23:27:01 +00:00
|
|
|
global_ip_cache_->update_addr(TR_AF_INET6);
|
2023-05-05 17:17:40 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (auto const& val = new_settings.default_trackers_str; force || val != old_settings.default_trackers_str)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
setDefaultTrackers(val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (auto const& val = new_settings.utp_enabled; force || val != old_settings.utp_enabled)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionSetUTPEnabled(this, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
useBlocklist(new_settings.blocklist_enabled);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
auto local_peer_port = force && settings_.peer_port_random_on_start ? randomPort() : new_settings.peer_port;
|
|
|
|
bool port_changed = false;
|
|
|
|
if (force || local_peer_port_ != local_peer_port)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-06 16:35:48 +00:00
|
|
|
local_peer_port_ = local_peer_port;
|
|
|
|
advertised_peer_port_ = local_peer_port;
|
|
|
|
port_changed = true;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-11 16:09:24 +00:00
|
|
|
bool addr_changed = false;
|
2022-11-06 16:35:48 +00:00
|
|
|
if (new_settings.tcp_enabled)
|
|
|
|
{
|
|
|
|
if (auto const& val = new_settings.bind_address_ipv4; force || port_changed || val != old_settings.bind_address_ipv4)
|
2022-07-25 22:25:55 +00:00
|
|
|
{
|
2023-06-20 15:51:07 +00:00
|
|
|
auto const addr = bind_address(TR_AF_INET);
|
2023-05-06 04:11:05 +00:00
|
|
|
bound_ipv4_.emplace(event_base(), addr, local_peer_port_, &tr_session::onIncomingPeerConnection, this);
|
2022-11-11 16:09:24 +00:00
|
|
|
addr_changed = true;
|
2022-07-25 22:25:55 +00:00
|
|
|
}
|
2022-07-29 13:11:47 +00:00
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
if (auto const& val = new_settings.bind_address_ipv6; force || port_changed || val != old_settings.bind_address_ipv6)
|
2022-07-25 22:25:55 +00:00
|
|
|
{
|
2023-06-20 15:51:07 +00:00
|
|
|
auto const addr = bind_address(TR_AF_INET6);
|
2023-05-06 04:11:05 +00:00
|
|
|
bound_ipv6_.emplace(event_base(), addr, local_peer_port_, &tr_session::onIncomingPeerConnection, this);
|
2022-11-11 16:09:24 +00:00
|
|
|
addr_changed = true;
|
2022-07-25 22:25:55 +00:00
|
|
|
}
|
2022-11-06 16:35:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bound_ipv4_.reset();
|
|
|
|
bound_ipv6_.reset();
|
2022-11-11 16:09:24 +00:00
|
|
|
addr_changed = true;
|
2022-11-06 16:35:48 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 07:31:48 +00:00
|
|
|
if (auto const& val = new_settings.port_forwarding_enabled; force || val != old_settings.port_forwarding_enabled)
|
|
|
|
{
|
|
|
|
tr_sessionSetPortForwardingEnabled(this, val);
|
|
|
|
}
|
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
if (port_changed)
|
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
port_forwarding_->local_port_changed();
|
2022-11-06 16:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool const dht_changed = new_settings.dht_enabled != old_settings.dht_enabled;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
if (!udp_core_ || force || port_changed || dht_changed)
|
|
|
|
{
|
|
|
|
udp_core_ = std::make_unique<tr_session::tr_udp_core>(*this, udpPort());
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
// Sends out announce messages with advertisedPeerPort(), so this
|
2023-04-30 18:29:29 +00:00
|
|
|
// section needs to happen here after the peer port settings changes
|
2022-11-06 16:35:48 +00:00
|
|
|
if (auto const& val = new_settings.lpd_enabled; force || val != old_settings.lpd_enabled)
|
|
|
|
{
|
|
|
|
if (val)
|
2022-04-21 15:58:13 +00:00
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
lpd_ = tr_lpd::create(lpd_mediator_, event_base());
|
2022-11-06 16:35:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lpd_.reset();
|
2022-04-21 15:58:13 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-11 16:09:24 +00:00
|
|
|
if (!allowsDHT())
|
|
|
|
{
|
|
|
|
dht_.reset();
|
|
|
|
}
|
|
|
|
else if (force || !dht_ || port_changed || addr_changed || dht_changed)
|
|
|
|
{
|
|
|
|
dht_ = tr_dht::create(dht_mediator_, localPeerPort(), udp_core_->socket4(), udp_core_->socket6());
|
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
// We need to update bandwidth if speed settings changed.
|
|
|
|
// It's a harmless call, so just call it instead of checking for settings changes
|
2023-01-07 22:55:00 +00:00
|
|
|
update_bandwidth(this, TR_UP);
|
|
|
|
update_bandwidth(this, TR_DOWN);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_sessionSet(tr_session* session, tr_variant* settings)
|
|
|
|
{
|
2022-11-10 18:04:49 +00:00
|
|
|
// do the work in the session thread
|
|
|
|
auto done_promise = std::promise<void>{};
|
|
|
|
auto done_future = done_promise.get_future();
|
|
|
|
session->runInSessionThread(
|
|
|
|
[&session, &settings, &done_promise]()
|
|
|
|
{
|
|
|
|
session->setSettings(settings, false);
|
|
|
|
done_promise.set_value();
|
|
|
|
});
|
|
|
|
done_future.wait();
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_sessionSetDownloadDir(tr_session* session, char const* dir)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2013-02-09 04:05:03 +00:00
|
|
|
|
2022-02-07 04:28:36 +00:00
|
|
|
session->setDownloadDir(dir != nullptr ? dir : "");
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_sessionGetDownloadDir(tr_session const* session)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2013-02-09 04:05:03 +00:00
|
|
|
|
2021-11-13 18:09:14 +00:00
|
|
|
return session->downloadDir().c_str();
|
2011-01-05 04:41:19 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 07:13:18 +00:00
|
|
|
char const* tr_sessionGetConfigDir(tr_session const* session)
|
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2022-07-23 07:13:18 +00:00
|
|
|
|
|
|
|
return session->configDir().c_str();
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2007-10-01 15:17:15 +00:00
|
|
|
|
2022-08-15 00:33:28 +00:00
|
|
|
void tr_sessionSetIncompleteFileNamingEnabled(tr_session* session, bool enabled)
|
2009-10-20 03:14:44 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-10-20 03:14:44 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.is_incomplete_file_naming_enabled = enabled;
|
2009-10-20 03:14:44 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsIncompleteFileNamingEnabled(tr_session const* session)
|
2009-10-20 03:14:44 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-10-20 03:14:44 +00:00
|
|
|
|
2022-08-15 00:33:28 +00:00
|
|
|
return session->isIncompleteFileNamingEnabled();
|
2009-10-20 03:14:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2009-10-20 03:14:44 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_sessionSetIncompleteDir(tr_session* session, char const* dir)
|
2009-10-19 05:05:00 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2022-02-07 04:28:36 +00:00
|
|
|
session->setIncompleteDir(dir != nullptr ? dir : "");
|
2009-10-19 05:05:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_sessionGetIncompleteDir(tr_session const* session)
|
2009-10-19 05:05:00 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2021-11-13 20:49:10 +00:00
|
|
|
return session->incompleteDir().c_str();
|
2009-10-19 05:05:00 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 19:49:43 +00:00
|
|
|
void tr_sessionSetIncompleteDirEnabled(tr_session* session, bool enabled)
|
2009-10-19 05:05:00 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2023-01-21 19:49:43 +00:00
|
|
|
session->useIncompleteDir(enabled);
|
2009-10-19 05:05:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsIncompleteDirEnabled(tr_session const* session)
|
2009-10-19 05:05:00 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2021-11-13 20:49:10 +00:00
|
|
|
return session->useIncompleteDir();
|
2009-10-19 05:05:00 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Peer Port
|
2007-11-22 06:13:57 +00:00
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
void tr_sessionSetPeerPort(tr_session* session, uint16_t hport)
|
2008-12-13 23:17:36 +00:00
|
|
|
{
|
2022-10-14 02:25:02 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
|
|
|
|
2023-08-18 03:13:01 +00:00
|
|
|
if (auto const port = tr_port::from_host(hport); port != session->localPeerPort())
|
2022-11-06 16:35:48 +00:00
|
|
|
{
|
|
|
|
session->runInSessionThread(
|
|
|
|
[session, port]()
|
|
|
|
{
|
|
|
|
auto settings = session->settings_;
|
|
|
|
settings.peer_port = port;
|
|
|
|
session->setSettings(std::move(settings), false);
|
|
|
|
});
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
uint16_t tr_sessionGetPeerPort(tr_session const* session)
|
2009-04-03 04:29:27 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
return session != nullptr ? session->localPeerPort().host() : 0U;
|
2009-04-03 04:29:27 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
uint16_t tr_sessionSetPeerPortRandom(tr_session* session)
|
2008-12-11 04:41:23 +00:00
|
|
|
{
|
2022-08-14 01:04:36 +00:00
|
|
|
auto const p = session->randomPort();
|
2022-08-03 03:38:37 +00:00
|
|
|
tr_sessionSetPeerPort(session, p.host());
|
|
|
|
return p.host();
|
2008-12-11 04:41:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetPeerPortRandomOnStart(tr_session* session, bool random)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.peer_port_random_on_start = random;
|
2009-04-03 04:29:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 22:19:24 +00:00
|
|
|
bool tr_sessionGetPeerPortRandomOnStart(tr_session const* session)
|
2009-04-03 04:29:27 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-04-03 04:29:27 +00:00
|
|
|
|
2022-08-15 00:33:28 +00:00
|
|
|
return session->isPortRandom();
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 13:59:10 +00:00
|
|
|
tr_port_forwarding_state tr_sessionGetPortForwarding(tr_session const* session)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-09-30 13:59:10 +00:00
|
|
|
return session->port_forwarding_->state();
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-11 16:09:24 +00:00
|
|
|
void tr_session::onAdvertisedPeerPortChanged()
|
|
|
|
{
|
|
|
|
for (auto* const tor : torrents())
|
|
|
|
{
|
|
|
|
tr_torrentChangeMyPort(tor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2007-07-20 08:33:59 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
void tr_sessionSetRatioLimited(tr_session* session, bool is_limited)
|
2009-03-01 13:56:22 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-01 13:56:22 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.ratio_limit_enabled = is_limited;
|
2009-03-01 13:56:22 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 00:33:28 +00:00
|
|
|
void tr_sessionSetRatioLimit(tr_session* session, double desired_ratio)
|
2009-03-01 13:56:22 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-01 13:56:22 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.ratio_limit = desired_ratio;
|
2009-03-01 13:56:22 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsRatioLimited(tr_session const* session)
|
2009-03-01 13:56:22 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-01 13:56:22 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
return session->isRatioLimited();
|
2009-03-01 13:56:22 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
double tr_sessionGetRatioLimit(tr_session const* session)
|
2009-03-01 13:56:22 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-01 13:56:22 +00:00
|
|
|
|
2022-08-15 00:33:28 +00:00
|
|
|
return session->desiredRatio();
|
2009-03-01 13:56:22 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2010-07-16 03:12:57 +00:00
|
|
|
|
2022-08-15 00:33:28 +00:00
|
|
|
void tr_sessionSetIdleLimited(tr_session* session, bool is_limited)
|
2010-07-16 03:12:57 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-07-16 03:12:57 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.idle_seeding_limit_enabled = is_limited;
|
2010-07-16 03:12:57 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
void tr_sessionSetIdleLimit(tr_session* session, uint16_t idle_minutes)
|
2010-07-16 03:12:57 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-07-16 03:12:57 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.idle_seeding_limit_minutes = idle_minutes;
|
2010-07-16 03:12:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsIdleLimited(tr_session const* session)
|
2010-07-16 03:12:57 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-07-16 03:12:57 +00:00
|
|
|
|
2022-08-15 00:33:28 +00:00
|
|
|
return session->isIdleLimited();
|
2010-07-16 03:12:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
uint16_t tr_sessionGetIdleLimit(tr_session const* session)
|
2010-07-16 03:12:57 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-07-16 03:12:57 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
return session->idleLimitMinutes();
|
2010-07-16 03:12:57 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Speed limits
|
2009-03-01 13:56:22 +00:00
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
std::optional<tr_bytes_per_second_t> tr_session::activeSpeedLimitBps(tr_direction dir) const noexcept
|
2009-03-28 16:47:01 +00:00
|
|
|
{
|
2022-08-14 19:41:57 +00:00
|
|
|
if (tr_sessionUsesAltSpeed(this))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
return tr_toSpeedBytes(tr_sessionGetAltSpeed_KBps(this, dir));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-06-30 18:08:50 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
if (this->isSpeedLimited(dir))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
return tr_toSpeedBytes(tr_sessionGetSpeedLimit_KBps(this, dir));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
return {};
|
2010-07-04 06:07:21 +00:00
|
|
|
}
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
time_t tr_session::AltSpeedMediator::time()
|
2009-03-28 16:47:01 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
return tr_time();
|
2010-02-04 23:39:50 +00:00
|
|
|
}
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void tr_session::AltSpeedMediator::is_active_changed(bool is_active, tr_session_alt_speeds::ChangeReason reason)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
auto const in_session_thread = [session = &session_, is_active, reason]()
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-01-07 22:55:00 +00:00
|
|
|
update_bandwidth(session, TR_UP);
|
|
|
|
update_bandwidth(session, TR_DOWN);
|
2010-03-12 19:35:28 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (session->alt_speed_active_changed_func_ != nullptr)
|
|
|
|
{
|
|
|
|
session->alt_speed_active_changed_func_(
|
|
|
|
session,
|
|
|
|
is_active,
|
|
|
|
reason == tr_session_alt_speeds::ChangeReason::User,
|
|
|
|
session->alt_speed_active_changed_func_user_data_);
|
|
|
|
}
|
|
|
|
};
|
2010-02-16 00:00:22 +00:00
|
|
|
|
2022-11-04 21:20:27 +00:00
|
|
|
session_.runInSessionThread(in_session_thread);
|
2008-12-13 22:52:15 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Session primary speed limits
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
void tr_sessionSetSpeedLimit_KBps(tr_session* session, tr_direction dir, tr_kilobytes_per_second_t limit)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2022-08-15 17:48:05 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (dir == TR_DOWN)
|
|
|
|
{
|
|
|
|
session->settings_.speed_limit_down = limit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
session->settings_.speed_limit_up = limit;
|
|
|
|
}
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
update_bandwidth(session, dir);
|
2009-03-28 16:47:01 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_kilobytes_per_second_t tr_sessionGetSpeedLimit_KBps(tr_session const* session, tr_direction dir)
|
2010-07-04 06:07:21 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
return dir == TR_DOWN ? session->settings_.speed_limit_down : session->settings_.speed_limit_up;
|
2010-07-04 06:07:21 +00:00
|
|
|
}
|
2008-05-12 16:33:17 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
void tr_sessionLimitSpeed(tr_session* session, tr_direction dir, bool limited)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2022-08-15 17:48:05 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2008-12-13 22:52:15 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (dir == TR_DOWN)
|
|
|
|
{
|
|
|
|
session->settings_.speed_limit_down_enabled = limited;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
session->settings_.speed_limit_up_enabled = limited;
|
|
|
|
}
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
update_bandwidth(session, dir);
|
2008-12-13 22:52:15 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
bool tr_sessionIsSpeedLimited(tr_session const* session, tr_direction dir)
|
2008-12-13 22:52:15 +00:00
|
|
|
{
|
2022-08-15 17:48:05 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2008-12-13 22:52:15 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
return session->isSpeedLimited(dir);
|
2007-01-08 21:53:55 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Session alt speed limits
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
void tr_sessionSetAltSpeed_KBps(tr_session* session, tr_direction dir, tr_kilobytes_per_second_t limit)
|
2009-03-25 19:50:58 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
session->alt_speeds_.set_limit_kbps(dir, limit);
|
2023-01-07 22:55:00 +00:00
|
|
|
update_bandwidth(session, dir);
|
2010-07-04 06:07:21 +00:00
|
|
|
}
|
2009-03-25 19:50:58 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_kilobytes_per_second_t tr_sessionGetAltSpeed_KBps(tr_session const* session, tr_direction dir)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
return session->alt_speeds_.limit_kbps(dir);
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
2010-02-04 23:39:50 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
void tr_sessionUseAltSpeedTime(tr_session* session, bool enabled)
|
2009-03-30 17:25:35 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
session->alt_speeds_.set_scheduler_enabled(enabled);
|
2009-03-30 17:25:35 +00:00
|
|
|
}
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
bool tr_sessionUsesAltSpeedTime(tr_session const* session)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
return session->alt_speeds_.is_scheduler_enabled();
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
void tr_sessionSetAltSpeedBegin(tr_session* session, size_t minutes_since_midnight)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-25 19:50:58 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
session->alt_speeds_.set_start_minute(minutes_since_midnight);
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
size_t tr_sessionGetAltSpeedBegin(tr_session const* session)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
return session->alt_speeds_.start_minute();
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
2022-11-02 00:32:26 +00:00
|
|
|
void tr_sessionSetAltSpeedEnd(tr_session* session, size_t minutes_since_midnight)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
session->alt_speeds_.set_end_minute(minutes_since_midnight);
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
size_t tr_sessionGetAltSpeedEnd(tr_session const* session)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
return session->alt_speeds_.end_minute();
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
void tr_sessionSetAltSpeedDay(tr_session* session, tr_sched_day days)
|
2009-04-04 02:17:39 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-04-04 02:17:39 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
session->alt_speeds_.set_weekdays(days);
|
2009-04-04 02:17:39 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_sched_day tr_sessionGetAltSpeedDay(tr_session const* session)
|
2009-04-04 02:17:39 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-04-04 02:17:39 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
return session->alt_speeds_.weekdays();
|
2009-04-04 02:17:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionUseAltSpeed(tr_session* session, bool enabled)
|
2009-03-30 17:25:35 +00:00
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
session->alt_speeds_.set_active(enabled, tr_session_alt_speeds::ChangeReason::User);
|
2009-03-30 17:25:35 +00:00
|
|
|
}
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
bool tr_sessionUsesAltSpeed(tr_session const* session)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
return session->alt_speeds_.is_active();
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
void tr_sessionSetAltSpeedFunc(tr_session* session, tr_altSpeedFunc func, void* user_data)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->alt_speed_active_changed_func_ = func;
|
|
|
|
session->alt_speed_active_changed_func_user_data_ = user_data;
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2007-12-20 21:44:16 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetPeerLimit(tr_session* session, uint16_t max_global_peers)
|
2007-12-20 21:44:16 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.peer_limit_global = max_global_peers;
|
2007-12-20 21:44:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
uint16_t tr_sessionGetPeerLimit(tr_session const* session)
|
2007-12-20 21:44:16 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-08-13 22:41:42 +00:00
|
|
|
return session->peerLimit();
|
2007-12-20 21:44:16 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetPeerLimitPerTorrent(tr_session* session, uint16_t max_peers)
|
2008-12-14 22:57:08 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.peer_limit_per_torrent = max_peers;
|
2008-12-14 22:57:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
uint16_t tr_sessionGetPeerLimitPerTorrent(tr_session const* session)
|
2008-12-14 22:57:08 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-08-13 22:41:42 +00:00
|
|
|
return session->peerLimitPerTorrent();
|
2008-12-14 22:57:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
// ---
|
2008-05-12 16:33:17 +00:00
|
|
|
|
2022-08-14 01:04:36 +00:00
|
|
|
void tr_sessionSetPaused(tr_session* session, bool is_paused)
|
2010-03-17 19:23:03 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-03-17 19:23:03 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.should_start_added_torrents = !is_paused;
|
2010-03-17 19:23:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionGetPaused(tr_session const* session)
|
2010-03-17 19:23:03 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-03-17 19:23:03 +00:00
|
|
|
|
2022-08-14 01:04:36 +00:00
|
|
|
return session->shouldPauseAddedTorrents();
|
2010-03-17 19:23:03 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 01:04:36 +00:00
|
|
|
void tr_sessionSetDeleteSource(tr_session* session, bool delete_source)
|
2010-03-17 19:23:03 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-03-17 19:23:03 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.should_delete_source_torrents = delete_source;
|
2010-03-17 19:23:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
// ---
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
double tr_sessionGetRawSpeed_KBps(tr_session const* session, tr_direction dir)
|
2010-07-04 06:07:21 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const bps = session != nullptr ? session->top_bandwidth_.get_raw_speed_bytes_per_second(0, dir) : 0;
|
2023-01-07 22:55:00 +00:00
|
|
|
return tr_toSpeedKBps(bps);
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
void tr_session::closeImplPart1(std::promise<void>* closed_promise, std::chrono::time_point<std::chrono::steady_clock> deadline)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2022-08-12 17:22:23 +00:00
|
|
|
is_closing_ = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-04 21:20:27 +00:00
|
|
|
// close the low-hanging fruit that can be closed immediately w/o consequences
|
2022-11-30 22:35:10 +00:00
|
|
|
utp_timer.reset();
|
2022-11-04 21:20:27 +00:00
|
|
|
verifier_.reset();
|
2022-08-12 17:22:23 +00:00
|
|
|
save_timer_.reset();
|
|
|
|
now_timer_.reset();
|
2022-11-04 21:20:27 +00:00
|
|
|
rpc_server_.reset();
|
2022-11-11 16:09:24 +00:00
|
|
|
dht_.reset();
|
2022-11-04 21:20:27 +00:00
|
|
|
lpd_.reset();
|
2022-11-11 16:09:24 +00:00
|
|
|
|
2022-09-30 13:59:10 +00:00
|
|
|
port_forwarding_.reset();
|
2022-11-06 16:35:48 +00:00
|
|
|
bound_ipv6_.reset();
|
|
|
|
bound_ipv4_.reset();
|
2009-08-13 14:47:56 +00:00
|
|
|
|
2022-11-04 21:20:27 +00:00
|
|
|
// Close the torrents in order of most active to least active
|
|
|
|
// so that the most important announce=stopped events are
|
|
|
|
// fired out first...
|
2022-08-14 19:41:57 +00:00
|
|
|
auto torrents = getAllTorrents();
|
2021-09-24 23:31:02 +00:00
|
|
|
std::sort(
|
|
|
|
std::begin(torrents),
|
|
|
|
std::end(torrents),
|
|
|
|
[](auto const* a, auto const* b)
|
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto const a_cur = a->downloadedCur + a->uploadedCur;
|
|
|
|
auto const b_cur = b->downloadedCur + b->uploadedCur;
|
|
|
|
return a_cur > b_cur; // larger xfers go first
|
2021-09-24 23:31:02 +00:00
|
|
|
});
|
|
|
|
for (auto* tor : torrents)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-04 21:20:27 +00:00
|
|
|
tr_torrentFreeInSessionThread(tor);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-09-24 23:31:02 +00:00
|
|
|
torrents.clear();
|
2022-11-30 03:05:11 +00:00
|
|
|
// ...now that all the torrents have been closed, any remaining
|
|
|
|
// `&event=stopped` announce messages are queued in the announcer.
|
|
|
|
// Tell the announcer to start shutdown, which sends out the stop
|
|
|
|
// events and stops scraping.
|
|
|
|
this->announcer_->startShutdown();
|
2023-05-05 17:17:40 +00:00
|
|
|
// ...since global_ip_cache_ relies on web_ to update global addresses,
|
|
|
|
// we tell it to stop updating before web_ starts to refuse new requests.
|
|
|
|
// But we keep it intact for now, so that udp_core_ can continue.
|
|
|
|
this->global_ip_cache_->try_shutdown();
|
|
|
|
// ...and now that those are done, tell web_ that we're shutting
|
2022-11-30 03:05:11 +00:00
|
|
|
// down soon. This leaves the `event=stopped` going but refuses any
|
|
|
|
// new tasks.
|
2022-11-30 19:00:34 +00:00
|
|
|
this->web_->startShutdown(10s);
|
2022-08-12 17:22:23 +00:00
|
|
|
this->cache.reset();
|
2015-12-31 14:24:15 +00:00
|
|
|
|
2022-11-04 21:20:27 +00:00
|
|
|
// recycle the now-unused save_timer_ here to wait for UDP shutdown
|
2022-08-12 17:22:23 +00:00
|
|
|
TR_ASSERT(!save_timer_);
|
2022-11-30 19:00:34 +00:00
|
|
|
save_timer_ = timerMaker().create([this, closed_promise, deadline]() { closeImplPart2(closed_promise, deadline); });
|
2023-05-06 04:11:05 +00:00
|
|
|
save_timer_->start_repeating(50ms);
|
2015-12-31 14:17:37 +00:00
|
|
|
}
|
2013-05-23 05:43:12 +00:00
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
void tr_session::closeImplPart2(std::promise<void>* closed_promise, std::chrono::time_point<std::chrono::steady_clock> deadline)
|
2015-12-31 14:17:37 +00:00
|
|
|
{
|
2022-11-04 21:20:27 +00:00
|
|
|
// try to keep the UDP announcer alive long enough to send out
|
2023-05-05 17:17:40 +00:00
|
|
|
// all the &event=stopped tracker announces.
|
|
|
|
// also wait for all ip cache updates to finish so that web_ can
|
|
|
|
// safely destruct.
|
|
|
|
if ((n_pending_stops_ != 0U || !global_ip_cache_->try_shutdown()) && std::chrono::steady_clock::now() < deadline)
|
2015-12-31 14:17:37 +00:00
|
|
|
{
|
2022-10-21 18:15:14 +00:00
|
|
|
announcer_udp_->upkeep();
|
2017-04-19 12:04:45 +00:00
|
|
|
return;
|
2015-12-31 14:17:37 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 17:22:23 +00:00
|
|
|
save_timer_.reset();
|
2017-01-17 20:55:34 +00:00
|
|
|
|
2022-11-30 03:05:11 +00:00
|
|
|
this->announcer_.reset();
|
2022-10-21 18:15:14 +00:00
|
|
|
this->announcer_udp_.reset();
|
2015-12-31 14:24:15 +00:00
|
|
|
|
2023-05-05 20:33:23 +00:00
|
|
|
stats().save();
|
2022-11-02 00:32:26 +00:00
|
|
|
peer_mgr_.reset();
|
2023-05-06 04:11:05 +00:00
|
|
|
openFiles().close_all();
|
2022-12-18 17:18:25 +00:00
|
|
|
tr_utpClose(this);
|
|
|
|
this->udp_core_.reset();
|
2022-11-10 02:30:34 +00:00
|
|
|
|
|
|
|
// tada we are done!
|
2022-11-10 18:04:49 +00:00
|
|
|
closed_promise->set_value();
|
2015-12-31 14:17:37 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
void tr_sessionClose(tr_session* session, size_t timeout_secs)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2023-05-06 04:11:05 +00:00
|
|
|
TR_ASSERT(!session->am_in_session_thread());
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddInfo(fmt::format(_("Transmission version {version} shutting down"), fmt::arg("version", LONG_VERSION_STRING)));
|
2008-02-04 19:54:47 +00:00
|
|
|
|
2022-11-10 18:04:49 +00:00
|
|
|
auto closed_promise = std::promise<void>{};
|
|
|
|
auto closed_future = closed_promise.get_future();
|
2022-11-30 19:00:34 +00:00
|
|
|
auto const deadline = std::chrono::steady_clock::now() + std::chrono::seconds{ timeout_secs };
|
|
|
|
session->runInSessionThread([&closed_promise, deadline, session]() { session->closeImplPart1(&closed_promise, deadline); });
|
|
|
|
closed_future.wait();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-24 23:31:02 +00:00
|
|
|
delete session;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-07-21 18:46:54 +00:00
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
namespace load_torrents_helpers
|
|
|
|
{
|
2023-02-25 13:11:19 +00:00
|
|
|
void session_load_torrents(tr_session* session, tr_ctor* ctor, std::promise<size_t>* loaded_promise)
|
|
|
|
{
|
|
|
|
auto n_torrents = size_t{};
|
|
|
|
auto const& folder = session->torrentDir();
|
|
|
|
|
2023-06-30 14:49:58 +00:00
|
|
|
for (auto const& name : tr_sys_dir_get_files(folder, [](auto name) { return tr_strv_ends_with(name, ".torrent"sv); }))
|
2023-02-25 13:11:19 +00:00
|
|
|
{
|
|
|
|
auto const path = tr_pathbuf{ folder, '/', name };
|
|
|
|
|
|
|
|
if (tr_ctorSetMetainfoFromFile(ctor, path.sv(), nullptr) && tr_torrentNew(ctor, nullptr) != nullptr)
|
|
|
|
{
|
|
|
|
++n_torrents;
|
2007-07-21 18:46:54 +00:00
|
|
|
}
|
2023-02-25 13:11:19 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-02-25 13:11:19 +00:00
|
|
|
auto buf = std::vector<char>{};
|
2023-06-30 14:49:58 +00:00
|
|
|
for (auto const& name : tr_sys_dir_get_files(folder, [](auto name) { return tr_strv_ends_with(name, ".magnet"sv); }))
|
2023-02-25 13:11:19 +00:00
|
|
|
{
|
|
|
|
auto const path = tr_pathbuf{ folder, '/', name };
|
|
|
|
|
2023-06-30 14:49:58 +00:00
|
|
|
if (tr_file_read(path, buf) &&
|
2023-02-25 13:11:19 +00:00
|
|
|
tr_ctorSetMetainfoFromMagnetLink(ctor, std::string_view{ std::data(buf), std::size(buf) }, nullptr) &&
|
|
|
|
tr_torrentNew(ctor, nullptr) != nullptr)
|
|
|
|
{
|
|
|
|
++n_torrents;
|
|
|
|
}
|
2007-07-21 18:46:54 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 18:04:49 +00:00
|
|
|
if (n_torrents != 0U)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-10 18:04:49 +00:00
|
|
|
tr_logAddInfo(fmt::format(
|
2022-12-26 21:13:21 +00:00
|
|
|
tr_ngettext("Loaded {count} torrent", "Loaded {count} torrents", n_torrents),
|
2022-11-10 18:04:49 +00:00
|
|
|
fmt::arg("count", n_torrents)));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 18:04:49 +00:00
|
|
|
loaded_promise->set_value(n_torrents);
|
2011-06-19 18:30:57 +00:00
|
|
|
}
|
2023-01-07 22:55:00 +00:00
|
|
|
} // namespace load_torrents_helpers
|
|
|
|
} // namespace
|
2011-06-19 18:30:57 +00:00
|
|
|
|
2022-08-18 14:14:12 +00:00
|
|
|
size_t tr_sessionLoadTorrents(tr_session* session, tr_ctor* ctor)
|
2011-06-19 18:30:57 +00:00
|
|
|
{
|
2023-01-07 22:55:00 +00:00
|
|
|
using namespace load_torrents_helpers;
|
|
|
|
|
2022-11-10 18:04:49 +00:00
|
|
|
auto loaded_promise = std::promise<size_t>{};
|
|
|
|
auto loaded_future = loaded_promise.get_future();
|
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
session->runInSessionThread(session_load_torrents, session, ctor, &loaded_promise);
|
2022-11-10 18:04:49 +00:00
|
|
|
loaded_future.wait();
|
|
|
|
auto const n_torrents = loaded_future.get();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-10 18:04:49 +00:00
|
|
|
return n_torrents;
|
2022-08-18 14:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t tr_sessionGetAllTorrents(tr_session* session, tr_torrent** buf, size_t buflen)
|
|
|
|
{
|
|
|
|
auto& torrents = session->torrents();
|
|
|
|
auto const n = std::size(torrents);
|
|
|
|
|
|
|
|
if (buflen >= n)
|
|
|
|
{
|
|
|
|
std::copy_n(std::begin(torrents), n, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
2007-07-21 18:46:54 +00:00
|
|
|
}
|
2007-12-24 05:03:40 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2007-12-24 05:03:40 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetPexEnabled(tr_session* session, bool enabled)
|
2007-12-24 05:03:40 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.pex_enabled = enabled;
|
2007-12-24 05:03:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsPexEnabled(tr_session const* session)
|
2007-12-24 05:03:40 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2023-07-14 15:51:52 +00:00
|
|
|
return session->allows_pex();
|
2007-12-24 05:03:40 +00:00
|
|
|
}
|
2008-04-01 19:20:21 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsDHTEnabled(tr_session const* session)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
return session->allowsDHT();
|
2009-06-12 22:17:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetDHTEnabled(tr_session* session, bool enabled)
|
2009-06-12 22:17:59 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-06-12 22:17:59 +00:00
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
if (enabled != session->allowsDHT())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-06 16:35:48 +00:00
|
|
|
session->runInSessionThread(
|
|
|
|
[session, enabled]()
|
|
|
|
{
|
|
|
|
auto settings = session->settings_;
|
|
|
|
settings.dht_enabled = enabled;
|
|
|
|
session->setSettings(std::move(settings), false);
|
|
|
|
});
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-21 00:15:54 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2011-02-18 00:33:11 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
bool tr_session::allowsUTP() const noexcept
|
2011-02-18 00:33:11 +00:00
|
|
|
{
|
2011-02-18 00:45:44 +00:00
|
|
|
#ifdef WITH_UTP
|
2022-11-02 00:32:26 +00:00
|
|
|
return settings_.utp_enabled;
|
2011-02-18 00:45:44 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
return false;
|
2011-02-18 00:45:44 +00:00
|
|
|
#endif
|
2011-02-18 00:33:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
bool tr_sessionIsUTPEnabled(tr_session const* session)
|
2011-02-18 00:33:11 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2011-02-18 00:33:11 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
return session->allowsUTP();
|
2011-02-18 00:33:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetUTPEnabled(tr_session* session, bool enabled)
|
2011-02-18 00:33:11 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2011-02-18 00:33:11 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
if (enabled == session->allowsUTP())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-15 17:48:05 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-02-18 00:33:11 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.utp_enabled = enabled;
|
2022-09-21 18:25:53 +00:00
|
|
|
}
|
2011-02-18 00:33:11 +00:00
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
void tr_sessionSetLPDEnabled(tr_session* session, bool enabled)
|
2010-08-07 03:29:47 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-08-07 03:29:47 +00:00
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
if (enabled != session->allowsLPD())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-06 16:35:48 +00:00
|
|
|
session->runInSessionThread(
|
|
|
|
[session, enabled]()
|
2022-08-14 19:41:57 +00:00
|
|
|
{
|
2022-11-06 16:35:48 +00:00
|
|
|
auto settings = session->settings_;
|
|
|
|
settings.lpd_enabled = enabled;
|
|
|
|
session->setSettings(std::move(settings), false);
|
|
|
|
});
|
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsLPDEnabled(tr_session const* session)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
return session->allowsLPD();
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2008-04-01 19:20:21 +00:00
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
void tr_sessionSetCacheLimit_MB(tr_session* session, size_t mb)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.cache_size_mb = mb;
|
2023-05-06 04:11:05 +00:00
|
|
|
session->cache->set_limit(tr_toMemBytes(mb));
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
size_t tr_sessionGetCacheLimit_MB(tr_session const* session)
|
2010-07-04 06:07:21 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-07-04 06:07:21 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
return session->settings_.cache_size_mb;
|
2010-07-07 23:37:03 +00:00
|
|
|
}
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-02-20 17:54:20 +00:00
|
|
|
void tr_session::setDefaultTrackers(std::string_view trackers)
|
|
|
|
{
|
2022-02-21 03:25:49 +00:00
|
|
|
auto const oldval = default_trackers_;
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
settings_.default_trackers_str = trackers;
|
2022-02-21 03:25:49 +00:00
|
|
|
default_trackers_.parse(trackers);
|
|
|
|
|
|
|
|
// if the list changed, update all the public torrents
|
|
|
|
if (default_trackers_ != oldval)
|
|
|
|
{
|
2022-03-01 23:06:29 +00:00
|
|
|
for (auto* const tor : torrents())
|
2022-02-21 03:25:49 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
if (tor->is_public())
|
2022-02-21 03:25:49 +00:00
|
|
|
{
|
2022-11-19 05:00:25 +00:00
|
|
|
announcer_->resetTorrent(tor);
|
2022-02-21 03:25:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-20 17:54:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_sessionSetDefaultTrackers(tr_session* session, char const* trackers)
|
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2022-02-20 17:54:20 +00:00
|
|
|
|
2022-02-20 19:00:52 +00:00
|
|
|
session->setDefaultTrackers(trackers != nullptr ? trackers : "");
|
2022-02-20 17:54:20 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2022-02-20 17:54:20 +00:00
|
|
|
|
2022-06-29 20:08:58 +00:00
|
|
|
tr_bandwidth& tr_session::getBandwidthGroup(std::string_view name)
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
2022-03-18 16:34:29 +00:00
|
|
|
auto& groups = this->bandwidth_groups_;
|
2022-03-18 13:11:59 +00:00
|
|
|
|
2022-04-22 13:08:02 +00:00
|
|
|
for (auto const& [group_name, group] : groups)
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
2022-04-22 13:08:02 +00:00
|
|
|
if (group_name == name)
|
|
|
|
{
|
|
|
|
return *group;
|
|
|
|
}
|
2022-03-18 13:11:59 +00:00
|
|
|
}
|
2022-03-18 16:34:29 +00:00
|
|
|
|
2022-06-29 20:08:58 +00:00
|
|
|
auto& [group_name, group] = groups.emplace_back(name, std::make_unique<tr_bandwidth>(new tr_bandwidth(&top_bandwidth_)));
|
2022-04-22 13:08:02 +00:00
|
|
|
return *group;
|
2022-03-18 13:11:59 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2022-03-18 13:11:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetPortForwardingEnabled(tr_session* session, bool enabled)
|
2010-02-06 20:25:25 +00:00
|
|
|
{
|
2023-02-12 02:49:16 +00:00
|
|
|
session->runInSessionThread(
|
|
|
|
[session, enabled]()
|
|
|
|
{
|
|
|
|
session->settings_.port_forwarding_enabled = enabled;
|
2023-05-06 04:11:05 +00:00
|
|
|
session->port_forwarding_->set_enabled(enabled);
|
2023-02-12 02:49:16 +00:00
|
|
|
});
|
2008-05-12 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsPortForwardingEnabled(tr_session const* session)
|
2008-05-12 16:33:17 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
return session->port_forwarding_->is_enabled();
|
2008-05-12 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2008-05-12 16:33:17 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
void tr_session::useBlocklist(bool enabled)
|
2009-12-01 00:07:18 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
settings_.blocklist_enabled = enabled;
|
2022-08-15 17:48:05 +00:00
|
|
|
|
|
|
|
std::for_each(
|
|
|
|
std::begin(blocklists_),
|
|
|
|
std::end(blocklists_),
|
2022-11-03 20:46:27 +00:00
|
|
|
[enabled](auto& blocklist) { blocklist.setEnabled(enabled); });
|
2022-08-15 17:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_session::addressIsBlocked(tr_address const& addr) const noexcept
|
|
|
|
{
|
|
|
|
return std::any_of(
|
|
|
|
std::begin(blocklists_),
|
|
|
|
std::end(blocklists_),
|
2022-11-03 20:46:27 +00:00
|
|
|
[&addr](auto& blocklist) { return blocklist.contains(addr); });
|
2009-12-01 00:07:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionReloadBlocklists(tr_session* session)
|
2009-12-01 00:07:18 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
session->blocklists_ = libtransmission::Blocklist::loadBlocklists(session->blocklist_dir_, session->useBlocklist());
|
2010-06-14 12:01:50 +00:00
|
|
|
|
2023-07-04 20:47:18 +00:00
|
|
|
session->blocklist_changed_.emit();
|
2009-12-01 00:07:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 05:31:24 +00:00
|
|
|
size_t tr_blocklistGetRuleCount(tr_session const* session)
|
2008-04-01 19:20:21 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2013-05-23 05:43:12 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
auto& src = session->blocklists_;
|
2022-11-03 20:46:27 +00:00
|
|
|
return std::accumulate(std::begin(src), std::end(src), 0, [](int sum, auto& cur) { return sum + std::size(cur); });
|
2008-04-01 19:20:21 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_blocklistIsEnabled(tr_session const* session)
|
2008-04-01 19:20:21 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2021-11-14 01:09:33 +00:00
|
|
|
return session->useBlocklist();
|
2008-04-01 19:20:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 01:09:33 +00:00
|
|
|
void tr_blocklistSetEnabled(tr_session* session, bool enabled)
|
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2021-11-14 01:09:33 +00:00
|
|
|
|
|
|
|
session->useBlocklist(enabled);
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_blocklistExists(tr_session const* session)
|
2008-04-01 19:20:21 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
return !std::empty(session->blocklists_);
|
2008-04-01 19:20:21 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
size_t tr_blocklistSetContent(tr_session* session, char const* content_filename)
|
2008-04-01 19:20:21 +00:00
|
|
|
{
|
2021-11-20 21:20:45 +00:00
|
|
|
auto const lock = session->unique_lock();
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
// These rules will replace the default blocklist.
|
|
|
|
// Build the path of the default blocklist .bin file where we'll save these rules.
|
|
|
|
auto const bin_file = tr_pathbuf{ session->blocklist_dir_, '/', DEFAULT_BLOCKLIST_FILENAME };
|
|
|
|
|
|
|
|
// Try to save it
|
|
|
|
auto added = libtransmission::Blocklist::saveNew(content_filename, bin_file, session->useBlocklist());
|
|
|
|
if (!added)
|
|
|
|
{
|
|
|
|
return 0U;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const n_rules = std::size(*added);
|
2021-12-16 22:58:58 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
// Add (or replace) it in our blocklists_ vector
|
|
|
|
auto& src = session->blocklists_;
|
|
|
|
if (auto iter = std::find_if(
|
|
|
|
std::begin(src),
|
|
|
|
std::end(src),
|
|
|
|
[&bin_file](auto const& candidate) { return bin_file == candidate.binFile(); });
|
|
|
|
iter != std::end(src))
|
2021-09-27 18:28:41 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
*iter = std::move(*added);
|
2008-06-11 16:15:45 +00:00
|
|
|
}
|
2021-09-27 18:28:41 +00:00
|
|
|
else
|
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
src.emplace_back(std::move(*added));
|
2021-09-27 18:28:41 +00:00
|
|
|
}
|
2008-06-11 16:15:45 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
return n_rules;
|
2008-04-01 19:20:21 +00:00
|
|
|
}
|
2008-04-14 14:39:13 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_blocklistSetURL(tr_session* session, char const* url)
|
2010-10-31 17:16:12 +00:00
|
|
|
{
|
2022-02-07 04:28:36 +00:00
|
|
|
session->setBlocklistUrl(url != nullptr ? url : "");
|
2010-10-31 17:16:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_blocklistGetURL(tr_session const* session)
|
2010-10-31 17:16:12 +00:00
|
|
|
{
|
2021-11-14 01:09:33 +00:00
|
|
|
return session->blocklistUrl().c_str();
|
2010-10-31 17:16:12 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2008-04-14 14:39:13 +00:00
|
|
|
|
2021-12-15 21:25:42 +00:00
|
|
|
void tr_session::setRpcWhitelist(std::string_view whitelist) const
|
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
this->rpc_server_->set_whitelist(whitelist);
|
2021-12-15 21:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_session::useRpcWhitelist(bool enabled) const
|
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
this->rpc_server_->set_whitelist_enabled(enabled);
|
2021-12-15 21:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_session::useRpcWhitelist() const
|
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
return this->rpc_server_->is_whitelist_enabled();
|
2021-12-15 21:25:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
void tr_sessionSetRPCEnabled(tr_session* session, bool is_enabled)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
session->rpc_server_->set_enabled(is_enabled);
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsRPCEnabled(tr_session const* session)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
return session->rpc_server_->is_enabled();
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
void tr_sessionSetRPCPort(tr_session* session, uint16_t hport)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
if (session->rpc_server_)
|
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
session->rpc_server_->set_port(tr_port::from_host(hport));
|
2022-04-21 15:58:13 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
uint16_t tr_sessionGetRPCPort(tr_session const* session)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
return session->rpc_server_ ? session->rpc_server_->port().host() : uint16_t{};
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetRPCCallback(tr_session* session, tr_rpc_func func, void* user_data)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-08-14 15:40:03 +00:00
|
|
|
session->rpc_func_ = func;
|
|
|
|
session->rpc_func_user_data_ = user_data;
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_sessionSetRPCWhitelist(tr_session* session, char const* whitelist)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-02-07 04:28:36 +00:00
|
|
|
session->setRpcWhitelist(whitelist != nullptr ? whitelist : "");
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_sessionGetRPCWhitelist(tr_session const* session)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
return session->rpc_server_->whitelist().c_str();
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-11-14 06:02:45 +00:00
|
|
|
void tr_sessionSetRPCWhitelistEnabled(tr_session* session, bool enabled)
|
2008-10-01 22:59:29 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2021-11-14 06:02:45 +00:00
|
|
|
session->useRpcWhitelist(enabled);
|
2008-10-01 22:59:29 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionGetRPCWhitelistEnabled(tr_session const* session)
|
2008-10-01 22:59:29 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2021-11-14 06:02:45 +00:00
|
|
|
return session->useRpcWhitelist();
|
2008-10-01 22:59:29 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_sessionSetRPCPassword(tr_session* session, char const* password)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
session->rpc_server_->set_password(password != nullptr ? password : "");
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_sessionGetRPCPassword(tr_session const* session)
|
2009-03-25 00:36:11 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-25 00:36:11 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
return session->rpc_server_->get_salted_password().c_str();
|
2009-03-25 00:36:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_sessionSetRPCUsername(tr_session* session, char const* username)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
session->rpc_server_->set_username(username != nullptr ? username : "");
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_sessionGetRPCUsername(tr_session const* session)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
return session->rpc_server_->username().c_str();
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
void tr_sessionSetRPCPasswordEnabled(tr_session* session, bool enabled)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
session->rpc_server_->set_password_enabled(enabled);
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsRPCPasswordEnabled(tr_session const* session)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
return session->rpc_server_->is_password_enabled();
|
2009-04-15 21:05:58 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2010-05-08 22:42:28 +00:00
|
|
|
|
2021-10-13 03:46:12 +00:00
|
|
|
void tr_sessionSetScriptEnabled(tr_session* session, TrScript type, bool enabled)
|
2021-10-12 21:32:32 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2021-10-13 03:46:12 +00:00
|
|
|
TR_ASSERT(type < TR_SCRIPT_N_TYPES);
|
2021-10-12 21:32:32 +00:00
|
|
|
|
2021-11-13 23:33:18 +00:00
|
|
|
session->useScript(type, enabled);
|
2021-10-12 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 03:46:12 +00:00
|
|
|
bool tr_sessionIsScriptEnabled(tr_session const* session, TrScript type)
|
2021-10-12 21:32:32 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2021-10-13 03:46:12 +00:00
|
|
|
TR_ASSERT(type < TR_SCRIPT_N_TYPES);
|
2021-10-12 21:32:32 +00:00
|
|
|
|
2021-11-13 23:33:18 +00:00
|
|
|
return session->useScript(type);
|
2021-10-12 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 03:46:12 +00:00
|
|
|
void tr_sessionSetScript(tr_session* session, TrScript type, char const* script)
|
2021-10-12 21:32:32 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2021-10-13 03:46:12 +00:00
|
|
|
TR_ASSERT(type < TR_SCRIPT_N_TYPES);
|
2021-10-12 21:32:32 +00:00
|
|
|
|
2022-02-07 04:28:36 +00:00
|
|
|
session->setScript(type, script != nullptr ? script : "");
|
2021-10-12 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 03:46:12 +00:00
|
|
|
char const* tr_sessionGetScript(tr_session const* session, TrScript type)
|
2021-10-12 21:32:32 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2021-10-13 03:46:12 +00:00
|
|
|
TR_ASSERT(type < TR_SCRIPT_N_TYPES);
|
2021-10-12 21:32:32 +00:00
|
|
|
|
2021-11-13 23:33:18 +00:00
|
|
|
return session->script(type).c_str();
|
2010-05-08 22:42:28 +00:00
|
|
|
}
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
void tr_sessionSetQueueSize(tr_session* session, tr_direction dir, size_t max_simultaneous_torrents)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (dir == TR_DOWN)
|
|
|
|
{
|
|
|
|
session->settings_.download_queue_size = max_simultaneous_torrents;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
session->settings_.seed_queue_size = max_simultaneous_torrents;
|
|
|
|
}
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
size_t tr_sessionGetQueueSize(tr_session const* session, tr_direction dir)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2022-08-13 19:56:10 +00:00
|
|
|
return session->queueSize(dir);
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
void tr_sessionSetQueueEnabled(tr_session* session, tr_direction dir, bool do_limit_simultaneous_torrents)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (dir == TR_DOWN)
|
|
|
|
{
|
|
|
|
session->settings_.download_queue_enabled = do_limit_simultaneous_torrents;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
session->settings_.seed_queue_enabled = do_limit_simultaneous_torrents;
|
|
|
|
}
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionGetQueueEnabled(tr_session const* session, tr_direction dir)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2022-08-13 19:56:10 +00:00
|
|
|
return session->queueEnabled(dir);
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetQueueStalledMinutes(tr_session* session, int minutes)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(minutes > 0);
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.queue_stalled_minutes = minutes;
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetQueueStalledEnabled(tr_session* session, bool is_enabled)
|
2011-08-07 19:24:33 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2011-08-07 19:24:33 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.queue_stalled_enabled = is_enabled;
|
2011-08-07 19:24:33 +00:00
|
|
|
}
|
2012-02-03 16:44:07 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionGetQueueStalledEnabled(tr_session const* session)
|
2011-08-07 19:24:33 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2011-08-07 19:24:33 +00:00
|
|
|
|
2022-08-13 19:56:10 +00:00
|
|
|
return session->queueStalledEnabled();
|
2011-08-07 19:24:33 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 16:19:45 +00:00
|
|
|
size_t tr_sessionGetQueueStalledMinutes(tr_session const* session)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2022-08-13 19:56:10 +00:00
|
|
|
return session->queueStalledMinutes();
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
// ---
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetAntiBruteForceThreshold(tr_session* session, int max_bad_requests)
|
2020-11-14 18:43:42 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2022-08-03 06:15:37 +00:00
|
|
|
TR_ASSERT(max_bad_requests > 0);
|
2022-04-22 14:19:20 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
session->rpc_server_->set_anti_brute_force_limit(max_bad_requests);
|
2020-11-14 18:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
int tr_sessionGetAntiBruteForceThreshold(tr_session const* session)
|
2020-11-14 18:43:42 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2020-11-14 18:43:42 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
return session->rpc_server_->get_anti_brute_force_limit();
|
2020-11-14 18:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
void tr_sessionSetAntiBruteForceEnabled(tr_session* session, bool is_enabled)
|
2020-11-14 18:43:42 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2020-11-14 18:43:42 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
session->rpc_server_->set_anti_brute_force_enabled(is_enabled);
|
2020-11-14 18:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
bool tr_sessionGetAntiBruteForceEnabled(tr_session const* session)
|
2020-11-14 18:43:42 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2020-11-14 18:43:42 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
return session->rpc_server_->is_anti_brute_force_enabled();
|
2020-11-14 18:43:42 +00:00
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
// ---
|
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
std::vector<tr_torrent*> tr_session::getNextQueuedTorrents(tr_direction dir, size_t num_wanted) const
|
2012-12-28 20:10:03 +00:00
|
|
|
{
|
2022-08-14 19:41:57 +00:00
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2012-12-28 20:10:03 +00:00
|
|
|
|
2021-09-15 14:32:07 +00:00
|
|
|
// build an array of the candidates
|
|
|
|
auto candidates = std::vector<tr_torrent*>{};
|
2022-08-14 19:41:57 +00:00
|
|
|
candidates.reserve(std::size(torrents()));
|
|
|
|
for (auto* const tor : torrents())
|
2011-08-02 03:59:54 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
if (tor->is_queued() && (dir == tor->queue_direction()))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 14:32:07 +00:00
|
|
|
candidates.push_back(tor);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 14:32:07 +00:00
|
|
|
// find the best n candidates
|
2021-10-16 14:04:19 +00:00
|
|
|
num_wanted = std::min(num_wanted, std::size(candidates));
|
2021-09-15 14:32:07 +00:00
|
|
|
if (num_wanted < candidates.size())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 14:32:07 +00:00
|
|
|
std::partial_sort(
|
|
|
|
std::begin(candidates),
|
|
|
|
std::begin(candidates) + num_wanted,
|
|
|
|
std::end(candidates),
|
|
|
|
[](auto const* a, auto const* b) { return tr_torrentGetQueuePosition(a) < tr_torrentGetQueuePosition(b); });
|
|
|
|
candidates.resize(num_wanted);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-28 20:10:03 +00:00
|
|
|
|
2021-09-29 22:21:04 +00:00
|
|
|
return candidates;
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
size_t tr_session::countQueueFreeSlots(tr_direction dir) const noexcept
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2022-08-14 19:41:57 +00:00
|
|
|
if (!queueEnabled(dir))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-14 19:41:57 +00:00
|
|
|
return std::numeric_limits<size_t>::max();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
auto const max = queueSize(dir);
|
|
|
|
auto const activity = dir == TR_UP ? TR_STATUS_SEED : TR_STATUS_DOWNLOAD;
|
|
|
|
|
2021-09-10 20:15:35 +00:00
|
|
|
/* count how many torrents are active */
|
2022-10-26 16:20:10 +00:00
|
|
|
auto active_count = size_t{};
|
2022-08-14 19:41:57 +00:00
|
|
|
bool const stalled_enabled = queueStalledEnabled();
|
2022-11-21 16:19:45 +00:00
|
|
|
auto const stalled_if_idle_for_n_seconds = queueStalledMinutes() * 60;
|
2021-09-10 20:15:35 +00:00
|
|
|
time_t const now = tr_time();
|
2022-08-14 19:41:57 +00:00
|
|
|
for (auto const* const tor : torrents())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-10 20:15:35 +00:00
|
|
|
/* is it the right activity? */
|
2022-11-22 20:00:09 +00:00
|
|
|
if (activity != tor->activity())
|
2021-09-15 14:32:07 +00:00
|
|
|
{
|
2021-09-10 20:15:35 +00:00
|
|
|
continue;
|
2021-09-15 14:32:07 +00:00
|
|
|
}
|
2021-09-10 20:15:35 +00:00
|
|
|
|
|
|
|
/* is it stalled? */
|
2022-11-23 19:47:04 +00:00
|
|
|
if (stalled_enabled && difftime(now, std::max(tor->startDate, tor->activityDate)) >= stalled_if_idle_for_n_seconds)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-23 19:47:04 +00:00
|
|
|
continue;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2021-09-10 20:15:35 +00:00
|
|
|
++active_count;
|
|
|
|
|
|
|
|
/* if we've reached the limit, no need to keep counting */
|
|
|
|
if (active_count >= max)
|
2021-09-15 14:32:07 +00:00
|
|
|
{
|
2021-09-10 20:15:35 +00:00
|
|
|
return 0;
|
2021-09-15 14:32:07 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-08-01 22:24:24 +00:00
|
|
|
|
|
|
|
return max - active_count;
|
|
|
|
}
|
2022-03-18 13:11:59 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-04-29 02:35:47 +00:00
|
|
|
|
|
|
|
void tr_session::closeTorrentFiles(tr_torrent* tor) noexcept
|
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
this->cache->flush_torrent(tor);
|
|
|
|
openFiles().close_torrent(tor->id());
|
2022-04-29 02:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_session::closeTorrentFile(tr_torrent* tor, tr_file_index_t file_num) noexcept
|
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
this->cache->flush_file(tor, file_num);
|
|
|
|
openFiles().close_file(tor->id(), file_num);
|
2022-04-29 02:35:47 +00:00
|
|
|
}
|
2022-07-23 06:04:34 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-07-23 06:04:34 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetQueueStartCallback(tr_session* session, void (*callback)(tr_session*, tr_torrent*, void*), void* user_data)
|
2022-07-23 06:04:34 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
session->setQueueStartCallback(callback, user_data);
|
2022-07-23 06:04:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetRatioLimitHitCallback(tr_session* session, tr_session_ratio_limit_hit_func callback, void* user_data)
|
2022-07-23 06:04:34 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
session->setRatioLimitHitCallback(callback, user_data);
|
2022-07-23 06:04:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetIdleLimitHitCallback(tr_session* session, tr_session_idle_limit_hit_func callback, void* user_data)
|
2022-07-23 06:04:34 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
session->setIdleLimitHitCallback(callback, user_data);
|
2022-07-23 06:04:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetMetadataCallback(tr_session* session, tr_session_metadata_func callback, void* user_data)
|
2022-07-23 06:04:34 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
session->setMetadataCallback(callback, user_data);
|
2022-07-23 06:04:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetCompletenessCallback(tr_session* session, tr_torrent_completeness_func callback, void* user_data)
|
2022-07-23 06:04:34 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
session->setTorrentCompletenessCallback(callback, user_data);
|
2022-07-23 06:04:34 +00:00
|
|
|
}
|
2022-07-23 07:13:18 +00:00
|
|
|
|
2022-07-28 23:56:40 +00:00
|
|
|
tr_session_stats tr_sessionGetStats(tr_session const* session)
|
|
|
|
{
|
|
|
|
return session->stats().current();
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_session_stats tr_sessionGetCumulativeStats(tr_session const* session)
|
|
|
|
{
|
|
|
|
return session->stats().cumulative();
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_sessionClearStats(tr_session* session)
|
|
|
|
{
|
|
|
|
session->stats().clear();
|
|
|
|
}
|
2022-07-23 07:13:18 +00:00
|
|
|
|
2022-07-28 23:56:40 +00:00
|
|
|
namespace
|
2022-07-23 07:13:18 +00:00
|
|
|
{
|
2023-01-07 22:55:00 +00:00
|
|
|
auto constexpr SaveIntervalSecs = 360s;
|
2022-07-23 07:13:18 +00:00
|
|
|
|
2022-07-28 23:56:40 +00:00
|
|
|
auto makeResumeDir(std::string_view config_dir)
|
|
|
|
{
|
2022-07-23 07:13:18 +00:00
|
|
|
#if defined(__APPLE__) || defined(_WIN32)
|
2022-07-28 23:56:40 +00:00
|
|
|
auto dir = fmt::format("{:s}/Resume"sv, config_dir);
|
2022-07-23 07:13:18 +00:00
|
|
|
#else
|
2022-07-28 23:56:40 +00:00
|
|
|
auto dir = fmt::format("{:s}/resume"sv, config_dir);
|
2022-07-23 07:13:18 +00:00
|
|
|
#endif
|
2022-07-28 23:56:40 +00:00
|
|
|
tr_sys_dir_create(dir.c_str(), TR_SYS_DIR_CREATE_PARENTS, 0777);
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto makeTorrentDir(std::string_view config_dir)
|
|
|
|
{
|
|
|
|
#if defined(__APPLE__) || defined(_WIN32)
|
|
|
|
auto dir = fmt::format("{:s}/Torrents"sv, config_dir);
|
|
|
|
#else
|
|
|
|
auto dir = fmt::format("{:s}/torrents"sv, config_dir);
|
|
|
|
#endif
|
|
|
|
tr_sys_dir_create(dir.c_str(), TR_SYS_DIR_CREATE_PARENTS, 0777);
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
auto makeBlocklistDir(std::string_view config_dir)
|
|
|
|
{
|
|
|
|
auto dir = fmt::format("{:s}/blocklists"sv, config_dir);
|
|
|
|
tr_sys_dir_create(dir.c_str(), TR_SYS_DIR_CREATE_PARENTS, 0777);
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
2022-07-28 23:56:40 +00:00
|
|
|
} // namespace
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_session::tr_session(std::string_view config_dir, tr_variant* settings_dict)
|
2022-10-13 21:25:54 +00:00
|
|
|
: config_dir_{ config_dir }
|
|
|
|
, resume_dir_{ makeResumeDir(config_dir) }
|
|
|
|
, torrent_dir_{ makeTorrentDir(config_dir) }
|
2022-11-03 20:46:27 +00:00
|
|
|
, blocklist_dir_{ makeBlocklistDir(config_dir) }
|
2022-11-04 21:20:27 +00:00
|
|
|
, session_thread_{ tr_session_thread::create() }
|
2023-05-06 04:11:05 +00:00
|
|
|
, timer_maker_{ std::make_unique<libtransmission::EvTimerMaker>(event_base()) }
|
2022-11-02 00:32:26 +00:00
|
|
|
, settings_{ settings_dict }
|
2022-10-13 21:25:54 +00:00
|
|
|
, session_id_{ tr_time }
|
2022-11-24 16:17:02 +00:00
|
|
|
, peer_mgr_{ tr_peerMgrNew(this), &tr_peerMgrFree }
|
2022-11-02 00:32:26 +00:00
|
|
|
, rpc_server_{ std::make_unique<tr_rpc_server>(this, settings_dict) }
|
2022-07-28 23:56:40 +00:00
|
|
|
{
|
2022-08-12 17:22:23 +00:00
|
|
|
now_timer_ = timerMaker().create([this]() { onNowTimer(); });
|
2023-05-06 04:11:05 +00:00
|
|
|
now_timer_->start_repeating(1s);
|
2022-08-12 17:22:23 +00:00
|
|
|
|
|
|
|
// Periodically save the .resume files of any torrents whose
|
|
|
|
// status has recently changed. This prevents loss of metadata
|
|
|
|
// in the case of a crash, unclean shutdown, clumsy user, etc.
|
|
|
|
save_timer_ = timerMaker().create(
|
|
|
|
[this]()
|
|
|
|
{
|
|
|
|
for (auto* const tor : torrents())
|
|
|
|
{
|
|
|
|
tr_torrentSave(tor);
|
|
|
|
}
|
|
|
|
|
2023-05-05 20:33:23 +00:00
|
|
|
stats().save();
|
2022-08-12 17:22:23 +00:00
|
|
|
});
|
2023-05-06 04:11:05 +00:00
|
|
|
save_timer_->start_repeating(SaveIntervalSecs);
|
2022-09-06 04:43:59 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
verifier_->add_callback(tr_torrentOnVerifyDone);
|
2022-07-23 07:13:18 +00:00
|
|
|
}
|
2022-10-14 02:25:02 +00:00
|
|
|
|
2022-12-06 16:28:28 +00:00
|
|
|
void tr_session::addIncoming(tr_peer_socket&& socket)
|
2022-10-14 02:25:02 +00:00
|
|
|
{
|
2022-12-06 16:28:28 +00:00
|
|
|
tr_peerMgrAddIncoming(peer_mgr_.get(), std::move(socket));
|
2022-10-14 02:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_session::addTorrent(tr_torrent* tor)
|
|
|
|
{
|
|
|
|
tor->unique_id_ = torrents().add(tor);
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_peerMgrAddTorrent(peer_mgr_.get(), tor);
|
2022-10-14 02:25:02 +00:00
|
|
|
}
|