Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
ForwardingProxy.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "rpc/Errors.hpp"
23#include "rpc/RPCHelpers.hpp"
24#include "rpc/common/Types.hpp"
25#include "util/log/Logger.hpp"
26#include "web/Context.hpp"
27
28#include <xrpl/protocol/ErrorCodes.h>
29
30#include <functional>
31#include <memory>
32#include <string>
33#include <unordered_set>
34
35namespace rpc::impl {
36
37template <typename LoadBalancerType, typename CountersType, typename HandlerProviderType>
39 util::Logger log_{"RPC"};
40
41 std::shared_ptr<LoadBalancerType> balancer_;
42 std::reference_wrapper<CountersType> counters_;
43 std::shared_ptr<HandlerProviderType const> handlerProvider_;
44
45public:
47 std::shared_ptr<LoadBalancerType> const& balancer,
48 CountersType& counters,
49 std::shared_ptr<HandlerProviderType const> const& handlerProvider
50 )
51 : balancer_{balancer}, counters_{std::ref(counters)}, handlerProvider_{handlerProvider}
52 {
53 }
54
55 bool
56 shouldForward(web::Context const& ctx) const
57 {
58 auto const& request = ctx.params;
59
60 if (ctx.method == "subscribe" || ctx.method == "unsubscribe")
61 return false;
62
63 if (handlerProvider_->isClioOnly(ctx.method))
64 return false;
65
66 if (isProxied(ctx.method))
67 return true;
68
70 return true;
71
72 if (isForcedForward(ctx))
73 return true;
74
75 auto const checkAccountInfoForward = [&]() {
76 return ctx.method == "account_info" and request.contains("queue") and request.at("queue").is_bool() and
77 request.at("queue").as_bool();
78 };
79
80 auto const checkLedgerForward = [&]() {
81 return ctx.method == "ledger" and request.contains("queue") and request.at("queue").is_bool() and
82 request.at("queue").as_bool();
83 };
84
85 return static_cast<bool>(checkAccountInfoForward() or checkLedgerForward());
86 }
87
88 Result
89 forward(web::Context const& ctx)
90 {
91 auto toForward = ctx.params;
92 toForward["command"] = ctx.method;
93
94 auto res = balancer_->forwardToRippled(toForward, ctx.clientIp, ctx.isAdmin, ctx.yield);
95 if (not res) {
96 notifyFailedToForward(ctx.method);
97 return Result{Status{CombinedError{res.error()}}};
98 }
99
100 notifyForwarded(ctx.method);
101 return Result{std::move(res).value()};
102 }
103
104 bool
105 isProxied(std::string const& method) const
106 {
107 static std::unordered_set<std::string> const kPROXIED_COMMANDS{
108 "server_definitions",
109 "server_state",
110 "submit",
111 "submit_multisigned",
112 "fee",
113 "ledger_closed",
114 "ledger_current",
115 "ripple_path_find",
116 "manifest",
117 "channel_authorize",
118 "channel_verify",
119 "simulate",
120 };
121
122 return kPROXIED_COMMANDS.contains(method);
123 }
124
125private:
126 void
127 notifyForwarded(std::string const& method)
128 {
129 if (validHandler(method))
130 counters_.get().rpcForwarded(method);
131 }
132
133 void
134 notifyFailedToForward(std::string const& method)
135 {
136 if (validHandler(method))
137 counters_.get().rpcFailedToForward(method);
138 }
139
140 bool
141 validHandler(std::string const& method) const
142 {
143 return handlerProvider_->contains(method) || isProxied(method);
144 }
145
146 bool
147 isForcedForward(web::Context const& ctx) const
148 {
149 static constexpr auto kFORCE_FORWARD = "force_forward";
150 return ctx.isAdmin and ctx.params.contains(kFORCE_FORWARD) and ctx.params.at(kFORCE_FORWARD).is_bool() and
151 ctx.params.at(kFORCE_FORWARD).as_bool();
152 }
153};
154
155} // namespace rpc::impl
Definition ForwardingProxy.hpp:38
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:110
std::variant< RippledError, ClioError > CombinedError
Clio operates on a combination of Rippled and Custom Clio error codes.
Definition Errors.hpp:79
bool specifiesCurrentOrClosedLedger(boost::json::object const &request)
Check whethe the request specifies the current or closed ledger.
Definition RPCHelpers.cpp:1332
Result type used to return responses or error statuses to the Webserver subsystem.
Definition Types.hpp:129
A status returned from any RPC handler.
Definition Errors.hpp:82
Context that is used by the Webserver to pass around information about an incoming request.
Definition Context.hpp:40