mirror of https://github.com/oxen-io/lokinet
Refactor endpoint state management to a new class
parent
0a7021d827
commit
e52492911d
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,119 @@
|
|||||||
|
#include <service/endpoint_state.hpp>
|
||||||
|
|
||||||
|
#include <exit/session.hpp>
|
||||||
|
#include <hook/shell.hpp>
|
||||||
|
#include <service/outbound_context.hpp>
|
||||||
|
#include <util/str.hpp>
|
||||||
|
|
||||||
|
namespace llarp
|
||||||
|
{
|
||||||
|
namespace service
|
||||||
|
{
|
||||||
|
bool
|
||||||
|
EndpointState::SetOption(const std::string& k, const std::string& v,
|
||||||
|
const std::string& name)
|
||||||
|
{
|
||||||
|
if(k == "keyfile")
|
||||||
|
{
|
||||||
|
m_Keyfile = v;
|
||||||
|
}
|
||||||
|
if(k == "tag")
|
||||||
|
{
|
||||||
|
m_Tag = v;
|
||||||
|
LogInfo("Setting tag to ", v);
|
||||||
|
}
|
||||||
|
if(k == "prefetch-tag")
|
||||||
|
{
|
||||||
|
m_PrefetchTags.insert(v);
|
||||||
|
}
|
||||||
|
if(k == "prefetch-addr")
|
||||||
|
{
|
||||||
|
Address addr;
|
||||||
|
if(addr.FromString(v))
|
||||||
|
m_PrefetchAddrs.insert(addr);
|
||||||
|
}
|
||||||
|
if(k == "min-latency")
|
||||||
|
{
|
||||||
|
auto val = atoi(v.c_str());
|
||||||
|
if(val > 0)
|
||||||
|
m_MinPathLatency = val;
|
||||||
|
}
|
||||||
|
if(k == "bundle-rc")
|
||||||
|
{
|
||||||
|
m_BundleRC = IsTrueValue(v.c_str());
|
||||||
|
}
|
||||||
|
if(k == "blacklist-snode")
|
||||||
|
{
|
||||||
|
RouterID snode;
|
||||||
|
if(!snode.FromString(v))
|
||||||
|
{
|
||||||
|
LogError(name, " invalid snode value: ", v);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const auto result = m_SnodeBlacklist.insert(snode);
|
||||||
|
if(!result.second)
|
||||||
|
{
|
||||||
|
LogError(name, " duplicate blacklist-snode: ", snode.ToString());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
LogInfo(name, " adding ", snode.ToString(), " to blacklist");
|
||||||
|
}
|
||||||
|
if(k == "on-up")
|
||||||
|
{
|
||||||
|
m_OnUp = hooks::ExecShellBackend(v);
|
||||||
|
if(m_OnUp)
|
||||||
|
LogInfo(name, " added on up script: ", v);
|
||||||
|
else
|
||||||
|
LogError(name, " failed to add on up script");
|
||||||
|
}
|
||||||
|
if(k == "on-down")
|
||||||
|
{
|
||||||
|
m_OnDown = hooks::ExecShellBackend(v);
|
||||||
|
if(m_OnDown)
|
||||||
|
LogInfo(name, " added on down script: ", v);
|
||||||
|
else
|
||||||
|
LogError(name, " failed to add on down script");
|
||||||
|
}
|
||||||
|
if(k == "on-ready")
|
||||||
|
{
|
||||||
|
m_OnReady = hooks::ExecShellBackend(v);
|
||||||
|
if(m_OnReady)
|
||||||
|
LogInfo(name, " added on ready script: ", v);
|
||||||
|
else
|
||||||
|
LogError(name, " failed to add on ready script");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
util::StatusObject
|
||||||
|
EndpointState::ExtractStatus(util::StatusObject& obj) const
|
||||||
|
{
|
||||||
|
obj.Put("lastPublished", m_LastPublish);
|
||||||
|
obj.Put("lastPublishAttempt", m_LastPublishAttempt);
|
||||||
|
obj.Put("introset", m_IntroSet.ExtractStatus());
|
||||||
|
|
||||||
|
if(!m_Tag.IsZero())
|
||||||
|
obj.Put("tag", m_Tag.ToString());
|
||||||
|
static auto getSecond = [](const auto& item) -> const auto&
|
||||||
|
{
|
||||||
|
return item.second;
|
||||||
|
};
|
||||||
|
obj.PutContainer("deadSessions", m_DeadSessions, getSecond);
|
||||||
|
obj.PutContainer("remoteSessions", m_RemoteSessions, getSecond);
|
||||||
|
obj.PutContainer("lookups", m_PendingLookups, getSecond);
|
||||||
|
obj.PutContainer("snodeSessions", m_SNodeSessions,
|
||||||
|
[](const auto& item) { return item.second.first; });
|
||||||
|
|
||||||
|
util::StatusObject sessionObj{};
|
||||||
|
|
||||||
|
for(const auto& item : m_Sessions)
|
||||||
|
{
|
||||||
|
std::string k = item.first.ToHex();
|
||||||
|
sessionObj.Put(k, item.second.ExtractStatus());
|
||||||
|
}
|
||||||
|
|
||||||
|
obj.Put("converstations", sessionObj);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
} // namespace service
|
||||||
|
} // namespace llarp
|
@ -0,0 +1,109 @@
|
|||||||
|
#ifndef LLARP_SERVICE_ENDPOINT_STATE_HPP
|
||||||
|
#define LLARP_SERVICE_ENDPOINT_STATE_HPP
|
||||||
|
|
||||||
|
#include <hook/ihook.hpp>
|
||||||
|
#include <router_id.hpp>
|
||||||
|
#include <service/address.hpp>
|
||||||
|
#include <service/pendingbuffer.hpp>
|
||||||
|
#include <service/router_lookup_job.hpp>
|
||||||
|
#include <service/session.hpp>
|
||||||
|
#include <service/tag_lookup_job.hpp>
|
||||||
|
#include <service/endpoint_types.hpp>
|
||||||
|
#include <util/compare_ptr.hpp>
|
||||||
|
#include <util/status.hpp>
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <queue>
|
||||||
|
#include <set>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
struct llarp_ev_loop;
|
||||||
|
using llarp_ev_loop_ptr = std::shared_ptr< llarp_ev_loop >;
|
||||||
|
|
||||||
|
namespace llarp
|
||||||
|
{
|
||||||
|
// clang-format off
|
||||||
|
namespace exit { struct BaseSession; }
|
||||||
|
namespace path { struct Path; using Path_ptr = std::shared_ptr< Path >; }
|
||||||
|
namespace routing { struct PathTransferMessage; }
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
namespace service
|
||||||
|
{
|
||||||
|
struct IServiceLookup;
|
||||||
|
struct OutboundContext;
|
||||||
|
|
||||||
|
struct EndpointState
|
||||||
|
{
|
||||||
|
hooks::Backend_ptr m_OnUp;
|
||||||
|
hooks::Backend_ptr m_OnDown;
|
||||||
|
hooks::Backend_ptr m_OnReady;
|
||||||
|
|
||||||
|
util::Mutex m_InboundTrafficQueueMutex;
|
||||||
|
/// ordered queue for inbound hidden service traffic
|
||||||
|
RecvPacketQueue_t m_InboundTrafficQueue
|
||||||
|
GUARDED_BY(m_InboundTrafficQueueMutex);
|
||||||
|
|
||||||
|
std::set< RouterID > m_SnodeBlacklist;
|
||||||
|
|
||||||
|
AbstractRouter* m_Router;
|
||||||
|
std::shared_ptr< Logic > m_IsolatedLogic = nullptr;
|
||||||
|
llarp_ev_loop_ptr m_IsolatedNetLoop = nullptr;
|
||||||
|
std::string m_Keyfile;
|
||||||
|
std::string m_Name;
|
||||||
|
std::string m_NetNS;
|
||||||
|
bool m_BundleRC = false;
|
||||||
|
|
||||||
|
util::Mutex m_SendQueueMutex;
|
||||||
|
std::deque< SendEvent_t > m_SendQueue GUARDED_BY(m_SendQueueMutex);
|
||||||
|
|
||||||
|
PendingTraffic m_PendingTraffic;
|
||||||
|
|
||||||
|
Sessions m_RemoteSessions;
|
||||||
|
Sessions m_DeadSessions;
|
||||||
|
|
||||||
|
std::set< ConvoTag > m_InboundConvos;
|
||||||
|
|
||||||
|
SNodeSessions m_SNodeSessions;
|
||||||
|
|
||||||
|
std::unordered_multimap< Address, PathEnsureHook, Address::Hash >
|
||||||
|
m_PendingServiceLookups;
|
||||||
|
|
||||||
|
std::unordered_map< RouterID, uint32_t, RouterID::Hash >
|
||||||
|
m_ServiceLookupFails;
|
||||||
|
|
||||||
|
PendingRouters m_PendingRouters;
|
||||||
|
|
||||||
|
uint64_t m_CurrentPublishTX = 0;
|
||||||
|
llarp_time_t m_LastPublish = 0;
|
||||||
|
llarp_time_t m_LastPublishAttempt = 0;
|
||||||
|
llarp_time_t m_MinPathLatency = (5 * 1000);
|
||||||
|
/// our introset
|
||||||
|
IntroSet m_IntroSet;
|
||||||
|
/// pending remote service lookups by id
|
||||||
|
PendingLookups m_PendingLookups;
|
||||||
|
/// prefetch remote address list
|
||||||
|
std::set< Address > m_PrefetchAddrs;
|
||||||
|
/// hidden service tag
|
||||||
|
Tag m_Tag;
|
||||||
|
/// prefetch descriptors for these hidden service tags
|
||||||
|
std::set< Tag > m_PrefetchTags;
|
||||||
|
/// on initialize functions
|
||||||
|
std::list< std::function< bool(void) > > m_OnInit;
|
||||||
|
|
||||||
|
/// conversations
|
||||||
|
ConvoMap m_Sessions;
|
||||||
|
|
||||||
|
std::unordered_map< Tag, CachedTagResult, Tag::Hash > m_PrefetchedTags;
|
||||||
|
|
||||||
|
bool
|
||||||
|
SetOption(const std::string& k, const std::string& v,
|
||||||
|
const std::string& name);
|
||||||
|
|
||||||
|
util::StatusObject
|
||||||
|
ExtractStatus(util::StatusObject& obj) const;
|
||||||
|
};
|
||||||
|
} // namespace service
|
||||||
|
} // namespace llarp
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,62 @@
|
|||||||
|
#ifndef LLARP_SERVICE_ENDPOINT_TYPES_HPP
|
||||||
|
#define LLARP_SERVICE_ENDPOINT_TYPES_HPP
|
||||||
|
|
||||||
|
#include <service/pendingbuffer.hpp>
|
||||||
|
#include <service/router_lookup_job.hpp>
|
||||||
|
#include <service/session.hpp>
|
||||||
|
#include <util/compare_ptr.hpp>
|
||||||
|
|
||||||
|
#include <deque>
|
||||||
|
#include <memory>
|
||||||
|
#include <queue>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
namespace llarp
|
||||||
|
{
|
||||||
|
// clang-format off
|
||||||
|
namespace exit { struct BaseSession; }
|
||||||
|
namespace path { struct Path; using Path_ptr = std::shared_ptr< Path >; }
|
||||||
|
namespace routing { struct PathTransferMessage; }
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
namespace service
|
||||||
|
{
|
||||||
|
struct IServiceLookup;
|
||||||
|
struct OutboundContext;
|
||||||
|
|
||||||
|
using Msg_ptr = std::shared_ptr< const routing::PathTransferMessage >;
|
||||||
|
using SendEvent_t = std::pair< Msg_ptr, path::Path_ptr >;
|
||||||
|
using PendingBufferQueue = std::deque< PendingBuffer >;
|
||||||
|
using PendingTraffic =
|
||||||
|
std::unordered_map< Address, PendingBufferQueue, Address::Hash >;
|
||||||
|
|
||||||
|
using ProtocolMessagePtr = std::shared_ptr< ProtocolMessage >;
|
||||||
|
using RecvPacketQueue_t =
|
||||||
|
std::priority_queue< ProtocolMessagePtr,
|
||||||
|
std::vector< ProtocolMessagePtr >,
|
||||||
|
ComparePtr< ProtocolMessagePtr > >;
|
||||||
|
|
||||||
|
using PendingRouters =
|
||||||
|
std::unordered_map< RouterID, RouterLookupJob, RouterID::Hash >;
|
||||||
|
|
||||||
|
using PendingLookups =
|
||||||
|
std::unordered_map< uint64_t, std::unique_ptr< IServiceLookup > >;
|
||||||
|
|
||||||
|
using Sessions =
|
||||||
|
std::unordered_multimap< Address, std::shared_ptr< OutboundContext >,
|
||||||
|
Address::Hash >;
|
||||||
|
|
||||||
|
using SNodeSessionValue =
|
||||||
|
std::pair< std::shared_ptr< exit::BaseSession >, ConvoTag >;
|
||||||
|
|
||||||
|
using SNodeSessions =
|
||||||
|
std::unordered_multimap< RouterID, SNodeSessionValue, RouterID::Hash >;
|
||||||
|
|
||||||
|
using ConvoMap = std::unordered_map< ConvoTag, Session, ConvoTag::Hash >;
|
||||||
|
|
||||||
|
using PathEnsureHook = std::function< void(Address, OutboundContext*) >;
|
||||||
|
|
||||||
|
} // namespace service
|
||||||
|
} // namespace llarp
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,15 @@
|
|||||||
|
#include <service/router_lookup_job.hpp>
|
||||||
|
|
||||||
|
#include <service/endpoint.hpp>
|
||||||
|
|
||||||
|
namespace llarp
|
||||||
|
{
|
||||||
|
namespace service
|
||||||
|
{
|
||||||
|
RouterLookupJob::RouterLookupJob(Endpoint* p, RouterLookupHandler h)
|
||||||
|
: handler(h), txid(p->GenTXID()), started(p->Now())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace service
|
||||||
|
} // namespace llarp
|
@ -0,0 +1,37 @@
|
|||||||
|
#ifndef LLARP_SERVICE_ROUTER_LOOKUP_JOB_HPP
|
||||||
|
#define LLARP_SERVICE_ROUTER_LOOKUP_JOB_HPP
|
||||||
|
|
||||||
|
#include <router_contact.hpp>
|
||||||
|
|
||||||
|
namespace llarp
|
||||||
|
{
|
||||||
|
namespace service
|
||||||
|
{
|
||||||
|
struct Endpoint;
|
||||||
|
|
||||||
|
struct RouterLookupJob
|
||||||
|
{
|
||||||
|
RouterLookupJob(Endpoint* p, RouterLookupHandler h);
|
||||||
|
|
||||||
|
RouterLookupHandler handler;
|
||||||
|
uint64_t txid;
|
||||||
|
llarp_time_t started;
|
||||||
|
|
||||||
|
bool
|
||||||
|
IsExpired(llarp_time_t now) const
|
||||||
|
{
|
||||||
|
if(now < started)
|
||||||
|
return false;
|
||||||
|
return now - started > 30000;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
InformResult(std::vector< RouterContact > result)
|
||||||
|
{
|
||||||
|
if(handler)
|
||||||
|
handler(result);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace service
|
||||||
|
} // namespace llarp
|
||||||
|
#endif
|
Loading…
Reference in New Issue