Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
WhitelistHandler.hpp
1#pragma once
2
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"
8
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>
15
16#include <algorithm>
17#include <optional>
18#include <string>
19#include <string_view>
20#include <unordered_set>
21#include <utility>
22#include <vector>
23
24namespace web::dosguard {
25
29class Whitelist {
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_;
33
34public:
41 std::expected<void, std::string>
42 add(std::string_view net);
43
50 [[nodiscard]] bool
51 isWhiteListed(std::string_view ip) const;
52
53private:
54 static bool
55 isInV4Subnet(boost::asio::ip::address const& addr, boost::asio::ip::network_v4 const& subnet);
56
57 static bool
58 isInV6Subnet(boost::asio::ip::address const& addr, boost::asio::ip::network_v6 const& subnet);
59
60 static bool
61 isV4(std::string_view net);
62
63 static bool
64 isV6(std::string_view net);
65
66 static bool
67 isMask(std::string_view net);
68};
69
74 Whitelist whitelist_;
75
76public:
82 explicit WhitelistHandler(Whitelist whitelist);
83
91 template <SomeResolver HostnameResolverType = Resolver>
92 static std::expected<WhitelistHandler, std::string>
93 create(util::config::ClioConfigDefinition const& config, HostnameResolverType&& resolver = {})
94 {
95 std::unordered_set<std::string> const arr =
96 getWhitelist(config, std::forward<HostnameResolverType>(resolver));
97 Whitelist whitelist;
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())
102 errors.emplace();
103 errors->append(std::move(result).error());
104 }
105 }
106 if (errors.has_value()) {
107 return std::unexpected{std::move(errors).value()};
108 }
109 return WhitelistHandler(std::move(whitelist));
110 }
111
118 [[nodiscard]] bool
119 isWhiteListed(std::string_view ip) const override
120 {
121 return whitelist_.isWhiteListed(ip);
122 }
123
124private:
125 template <SomeResolver HostnameResolverType>
126 [[nodiscard]] static std::unordered_set<std::string>
127 getWhitelist(util::config::ClioConfigDefinition const& config, HostnameResolverType&& resolver)
128 {
129 auto const whitelist = config.getArray("dos_guard.whitelist");
130 std::unordered_set<std::string> hostnames{};
131 // resolve hostnames to ips
132 std::unordered_set<std::string> ips;
133
134 for (auto it = whitelist.begin<util::config::ValueView>();
135 it != whitelist.end<util::config::ValueView>();
136 ++it)
137 hostnames.insert((*it).asString());
138
139 for (auto const& hostname : hostnames) {
140 auto resolvedIps = resolver.resolve(hostname);
141 for (auto& ip : resolvedIps) {
142 ips.insert(std::move(ip));
143 }
144 };
145
146 return ips;
147 }
148};
149
150} // namespace web::dosguard
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