mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-04-27 11:17:49 +02:00
handle explicitPeers I2CP parameter
This commit is contained in:
parent
0e8bdf8299
commit
ef6a038451
5 changed files with 80 additions and 7 deletions
|
@ -1,3 +1,4 @@
|
|||
#include <algorithm>
|
||||
#include "I2PEndian.h"
|
||||
#include "CryptoConst.h"
|
||||
#include "Tunnel.h"
|
||||
|
@ -22,6 +23,27 @@ namespace tunnel
|
|||
DetachTunnels ();
|
||||
}
|
||||
|
||||
void TunnelPool::SetExplicitPeers (std::shared_ptr<std::vector<i2p::data::IdentHash> > explicitPeers)
|
||||
{
|
||||
m_ExplicitPeers = explicitPeers;
|
||||
if (m_ExplicitPeers)
|
||||
{
|
||||
int size = m_ExplicitPeers->size ();
|
||||
if (m_NumInboundHops > size)
|
||||
{
|
||||
m_NumInboundHops = size;
|
||||
LogPrint (eLogInfo, "Inbound tunnel length has beed adjusted to ", size, " for explicit peers");
|
||||
}
|
||||
if (m_NumOutboundHops > size)
|
||||
{
|
||||
m_NumOutboundHops = size;
|
||||
LogPrint (eLogInfo, "Outbound tunnel length has beed adjusted to ", size, " for explicit peers");
|
||||
}
|
||||
m_NumInboundTunnels = 1;
|
||||
m_NumOutboundTunnels = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void TunnelPool::DetachTunnels ()
|
||||
{
|
||||
{
|
||||
|
@ -291,10 +313,11 @@ namespace tunnel
|
|||
return hop;
|
||||
}
|
||||
|
||||
bool TunnelPool::SelectPeers (std::vector<std::shared_ptr<const i2p::data::RouterInfo> >& hops)
|
||||
bool TunnelPool::SelectPeers (std::vector<std::shared_ptr<const i2p::data::RouterInfo> >& hops, bool isInbound)
|
||||
{
|
||||
if (m_ExplicitPeers) return SelectExplicitPeers (hops, isInbound);
|
||||
auto prevHop = i2p::context.GetSharedRouterInfo ();
|
||||
int numHops = m_NumInboundHops;
|
||||
int numHops = isInbound ? m_NumInboundHops : m_NumOutboundHops;
|
||||
if (i2p::transport::transports.GetNumPeers () > 25)
|
||||
{
|
||||
auto r = i2p::transport::transports.GetRandomPeer ();
|
||||
|
@ -319,7 +342,31 @@ namespace tunnel
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool TunnelPool::SelectExplicitPeers (std::vector<std::shared_ptr<const i2p::data::RouterInfo> >& hops, bool isInbound)
|
||||
{
|
||||
int size = m_ExplicitPeers->size ();
|
||||
std::vector<int> peerIndicies;
|
||||
for (int i = 0; i < size; i++) peerIndicies.push_back(i);
|
||||
std::random_shuffle (peerIndicies.begin(), peerIndicies.end());
|
||||
|
||||
int numHops = isInbound ? m_NumInboundHops : m_NumOutboundHops;
|
||||
for (int i = 0; i < numHops; i++)
|
||||
{
|
||||
auto& ident = (*m_ExplicitPeers)[peerIndicies[i]];
|
||||
auto r = i2p::data::netdb.FindRouter (ident);
|
||||
if (r)
|
||||
hops.push_back (r);
|
||||
else
|
||||
{
|
||||
LogPrint (eLogInfo, "Can't find router for ", ident.ToBase64 ());
|
||||
i2p::data::netdb.RequestDestination (ident);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void TunnelPool::CreateInboundTunnel ()
|
||||
{
|
||||
auto outboundTunnel = GetNextOutboundTunnel ();
|
||||
|
@ -327,7 +374,7 @@ namespace tunnel
|
|||
outboundTunnel = tunnels.GetNextOutboundTunnel ();
|
||||
LogPrint ("Creating destination inbound tunnel...");
|
||||
std::vector<std::shared_ptr<const i2p::data::RouterInfo> > hops;
|
||||
if (SelectPeers (hops))
|
||||
if (SelectPeers (hops, true))
|
||||
{
|
||||
std::reverse (hops.begin (), hops.end ());
|
||||
auto tunnel = tunnels.CreateTunnel<InboundTunnel> (std::make_shared<TunnelConfig> (hops), outboundTunnel);
|
||||
|
@ -356,7 +403,7 @@ namespace tunnel
|
|||
{
|
||||
LogPrint ("Creating destination outbound tunnel...");
|
||||
std::vector<std::shared_ptr<const i2p::data::RouterInfo> > hops;
|
||||
if (SelectPeers (hops))
|
||||
if (SelectPeers (hops, false))
|
||||
{
|
||||
auto tunnel = tunnels.CreateTunnel<OutboundTunnel> (
|
||||
std::make_shared<TunnelConfig> (hops, inboundTunnel->GetTunnelConfig ()));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue