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
92 template <SomeResolver HostnameResolverType = Resolver>
93 static std::expected<WhitelistHandler, std::string>
94 create(util::config::ClioConfigDefinition const& config, HostnameResolverType&& resolver = {})
95 {
96 std::unordered_set<std::string> const arr =
97 getWhitelist(config, std::forward<HostnameResolverType>(resolver));
98 Whitelist whitelist;
99 std::optional<std::string> errors;
100 for (auto const& net : arr) {
101 if (auto result = whitelist.add(net); !result.has_value()) {
102 if (!errors.has_value())
103 errors.emplace();
104 errors->append(std::move(result).error());
105 }
106 }
107 if (errors.has_value()) {
108 return std::unexpected{std::move(errors).value()};
109 }
110 return WhitelistHandler(std::move(whitelist));
111 }
112
119 [[nodiscard]] bool
120 isWhiteListed(std::string_view ip) const override
121 {
122 return whitelist_.isWhiteListed(ip);
123 }
124
125private:
126 template <SomeResolver HostnameResolverType>
127 [[nodiscard]] static std::unordered_set<std::string>
128 getWhitelist(util::config::ClioConfigDefinition const& config, HostnameResolverType&& resolver)
129 {
130 auto const whitelist = config.getArray("dos_guard.whitelist");
131 std::unordered_set<std::string> hostnames{};
132 // resolve hostnames to ips
133 std::unordered_set<std::string> ips;
134
135 for (auto const& hostname : whitelist.values())
136 hostnames.insert(hostname.asString());
137
138 for (auto const& hostname : hostnames) {
139 auto resolvedIps = resolver.resolve(hostname);
140 for (auto& ip : resolvedIps) {
141 ips.insert(std::move(ip));
142 }
143 };
144
145 return ips;
146 }
147};
148
149} // 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
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:94
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:120
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
Denial of service protection: rate limiting, IP whitelisting and request weighting.
Definition DOSGuard.cpp:22