xrpld
Loading...
Searching...
No Matches
WSInfoSub.h
1#pragma once
2
3#include <xrpld/rpc/Role.h>
4
5#include <xrpl/beast/net/IPAddressConversion.h>
6#include <xrpl/json/json_writer.h>
7#include <xrpl/server/InfoSub.h>
8#include <xrpl/server/WSSession.h>
9
10#include <cstddef>
11#include <memory>
12#include <string>
13#include <string_view>
14#include <utility>
15
16namespace xrpl {
17
18class WSInfoSub : public InfoSub
19{
23
24public:
25 WSInfoSub(Source& source, std::shared_ptr<WSSession> const& ws) : InfoSub(source), ws_(ws)
26 {
27 auto const& h = ws->request();
28 if (ipAllowed(
29 beast::IPAddressConversion::fromAsio(ws->remoteEndpoint()).address(),
30 ws->port().secureGatewayNetsV4,
31 ws->port().secureGatewayNetsV6))
32 {
33 auto it = h.find("X-User");
34 if (it != h.end())
35 user_ = it->value();
36 fwdfor_ = std::string(::xrpl::forwardedFor(h));
37 }
38 }
39
40 [[nodiscard]] std::string_view
41 user() const
42 {
43 return user_;
44 }
45
46 [[nodiscard]] std::string_view
48 {
49 return fwdfor_;
50 }
51
52 void
53 send(json::Value const& jv, bool) override
54 {
55 auto sp = ws_.lock();
56 if (!sp)
57 return;
58 boost::beast::multi_buffer sb;
59 json::stream(jv, [&](void const* data, std::size_t n) {
60 sb.commit(boost::asio::buffer_copy(sb.prepare(n), boost::asio::buffer(data, n)));
61 });
62 auto m = std::make_shared<StreambufWSMsg<decltype(sb)>>(std::move(sb));
63 sp->send(m);
64 }
65};
66
67} // namespace xrpl
Represents a JSON value.
Definition json_value.h:117
Abstracts the source of subscription data.
Definition InfoSub.h:106
InfoSub(Source &source)
std::string fwdfor_
Definition WSInfoSub.h:22
WSInfoSub(Source &source, std::shared_ptr< WSSession > const &ws)
Definition WSInfoSub.h:25
std::weak_ptr< WSSession > ws_
Definition WSInfoSub.h:20
std::string_view user() const
Definition WSInfoSub.h:41
std::string user_
Definition WSInfoSub.h:21
std::string_view forwardedFor() const
Definition WSInfoSub.h:47
void send(json::Value const &jv, bool) override
Definition WSInfoSub.h:53
T make_shared(T... args)
void stream(json::Value const &jv, Write const &write)
Stream compact JSON to the specified function.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool ipAllowed(beast::ip::Address const &remoteIp, std::vector< boost::asio::ip::network_v4 > const &nets4, std::vector< boost::asio::ip::network_v6 > const &nets6)
True if remoteIp is in any of adminIp.
Definition Role.cpp:42
static ip::Endpoint fromAsio(boost::asio::ip::address const &address)