2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2008-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2022-05-15 16:32:22 +00:00
|
|
|
#include <algorithm>
|
2022-08-25 01:19:21 +00:00
|
|
|
#include <array>
|
2022-08-27 19:05:21 +00:00
|
|
|
#include <fstream>
|
2022-04-04 18:36:48 +00:00
|
|
|
#include <string_view>
|
2022-07-26 02:45:54 +00:00
|
|
|
#include <vector>
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2008-03-29 18:37:07 +00:00
|
|
|
#include "transmission.h"
|
2022-03-11 21:09:22 +00:00
|
|
|
|
2008-03-30 13:22:45 +00:00
|
|
|
#include "blocklist.h"
|
2014-07-28 04:13:38 +00:00
|
|
|
#include "error.h"
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "file.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2008-12-02 03:41:58 +00:00
|
|
|
#include "net.h"
|
2022-11-03 20:46:27 +00:00
|
|
|
#include "tr-assert.h"
|
2022-09-30 23:29:26 +00:00
|
|
|
#include "tr-strbuf.h"
|
2022-11-03 20:46:27 +00:00
|
|
|
#include "utils.h" // for _(), tr_strerror(), tr_strvEndsWith()
|
2008-04-01 19:20:21 +00:00
|
|
|
|
2022-09-30 23:29:26 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
namespace libtransmission
|
2008-03-29 21:05:51 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
namespace
|
2008-03-29 18:37:07 +00:00
|
|
|
{
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
// A string at the beginning of .bin files to test & make sure we don't load incompatible files
|
|
|
|
auto constexpr BinContentsPrefix = std::string_view{ "-tr-blocklist-file-format-v3-" };
|
|
|
|
|
|
|
|
// In the blocklists directory, the The plaintext source file can be anything, e.g. "level1".
|
|
|
|
// The pre-parsed, fast-to-load binary file will have a ".bin" suffix e.g. "level1.bin".
|
|
|
|
auto constexpr BinFileSuffix = std::string_view{ ".bin" };
|
|
|
|
|
|
|
|
using address_range_t = std::pair<tr_address, tr_address>;
|
|
|
|
|
|
|
|
void save(std::string_view filename, address_range_t const* ranges, size_t n_ranges)
|
|
|
|
{
|
|
|
|
auto out = std::ofstream{ tr_pathbuf{ filename }, std::ios_base::out | std::ios_base::trunc | std::ios_base::binary };
|
|
|
|
if (!out.is_open())
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddWarn(fmt::format(
|
2022-03-16 00:51:36 +00:00
|
|
|
_("Couldn't read '{path}': {error} ({error_code})"),
|
2022-11-03 20:46:27 +00:00
|
|
|
fmt::arg("path", filename),
|
2022-08-27 19:05:21 +00:00
|
|
|
fmt::arg("error", tr_strerror(errno)),
|
|
|
|
fmt::arg("error_code", errno)));
|
2017-04-19 12:04:45 +00:00
|
|
|
return;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
2008-04-05 16:45:35 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
if (!out.write(std::data(BinContentsPrefix), std::size(BinContentsPrefix)) ||
|
|
|
|
!out.write(reinterpret_cast<char const*>(ranges), n_ranges * sizeof(*ranges)))
|
2022-09-30 17:11:52 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
_("Couldn't save '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", filename),
|
|
|
|
fmt::arg("error", tr_strerror(errno)),
|
|
|
|
fmt::arg("error_code", errno)));
|
2022-09-30 17:11:52 +00:00
|
|
|
}
|
|
|
|
else
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
tr_logAddInfo(fmt::format(
|
|
|
|
ngettext("Blocklist '{path}' has {count} entry", "Blocklist '{path}' has {count} entries", n_ranges),
|
|
|
|
fmt::arg("path", tr_sys_path_basename(filename)),
|
|
|
|
fmt::arg("count", n_ranges)));
|
2022-09-30 23:29:26 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
out.close();
|
2022-09-30 23:29:26 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
namespace ParseHelpers
|
2008-03-30 13:22:45 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
// P2P plaintext format: "comment:x.x.x.x-y.y.y.y" / "comment:x:x:x:x:x:x:x:x-x:x:x:x:x:x:x:x"
|
|
|
|
// https://web.archive.org/web/20100328075307/http://wiki.phoenixlabs.org/wiki/P2P_Format
|
|
|
|
// https://en.wikipedia.org/wiki/PeerGuardian#P2P_plaintext_format
|
|
|
|
std::optional<address_range_t> parsePeerGuardianLine(std::string_view line)
|
2010-05-31 11:38:36 +00:00
|
|
|
{
|
2022-04-04 18:36:48 +00:00
|
|
|
// remove leading "comment:"
|
|
|
|
auto pos = line.find(':');
|
|
|
|
if (pos == std::string_view::npos)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-04-04 18:36:48 +00:00
|
|
|
line = line.substr(pos + 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-04 18:36:48 +00:00
|
|
|
// parse the leading 'x.x.x.x'
|
|
|
|
pos = line.find('-');
|
|
|
|
if (pos == std::string_view::npos)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-11-03 20:46:27 +00:00
|
|
|
|
|
|
|
auto addrpair = address_range_t{};
|
2022-12-09 02:27:52 +00:00
|
|
|
if (auto const addr = tr_address::from_string(line.substr(0, pos)); addr)
|
2022-04-04 18:36:48 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
addrpair.first = *addr;
|
2022-04-04 18:36:48 +00:00
|
|
|
}
|
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-11-03 20:46:27 +00:00
|
|
|
|
2022-04-04 18:36:48 +00:00
|
|
|
line = line.substr(pos + 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-04 18:36:48 +00:00
|
|
|
// parse the trailing 'y.y.y.y'
|
2022-12-09 02:27:52 +00:00
|
|
|
if (auto const addr = tr_address::from_string(line); addr)
|
2022-04-04 18:36:48 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
addrpair.second = *addr;
|
2022-04-04 18:36:48 +00:00
|
|
|
}
|
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
return addrpair;
|
2010-05-31 11:38:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
// DAT / eMule format: "000.000.000.000 - 000.255.255.255 , 000 , invalid ip"
|
|
|
|
// https://sourceforge.net/p/peerguardian/wiki/dev-blocklist-format-dat/
|
|
|
|
std::optional<address_range_t> parseEmuleLine(std::string_view line)
|
2010-05-31 11:38:36 +00:00
|
|
|
{
|
2022-04-04 18:36:48 +00:00
|
|
|
static auto constexpr Delim1 = std::string_view{ " - " };
|
|
|
|
static auto constexpr Delim2 = std::string_view{ " , " };
|
|
|
|
|
|
|
|
auto pos = line.find(Delim1);
|
|
|
|
if (pos == std::string_view::npos)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
auto addrpair = address_range_t{};
|
|
|
|
|
2022-12-09 02:27:52 +00:00
|
|
|
if (auto const addr = tr_address::from_string(line.substr(0, pos)); addr)
|
2022-04-04 18:36:48 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
addrpair.first = *addr;
|
2022-04-04 18:36:48 +00:00
|
|
|
}
|
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 18:36:48 +00:00
|
|
|
line = line.substr(pos + std::size(Delim1));
|
|
|
|
pos = line.find(Delim2);
|
|
|
|
if (pos == std::string_view::npos)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 02:27:52 +00:00
|
|
|
if (auto const addr = tr_address::from_string(line.substr(0, pos)); addr)
|
2022-04-04 18:36:48 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
addrpair.second = *addr;
|
2022-04-04 18:36:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
return {};
|
2022-04-04 18:36:48 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
return addrpair;
|
2010-05-31 11:38:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
// CIDR notation: "0.0.0.0/8", "::/64"
|
|
|
|
// https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#CIDR_notation
|
|
|
|
// Example: `10.5.6.7/8` will block the range [10.0.0.0 .. 10.255.255.255]
|
|
|
|
std::optional<address_range_t> parseCidrLine(std::string_view line)
|
2019-02-10 11:05:16 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
auto addrpair = address_range_t{};
|
2018-10-13 21:20:40 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
auto pos = line.find('/');
|
|
|
|
if (pos == std::string_view::npos)
|
2019-02-10 11:05:16 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
return {};
|
2018-10-13 21:20:40 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 02:27:52 +00:00
|
|
|
if (auto const addr = tr_address::from_string(line.substr(0, pos)); addr && addr->is_ipv4())
|
2019-02-10 11:05:16 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
addrpair.first = *addr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return {};
|
2018-10-13 21:20:40 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
auto const pflen = tr_parseNum<size_t>(line.substr(pos + 1));
|
|
|
|
if (!pflen)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2018-10-13 21:20:40 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
auto const mask = uint32_t{ 0xFFFFFFFF } << (32 - *pflen);
|
|
|
|
auto const ip_u = htonl(addrpair.first.addr.addr4.s_addr);
|
|
|
|
addrpair.first.addr.addr4.s_addr = ntohl(ip_u & mask);
|
|
|
|
addrpair.second.addr.addr4.s_addr = ntohl(ip_u | (~mask));
|
|
|
|
return addrpair;
|
2010-05-31 11:38:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
std::optional<address_range_t> parseLine(std::string_view line)
|
2011-02-13 16:14:31 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
for (auto const& line_parser : { parsePeerGuardianLine, parseEmuleLine, parseCidrLine })
|
2022-09-30 17:11:52 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
if (auto range = line_parser(line); range)
|
|
|
|
{
|
|
|
|
return range;
|
|
|
|
}
|
2022-09-30 17:11:52 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
return {};
|
2011-02-13 16:14:31 +00:00
|
|
|
}
|
2022-11-03 20:46:27 +00:00
|
|
|
} // namespace ParseHelpers
|
2011-02-13 16:14:31 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
auto parseFile(std::string_view filename)
|
2008-03-29 18:37:07 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
using namespace ParseHelpers;
|
|
|
|
|
|
|
|
auto ranges = std::vector<address_range_t>{};
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
auto in = std::ifstream{ tr_pathbuf{ filename } };
|
2022-08-27 19:05:21 +00:00
|
|
|
if (!in.is_open())
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddWarn(fmt::format(
|
2022-03-16 00:51:36 +00:00
|
|
|
_("Couldn't read '{path}': {error} ({error_code})"),
|
2022-03-14 04:43:35 +00:00
|
|
|
fmt::arg("path", filename),
|
2022-08-27 19:05:21 +00:00
|
|
|
fmt::arg("error", tr_strerror(errno)),
|
|
|
|
fmt::arg("error_code", errno)));
|
2022-11-03 20:46:27 +00:00
|
|
|
return ranges;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 19:05:21 +00:00
|
|
|
auto line = std::string{};
|
|
|
|
auto line_number = size_t{ 0U };
|
|
|
|
while (std::getline(in, line))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-08-27 19:05:21 +00:00
|
|
|
++line_number;
|
2022-11-03 20:46:27 +00:00
|
|
|
if (auto range = parseLine(line); range && (range->first.type == range->second.type))
|
2010-04-30 22:58:03 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
ranges.push_back(*range);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// don't try to display the actual lines - it causes issues
|
2022-08-27 19:05:21 +00:00
|
|
|
tr_logAddWarn(fmt::format(_("Couldn't parse line: '{line}'"), fmt::arg("line", line_number)));
|
2010-04-30 22:58:03 +00:00
|
|
|
}
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
2022-08-27 19:05:21 +00:00
|
|
|
in.close();
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2022-08-27 19:05:21 +00:00
|
|
|
if (std::empty(ranges))
|
2008-06-11 16:15:45 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
return ranges;
|
|
|
|
}
|
|
|
|
|
|
|
|
// safeguard against some joker swapping the begin & end ranges
|
2022-11-12 15:53:09 +00:00
|
|
|
for (auto& [low, high] : ranges)
|
2022-11-03 20:46:27 +00:00
|
|
|
{
|
2022-11-12 15:53:09 +00:00
|
|
|
if (low > high)
|
2022-11-03 20:46:27 +00:00
|
|
|
{
|
2022-11-12 15:53:09 +00:00
|
|
|
std::swap(low, high);
|
2022-11-03 20:46:27 +00:00
|
|
|
}
|
2022-08-27 19:05:21 +00:00
|
|
|
}
|
2011-02-13 16:14:31 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
// sort ranges by start address
|
|
|
|
std::sort(std::begin(ranges), std::end(ranges), [](auto const& a, auto const& b) { return a.first < b.first; });
|
2022-11-02 14:41:05 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
// merge overlapping ranges
|
|
|
|
auto keep = size_t{ 0U };
|
2022-11-02 14:41:05 +00:00
|
|
|
for (auto const& range : ranges)
|
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
if (ranges[keep].second < range.first)
|
2022-11-02 14:41:05 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
ranges[++keep] = range;
|
2022-11-02 14:41:05 +00:00
|
|
|
}
|
2022-11-03 20:46:27 +00:00
|
|
|
else if (ranges[keep].second < range.second)
|
2022-11-02 14:41:05 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
ranges[keep].second = range.second;
|
2022-11-02 14:41:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
TR_ASSERT_MSG(keep + 1 <= std::size(ranges), "Can shrink `ranges` or leave intact, but not grow");
|
|
|
|
ranges.resize(keep + 1);
|
|
|
|
|
|
|
|
#ifdef TR_ENABLE_ASSERTS
|
2022-11-12 15:53:09 +00:00
|
|
|
for (auto const& [low, high] : ranges)
|
2022-11-03 20:46:27 +00:00
|
|
|
{
|
2022-11-12 15:53:09 +00:00
|
|
|
TR_ASSERT(low <= high);
|
2022-11-03 20:46:27 +00:00
|
|
|
}
|
|
|
|
for (size_t i = 1, n = std::size(ranges); i < n; ++i)
|
|
|
|
{
|
|
|
|
TR_ASSERT(ranges[i - 1].second < ranges[i].first);
|
|
|
|
}
|
|
|
|
#endif
|
2013-01-21 17:39:20 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
return ranges;
|
|
|
|
}
|
2022-11-02 14:41:05 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
auto getFilenamesInDir(std::string_view folder)
|
|
|
|
{
|
|
|
|
auto files = std::vector<std::string>{};
|
2022-08-27 19:05:21 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
if (auto const odir = tr_sys_dir_open(tr_pathbuf{ folder }); odir != TR_BAD_SYS_DIR)
|
2022-08-27 19:05:21 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
char const* name = nullptr;
|
|
|
|
auto const prefix = std::string{ folder } + '/';
|
|
|
|
while ((name = tr_sys_dir_read_name(odir)) != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
if (name[0] == '.') // ignore dotfiles
|
2022-09-30 17:11:52 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
continue;
|
2022-09-30 17:11:52 +00:00
|
|
|
}
|
2022-11-03 20:46:27 +00:00
|
|
|
|
|
|
|
files.emplace_back(prefix + name);
|
2022-08-27 19:05:21 +00:00
|
|
|
}
|
2022-11-03 20:46:27 +00:00
|
|
|
|
|
|
|
tr_sys_dir_close(odir);
|
2022-08-27 19:05:21 +00:00
|
|
|
}
|
2013-01-21 17:39:20 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
return files;
|
|
|
|
}
|
2011-02-13 16:14:31 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
} // namespace
|
2022-08-27 19:05:21 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
void Blocklist::ensureLoaded() const
|
|
|
|
{
|
|
|
|
if (!std::empty(rules_))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the file's size
|
|
|
|
tr_error* error = nullptr;
|
|
|
|
auto const file_info = tr_sys_path_get_info(bin_file_, 0, &error);
|
|
|
|
if (error != nullptr)
|
2022-08-27 19:05:21 +00:00
|
|
|
{
|
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
_("Couldn't read '{path}': {error} ({error_code})"),
|
2022-11-03 20:46:27 +00:00
|
|
|
fmt::arg("path", bin_file_),
|
|
|
|
fmt::arg("error", error->message),
|
|
|
|
fmt::arg("error_code", error->code)));
|
|
|
|
tr_error_clear(&error);
|
2022-11-12 15:53:09 +00:00
|
|
|
}
|
|
|
|
if (!file_info)
|
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
return;
|
2011-02-13 16:14:31 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
// open the file
|
|
|
|
auto in = std::ifstream{ bin_file_, std::ios_base::in | std::ios_base::binary };
|
|
|
|
if (!in)
|
2013-01-21 17:39:20 +00:00
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddWarn(fmt::format(
|
2022-11-03 20:46:27 +00:00
|
|
|
_("Couldn't read '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", bin_file_),
|
2022-08-27 19:05:21 +00:00
|
|
|
fmt::arg("error", tr_strerror(errno)),
|
|
|
|
fmt::arg("error_code", errno)));
|
2022-11-03 20:46:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check to see if the file is usable
|
|
|
|
bool supported_file = true;
|
|
|
|
if (file_info->size < std::size(BinContentsPrefix)) // too small
|
|
|
|
{
|
|
|
|
supported_file = false;
|
|
|
|
}
|
|
|
|
else if (((file_info->size - std::size(BinContentsPrefix)) % sizeof(address_range_t)) != 0) // wrong size
|
|
|
|
{
|
|
|
|
supported_file = false;
|
2013-01-21 17:39:20 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2013-01-21 17:39:20 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
auto tmp = std::array<char, std::size(BinContentsPrefix)>{};
|
|
|
|
in.read(std::data(tmp), std::size(tmp));
|
|
|
|
supported_file = BinContentsPrefix == std::string_view{ std::data(tmp), std::size(tmp) };
|
2008-06-11 16:15:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
if (!supported_file)
|
2022-09-30 17:11:52 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
// bad binary file; try to rebuild it
|
|
|
|
in.close();
|
2022-11-12 15:53:09 +00:00
|
|
|
if (auto const sz_src_file = std::string{ std::data(bin_file_), std::size(bin_file_) - std::size(BinFileSuffix) };
|
|
|
|
tr_sys_path_exists(sz_src_file))
|
2022-11-03 20:46:27 +00:00
|
|
|
{
|
2022-11-06 16:35:48 +00:00
|
|
|
rules_ = parseFile(sz_src_file);
|
|
|
|
if (!std::empty(rules_))
|
|
|
|
{
|
|
|
|
tr_logAddInfo(_("Rewriting old blocklist file format to new format"));
|
|
|
|
tr_sys_path_remove(bin_file_);
|
|
|
|
save(bin_file_, std::data(rules_), std::size(rules_));
|
|
|
|
}
|
2022-11-03 20:46:27 +00:00
|
|
|
}
|
2022-09-30 17:11:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
auto range = address_range_t{};
|
2022-11-12 15:53:09 +00:00
|
|
|
rules_.reserve(file_info->size - std::size(BinContentsPrefix) / sizeof(address_range_t));
|
2022-11-03 20:46:27 +00:00
|
|
|
while (in.read(reinterpret_cast<char*>(&range), sizeof(range)))
|
2022-09-30 17:11:52 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
rules_.emplace_back(range);
|
2022-09-30 17:11:52 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
tr_logAddInfo(fmt::format(
|
|
|
|
ngettext("Blocklist '{path}' has {count} entry", "Blocklist '{path}' has {count} entries", std::size(rules_)),
|
|
|
|
fmt::arg("path", tr_sys_path_basename(bin_file_)),
|
|
|
|
fmt::arg("count", std::size(rules_))));
|
2022-09-30 17:11:52 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
std::vector<Blocklist> Blocklist::loadBlocklists(std::string_view const blocklist_dir, bool const is_enabled)
|
2022-05-15 16:32:22 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
// check for files that need to be updated
|
|
|
|
for (auto const& src_file : getFilenamesInDir(blocklist_dir))
|
2022-05-15 16:32:22 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
if (tr_strvEndsWith(src_file, BinFileSuffix))
|
2022-09-30 17:11:52 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
continue;
|
2022-09-30 17:11:52 +00:00
|
|
|
}
|
2022-11-03 20:46:27 +00:00
|
|
|
|
|
|
|
// ensure this src_file has an up-to-date corresponding bin_file
|
|
|
|
auto const src_info = tr_sys_path_get_info(src_file);
|
|
|
|
auto const bin_file = tr_pathbuf{ src_file, BinFileSuffix };
|
|
|
|
auto const bin_info = tr_sys_path_get_info(bin_file);
|
|
|
|
auto const bin_needs_update = src_info && (!bin_info || bin_info->last_modified_at <= src_info->last_modified_at);
|
|
|
|
if (bin_needs_update)
|
2022-09-30 17:11:52 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
if (auto const ranges = parseFile(src_file); !std::empty(ranges))
|
|
|
|
{
|
|
|
|
save(bin_file, std::data(ranges), std::size(ranges));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ret = std::vector<Blocklist>{};
|
|
|
|
for (auto const& bin_file : getFilenamesInDir(blocklist_dir))
|
|
|
|
{
|
|
|
|
if (tr_strvEndsWith(bin_file, BinFileSuffix))
|
|
|
|
{
|
|
|
|
ret.emplace_back(bin_file, is_enabled);
|
2022-09-30 17:11:52 +00:00
|
|
|
}
|
2022-05-15 16:32:22 +00:00
|
|
|
}
|
2022-11-03 20:46:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Blocklist::contains(tr_address const& addr) const
|
|
|
|
{
|
2022-12-09 02:27:52 +00:00
|
|
|
TR_ASSERT(addr.is_valid());
|
2022-11-03 20:46:27 +00:00
|
|
|
|
|
|
|
if (!is_enabled_)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-15 16:32:22 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
ensureLoaded();
|
2022-09-30 17:11:52 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
struct Compare
|
2022-05-15 16:32:22 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
[[nodiscard]] static auto compare(tr_address const& a, address_range_t const& b) noexcept // <=>
|
|
|
|
{
|
|
|
|
if (a < b.first)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (b.second < a)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] static auto compare(address_range_t const& a, tr_address const& b) noexcept // <=>
|
2022-09-30 17:11:52 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
return -compare(b, a);
|
2022-09-30 17:11:52 +00:00
|
|
|
}
|
2022-11-03 20:46:27 +00:00
|
|
|
|
|
|
|
[[nodiscard]] auto operator()(address_range_t const& a, tr_address const& b) const noexcept // <
|
2022-09-30 17:11:52 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
return compare(a, b) < 0;
|
2022-09-30 17:11:52 +00:00
|
|
|
}
|
2022-11-03 20:46:27 +00:00
|
|
|
|
|
|
|
[[nodiscard]] auto operator()(tr_address const& a, address_range_t const& b) const noexcept // <
|
|
|
|
{
|
|
|
|
return compare(a, b) < 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return std::binary_search(std::begin(rules_), std::end(rules_), addr, Compare{});
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<Blocklist> Blocklist::saveNew(std::string_view external_file, std::string_view bin_file, bool is_enabled)
|
|
|
|
{
|
|
|
|
// if we can't parse the file, do nothing
|
|
|
|
auto rules = parseFile(external_file);
|
|
|
|
if (std::empty(rules))
|
|
|
|
{
|
|
|
|
return {};
|
2022-09-30 17:11:52 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
// make a copy of `external_file` for our own safekeeping
|
|
|
|
auto const src_file = std::string{ std::data(bin_file), std::size(bin_file) - std::size(BinFileSuffix) };
|
|
|
|
tr_sys_path_remove(src_file.c_str());
|
|
|
|
tr_error* error = nullptr;
|
2022-11-12 15:53:09 +00:00
|
|
|
auto const copied = tr_sys_path_copy(tr_pathbuf{ external_file }, src_file.c_str(), &error);
|
|
|
|
if (error != nullptr)
|
|
|
|
{
|
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
_("Couldn't save '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", src_file),
|
|
|
|
fmt::arg("error", error->message),
|
|
|
|
fmt::arg("error_code", error->code)));
|
|
|
|
tr_error_clear(&error);
|
|
|
|
}
|
|
|
|
if (!copied)
|
2022-11-03 20:46:27 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
save(bin_file, std::data(rules), std::size(rules));
|
|
|
|
|
|
|
|
// return a new Blocklist with these rules
|
|
|
|
auto ret = Blocklist{ bin_file, is_enabled };
|
|
|
|
ret.rules_ = std::move(rules);
|
|
|
|
return ret;
|
2022-05-15 16:32:22 +00:00
|
|
|
}
|
2022-11-03 20:46:27 +00:00
|
|
|
|
|
|
|
} // namespace libtransmission
|