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 <memory>
11#include <string>
12
13namespace xrpl {
14
15class WSInfoSub : public InfoSub
16{
20
21public:
22 WSInfoSub(Source& source, std::shared_ptr<WSSession> const& ws) : InfoSub(source), ws_(ws)
23 {
24 auto const& h = ws->request();
25 if (ipAllowed(
26 beast::IPAddressConversion::fromAsio(ws->remoteEndpoint()).address(),
27 ws->port().secureGatewayNetsV4,
28 ws->port().secureGatewayNetsV6))
29 {
30 auto it = h.find("X-User");
31 if (it != h.end())
32 user_ = it->value();
33 fwdfor_ = std::string(::xrpl::forwardedFor(h));
34 }
35 }
36
37 [[nodiscard]] std::string_view
38 user() const
39 {
40 return user_;
41 }
42
43 [[nodiscard]] std::string_view
45 {
46 return fwdfor_;
47 }
48
49 void
50 send(json::Value const& jv, bool) override
51 {
52 auto sp = ws_.lock();
53 if (!sp)
54 return;
55 boost::beast::multi_buffer sb;
56 json::stream(jv, [&](void const* data, std::size_t n) {
57 sb.commit(boost::asio::buffer_copy(sb.prepare(n), boost::asio::buffer(data, n)));
58 });
59 auto m = std::make_shared<StreambufWSMsg<decltype(sb)>>(std::move(sb));
60 sp->send(m);
61 }
62};
63
64} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
Abstracts the source of subscription data.
Definition InfoSub.h:61
InfoSub(Source &source)
Definition InfoSub.cpp:56
std::string fwdfor_
Definition WSInfoSub.h:19
WSInfoSub(Source &source, std::shared_ptr< WSSession > const &ws)
Definition WSInfoSub.h:22
std::weak_ptr< WSSession > ws_
Definition WSInfoSub.h:17
std::string_view user() const
Definition WSInfoSub.h:38
std::string user_
Definition WSInfoSub.h:18
std::string_view forwardedFor() const
Definition WSInfoSub.h:44
void send(json::Value const &jv, bool) override
Definition WSInfoSub.h:50
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)