From d3e69fe3c5baa5d00baf5938f33998996f13ba48 Mon Sep 17 00:00:00 2001 From: dan Date: Tue, 24 Jan 2023 09:44:02 -0800 Subject: [PATCH] added unmapexit and listexit endpoints --- daemon/lokinet-vpn.cpp | 6 +-- llarp/rpc/rpc_request_definitions.hpp | 55 ++++++++++++++++++----- llarp/rpc/rpc_request_parser.cpp | 20 ++++++--- llarp/rpc/rpc_request_parser.hpp | 4 +- llarp/rpc/rpc_server.cpp | 64 ++++++++++++++++++++++----- llarp/rpc/rpc_server.hpp | 6 ++- 6 files changed, 120 insertions(+), 35 deletions(-) diff --git a/daemon/lokinet-vpn.cpp b/daemon/lokinet-vpn.cpp index c1c78f4d1..98d551f3a 100644 --- a/daemon/lokinet-vpn.cpp +++ b/daemon/lokinet-vpn.cpp @@ -223,11 +223,11 @@ main(int argc, char* argv[]) } if (options.vpnUp) { - nlohmann::json opts{{"exit", options.exitAddress}, {"token", options.token}}; + nlohmann::json opts{{"address", options.exitAddress}, {"token", options.token}}; if (options.range) - opts["range"] = *options.range; + opts["IP_range"] = *options.range; - auto maybe_result = OMQ_Request(omq, connectionID, "llarp.exit", std::move(opts)); + auto maybe_result = OMQ_Request(omq, connectionID, "llarp.map_exit", std::move(opts)); if (not maybe_result) return exit_error("could not add exit"); diff --git a/llarp/rpc/rpc_request_definitions.hpp b/llarp/rpc/rpc_request_definitions.hpp index 7f40588a8..0b7401840 100644 --- a/llarp/rpc/rpc_request_definitions.hpp +++ b/llarp/rpc/rpc_request_definitions.hpp @@ -13,6 +13,7 @@ #include #include #include +#include namespace llarp::rpc { @@ -158,29 +159,25 @@ namespace llarp::rpc } request; }; - // RPC: exit - // Seems like this adds an exit node? - // - // Note: ask Jason about the internals of this + // RPC: map_exit + // Map a new connection to an exit node // // Inputs: - // "endpoint" : - // "unmap" : if true, unmaps connection to exit node (bool) + // "address" : ID of endpoint to map // "range" : IP range to map to exit node - // "token" : + // "token" : auth token // // Returns: // - struct Exit : RPCRequest + struct MapExit : RPCRequest { - static constexpr auto name = "exit"sv; + static constexpr auto name = "map_exit"sv; struct request_parameters { std::string address; - std::string ip_range; + std::vector ip_range; std::string token; - bool unmap; } request; void @@ -246,6 +243,38 @@ namespace llarp::rpc } }; + // RPC: list_exits + // List all currently mapped exit node connections + // + // Inputs: none + // + // Returns: + // + struct ListExits : NoArgs + { + static constexpr auto name = "list_exits"sv; + }; + + // RPC: unmap_exit + // Unmap a connection to an exit node + // + // Inputs: + // "endpoint" : ID of endpoint to map + // "range" : IP range to map to exit node + // "token" : auth token + // + // Returns: + // + struct UnmapExit : RPCRequest + { + static constexpr auto name = "unmap_exit"sv; + + struct request_parameters + { + std::vector ip_range; + } request; + }; + // RPC: dns_query // Attempts to query endpoint by domain name // @@ -301,7 +330,9 @@ namespace llarp::rpc QuicConnect, QuicListener, LookupSnode, - Exit, + MapExit, + ListExits, + UnmapExit, DNSQuery, Config>; diff --git a/llarp/rpc/rpc_request_parser.cpp b/llarp/rpc/rpc_request_parser.cpp index 8a193df1a..1a0732d77 100644 --- a/llarp/rpc/rpc_request_parser.cpp +++ b/llarp/rpc/rpc_request_parser.cpp @@ -1,4 +1,5 @@ #include "rpc_request_parser.hpp" +#include "llarp/rpc/rpc_request_definitions.hpp" #include "param_parser.hpp" #include #include @@ -51,18 +52,25 @@ namespace llarp::rpc } void - parse_request(Exit& exit, rpc_input input) + parse_request(MapExit& mapexit, rpc_input input) { get_values( input, "address", - exit.request.address, + mapexit.request.address, "IP_range", - exit.request.ip_range, + mapexit.request.ip_range, "token", - exit.request.token, - "unmap", - exit.request.unmap); + mapexit.request.token); + } + + void + parse_request(UnmapExit& unmapexit, rpc_input input) + { + get_values( + input, + "IP_range", + unmapexit.request.ip_range); } void diff --git a/llarp/rpc/rpc_request_parser.hpp b/llarp/rpc/rpc_request_parser.hpp index f7a71747c..ac1e0ee71 100644 --- a/llarp/rpc/rpc_request_parser.hpp +++ b/llarp/rpc/rpc_request_parser.hpp @@ -22,7 +22,9 @@ namespace llarp::rpc void parse_request(LookupSnode& lookupsnode, rpc_input input); void - parse_request(Exit& exit, rpc_input input); + parse_request(MapExit& mapexit, rpc_input input); + void + parse_request(UnmapExit& unmapexit, rpc_input input); void parse_request(DNSQuery& dnsquery, rpc_input input); void diff --git a/llarp/rpc/rpc_server.cpp b/llarp/rpc/rpc_server.cpp index 0aec94c47..40a4ae6af 100644 --- a/llarp/rpc/rpc_server.cpp +++ b/llarp/rpc/rpc_server.cpp @@ -1,6 +1,8 @@ #include "rpc_server.hpp" +#include "llarp/rpc/rpc_request_definitions.hpp" #include "rpc_request.hpp" #include "llarp/service/address.hpp" +#include #include #include #include @@ -16,6 +18,7 @@ #include #include #include +#include #include namespace llarp::rpc @@ -346,22 +349,59 @@ namespace llarp::rpc }); } - // get a ptr/ref to something in the lokinet service endpoint - // call some fxn like "obtain_exit_to" and pass exit info - // plus optional additional info on how to map - // plus callback to report when this happens - // callback is called much later (1-2s) when exit node flow is secured - // exit is now ready to use - // void - RPCServer::invoke(Exit& exit) + RPCServer::invoke(MapExit& mapexit) { - Exit exit_request; + MapExit exit_request; // steal replier from exit RPC endpoint - exit_request.replier.emplace(std::move(*exit.replier)); + exit_request.replier.emplace(std::move(*mapexit.replier)); + + // + // + // + // + // + } + + void + RPCServer::invoke(ListExits& listexits) + { + if (not m_Router.hiddenServiceContext().hasEndpoints()) + listexits.response = CreateJSONError("No mapped endpoints found"); + else + listexits.response = CreateJSONResponse( + m_Router.hiddenServiceContext().GetDefault()->ExtractStatus()["m_ExitMap"]); + } + + void + RPCServer::invoke(UnmapExit& unmapexit) + { + if (unmapexit.request.ip_range.empty()) + { + unmapexit.response = CreateJSONError("No IP range provided"); + return; + } + + std::vector range{}; + + for (auto& ip : unmapexit.request.ip_range) + { + try { + range.push_back(IPRange::StringInit(ip)); + } catch (std::exception& e) { + unmapexit.response = CreateJSONError(e.what()); + } + } + + try { + m_Router.routePoker()->Down(); + for (auto& ip : range) + m_Router.hiddenServiceContext().GetDefault()->UnmapExitRange(ip); + } catch (std::exception& e) { + unmapexit.response = CreateJSONError("Unable to unmap to given range"); + } - IPRange range = IPRange::StringInit(exit.request.ip_range); - service::Address exitAddr{exit.request.address}; + unmapexit.response = CreateJSONResponse("OK"); } void diff --git a/llarp/rpc/rpc_server.hpp b/llarp/rpc/rpc_server.hpp index fe64ca1b0..dc07db30f 100644 --- a/llarp/rpc/rpc_server.hpp +++ b/llarp/rpc/rpc_server.hpp @@ -103,7 +103,11 @@ namespace llarp::rpc void invoke(LookupSnode& lookupsnode); void - invoke(Exit& exit); + invoke(MapExit& mapexit); + void + invoke(ListExits& listexits); + void + invoke(UnmapExit& unmapexit); void invoke(DNSQuery& dnsquery); void