3#include "util/config/ArrayView.hpp"
4#include "util/config/ConfigDefinition.hpp"
5#include "util/config/ValueView.hpp"
6#include "web/Resolver.hpp"
7#include "web/dosguard/WhitelistHandlerInterface.hpp"
9#include <boost/asio.hpp>
10#include <boost/asio/ip/address.hpp>
11#include <boost/asio/ip/network_v4.hpp>
12#include <boost/asio/ip/network_v6.hpp>
13#include <boost/iterator/transform_iterator.hpp>
14#include <fmt/format.h>
20#include <unordered_set>
24namespace web::dosguard {
30 std::vector<boost::asio::ip::network_v4> subnetsV4_;
31 std::vector<boost::asio::ip::network_v6> subnetsV6_;
32 std::vector<boost::asio::ip::address> ips_;
41 std::expected<void, std::string>
42 add(std::string_view net);
55 isInV4Subnet(boost::asio::ip::address
const& addr, boost::asio::ip::network_v4
const& subnet);
58 isInV6Subnet(boost::asio::ip::address
const& addr, boost::asio::ip::network_v6
const& subnet);
61 isV4(std::string_view net);
64 isV6(std::string_view net);
67 isMask(std::string_view net);
91 template <SomeResolver HostnameResolverType = Resolver>
92 static std::expected<WhitelistHandler, std::string>
95 std::unordered_set<std::string>
const arr =
96 getWhitelist(config, std::forward<HostnameResolverType>(resolver));
98 std::optional<std::string> errors;
99 for (
auto const& net : arr) {
100 if (
auto result = whitelist.
add(net); !result.has_value()) {
101 if (!errors.has_value())
103 errors->append(std::move(result).error());
106 if (errors.has_value()) {
107 return std::unexpected{std::move(errors).value()};
121 return whitelist_.isWhiteListed(ip);
125 template <SomeResolver HostnameResolverType>
126 [[nodiscard]]
static std::unordered_set<std::string>
129 auto const whitelist = config.
getArray(
"dos_guard.whitelist");
130 std::unordered_set<std::string> hostnames{};
132 std::unordered_set<std::string> ips;
137 hostnames.insert((*it).asString());
139 for (
auto const& hostname : hostnames) {
140 auto resolvedIps = resolver.resolve(hostname);
141 for (
auto& ip : resolvedIps) {
142 ips.insert(std::move(ip));
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:31
ArrayView getArray(std::string_view prefix) const
Returns the specified Array object from ClioConfigDefinition.
Definition ConfigDefinition.cpp:66
Provides view into ConfigValues that represents values in Clio Config.
Definition ValueView.hpp:27
Interface for a whitelist handler.
Definition WhitelistHandlerInterface.hpp:10
static std::expected< WhitelistHandler, std::string > create(util::config::ClioConfigDefinition const &config, HostnameResolverType &&resolver={})
Creates a WhitelistHandler by loading all whitelisted IPs and masks from config.
Definition WhitelistHandler.hpp:93
WhitelistHandler(Whitelist whitelist)
Constructs a WhitelistHandler from an already-built Whitelist.
Definition WhitelistHandler.cpp:19
bool isWhiteListed(std::string_view ip) const override
Checks to see if the given IP is whitelisted.
Definition WhitelistHandler.hpp:119
A whitelist to remove rate limits of certain IP addresses.
Definition WhitelistHandler.hpp:29
std::expected< void, std::string > add(std::string_view net)
Add network address to whitelist.
Definition WhitelistHandler.cpp:24
bool isWhiteListed(std::string_view ip) const
Checks to see if ip address is whitelisted.
Definition WhitelistHandler.cpp:56