review comments

pull/2121/head
dan 1 year ago
parent f6aa58482a
commit d37398a915

@ -29,7 +29,7 @@ namespace llarp
{ {
IPRange range{}; IPRange range{};
range.FromString(_range); range.FromString(_range);
return range; return range;
} }
static constexpr IPRange static constexpr IPRange

@ -43,15 +43,15 @@ namespace llarp::rpc
response_hex.format = llarp::rpc::json_binary_proxy::fmt::bt; response_hex.format = llarp::rpc::json_binary_proxy::fmt::bt;
} }
// Invoked if this.replier is still present. If it is "stolen" by endpoint (moved from // Invoked if this.replier is still present. If it is "stolen" by endpoint (moved from
// RPC struct), then endpoint handles sending reply // RPC struct), then endpoint handles sending reply
void void
send_response() send_response()
{ {
replier->reply(is_bt() ? oxenc::bt_serialize(json_to_bt(std::move(response))) replier->reply(
: response.dump()); is_bt() ? oxenc::bt_serialize(json_to_bt(std::move(response))) : response.dump());
} }
void void
send_response(nlohmann::json _response) send_response(nlohmann::json _response)
{ {
@ -97,9 +97,9 @@ namespace llarp::rpc
// The oxenmq deferred send object into which the response will be set. If this optional is // The oxenmq deferred send object into which the response will be set. If this optional is
// still set when the `invoke` call returns then the response is sent at that point; if it has // still set when the `invoke` call returns then the response is sent at that point; if it has
// been moved out (i.e. either just this instance or the whole request struct is stolen/moved by // been moved out (i.e. either just this instance or the whole request struct is stolen/moved
// the invoke function) then it is the invoke function's job to send a reply. Typically this is // by the invoke function) then it is the invoke function's job to send a reply. Typically
// done when a response cannot be sent immediately // this is done when a response cannot be sent immediately
std::optional<oxenmq::Message::DeferredSend> replier; std::optional<oxenmq::Message::DeferredSend> replier;
}; };

@ -1,12 +1,12 @@
#pragma once #pragma once
#include "rpc_request_decorators.hpp" #include "rpc_request_decorators.hpp"
#include "net/ip_range.hpp" #include "llarp/net/ip_range.hpp"
#include "router/abstractrouter.hpp" #include "llarp/router/abstractrouter.hpp"
#include "router/route_poker.hpp" #include "llarp/router/route_poker.hpp"
#include "service/address.hpp" #include "llarp/service/address.hpp"
#include "service/endpoint.hpp" #include "llarp/service/endpoint.hpp"
#include "service/outbound_context.hpp" #include "llarp/service/outbound_context.hpp"
#include <string_view> #include <string_view>
#include <llarp/config/config.hpp> #include <llarp/config/config.hpp>
#include <oxenmq/oxenmq.h> #include <oxenmq/oxenmq.h>
@ -164,10 +164,10 @@ namespace llarp::rpc
// Note: ask Jason about the internals of this // Note: ask Jason about the internals of this
// //
// Inputs: // Inputs:
// "endpoint" : // "endpoint" :
// "unmap" : if true, unmaps connection to exit node (bool) // "unmap" : if true, unmaps connection to exit node (bool)
// "range" : IP range to map to exit node // "range" : IP range to map to exit node
// "token" : // "token" :
// //
// Returns: // Returns:
// //
@ -186,21 +186,26 @@ namespace llarp::rpc
void void
onGoodResult(std::string reason, bool hasClient) onGoodResult(std::string reason, bool hasClient)
{ {
response = (hasClient) ? response = (hasClient) ? nlohmann::json{{"result", reason}}.dump()
nlohmann::json{{"result", reason}}.dump() : : nlohmann::json{{"error", "We don't have an exit?"}}.dump();
nlohmann::json{{"error", "We don't have an exit?"}}.dump();
} }
void void
onBadResult(std::string reason, AbstractRouter& abs, llarp::service::Endpoint_ptr eptr, IPRange range) onBadResult(
std::string reason, AbstractRouter& abs, llarp::service::Endpoint_ptr eptr, IPRange range)
{ {
abs.routePoker()->Down(); abs.routePoker()->Down();
eptr->UnmapExitRange(range); eptr->UnmapExitRange(range);
response = nlohmann::json{{"result", reason}}.dump(); response = nlohmann::json{{"result", reason}}.dump();
} }
void void
mapExit(service::Address addr, AbstractRouter& router, llarp::service::Endpoint_ptr eptr, IPRange range, service::Address exitAddr) mapExit(
service::Address addr,
AbstractRouter& router,
llarp::service::Endpoint_ptr eptr,
IPRange range,
service::Address exitAddr)
{ {
eptr->MapExitRange(range, addr); eptr->MapExitRange(range, addr);

@ -1,6 +1,6 @@
#include "rpc_server.hpp" #include "rpc_server.hpp"
#include "rpc_request.hpp" #include "rpc_request.hpp"
#include "service/address.hpp" #include "llarp/service/address.hpp"
#include <llarp/router/route_poker.hpp> #include <llarp/router/route_poker.hpp>
#include <llarp/config/config.hpp> #include <llarp/config/config.hpp>
#include <llarp/config/ini.hpp> #include <llarp/config/ini.hpp>
@ -85,7 +85,7 @@ namespace llarp::rpc
template <typename RPC> template <typename RPC>
void void
register_rpc_command(std::unordered_map<std::string, std::shared_ptr<const rpc_callback>>& regs) register_rpc_command(std::unordered_map<std::string, rpc_callback>& regs)
{ {
static_assert(std::is_base_of_v<RPCRequest, RPC>); static_assert(std::is_base_of_v<RPCRequest, RPC>);
auto cback = std::make_shared<rpc_callback>(); auto cback = std::make_shared<rpc_callback>();
@ -109,17 +109,17 @@ namespace llarp::rpc
} }
template <typename... RPC> template <typename... RPC>
std::unordered_map<std::string, std::shared_ptr<const rpc_callback>> std::unordered_map<std::string, rpc_callback>
register_rpc_requests(tools::type_list<RPC...>) register_rpc_requests(tools::type_list<RPC...>)
{ {
std::unordered_map<std::string, std::shared_ptr<const rpc_callback>> regs; std::unordered_map<std::string, rpc_callback> regs;
(register_rpc_command<RPC>(regs), ...); (register_rpc_command<RPC>(regs), ...);
return regs; return regs;
} }
const std::unordered_map<std::string, std::shared_ptr<const rpc_callback>> rpc_request_map = const std::unordered_map<std::string, rpc_callback> rpc_request_map =
register_rpc_requests(rpc::rpc_request_types{}); register_rpc_requests(rpc::rpc_request_types{});
void void
@ -133,7 +133,7 @@ namespace llarp::rpc
m_LMQ->add_request_command( m_LMQ->add_request_command(
"llarp", "llarp",
req.first, req.first,
[name = std::string_view{req.first}, &call = *req.second, this](oxenmq::Message& m) { [name = std::string_view{req.first}, &call = req.second, this](oxenmq::Message& m) {
call.invoke(m, *this); call.invoke(m, *this);
}); });
} }
@ -352,7 +352,7 @@ namespace llarp::rpc
// plus callback to report when this happens // plus callback to report when this happens
// callback is called much later (1-2s) when exit node flow is secured // callback is called much later (1-2s) when exit node flow is secured
// exit is now ready to use // exit is now ready to use
// //
void void
RPCServer::invoke(Exit& exit) RPCServer::invoke(Exit& exit)
{ {
@ -362,9 +362,6 @@ namespace llarp::rpc
IPRange range = IPRange::StringInit(exit.request.ip_range); IPRange range = IPRange::StringInit(exit.request.ip_range);
service::Address exitAddr{exit.request.address}; service::Address exitAddr{exit.request.address};
m_Router.hiddenServiceContext().GetDefault()->MapExitRange(range, exitAddr);
} }
void void

@ -38,7 +38,7 @@ namespace llarp::rpc
// Stores references to RPC requests in a unordered map for ease of reference // Stores references to RPC requests in a unordered map for ease of reference
// when adding to server. To add endpoints, define in rpc_request_definitions.hpp // when adding to server. To add endpoints, define in rpc_request_definitions.hpp
// and register in rpc_server.cpp // and register in rpc_server.cpp
extern const std::unordered_map<std::string, std::shared_ptr<const rpc_callback>> rpc_request_map; extern const std::unordered_map<std::string, rpc_callback> rpc_request_map;
// Exception used to signal various types of errors with a request back to the caller. This // Exception used to signal various types of errors with a request back to the caller. This
// exception indicates that the caller did something wrong: bad data, invalid value, etc., but // exception indicates that the caller did something wrong: bad data, invalid value, etc., but
@ -121,8 +121,7 @@ namespace llarp::rpc
RPCServer& server; RPCServer& server;
RPC rpc{}; RPC rpc{};
EndpointHandler(RPCServer& _server, DeferredSend _replier) EndpointHandler(RPCServer& _server, DeferredSend _replier) : server{_server}
: server{_server}
{ {
rpc.replier.emplace(std::move(_replier)); rpc.replier.emplace(std::move(_replier));
} }
@ -145,7 +144,7 @@ namespace llarp::rpc
log::info(logcat, "RPC request 'rpc.{}' raised an exception: {}", rpc.name, e.what()); log::info(logcat, "RPC request 'rpc.{}' raised an exception: {}", rpc.name, e.what());
rpc.response = CreateJSONError( rpc.response = CreateJSONError(
fmt::format("RPC request 'rpc.{}' raised an exception: {}", rpc.name, e.what())); fmt::format("RPC request 'rpc.{}' raised an exception: {}", rpc.name, e.what()));
}; }
// check if std::optional in rpc is present // check if std::optional in rpc is present
// then rpc.send_response // then rpc.send_response

Loading…
Cancel
Save