From 02b392881b89e9c1be5682024f22a5e6ca37a979 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Tue, 24 Jan 2023 13:14:00 -0500 Subject: [PATCH] add llarp::service::Endpoint::map_exit --- llarp/service/endpoint.cpp | 80 ++++++++++++++++++++++++++++++++++- llarp/service/endpoint.hpp | 7 +++ llarp/service/sendcontext.cpp | 5 +-- 3 files changed, 87 insertions(+), 5 deletions(-) diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index 7768c8685..8bb148589 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -1,15 +1,15 @@ -#include -#include #include "endpoint.hpp" #include "endpoint_state.hpp" #include "endpoint_util.hpp" #include "hidden_service_address_lookup.hpp" +#include "auth.hpp" #include "outbound_context.hpp" #include "protocol.hpp" #include "info.hpp" #include "protocol_type.hpp" #include +#include #include #include #include @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -35,6 +36,7 @@ #include #include +#include #include #include #include @@ -215,6 +217,75 @@ namespace llarp return std::nullopt; } + void + Endpoint::map_exit( + std::string name, + std::string token, + std::vector ranges, + std::function result_handler) + { + if (ranges.empty()) + { + result_handler(false, "no ranges provided"); + return; + } + + LookupNameAsync( + name, + [ptr = std::static_pointer_cast(GetSelf()), + name, + auth = AuthInfo{token}, + ranges, + result_handler, + poker = m_router->routePoker()](auto maybe_addr) { + if (not maybe_addr) + { + result_handler(false, "exit not found: {}"_format(name)); + return; + } + if (auto* addr_ptr = std::get_if
(&*maybe_addr)) + { + Address addr{*addr_ptr}; + + ptr->SetAuthInfoForEndpoint(addr, auth); + ptr->MarkAddressOutbound(addr); + auto result = ptr->EnsurePathToService( + addr, + [ptr, name, ranges, result_handler, poker](auto addr, auto* ctx) { + if (ctx == nullptr) + { + result_handler(false, "could not establish flow to {}"_format(name)); + return; + } + + // make a lambda that sends the reply after doing auth + auto apply_result = + [ptr, poker, addr, result_handler, ranges](AuthResult result) { + if (result.code != AuthResultCode::eAuthAccepted) + { + result_handler(false, result.reason); + return; + } + for (const auto& range : ranges) + ptr->MapExitRange(range, addr); + + if (poker) + poker->Up(); + result_handler(true, result.reason); + }; + + ctx->AsyncSendAuth(apply_result); + }, + ptr->PathAlignmentTimeout()); + + if (not result) + result_handler(false, "did not build path to {}"_format(name)); + } + else + result_handler(false, "exit via snode not supported"); + }); + } + void Endpoint::LookupServiceAsync( std::string name, @@ -2086,6 +2157,11 @@ namespace llarp void Endpoint::SetAuthInfoForEndpoint(Address addr, AuthInfo info) { + if (info.token.empty()) + { + m_RemoteAuthInfos.erase(addr); + return; + } m_RemoteAuthInfos[addr] = std::move(info); } diff --git a/llarp/service/endpoint.hpp b/llarp/service/endpoint.hpp index 619f58fdd..74de81355 100644 --- a/llarp/service/endpoint.hpp +++ b/llarp/service/endpoint.hpp @@ -284,6 +284,13 @@ namespace llarp void UnmapExitRange(IPRange range); + void + map_exit( + std::string name, + std::string token, + std::vector ranges, + std::function result); + void PutLookup(IServiceLookup* lookup, uint64_t txid) override; diff --git a/llarp/service/sendcontext.cpp b/llarp/service/sendcontext.cpp index 20fb744a0..0629ba3d0 100644 --- a/llarp/service/sendcontext.cpp +++ b/llarp/service/sendcontext.cpp @@ -125,8 +125,7 @@ namespace llarp void SendContext::AsyncSendAuth(std::function resultHandler) { - const auto maybe = m_Endpoint->MaybeGetAuthInfoForEndpoint(remoteIdent.Addr()); - if (maybe.has_value()) + if (const auto maybe = m_Endpoint->MaybeGetAuthInfoForEndpoint(remoteIdent.Addr())) { // send auth message const llarp_buffer_t authdata{maybe->token}; @@ -134,7 +133,7 @@ namespace llarp authResultListener = resultHandler; } else - resultHandler({AuthResultCode::eAuthFailed, "no auth for given endpoint"}); + resultHandler({AuthResultCode::eAuthAccepted, "no auth needed"}); } void