2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-08-08 18:05:39 +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.
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2008-11-24 20:17:36 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2008-11-24 20:17:36 +00:00
|
|
|
#endif
|
|
|
|
|
2023-11-03 17:03:26 +00:00
|
|
|
#include <algorithm> // std::any_of
|
|
|
|
#include <cstddef> // size_t
|
2023-10-31 23:20:01 +00:00
|
|
|
#include <numeric>
|
2022-11-03 20:46:27 +00:00
|
|
|
#include <optional>
|
2022-05-15 16:32:22 +00:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2022-11-03 20:46:27 +00:00
|
|
|
#include <utility> // for std::pair
|
2022-05-15 16:32:22 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2023-10-31 23:20:01 +00:00
|
|
|
#include "libtransmission/tr-macros.h" // for TR_CONSTEXPR20
|
|
|
|
#include "libtransmission/net.h" // for tr_address
|
|
|
|
#include "libtransmission/observable.h"
|
2022-05-15 16:32:22 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
namespace libtransmission
|
|
|
|
{
|
|
|
|
|
2023-10-31 23:20:01 +00:00
|
|
|
class Blocklists
|
2022-05-15 16:32:22 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-10-31 23:20:01 +00:00
|
|
|
Blocklists() = default;
|
2022-05-15 16:32:22 +00:00
|
|
|
|
2023-10-31 23:20:01 +00:00
|
|
|
[[nodiscard]] bool contains(tr_address const& addr) const noexcept
|
2022-05-15 16:32:22 +00:00
|
|
|
{
|
2023-10-31 23:20:01 +00:00
|
|
|
return std::any_of(
|
|
|
|
std::begin(blocklists_),
|
|
|
|
std::end(blocklists_),
|
|
|
|
[&addr](auto const& blocklist) { return blocklist.enabled() && blocklist.contains(addr); });
|
2022-05-15 16:32:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 23:20:01 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto num_lists() const noexcept
|
2022-05-15 16:32:22 +00:00
|
|
|
{
|
2023-10-31 23:20:01 +00:00
|
|
|
return std::size(blocklists_);
|
2022-05-15 16:32:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 23:20:01 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto num_rules() const noexcept
|
2022-05-15 16:32:22 +00:00
|
|
|
{
|
2023-10-31 23:20:01 +00:00
|
|
|
return std::accumulate(
|
|
|
|
std::begin(blocklists_),
|
|
|
|
std::end(blocklists_),
|
|
|
|
size_t{},
|
|
|
|
[](int sum, auto& cur) { return sum + std::size(cur); });
|
2022-05-15 16:32:22 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2023-10-31 23:20:01 +00:00
|
|
|
void load(std::string_view folder, bool is_enabled);
|
|
|
|
void set_enabled(bool is_enabled);
|
|
|
|
size_t update_primary_blocklist(std::string_view external_file, bool is_enabled);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2023-10-31 23:20:01 +00:00
|
|
|
template<typename Observer>
|
|
|
|
[[nodiscard]] auto observe_changes(Observer observer)
|
2022-11-03 20:46:27 +00:00
|
|
|
{
|
2023-10-31 23:20:01 +00:00
|
|
|
return changed_.observe(std::move(observer));
|
2022-11-03 20:46:27 +00:00
|
|
|
}
|
2022-09-30 23:29:26 +00:00
|
|
|
|
2022-05-15 16:32:22 +00:00
|
|
|
private:
|
2023-10-31 23:20:01 +00:00
|
|
|
class Blocklist
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static std::optional<Blocklist> saveNew(std::string_view external_file, std::string_view bin_file, bool is_enabled);
|
2022-05-15 16:32:22 +00:00
|
|
|
|
2023-10-31 23:20:01 +00:00
|
|
|
Blocklist() = default;
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2023-10-31 23:20:01 +00:00
|
|
|
Blocklist(std::string_view bin_file, bool is_enabled)
|
|
|
|
: bin_file_{ bin_file }
|
|
|
|
, is_enabled_{ is_enabled }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool contains(tr_address const& addr) const;
|
|
|
|
|
|
|
|
[[nodiscard]] auto size() const
|
|
|
|
{
|
|
|
|
ensureLoaded();
|
|
|
|
|
|
|
|
return std::size(rules_);
|
|
|
|
}
|
2022-11-03 20:46:27 +00:00
|
|
|
|
2023-10-31 23:20:01 +00:00
|
|
|
[[nodiscard]] constexpr bool enabled() const noexcept
|
|
|
|
{
|
|
|
|
return is_enabled_;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void setEnabled(bool is_enabled) noexcept
|
|
|
|
{
|
|
|
|
is_enabled_ = is_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto const& binFile() const noexcept
|
|
|
|
{
|
|
|
|
return bin_file_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void ensureLoaded() const;
|
|
|
|
|
|
|
|
mutable std::vector<std::pair<tr_address, tr_address>> rules_;
|
|
|
|
|
|
|
|
std::string bin_file_;
|
|
|
|
bool is_enabled_ = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<Blocklist> blocklists_;
|
|
|
|
|
|
|
|
std::string folder_;
|
|
|
|
|
|
|
|
libtransmission::SimpleObservable<> changed_;
|
|
|
|
|
|
|
|
[[nodiscard]] static std::vector<Blocklist> load_folder(std::string_view folder, bool is_enabled);
|
|
|
|
};
|
2022-11-03 20:46:27 +00:00
|
|
|
} // namespace libtransmission
|