2020-05-22 13:18:41 +00:00
|
|
|
/*
|
2023-02-03 20:59:56 +00:00
|
|
|
* Copyright (c) 2013-2023, The PurpleI2P Project
|
2020-05-22 13:18:41 +00:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2017-04-08 16:51:35 +00:00
|
|
|
#include "MatchedDestination.h"
|
|
|
|
#include "Log.h"
|
|
|
|
#include "ClientContext.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace client
|
|
|
|
{
|
|
|
|
MatchedTunnelDestination::MatchedTunnelDestination(const i2p::data::PrivateKeys & keys, const std::string & remoteName, const std::map<std::string, std::string> * params)
|
2020-02-04 19:17:23 +00:00
|
|
|
: RunnableClientDestination(keys, false, params),
|
2017-04-08 16:51:35 +00:00
|
|
|
m_RemoteName(remoteName) {}
|
|
|
|
|
2017-04-08 18:15:08 +00:00
|
|
|
|
2017-04-08 16:51:35 +00:00
|
|
|
void MatchedTunnelDestination::ResolveCurrentLeaseSet()
|
|
|
|
{
|
2020-03-01 10:25:50 +00:00
|
|
|
auto addr = i2p::client::context.GetAddressBook().GetAddress (m_RemoteName);
|
2019-03-28 16:19:19 +00:00
|
|
|
if(addr && addr->IsIdentHash ())
|
2017-04-08 16:51:35 +00:00
|
|
|
{
|
2020-03-01 10:25:50 +00:00
|
|
|
m_RemoteIdent = addr->identHash;
|
2017-04-08 16:51:35 +00:00
|
|
|
auto ls = FindLeaseSet(m_RemoteIdent);
|
|
|
|
if(ls)
|
|
|
|
HandleFoundCurrentLeaseSet(ls);
|
|
|
|
else
|
|
|
|
RequestDestination(m_RemoteIdent, std::bind(&MatchedTunnelDestination::HandleFoundCurrentLeaseSet, this, std::placeholders::_1));
|
|
|
|
}
|
|
|
|
else
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogWarning, "Destination: Failed to resolve ", m_RemoteName);
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MatchedTunnelDestination::HandleFoundCurrentLeaseSet(std::shared_ptr<const i2p::data::LeaseSet> ls)
|
|
|
|
{
|
|
|
|
if(ls)
|
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogDebug, "Destination: Resolved remote lease set for ", m_RemoteName);
|
2017-04-08 16:51:35 +00:00
|
|
|
m_RemoteLeaseSet = ls;
|
|
|
|
}
|
|
|
|
else
|
2017-04-08 18:15:08 +00:00
|
|
|
{
|
|
|
|
m_ResolveTimer->expires_from_now(boost::posix_time::seconds(1));
|
|
|
|
m_ResolveTimer->async_wait([&](const boost::system::error_code & ec) {
|
2020-03-01 10:25:50 +00:00
|
|
|
if(!ec) ResolveCurrentLeaseSet();
|
2017-04-08 18:15:08 +00:00
|
|
|
});
|
|
|
|
}
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-02 23:58:58 +00:00
|
|
|
void MatchedTunnelDestination::Start()
|
2017-04-08 16:51:35 +00:00
|
|
|
{
|
2020-02-02 23:58:58 +00:00
|
|
|
ClientDestination::Start();
|
|
|
|
m_ResolveTimer = std::make_shared<boost::asio::deadline_timer>(GetService());
|
|
|
|
GetTunnelPool()->SetCustomPeerSelector(this);
|
2020-03-01 10:25:50 +00:00
|
|
|
ResolveCurrentLeaseSet();
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 23:58:58 +00:00
|
|
|
void MatchedTunnelDestination::Stop()
|
2017-04-08 16:51:35 +00:00
|
|
|
{
|
2020-02-02 23:58:58 +00:00
|
|
|
ClientDestination::Stop();
|
|
|
|
if(m_ResolveTimer)
|
|
|
|
m_ResolveTimer->cancel();
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool MatchedTunnelDestination::SelectPeers(i2p::tunnel::Path & path, int hops, bool inbound)
|
|
|
|
{
|
|
|
|
auto pool = GetTunnelPool();
|
2023-02-03 20:59:56 +00:00
|
|
|
if(!pool || !pool->StandardSelectPeers(path, hops, inbound,
|
2021-03-02 00:02:27 +00:00
|
|
|
std::bind(&i2p::tunnel::TunnelPool::SelectNextHop, pool, std::placeholders::_1, std::placeholders::_2)))
|
2017-04-08 16:51:35 +00:00
|
|
|
return false;
|
|
|
|
// more here for outbound tunnels
|
|
|
|
if(!inbound && m_RemoteLeaseSet)
|
|
|
|
{
|
|
|
|
if(m_RemoteLeaseSet->IsExpired())
|
|
|
|
ResolveCurrentLeaseSet();
|
|
|
|
if(m_RemoteLeaseSet && !m_RemoteLeaseSet->IsExpired())
|
|
|
|
{
|
|
|
|
// remote lease set is good
|
|
|
|
auto leases = m_RemoteLeaseSet->GetNonExpiredLeases();
|
|
|
|
// pick lease
|
|
|
|
std::shared_ptr<i2p::data::RouterInfo> obep;
|
2021-11-27 20:30:35 +00:00
|
|
|
while(!obep && leases.size() > 0)
|
2021-07-23 00:58:35 +00:00
|
|
|
{
|
2017-04-08 16:51:35 +00:00
|
|
|
auto idx = rand() % leases.size();
|
|
|
|
auto lease = leases[idx];
|
|
|
|
obep = i2p::data::netdb.FindRouter(lease->tunnelGateway);
|
|
|
|
leases.erase(leases.begin()+idx);
|
|
|
|
}
|
2021-11-27 20:30:35 +00:00
|
|
|
if(obep)
|
2021-07-23 00:58:35 +00:00
|
|
|
{
|
|
|
|
path.Add (obep);
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogDebug, "Destination: Found OBEP matching IBGW");
|
2017-04-08 16:51:35 +00:00
|
|
|
} else
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogWarning, "Destination: Could not find proper IBGW for matched outbound tunnel");
|
2017-04-08 16:51:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|