mirror of https://github.com/oxen-io/lokinet
add llarp::service::Endpoint::map_exit
This commit is contained in:
parent
d3e69fe3c5
commit
02b392881b
|
@ -1,15 +1,15 @@
|
|||
#include <chrono>
|
||||
#include <memory>
|
||||
#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 <llarp/net/ip.hpp>
|
||||
#include <llarp/net/ip_range.hpp>
|
||||
#include <llarp/dht/context.hpp>
|
||||
#include <llarp/dht/key.hpp>
|
||||
#include <llarp/dht/messages/findintro.hpp>
|
||||
|
@ -22,6 +22,7 @@
|
|||
#include <llarp/nodedb.hpp>
|
||||
#include <llarp/profiling.hpp>
|
||||
#include <llarp/router/abstractrouter.hpp>
|
||||
#include <llarp/router/route_poker.hpp>
|
||||
#include <llarp/routing/dht_message.hpp>
|
||||
#include <llarp/routing/path_transfer_message.hpp>
|
||||
|
||||
|
@ -35,6 +36,7 @@
|
|||
#include <llarp/util/priority_queue.hpp>
|
||||
|
||||
#include <optional>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <uvw.hpp>
|
||||
#include <variant>
|
||||
|
@ -215,6 +217,75 @@ namespace llarp
|
|||
return std::nullopt;
|
||||
}
|
||||
|
||||
void
|
||||
Endpoint::map_exit(
|
||||
std::string name,
|
||||
std::string token,
|
||||
std::vector<IPRange> ranges,
|
||||
std::function<void(bool, std::string)> result_handler)
|
||||
{
|
||||
if (ranges.empty())
|
||||
{
|
||||
result_handler(false, "no ranges provided");
|
||||
return;
|
||||
}
|
||||
|
||||
LookupNameAsync(
|
||||
name,
|
||||
[ptr = std::static_pointer_cast<Endpoint>(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<Address>(&*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);
|
||||
}
|
||||
|
||||
|
|
|
@ -284,6 +284,13 @@ namespace llarp
|
|||
void
|
||||
UnmapExitRange(IPRange range);
|
||||
|
||||
void
|
||||
map_exit(
|
||||
std::string name,
|
||||
std::string token,
|
||||
std::vector<IPRange> ranges,
|
||||
std::function<void(bool, std::string)> result);
|
||||
|
||||
void
|
||||
PutLookup(IServiceLookup* lookup, uint64_t txid) override;
|
||||
|
||||
|
|
|
@ -125,8 +125,7 @@ namespace llarp
|
|||
void
|
||||
SendContext::AsyncSendAuth(std::function<void(AuthResult)> 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
|
||||
|
|
Loading…
Reference in New Issue