22#include "util/config/ArrayView.hpp"
23#include "util/config/ConfigDefinition.hpp"
24#include "util/config/ValueView.hpp"
25#include "web/Resolver.hpp"
26#include "web/dosguard/WhitelistHandlerInterface.hpp"
28#include <boost/asio.hpp>
29#include <boost/asio/ip/address.hpp>
30#include <boost/asio/ip/network_v4.hpp>
31#include <boost/asio/ip/network_v6.hpp>
32#include <boost/iterator/transform_iterator.hpp>
33#include <fmt/format.h>
39#include <unordered_map>
40#include <unordered_set>
43namespace web::dosguard {
49 std::vector<boost::asio::ip::network_v4> subnetsV4_;
50 std::vector<boost::asio::ip::network_v6> subnetsV6_;
51 std::vector<boost::asio::ip::address> ips_;
61 add(std::string_view net);
75 isInV4Subnet(boost::asio::ip::address
const& addr, boost::asio::ip::network_v4
const& subnet);
78 isInV6Subnet(boost::asio::ip::address
const& addr, boost::asio::ip::network_v6
const& subnet);
81 isV4(std::string_view net);
84 isV6(std::string_view net);
87 isMask(std::string_view net);
103 template <SomeResolver HostnameResolverType = Resolver>
106 HostnameResolverType&& resolver = {}
109 std::unordered_set<std::string>
const arr =
110 getWhitelist(config, std::forward<HostnameResolverType>(resolver));
111 for (
auto const& net : arr)
124 return whitelist_.isWhiteListed(ip);
128 template <SomeResolver HostnameResolverType>
129 [[nodiscard]]
static std::unordered_set<std::string>
132 auto const whitelist = config.
getArray(
"dos_guard.whitelist");
133 std::unordered_set<std::string> hostnames{};
135 std::unordered_set<std::string> ips;
140 hostnames.insert((*it).asString());
142 for (
auto const& hostname : hostnames) {
143 auto resolvedIps = resolver.resolve(hostname);
144 for (
auto& ip : resolvedIps) {
145 ips.insert(std::move(ip));
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:50
ArrayView getArray(std::string_view prefix) const
Returns the specified Array object from ClioConfigDefinition.
Definition ConfigDefinition.cpp:85
Provides view into ConfigValues that represents values in Clio Config.
Definition ValueView.hpp:46
Interface for a whitelist handler.
Definition WhitelistHandlerInterface.hpp:29
WhitelistHandler(util::config::ClioConfigDefinition const &config, HostnameResolverType &&resolver={})
Adds all whitelisted IPs and masks from the given config.
Definition WhitelistHandler.hpp:104
bool isWhiteListed(std::string_view ip) const override
Checks to see if the given IP is whitelisted.
Definition WhitelistHandler.hpp:122
A whitelist to remove rate limits of certain IP addresses.
Definition WhitelistHandler.hpp:48
bool isWhiteListed(std::string_view ip) const
Checks to see if ip address is whitelisted.
Definition WhitelistHandler.cpp:59
void add(std::string_view net)
Add network address to whitelist.
Definition WhitelistHandler.cpp:40