rippled
Loading...
Searching...
No Matches
WSInfoSub.h
1#ifndef XRPL_RPC_WSINFOSUB_H
2#define XRPL_RPC_WSINFOSUB_H
3
4#include <xrpld/rpc/InfoSub.h>
5#include <xrpld/rpc/Role.h>
6
7#include <xrpl/beast/net/IPAddressConversion.h>
8#include <xrpl/json/json_writer.h>
9#include <xrpl/server/WSSession.h>
10
11#include <memory>
12#include <string>
13
14namespace ripple {
15
16class WSInfoSub : public InfoSub
17{
21
22public:
24 : InfoSub(source), ws_(ws)
25 {
26 auto const& h = ws->request();
27 if (ipAllowed(
28 beast::IPAddressConversion::from_asio(ws->remote_endpoint())
29 .address(),
30 ws->port().secure_gateway_nets_v4,
31 ws->port().secure_gateway_nets_v6))
32 {
33 auto it = h.find("X-User");
34 if (it != h.end())
35 user_ = it->value();
37 }
38 }
39
41 user() const
42 {
43 return user_;
44 }
45
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(
61 sb.prepare(n), boost::asio::buffer(data, n)));
62 });
63 auto m = std::make_shared<StreambufWSMsg<decltype(sb)>>(std::move(sb));
64 sp->send(m);
65 }
66};
67
68} // namespace ripple
69
70#endif
Represents a JSON value.
Definition json_value.h:130
Address const & address() const
Returns the address portion of this endpoint.
Definition IPEndpoint.h:56
Abstracts the source of subscription data.
Definition InfoSub.h:49
Manages a client's subscription to data feeds.
Definition InfoSub.h:33
std::string_view forwarded_for() const
Definition WSInfoSub.h:47
std::string user_
Definition WSInfoSub.h:19
std::weak_ptr< WSSession > ws_
Definition WSInfoSub.h:18
WSInfoSub(Source &source, std::shared_ptr< WSSession > const &ws)
Definition WSInfoSub.h:23
std::string_view user() const
Definition WSInfoSub.h:41
void send(Json::Value const &jv, bool) override
Definition WSInfoSub.h:53
std::string fwdfor_
Definition WSInfoSub.h:20
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:6
std::string_view forwardedFor(http_request_type const &request)
Definition Role.cpp:243
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:27
static IP::Endpoint from_asio(boost::asio::ip::address const &address)