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_map>
21#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_;
42 add(std::string_view net);
56 isInV4Subnet(boost::asio::ip::address
const& addr, boost::asio::ip::network_v4
const& subnet);
59 isInV6Subnet(boost::asio::ip::address
const& addr, boost::asio::ip::network_v6
const& subnet);
62 isV4(std::string_view net);
65 isV6(std::string_view net);
68 isMask(std::string_view net);
84 template <SomeResolver HostnameResolverType = Resolver>
87 HostnameResolverType&& resolver = {}
90 std::unordered_set<std::string>
const arr =
91 getWhitelist(config, std::forward<HostnameResolverType>(resolver));
92 for (
auto const& net : arr)
105 return whitelist_.isWhiteListed(ip);
109 template <SomeResolver HostnameResolverType>
110 [[nodiscard]]
static std::unordered_set<std::string>
113 auto const whitelist = config.
getArray(
"dos_guard.whitelist");
114 std::unordered_set<std::string> hostnames{};
116 std::unordered_set<std::string> ips;
121 hostnames.insert((*it).asString());
123 for (
auto const& hostname : hostnames) {
124 auto resolvedIps = resolver.resolve(hostname);
125 for (
auto& ip : resolvedIps) {
126 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
WhitelistHandler(util::config::ClioConfigDefinition const &config, HostnameResolverType &&resolver={})
Adds all whitelisted IPs and masks from the given config.
Definition WhitelistHandler.hpp:85
bool isWhiteListed(std::string_view ip) const override
Checks to see if the given IP is whitelisted.
Definition WhitelistHandler.hpp:103
A whitelist to remove rate limits of certain IP addresses.
Definition WhitelistHandler.hpp:29
bool isWhiteListed(std::string_view ip) const
Checks to see if ip address is whitelisted.
Definition WhitelistHandler.cpp:40
void add(std::string_view net)
Add network address to whitelist.
Definition WhitelistHandler.cpp:21