|
|
@ -1,3 +1,4 @@ |
|
|
|
|
|
|
|
#include <algorithm> |
|
|
|
#include "I2PEndian.h" |
|
|
|
#include "I2PEndian.h" |
|
|
|
#include "CryptoConst.h" |
|
|
|
#include "CryptoConst.h" |
|
|
|
#include "Tunnel.h" |
|
|
|
#include "Tunnel.h" |
|
|
@ -22,6 +23,27 @@ namespace tunnel |
|
|
|
DetachTunnels (); |
|
|
|
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 () |
|
|
|
void TunnelPool::DetachTunnels () |
|
|
|
{ |
|
|
|
{ |
|
|
|
{ |
|
|
|
{ |
|
|
@ -291,10 +313,11 @@ namespace tunnel |
|
|
|
return hop; |
|
|
|
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 (); |
|
|
|
auto prevHop = i2p::context.GetSharedRouterInfo (); |
|
|
|
int numHops = m_NumInboundHops; |
|
|
|
int numHops = isInbound ? m_NumInboundHops : m_NumOutboundHops; |
|
|
|
if (i2p::transport::transports.GetNumPeers () > 25) |
|
|
|
if (i2p::transport::transports.GetNumPeers () > 25) |
|
|
|
{ |
|
|
|
{ |
|
|
|
auto r = i2p::transport::transports.GetRandomPeer (); |
|
|
|
auto r = i2p::transport::transports.GetRandomPeer (); |
|
|
@ -319,7 +342,31 @@ namespace tunnel |
|
|
|
} |
|
|
|
} |
|
|
|
return true; |
|
|
|
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 () |
|
|
|
void TunnelPool::CreateInboundTunnel () |
|
|
|
{ |
|
|
|
{ |
|
|
|
auto outboundTunnel = GetNextOutboundTunnel (); |
|
|
|
auto outboundTunnel = GetNextOutboundTunnel (); |
|
|
@ -327,7 +374,7 @@ namespace tunnel |
|
|
|
outboundTunnel = tunnels.GetNextOutboundTunnel (); |
|
|
|
outboundTunnel = tunnels.GetNextOutboundTunnel (); |
|
|
|
LogPrint ("Creating destination inbound tunnel..."); |
|
|
|
LogPrint ("Creating destination inbound tunnel..."); |
|
|
|
std::vector<std::shared_ptr<const i2p::data::RouterInfo> > hops; |
|
|
|
std::vector<std::shared_ptr<const i2p::data::RouterInfo> > hops; |
|
|
|
if (SelectPeers (hops)) |
|
|
|
if (SelectPeers (hops, true)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
std::reverse (hops.begin (), hops.end ()); |
|
|
|
std::reverse (hops.begin (), hops.end ()); |
|
|
|
auto tunnel = tunnels.CreateTunnel<InboundTunnel> (std::make_shared<TunnelConfig> (hops), outboundTunnel); |
|
|
|
auto tunnel = tunnels.CreateTunnel<InboundTunnel> (std::make_shared<TunnelConfig> (hops), outboundTunnel); |
|
|
@ -356,7 +403,7 @@ namespace tunnel |
|
|
|
{ |
|
|
|
{ |
|
|
|
LogPrint ("Creating destination outbound tunnel..."); |
|
|
|
LogPrint ("Creating destination outbound tunnel..."); |
|
|
|
std::vector<std::shared_ptr<const i2p::data::RouterInfo> > hops; |
|
|
|
std::vector<std::shared_ptr<const i2p::data::RouterInfo> > hops; |
|
|
|
if (SelectPeers (hops)) |
|
|
|
if (SelectPeers (hops, false)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
auto tunnel = tunnels.CreateTunnel<OutboundTunnel> ( |
|
|
|
auto tunnel = tunnels.CreateTunnel<OutboundTunnel> ( |
|
|
|
std::make_shared<TunnelConfig> (hops, inboundTunnel->GetTunnelConfig ())); |
|
|
|
std::make_shared<TunnelConfig> (hops, inboundTunnel->GetTunnelConfig ())); |
|
|
|