2018-08-04 02:59:32 +00:00
|
|
|
|
2018-07-18 03:10:21 +00:00
|
|
|
#include <llarp/dht/messages/findintro.hpp>
|
|
|
|
#include <llarp/messages/dht.hpp>
|
2018-07-11 16:11:19 +00:00
|
|
|
#include <llarp/service/endpoint.hpp>
|
2018-07-19 04:58:39 +00:00
|
|
|
#include <llarp/service/protocol.hpp>
|
|
|
|
#include "buffer.hpp"
|
2018-07-11 16:11:19 +00:00
|
|
|
#include "router.hpp"
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace service
|
|
|
|
{
|
|
|
|
Endpoint::Endpoint(const std::string& name, llarp_router* r)
|
2018-07-19 21:08:11 +00:00
|
|
|
: llarp_pathbuilder_context(r, r->dht, 2, 4), m_Router(r), m_Name(name)
|
2018-07-11 16:11:19 +00:00
|
|
|
{
|
2018-07-18 22:50:05 +00:00
|
|
|
m_Tag.Zero();
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Endpoint::SetOption(const std::string& k, const std::string& v)
|
|
|
|
{
|
|
|
|
if(k == "keyfile")
|
|
|
|
{
|
|
|
|
m_Keyfile = v;
|
|
|
|
}
|
2018-07-18 03:10:21 +00:00
|
|
|
if(k == "tag")
|
|
|
|
{
|
|
|
|
m_Tag = v;
|
2018-07-18 20:58:16 +00:00
|
|
|
llarp::LogInfo("Setting tag to ", v);
|
2018-07-18 03:10:21 +00:00
|
|
|
}
|
|
|
|
if(k == "prefetch-tag")
|
|
|
|
{
|
|
|
|
m_PrefetchTags.insert(v);
|
|
|
|
}
|
2018-08-02 00:48:43 +00:00
|
|
|
if(k == "prefetch-addr")
|
|
|
|
{
|
|
|
|
Address addr;
|
|
|
|
if(addr.FromString(v))
|
|
|
|
m_PrefetchAddrs.insert(addr);
|
|
|
|
}
|
2018-07-12 13:43:37 +00:00
|
|
|
return true;
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 04:58:39 +00:00
|
|
|
struct PathAlignJob
|
|
|
|
{
|
|
|
|
Address remote;
|
|
|
|
|
|
|
|
PathAlignJob(const Address& addr) : remote(addr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HandleResult(Endpoint::OutboundContext* context)
|
|
|
|
{
|
|
|
|
if(context)
|
|
|
|
{
|
2018-08-02 00:48:43 +00:00
|
|
|
llarp::LogInfo("BEEP");
|
2018-07-19 04:58:39 +00:00
|
|
|
byte_t tmp[128] = {0};
|
|
|
|
memcpy(tmp, "BEEP", 4);
|
|
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
|
|
|
context->AsyncEncryptAndSendTo(buf, eProtocolText);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogWarn("PathAlignJob timed out");
|
|
|
|
}
|
2018-07-19 21:08:11 +00:00
|
|
|
delete this;
|
2018-07-19 04:58:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-11 16:11:19 +00:00
|
|
|
void
|
2018-07-18 22:50:05 +00:00
|
|
|
Endpoint::Tick(llarp_time_t now)
|
2018-07-11 16:11:19 +00:00
|
|
|
{
|
2018-07-30 00:30:10 +00:00
|
|
|
/// reset tx id for publish
|
|
|
|
if(now - m_LastPublishAttempt >= INTROSET_PUBLISH_RETRY_INTERVAL)
|
|
|
|
m_CurrentPublishTX = 0;
|
2018-07-19 04:58:39 +00:00
|
|
|
// publish descriptors
|
2018-07-18 22:50:05 +00:00
|
|
|
if(ShouldPublishDescriptors(now))
|
2018-07-11 16:11:19 +00:00
|
|
|
{
|
2018-07-19 04:58:39 +00:00
|
|
|
std::set< Introduction > I;
|
2018-07-18 03:10:21 +00:00
|
|
|
if(!GetCurrentIntroductions(I))
|
2018-07-11 16:11:19 +00:00
|
|
|
{
|
2018-07-16 03:32:13 +00:00
|
|
|
llarp::LogWarn("could not publish descriptors for endpoint ", Name(),
|
2018-07-11 16:11:19 +00:00
|
|
|
" because we couldn't get any introductions");
|
|
|
|
return;
|
|
|
|
}
|
2018-07-20 04:50:28 +00:00
|
|
|
m_IntroSet.I.clear();
|
|
|
|
for(const auto& intro : I)
|
|
|
|
m_IntroSet.I.push_back(intro);
|
2018-07-18 22:50:05 +00:00
|
|
|
m_IntroSet.topic = m_Tag;
|
2018-07-18 03:10:21 +00:00
|
|
|
if(!m_Identity.SignIntroSet(m_IntroSet, &m_Router->crypto))
|
2018-07-11 16:11:19 +00:00
|
|
|
{
|
2018-07-16 03:32:13 +00:00
|
|
|
llarp::LogWarn("failed to sign introset for endpoint ", Name());
|
2018-07-11 16:11:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-07-17 06:17:13 +00:00
|
|
|
if(PublishIntroSet(m_Router))
|
2018-07-11 16:11:19 +00:00
|
|
|
{
|
2018-07-16 03:32:13 +00:00
|
|
|
llarp::LogInfo("publishing introset for endpoint ", Name());
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-16 03:32:13 +00:00
|
|
|
llarp::LogWarn("failed to publish intro set for endpoint ", Name());
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-23 07:38:29 +00:00
|
|
|
// expire pending tx
|
|
|
|
{
|
|
|
|
auto itr = m_PendingLookups.begin();
|
|
|
|
while(itr != m_PendingLookups.end())
|
|
|
|
{
|
|
|
|
if(itr->second->IsTimedOut(now))
|
|
|
|
{
|
|
|
|
itr->second->HandleResponse({});
|
|
|
|
itr = m_PendingLookups.erase(itr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 00:48:43 +00:00
|
|
|
for(const auto& addr : m_PrefetchAddrs)
|
|
|
|
{
|
|
|
|
if(!HasPathToService(addr))
|
|
|
|
{
|
|
|
|
PathAlignJob* j = new PathAlignJob(addr);
|
|
|
|
if(!EnsurePathToService(j->remote,
|
|
|
|
std::bind(&PathAlignJob::HandleResult, j,
|
|
|
|
std::placeholders::_1),
|
|
|
|
10000))
|
|
|
|
{
|
|
|
|
llarp::LogWarn("failed to ensure path to ", addr);
|
|
|
|
delete j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-19 04:58:39 +00:00
|
|
|
// prefetch tags
|
2018-07-18 03:10:21 +00:00
|
|
|
for(const auto& tag : m_PrefetchTags)
|
|
|
|
{
|
|
|
|
auto itr = m_PrefetchedTags.find(tag);
|
|
|
|
if(itr == m_PrefetchedTags.end())
|
|
|
|
{
|
2018-07-19 04:58:39 +00:00
|
|
|
itr = m_PrefetchedTags
|
2018-08-04 02:59:32 +00:00
|
|
|
.insert(std::make_pair(
|
|
|
|
tag, CachedTagResult(this, tag, GenTXID())))
|
2018-07-19 04:58:39 +00:00
|
|
|
.first;
|
|
|
|
}
|
|
|
|
for(const auto& introset : itr->second.result)
|
|
|
|
{
|
|
|
|
PathAlignJob* j = new PathAlignJob(introset.A.Addr());
|
|
|
|
if(!EnsurePathToService(j->remote,
|
|
|
|
std::bind(&PathAlignJob::HandleResult, j,
|
|
|
|
std::placeholders::_1),
|
|
|
|
10000))
|
2018-08-02 00:48:43 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("failed to ensure path to ", introset.A.Addr(),
|
|
|
|
" for tag");
|
2018-07-19 04:58:39 +00:00
|
|
|
delete j;
|
2018-08-02 00:48:43 +00:00
|
|
|
}
|
2018-07-18 03:10:21 +00:00
|
|
|
}
|
2018-07-18 22:50:05 +00:00
|
|
|
itr->second.Expire(now);
|
|
|
|
if(itr->second.ShouldRefresh(now))
|
2018-07-18 03:10:21 +00:00
|
|
|
{
|
|
|
|
auto path = PickRandomEstablishedPath();
|
|
|
|
if(path)
|
|
|
|
{
|
2018-08-04 02:59:32 +00:00
|
|
|
itr->second.txid = GenTXID();
|
2018-07-18 22:50:05 +00:00
|
|
|
itr->second.SendRequestViaPath(path, m_Router);
|
2018-07-18 03:10:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-23 07:38:29 +00:00
|
|
|
|
|
|
|
// tick remote sessions
|
|
|
|
{
|
|
|
|
auto itr = m_RemoteSessions.begin();
|
|
|
|
while(itr != m_RemoteSessions.end())
|
|
|
|
{
|
|
|
|
if(itr->second->Tick(now))
|
|
|
|
{
|
|
|
|
delete itr->second;
|
|
|
|
itr = m_RemoteSessions.erase(itr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
2018-07-18 03:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
Endpoint::GenTXID()
|
|
|
|
{
|
2018-07-20 04:50:28 +00:00
|
|
|
uint64_t txid = llarp_randint();
|
2018-07-18 03:10:21 +00:00
|
|
|
while(m_PendingLookups.find(txid) != m_PendingLookups.end())
|
|
|
|
++txid;
|
|
|
|
return txid;
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 03:32:13 +00:00
|
|
|
std::string
|
|
|
|
Endpoint::Name() const
|
|
|
|
{
|
|
|
|
return m_Name + ":" + m_Identity.pub.Name();
|
|
|
|
}
|
|
|
|
|
2018-08-02 00:48:43 +00:00
|
|
|
bool
|
|
|
|
Endpoint::HasPathToService(const Address& addr) const
|
|
|
|
{
|
|
|
|
return m_RemoteSessions.find(addr) != m_RemoteSessions.end();
|
|
|
|
}
|
|
|
|
|
2018-08-04 02:59:32 +00:00
|
|
|
void
|
|
|
|
Endpoint::PutLookup(IServiceLookup* lookup, uint64_t txid)
|
|
|
|
{
|
|
|
|
m_PendingLookups.insert(std::make_pair(txid, lookup));
|
|
|
|
}
|
|
|
|
|
2018-07-11 16:11:19 +00:00
|
|
|
bool
|
|
|
|
Endpoint::HandleGotIntroMessage(const llarp::dht::GotIntroMessage* msg)
|
|
|
|
{
|
|
|
|
auto crypto = &m_Router->crypto;
|
2018-07-18 03:10:21 +00:00
|
|
|
std::set< IntroSet > remote;
|
2018-07-11 16:11:19 +00:00
|
|
|
for(const auto& introset : msg->I)
|
|
|
|
{
|
2018-07-18 22:50:05 +00:00
|
|
|
if(!introset.VerifySignature(crypto))
|
2018-07-11 16:11:19 +00:00
|
|
|
{
|
2018-07-18 22:50:05 +00:00
|
|
|
llarp::LogInfo("invalid introset signature for ", introset,
|
|
|
|
" on endpoint ", Name());
|
2018-07-19 04:58:39 +00:00
|
|
|
if(m_Identity.pub == introset.A && m_CurrentPublishTX == msg->T)
|
2018-07-18 20:58:16 +00:00
|
|
|
{
|
|
|
|
IntroSetPublishFail();
|
|
|
|
}
|
2018-07-18 22:50:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-07-19 04:58:39 +00:00
|
|
|
if(m_Identity.pub == introset.A && m_CurrentPublishTX == msg->T)
|
2018-07-18 22:50:05 +00:00
|
|
|
{
|
2018-07-11 16:11:19 +00:00
|
|
|
llarp::LogInfo(
|
|
|
|
"got introset publish confirmation for hidden service endpoint ",
|
2018-07-16 03:32:13 +00:00
|
|
|
Name());
|
2018-07-17 06:17:13 +00:00
|
|
|
IntroSetPublished();
|
2018-07-18 03:10:21 +00:00
|
|
|
return true;
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-18 03:10:21 +00:00
|
|
|
remote.insert(introset);
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-18 03:10:21 +00:00
|
|
|
auto itr = m_PendingLookups.find(msg->T);
|
|
|
|
if(itr == m_PendingLookups.end())
|
|
|
|
{
|
|
|
|
llarp::LogWarn("invalid lookup response for hidden service endpoint ",
|
|
|
|
Name(), " txid=", msg->T);
|
2018-07-20 04:50:28 +00:00
|
|
|
return true;
|
2018-07-18 03:10:21 +00:00
|
|
|
}
|
|
|
|
bool result = itr->second->HandleResponse(remote);
|
|
|
|
m_PendingLookups.erase(itr);
|
|
|
|
return result;
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Endpoint::Start()
|
|
|
|
{
|
|
|
|
auto crypto = &m_Router->crypto;
|
|
|
|
if(m_Keyfile.size())
|
|
|
|
{
|
|
|
|
if(!m_Identity.EnsureKeys(m_Keyfile, crypto))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Identity.RegenerateKeys(crypto);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Endpoint::~Endpoint()
|
|
|
|
{
|
|
|
|
}
|
2018-07-12 18:21:44 +00:00
|
|
|
|
2018-07-18 03:10:21 +00:00
|
|
|
Endpoint::CachedTagResult::~CachedTagResult()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Endpoint::CachedTagResult::HandleResponse(
|
2018-07-18 22:50:05 +00:00
|
|
|
const std::set< IntroSet >& introsets)
|
2018-07-18 03:10:21 +00:00
|
|
|
{
|
2018-07-19 04:58:39 +00:00
|
|
|
auto now = llarp_time_now_ms();
|
|
|
|
|
2018-08-04 02:59:32 +00:00
|
|
|
txid = 0;
|
2018-07-18 22:50:05 +00:00
|
|
|
for(const auto& introset : introsets)
|
2018-07-19 04:58:39 +00:00
|
|
|
if(result.insert(introset).second)
|
|
|
|
lastModified = now;
|
|
|
|
llarp::LogInfo("Tag result for ", tag.ToString(), " got ",
|
|
|
|
introsets.size(), " results from lookup, have ",
|
|
|
|
result.size(), " cached last modified at ", lastModified,
|
|
|
|
" is ", now - lastModified, "ms old");
|
2018-07-18 03:10:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-18 22:50:05 +00:00
|
|
|
void
|
|
|
|
Endpoint::CachedTagResult::Expire(llarp_time_t now)
|
|
|
|
{
|
|
|
|
auto itr = result.begin();
|
|
|
|
while(itr != result.end())
|
|
|
|
{
|
|
|
|
if(itr->HasExpiredIntros(now))
|
|
|
|
{
|
2018-07-19 04:58:39 +00:00
|
|
|
llarp::LogInfo("Removing expired tag Entry ", itr->A.Name());
|
|
|
|
itr = result.erase(itr);
|
|
|
|
lastModified = now;
|
2018-07-18 22:50:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 03:10:21 +00:00
|
|
|
llarp::routing::IMessage*
|
|
|
|
Endpoint::CachedTagResult::BuildRequestMessage()
|
|
|
|
{
|
|
|
|
llarp::routing::DHTMessage* msg = new llarp::routing::DHTMessage();
|
2018-08-04 02:59:32 +00:00
|
|
|
msg->M.push_back(new llarp::dht::FindIntroMessage(tag, txid));
|
2018-07-19 04:58:39 +00:00
|
|
|
lastRequest = llarp_time_now_ms();
|
2018-08-04 02:59:32 +00:00
|
|
|
parent->PutLookup(this, txid);
|
2018-07-18 03:10:21 +00:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Endpoint::PublishIntroSet(llarp_router* r)
|
|
|
|
{
|
2018-08-02 00:48:43 +00:00
|
|
|
auto path = GetEstablishedPathClosestTo(m_Identity.pub.Addr());
|
2018-07-18 03:10:21 +00:00
|
|
|
if(path)
|
|
|
|
{
|
2018-07-20 04:50:28 +00:00
|
|
|
m_CurrentPublishTX = llarp_randint();
|
2018-07-18 03:10:21 +00:00
|
|
|
llarp::routing::DHTMessage msg;
|
|
|
|
msg.M.push_back(new llarp::dht::PublishIntroMessage(
|
2018-08-04 02:59:32 +00:00
|
|
|
m_IntroSet, m_CurrentPublishTX, 4));
|
2018-07-18 03:10:21 +00:00
|
|
|
if(path->SendRoutingMessage(&msg, r))
|
|
|
|
{
|
|
|
|
m_LastPublishAttempt = llarp_time_now_ms();
|
|
|
|
llarp::LogInfo(Name(), " publishing introset");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-08-04 02:59:32 +00:00
|
|
|
llarp::LogWarn(Name(), " publish introset failed, no path");
|
2018-07-18 20:58:16 +00:00
|
|
|
return false;
|
2018-07-18 03:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Endpoint::IntroSetPublishFail()
|
|
|
|
{
|
|
|
|
llarp::LogWarn("failed to publish introset for ", Name());
|
|
|
|
m_CurrentPublishTX = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-07-18 22:50:05 +00:00
|
|
|
Endpoint::ShouldPublishDescriptors(llarp_time_t now) const
|
2018-07-18 03:10:21 +00:00
|
|
|
{
|
2018-07-18 22:50:05 +00:00
|
|
|
if(m_IntroSet.HasExpiredIntros(now))
|
2018-07-18 03:10:21 +00:00
|
|
|
return m_CurrentPublishTX == 0
|
|
|
|
&& now - m_LastPublishAttempt >= INTROSET_PUBLISH_RETRY_INTERVAL;
|
|
|
|
return m_CurrentPublishTX == 0
|
|
|
|
&& now - m_LastPublish >= INTROSET_PUBLISH_INTERVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Endpoint::IntroSetPublished()
|
|
|
|
{
|
|
|
|
m_CurrentPublishTX = 0;
|
|
|
|
m_LastPublish = llarp_time_now_ms();
|
|
|
|
llarp::LogInfo(Name(), " IntroSet publish confirmed");
|
|
|
|
}
|
|
|
|
|
2018-07-19 21:08:11 +00:00
|
|
|
struct HiddenServiceAddressLookup : public IServiceLookup
|
|
|
|
{
|
2018-07-22 23:14:29 +00:00
|
|
|
Address remote;
|
2018-08-04 02:59:32 +00:00
|
|
|
Endpoint* endpoint;
|
|
|
|
|
|
|
|
HiddenServiceAddressLookup(Endpoint* p, const Address& addr, uint64_t tx)
|
|
|
|
: IServiceLookup(p, tx), remote(addr), endpoint(p)
|
2018-07-19 21:08:11 +00:00
|
|
|
{
|
2018-07-23 07:38:29 +00:00
|
|
|
llarp::LogInfo("New hidden service lookup for ", addr.ToString());
|
2018-07-19 21:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
HandleResponse(const std::set< IntroSet >& results)
|
|
|
|
{
|
2018-07-22 23:14:29 +00:00
|
|
|
if(results.size() == 1)
|
2018-07-19 21:08:11 +00:00
|
|
|
{
|
2018-07-23 07:38:29 +00:00
|
|
|
llarp::LogInfo("hidden service lookup for ", remote.ToString(),
|
|
|
|
" success");
|
2018-07-22 23:14:29 +00:00
|
|
|
endpoint->PutNewOutboundContext(*results.begin());
|
2018-07-19 21:08:11 +00:00
|
|
|
}
|
2018-07-23 07:38:29 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogInfo("no response in hidden service lookup for ",
|
|
|
|
remote.ToString());
|
|
|
|
}
|
2018-07-22 23:14:29 +00:00
|
|
|
delete this;
|
2018-07-19 21:08:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-07-22 23:14:29 +00:00
|
|
|
|
|
|
|
llarp::routing::IMessage*
|
|
|
|
BuildRequestMessage()
|
|
|
|
{
|
|
|
|
llarp::routing::DHTMessage* msg = new llarp::routing::DHTMessage();
|
|
|
|
msg->M.push_back(new llarp::dht::FindIntroMessage(remote, txid));
|
|
|
|
return msg;
|
|
|
|
}
|
2018-07-19 21:08:11 +00:00
|
|
|
};
|
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
void
|
|
|
|
Endpoint::PutNewOutboundContext(const llarp::service::IntroSet& introset)
|
|
|
|
{
|
|
|
|
Address addr;
|
|
|
|
introset.A.CalculateAddress(addr);
|
|
|
|
|
|
|
|
// only add new session if it's not there
|
|
|
|
if(m_RemoteSessions.find(addr) == m_RemoteSessions.end())
|
|
|
|
{
|
|
|
|
OutboundContext* ctx = new OutboundContext(introset, this);
|
|
|
|
m_RemoteSessions.insert(std::make_pair(addr, ctx));
|
|
|
|
llarp::LogInfo("Created New outbound context for ", addr.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
// inform pending
|
|
|
|
auto itr = m_PendingServiceLookups.find(addr);
|
|
|
|
if(itr != m_PendingServiceLookups.end())
|
|
|
|
{
|
|
|
|
itr->second(m_RemoteSessions.at(addr));
|
|
|
|
m_PendingServiceLookups.erase(itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 00:48:43 +00:00
|
|
|
void
|
|
|
|
Endpoint::HandlePathBuilt(path::Path* p)
|
|
|
|
{
|
|
|
|
p->SetDataHandler(std::bind(&Endpoint::HandleHiddenServiceFrame, this,
|
|
|
|
std::placeholders::_1));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Endpoint::HandleHiddenServiceFrame(const ProtocolFrame* frame)
|
|
|
|
{
|
|
|
|
llarp::LogInfo("handle hidden service frame");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Endpoint::OutboundContext::HandlePathBuilt(path::Path* p)
|
|
|
|
{
|
|
|
|
p->SetDataHandler(
|
|
|
|
std::bind(&Endpoint::OutboundContext::HandleHiddenServiceFrame, this,
|
|
|
|
std::placeholders::_1));
|
|
|
|
}
|
|
|
|
|
2018-08-04 02:59:32 +00:00
|
|
|
void
|
|
|
|
Endpoint::OutboundContext::PutLookup(IServiceLookup* lookup, uint64_t txid)
|
|
|
|
{
|
|
|
|
m_Parent->PutLookup(lookup, txid);
|
|
|
|
}
|
|
|
|
|
2018-08-02 00:48:43 +00:00
|
|
|
bool
|
|
|
|
Endpoint::OutboundContext::HandleHiddenServiceFrame(
|
|
|
|
const ProtocolFrame* frame)
|
|
|
|
{
|
|
|
|
return m_Parent->HandleHiddenServiceFrame(frame);
|
|
|
|
}
|
|
|
|
|
2018-07-19 04:58:39 +00:00
|
|
|
bool
|
|
|
|
Endpoint::EnsurePathToService(const Address& remote, PathEnsureHook hook,
|
|
|
|
llarp_time_t timeoutMS)
|
|
|
|
{
|
2018-08-02 00:48:43 +00:00
|
|
|
auto path = GetEstablishedPathClosestTo(remote);
|
2018-07-23 07:38:29 +00:00
|
|
|
if(!path)
|
|
|
|
{
|
|
|
|
llarp::LogWarn("No outbound path for lookup yet");
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-02 00:48:43 +00:00
|
|
|
llarp::LogInfo(Name(), " Ensure Path to ", remote.ToString());
|
2018-07-22 23:14:29 +00:00
|
|
|
{
|
|
|
|
auto itr = m_RemoteSessions.find(remote);
|
|
|
|
if(itr != m_RemoteSessions.end())
|
|
|
|
{
|
|
|
|
hook(itr->second);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto itr = m_PendingServiceLookups.find(remote);
|
|
|
|
if(itr != m_PendingServiceLookups.end())
|
|
|
|
{
|
|
|
|
// duplicate
|
2018-08-02 00:48:43 +00:00
|
|
|
llarp::LogWarn("duplicate pending service lookup to ",
|
|
|
|
remote.ToString());
|
2018-07-22 23:14:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-07-23 07:38:29 +00:00
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
m_PendingServiceLookups.insert(std::make_pair(remote, hook));
|
2018-07-23 07:38:29 +00:00
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
HiddenServiceAddressLookup* job =
|
|
|
|
new HiddenServiceAddressLookup(this, remote, GenTXID());
|
2018-07-23 07:38:29 +00:00
|
|
|
|
|
|
|
return job->SendRequestViaPath(path, Router());
|
2018-07-19 04:58:39 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 21:08:11 +00:00
|
|
|
Endpoint::OutboundContext::OutboundContext(const IntroSet& intro,
|
|
|
|
Endpoint* parent)
|
|
|
|
: llarp_pathbuilder_context(parent->m_Router, parent->m_Router->dht, 2,
|
|
|
|
4)
|
|
|
|
, currentIntroSet(intro)
|
2018-07-12 18:21:44 +00:00
|
|
|
, m_Parent(parent)
|
2018-07-19 21:08:11 +00:00
|
|
|
|
2018-07-12 18:21:44 +00:00
|
|
|
{
|
2018-07-22 23:14:29 +00:00
|
|
|
selectedIntro.Clear();
|
2018-07-23 07:38:29 +00:00
|
|
|
ShiftIntroduction();
|
2018-07-12 18:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Endpoint::OutboundContext::~OutboundContext()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
void
|
|
|
|
Endpoint::OutboundContext::ShiftIntroduction()
|
|
|
|
{
|
|
|
|
for(const auto& intro : currentIntroSet.I)
|
|
|
|
{
|
|
|
|
if(intro.expiresAt > selectedIntro.expiresAt)
|
|
|
|
{
|
|
|
|
selectedIntro = intro;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 18:21:44 +00:00
|
|
|
bool
|
|
|
|
Endpoint::OutboundContext::HandleGotIntroMessage(
|
|
|
|
const llarp::dht::GotIntroMessage* msg)
|
|
|
|
{
|
2018-08-04 02:59:32 +00:00
|
|
|
if(msg->T != m_UpdateIntrosetTX)
|
|
|
|
{
|
|
|
|
llarp::LogError("unwarrented introset message txid=", msg->T);
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-19 21:08:11 +00:00
|
|
|
auto crypto = m_Parent->Crypto();
|
|
|
|
if(msg->I.size() == 1)
|
|
|
|
{
|
|
|
|
// found intro set
|
2018-08-04 02:59:32 +00:00
|
|
|
const auto& introset = msg->I[0];
|
|
|
|
if(introset.VerifySignature(crypto) && currentIntroSet.A == introset.A)
|
2018-07-19 21:08:11 +00:00
|
|
|
{
|
2018-08-04 02:59:32 +00:00
|
|
|
// update
|
|
|
|
currentIntroSet = introset;
|
|
|
|
// reset tx
|
|
|
|
m_UpdateIntrosetTX = 0;
|
|
|
|
// shift to newest intro
|
|
|
|
// TODO: check timestamp on introset to make sure it's new enough
|
2018-07-22 23:14:29 +00:00
|
|
|
ShiftIntroduction();
|
2018-07-19 21:08:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-04 02:59:32 +00:00
|
|
|
llarp::LogError("Signature Error for intro set ", introset);
|
2018-07-19 21:08:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llarp::LogError("Bad number of intro sets in response");
|
2018-07-12 18:21:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-07-19 04:58:39 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Endpoint::OutboundContext::AsyncEncryptAndSendTo(llarp_buffer_t data,
|
|
|
|
ProtocolType protocol)
|
|
|
|
{
|
|
|
|
if(sequenceNo)
|
|
|
|
{
|
2018-07-22 23:14:29 +00:00
|
|
|
AsyncEncrypt(data);
|
2018-07-19 04:58:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-22 23:14:29 +00:00
|
|
|
AsyncGenIntro(data);
|
2018-07-19 04:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
struct AsyncIntroGen
|
2018-07-19 04:58:39 +00:00
|
|
|
{
|
|
|
|
llarp_logic* logic;
|
|
|
|
llarp_crypto* crypto;
|
|
|
|
byte_t* sharedKey;
|
|
|
|
byte_t* remotePubkey;
|
2018-07-22 23:14:29 +00:00
|
|
|
Identity* m_LocalIdentity;
|
|
|
|
ProtocolMessage msg;
|
|
|
|
ProtocolFrame frame;
|
|
|
|
std::function< void(ProtocolFrame&) > hook;
|
2018-07-19 04:58:39 +00:00
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
AsyncIntroGen(llarp_logic* l, llarp_crypto* c, byte_t* key,
|
|
|
|
byte_t* remote, Identity* localident)
|
2018-07-19 04:58:39 +00:00
|
|
|
: logic(l)
|
|
|
|
, crypto(c)
|
|
|
|
, sharedKey(key)
|
|
|
|
, remotePubkey(remote)
|
2018-07-22 23:14:29 +00:00
|
|
|
, m_LocalIdentity(localident)
|
2018-07-19 04:58:39 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
static void
|
|
|
|
Result(void* user)
|
|
|
|
{
|
|
|
|
AsyncIntroGen* self = static_cast< AsyncIntroGen* >(user);
|
|
|
|
self->hook(self->frame);
|
|
|
|
delete self;
|
|
|
|
}
|
|
|
|
|
2018-07-19 04:58:39 +00:00
|
|
|
static void
|
|
|
|
Work(void* user)
|
|
|
|
{
|
2018-07-22 23:14:29 +00:00
|
|
|
AsyncIntroGen* self = static_cast< AsyncIntroGen* >(user);
|
|
|
|
// randomize Nounce
|
|
|
|
self->frame.N.Randomize();
|
|
|
|
// derive session key
|
2018-07-19 04:58:39 +00:00
|
|
|
self->crypto->dh_server(self->sharedKey, self->remotePubkey,
|
2018-07-22 23:14:29 +00:00
|
|
|
self->m_LocalIdentity->enckey, self->frame.N);
|
|
|
|
// encrypt and sign
|
|
|
|
self->frame.EncryptAndSign(self->crypto, &self->msg, self->sharedKey,
|
|
|
|
self->m_LocalIdentity->signkey);
|
|
|
|
// inform result
|
|
|
|
llarp_logic_queue_job(self->logic, {self, &Result});
|
2018-07-19 04:58:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2018-07-22 23:14:29 +00:00
|
|
|
Endpoint::OutboundContext::AsyncGenIntro(llarp_buffer_t payload)
|
2018-07-19 04:58:39 +00:00
|
|
|
{
|
2018-07-22 23:14:29 +00:00
|
|
|
AsyncIntroGen* ex =
|
|
|
|
new AsyncIntroGen(m_Parent->Logic(), m_Parent->Crypto(), sharedKey,
|
|
|
|
currentIntroSet.A.enckey, m_Parent->GetIdentity());
|
|
|
|
ex->hook = std::bind(&Endpoint::OutboundContext::Send, this,
|
|
|
|
std::placeholders::_1);
|
|
|
|
|
|
|
|
ex->msg.PutBuffer(payload);
|
2018-07-19 04:58:39 +00:00
|
|
|
llarp_threadpool_queue_job(m_Parent->Worker(),
|
2018-07-22 23:14:29 +00:00
|
|
|
{ex, &AsyncIntroGen::Work});
|
2018-07-19 04:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-07-22 23:14:29 +00:00
|
|
|
Endpoint::OutboundContext::Send(ProtocolFrame& msg)
|
2018-07-19 04:58:39 +00:00
|
|
|
{
|
2018-07-22 23:14:29 +00:00
|
|
|
// in this context we assume the message contents are encrypted
|
2018-07-23 07:38:29 +00:00
|
|
|
auto now = llarp_time_now_ms();
|
|
|
|
if(currentIntroSet.HasExpiredIntros(now))
|
|
|
|
{
|
|
|
|
UpdateIntroSet();
|
|
|
|
}
|
|
|
|
if(selectedIntro.expiresAt <= now || now - selectedIntro.expiresAt > 1000)
|
|
|
|
{
|
|
|
|
ShiftIntroduction();
|
|
|
|
}
|
2018-07-22 23:14:29 +00:00
|
|
|
auto path = GetPathByRouter(selectedIntro.router);
|
|
|
|
if(path)
|
|
|
|
{
|
|
|
|
routing::PathTransferMessage transfer;
|
|
|
|
transfer.T = &msg;
|
|
|
|
transfer.Y.Randomize();
|
|
|
|
transfer.P = selectedIntro.pathID;
|
2018-07-23 07:38:29 +00:00
|
|
|
llarp::LogInfo("sending frame via ", path->Upstream(), " to ",
|
2018-08-04 02:59:32 +00:00
|
|
|
path->Endpoint(), " for ", Name());
|
2018-07-22 23:14:29 +00:00
|
|
|
path->SendRoutingMessage(&transfer, m_Parent->Router());
|
|
|
|
}
|
2018-07-23 07:38:29 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogWarn("No path to ", selectedIntro.router);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-04 02:59:32 +00:00
|
|
|
std::string
|
|
|
|
Endpoint::OutboundContext::Name() const
|
|
|
|
{
|
|
|
|
return "OBContext:" + m_Parent->Name() + "-"
|
|
|
|
+ currentIntroSet.A.Addr().ToString();
|
|
|
|
}
|
|
|
|
|
2018-07-23 07:38:29 +00:00
|
|
|
void
|
|
|
|
Endpoint::OutboundContext::UpdateIntroSet()
|
|
|
|
{
|
2018-08-04 02:59:32 +00:00
|
|
|
auto path = GetEstablishedPathClosestTo(currentIntroSet.A.Addr());
|
2018-07-23 07:38:29 +00:00
|
|
|
if(path)
|
|
|
|
{
|
2018-08-04 02:59:32 +00:00
|
|
|
if(m_UpdateIntrosetTX == 0)
|
|
|
|
{
|
|
|
|
m_UpdateIntrosetTX = llarp_randint();
|
|
|
|
routing::DHTMessage msg;
|
|
|
|
msg.M.push_back(new llarp::dht::FindIntroMessage(
|
|
|
|
currentIntroSet.A.Addr(), m_UpdateIntrosetTX));
|
|
|
|
path->SendRoutingMessage(&msg, m_Parent->Router());
|
|
|
|
}
|
2018-07-23 07:38:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogWarn(
|
|
|
|
"Cannot update introset no path for outbound session to ",
|
|
|
|
currentIntroSet.A.Addr().ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Endpoint::OutboundContext::Tick(llarp_time_t now)
|
|
|
|
{
|
|
|
|
if(selectedIntro.expiresAt >= now || selectedIntro.expiresAt - now < 5000)
|
|
|
|
{
|
|
|
|
UpdateIntroSet();
|
|
|
|
}
|
|
|
|
// TODO: check for expiration
|
|
|
|
return false;
|
2018-07-19 04:58:39 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 21:08:11 +00:00
|
|
|
bool
|
|
|
|
Endpoint::OutboundContext::SelectHop(llarp_nodedb* db, llarp_rc* prev,
|
|
|
|
llarp_rc* cur, size_t hop)
|
|
|
|
{
|
|
|
|
// TODO: don't hard code
|
2018-07-23 07:38:29 +00:00
|
|
|
llarp::LogInfo("Select hop ", hop);
|
2018-07-19 21:08:11 +00:00
|
|
|
if(hop == 3)
|
|
|
|
{
|
2018-07-23 07:38:29 +00:00
|
|
|
auto localcopy = llarp_nodedb_get_rc(db, selectedIntro.router);
|
2018-07-19 21:08:11 +00:00
|
|
|
if(localcopy)
|
|
|
|
{
|
|
|
|
llarp_rc_copy(cur, localcopy);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we don't have it?
|
|
|
|
llarp::LogError(
|
|
|
|
"cannot build aligned path, don't have router for introduction ",
|
2018-07-23 07:38:29 +00:00
|
|
|
selectedIntro);
|
2018-07-19 21:08:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return llarp_pathbuilder_context::SelectHop(db, prev, cur, hop);
|
|
|
|
}
|
|
|
|
|
2018-07-19 04:58:39 +00:00
|
|
|
void
|
2018-07-22 23:14:29 +00:00
|
|
|
Endpoint::OutboundContext::AsyncEncrypt(llarp_buffer_t payload)
|
2018-07-19 04:58:39 +00:00
|
|
|
{
|
|
|
|
// TODO: implement me
|
|
|
|
}
|
|
|
|
|
|
|
|
llarp_logic*
|
|
|
|
Endpoint::Logic()
|
|
|
|
{
|
|
|
|
return m_Router->logic;
|
|
|
|
}
|
|
|
|
|
|
|
|
llarp_crypto*
|
|
|
|
Endpoint::Crypto()
|
|
|
|
{
|
|
|
|
return &m_Router->crypto;
|
|
|
|
}
|
|
|
|
|
|
|
|
llarp_threadpool*
|
|
|
|
Endpoint::Worker()
|
|
|
|
{
|
|
|
|
return m_Router->tp;
|
|
|
|
}
|
|
|
|
|
2018-07-12 18:21:44 +00:00
|
|
|
} // namespace service
|
2018-07-16 03:32:13 +00:00
|
|
|
} // namespace llarp
|